| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "extensions/renderer/api_binding_types.h" | 14 #include "extensions/renderer/api_binding_types.h" |
| 15 #include "extensions/renderer/argument_spec.h" |
| 15 #include "v8/include/v8.h" | 16 #include "v8/include/v8.h" |
| 16 | 17 |
| 17 namespace gin { | 18 namespace gin { |
| 18 class Arguments; | 19 class Arguments; |
| 19 } | 20 } |
| 20 | 21 |
| 21 namespace extensions { | 22 namespace extensions { |
| 22 class APISignature; | 23 class APISignature; |
| 23 | 24 |
| 24 // A class to register custom hooks for given API calls that need different | 25 // A class to register custom hooks for given API calls that need different |
| 25 // handling. An instance exists for a single API, but can be used across | 26 // handling. An instance exists for a single API, but can be used across |
| 26 // multiple contexts (but only on the same thread). | 27 // multiple contexts (but only on the same thread). |
| 27 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows | 28 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows |
| 28 // for registration of C++ handlers. Add JS support. | 29 // for registration of C++ handlers. Add JS support. |
| 29 class APIBindingHooks { | 30 class APIBindingHooks { |
| 30 public: | 31 public: |
| 32 // The result of checking for hooks to handle a request. |
| 33 enum class RequestResult { |
| 34 HANDLED, // A custom hook handled the request. |
| 35 INVALID_INVOCATION, // The request was called with invalid arguments. |
| 36 NOT_HANDLED, // The request was not handled. |
| 37 }; |
| 38 |
| 31 // The callback to handle an API method. We pass in the expected signature | 39 // The callback to handle an API method. We pass in the expected signature |
| 32 // (so the caller can verify arguments, optionally after modifying/"massaging" | 40 // (so the caller can verify arguments, optionally after modifying/"massaging" |
| 33 // them) and the passed arguments. The handler is responsible for returning, | 41 // them) and the passed arguments. The handler is responsible for returning, |
| 34 // which depending on the API could mean either returning synchronously | 42 // which depending on the API could mean either returning synchronously |
| 35 // through gin::Arguments::Return or asynchronously through a passed callback. | 43 // through gin::Arguments::Return or asynchronously through a passed callback. |
| 36 // TODO(devlin): As we continue expanding the hooks interface, we should allow | 44 // TODO(devlin): As we continue expanding the hooks interface, we should allow |
| 37 // handlers to register a request so that they don't have to maintain a | 45 // handlers to register a request so that they don't have to maintain a |
| 38 // reference to the callback themselves. | 46 // reference to the callback themselves. |
| 39 using HandleRequestHook = | 47 using HandleRequestHook = |
| 40 base::Callback<void(const APISignature*, gin::Arguments*)>; | 48 base::Callback<RequestResult(const APISignature*, |
| 49 gin::Arguments*, |
| 50 const ArgumentSpec::RefMap&)>; |
| 41 | 51 |
| 42 explicit APIBindingHooks(const binding::RunJSFunction& run_js); | 52 explicit APIBindingHooks(const binding::RunJSFunction& run_js); |
| 43 ~APIBindingHooks(); | 53 ~APIBindingHooks(); |
| 44 | 54 |
| 45 // Register a custom binding to handle requests. | 55 // Register a custom binding to handle requests. |
| 46 void RegisterHandleRequest(const std::string& method_name, | 56 void RegisterHandleRequest(const std::string& method_name, |
| 47 const HandleRequestHook& hook); | 57 const HandleRequestHook& hook); |
| 48 | 58 |
| 49 // Registers a JS script to be compiled and run in order to initialize any JS | 59 // Registers a JS script to be compiled and run in order to initialize any JS |
| 50 // hooks within a v8 context. | 60 // hooks within a v8 context. |
| 51 void RegisterJsSource(v8::Global<v8::String> source, | 61 void RegisterJsSource(v8::Global<v8::String> source, |
| 52 v8::Global<v8::String> resource_name); | 62 v8::Global<v8::String> resource_name); |
| 53 | 63 |
| 54 // Initializes JS hooks within a context. | 64 // Initializes JS hooks within a context. |
| 55 void InitializeInContext(v8::Local<v8::Context> context, | 65 void InitializeInContext(v8::Local<v8::Context> context, |
| 56 const std::string& api_name); | 66 const std::string& api_name); |
| 57 | 67 |
| 58 // Looks for a custom hook to handle the given request and, if one exists, | 68 // Looks for a custom hook to handle the given request and, if one exists, |
| 59 // runs it. Returns true if a hook was found and run. | 69 // runs it. Returns the result of trying to run the hook, or NOT_HANDLED if no |
| 60 bool HandleRequest(const std::string& api_name, | 70 // hook was found. |
| 61 const std::string& method_name, | 71 RequestResult HandleRequest(const std::string& api_name, |
| 62 v8::Local<v8::Context> context, | 72 const std::string& method_name, |
| 63 const APISignature* signature, | 73 v8::Local<v8::Context> context, |
| 64 gin::Arguments* arguments); | 74 const APISignature* signature, |
| 75 gin::Arguments* arguments, |
| 76 const ArgumentSpec::RefMap& type_refs); |
| 65 | 77 |
| 66 // Returns a JS interface that can be used to register hooks. | 78 // Returns a JS interface that can be used to register hooks. |
| 67 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, | 79 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, |
| 68 v8::Local<v8::Context> context); | 80 v8::Local<v8::Context> context); |
| 69 | 81 |
| 70 private: | 82 private: |
| 71 // Whether we've tried to use any hooks associated with this object. | 83 // Whether we've tried to use any hooks associated with this object. |
| 72 bool hooks_used_ = false; | 84 bool hooks_used_ = false; |
| 73 | 85 |
| 74 // All registered request handlers. | 86 // All registered request handlers. |
| 75 std::map<std::string, HandleRequestHook> request_hooks_; | 87 std::map<std::string, HandleRequestHook> request_hooks_; |
| 76 | 88 |
| 77 // The script to run to initialize JS hooks, if any. | 89 // The script to run to initialize JS hooks, if any. |
| 78 v8::Global<v8::String> js_hooks_source_; | 90 v8::Global<v8::String> js_hooks_source_; |
| 79 | 91 |
| 80 // The name of the JS resource for the hooks. Used to create a ScriptOrigin | 92 // The name of the JS resource for the hooks. Used to create a ScriptOrigin |
| 81 // to make exception stack traces more readable. | 93 // to make exception stack traces more readable. |
| 82 v8::Global<v8::String> js_resource_name_; | 94 v8::Global<v8::String> js_resource_name_; |
| 83 | 95 |
| 84 binding::RunJSFunction run_js_; | 96 binding::RunJSFunction run_js_; |
| 85 | 97 |
| 86 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 98 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 87 }; | 99 }; |
| 88 | 100 |
| 89 } // namespace extensions | 101 } // namespace extensions |
| 90 | 102 |
| 91 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 103 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |