js_asker.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ATOM_BROWSER_NET_JS_ASKER_H_
  5. #define ATOM_BROWSER_NET_JS_ASKER_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/values.h"
  9. #include "native_mate/arguments.h"
  10. #include "net/url_request/url_request_context_getter.h"
  11. #include "v8/include/v8.h"
  12. namespace atom {
  13. using JavaScriptHandler =
  14. base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
  15. using BeforeStartCallback = base::Callback<void(mate::Arguments* args)>;
  16. class JsAsker {
  17. public:
  18. JsAsker();
  19. ~JsAsker();
  20. // Called by |CustomProtocolHandler| to store handler related information.
  21. void SetHandlerInfo(v8::Isolate* isolate,
  22. net::URLRequestContextGetter* request_context_getter,
  23. const JavaScriptHandler& handler);
  24. // Ask handler for options in UI thread.
  25. static void AskForOptions(
  26. v8::Isolate* isolate,
  27. const JavaScriptHandler& handler,
  28. std::unique_ptr<base::DictionaryValue> request_details,
  29. const BeforeStartCallback& before_start);
  30. // Test whether the |options| means an error.
  31. static bool IsErrorOptions(base::Value* value, int* error);
  32. net::URLRequestContextGetter* request_context_getter() const {
  33. return request_context_getter_;
  34. }
  35. v8::Isolate* isolate() { return isolate_; }
  36. JavaScriptHandler handler() { return handler_; }
  37. private:
  38. v8::Isolate* isolate_;
  39. net::URLRequestContextGetter* request_context_getter_;
  40. JavaScriptHandler handler_;
  41. DISALLOW_COPY_AND_ASSIGN(JsAsker);
  42. };
  43. } // namespace atom
  44. #endif // ATOM_BROWSER_NET_JS_ASKER_H_