| 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> | |
| 9 #include <memory> | 8 #include <memory> |
| 10 #include <string> | 9 #include <string> |
| 11 | 10 |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | 11 #include "base/macros.h" |
| 14 #include "extensions/renderer/api_binding_types.h" | 12 #include "extensions/renderer/api_binding_types.h" |
| 15 #include "extensions/renderer/argument_spec.h" | |
| 16 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 17 | 14 |
| 18 namespace gin { | |
| 19 class Arguments; | |
| 20 } | |
| 21 | |
| 22 namespace extensions { | 15 namespace extensions { |
| 23 class APIBindingHooksDelegate; | 16 class APIBindingHooksDelegate; |
| 17 class APITypeReferenceMap; |
| 24 class APISignature; | 18 class APISignature; |
| 25 | 19 |
| 26 // A class to register custom hooks for given API calls that need different | 20 // A class to register custom hooks for given API calls that need different |
| 27 // handling. An instance exists for a single API, but can be used across | 21 // handling. An instance exists for a single API, but can be used across |
| 28 // multiple contexts (but only on the same thread). | 22 // multiple contexts (but only on the same thread). |
| 29 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows | 23 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows |
| 30 // for registration of C++ handlers. Add JS support. | 24 // for registration of C++ handlers. Add JS support. |
| 31 class APIBindingHooks { | 25 class APIBindingHooks { |
| 32 public: | 26 public: |
| 33 // The result of checking for hooks to handle a request. | 27 // The result of checking for hooks to handle a request. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 44 explicit RequestResult(ResultCode code); | 38 explicit RequestResult(ResultCode code); |
| 45 RequestResult(ResultCode code, v8::Local<v8::Function> custom_callback); | 39 RequestResult(ResultCode code, v8::Local<v8::Function> custom_callback); |
| 46 RequestResult(const RequestResult& other); | 40 RequestResult(const RequestResult& other); |
| 47 ~RequestResult(); | 41 ~RequestResult(); |
| 48 | 42 |
| 49 ResultCode code; | 43 ResultCode code; |
| 50 v8::Local<v8::Function> custom_callback; | 44 v8::Local<v8::Function> custom_callback; |
| 51 v8::Local<v8::Value> return_value; // Only valid if code == HANDLED. | 45 v8::Local<v8::Value> return_value; // Only valid if code == HANDLED. |
| 52 }; | 46 }; |
| 53 | 47 |
| 54 // The callback to handle an API method. We pass in the expected signature | |
| 55 // (so the caller can verify arguments, optionally after modifying/"massaging" | |
| 56 // them) and the passed arguments. The handler is responsible for returning, | |
| 57 // which depending on the API could mean either returning synchronously | |
| 58 // through gin::Arguments::Return or asynchronously through a passed callback. | |
| 59 // TODO(devlin): As we continue expanding the hooks interface, we should allow | |
| 60 // handlers to register a request so that they don't have to maintain a | |
| 61 // reference to the callback themselves. | |
| 62 using HandleRequestHook = | |
| 63 base::Callback<RequestResult(const APISignature*, | |
| 64 v8::Local<v8::Context> context, | |
| 65 std::vector<v8::Local<v8::Value>>*, | |
| 66 const APITypeReferenceMap&)>; | |
| 67 | |
| 68 APIBindingHooks(const std::string& api_name, | 48 APIBindingHooks(const std::string& api_name, |
| 69 const binding::RunJSFunctionSync& run_js); | 49 const binding::RunJSFunctionSync& run_js); |
| 70 ~APIBindingHooks(); | 50 ~APIBindingHooks(); |
| 71 | 51 |
| 72 // Register a custom binding to handle requests. | |
| 73 // TODO(devlin): Remove this in favor of a method on APIBindingHooksDelegate. | |
| 74 void RegisterHandleRequest(const std::string& method_name, | |
| 75 const HandleRequestHook& hook); | |
| 76 | |
| 77 // Looks for any custom hooks associated with the given request, and, if any | 52 // Looks for any custom hooks associated with the given request, and, if any |
| 78 // are found, runs them. Returns the result of running the hooks, if any. | 53 // are found, runs them. Returns the result of running the hooks, if any. |
| 79 RequestResult RunHooks(const std::string& method_name, | 54 RequestResult RunHooks(const std::string& method_name, |
| 80 v8::Local<v8::Context> context, | 55 v8::Local<v8::Context> context, |
| 81 const APISignature* signature, | 56 const APISignature* signature, |
| 82 std::vector<v8::Local<v8::Value>>* arguments, | 57 std::vector<v8::Local<v8::Value>>* arguments, |
| 83 const APITypeReferenceMap& type_refs); | 58 const APITypeReferenceMap& type_refs); |
| 84 | 59 |
| 85 // Returns a JS interface that can be used to register hooks. | 60 // Returns a JS interface that can be used to register hooks. |
| 86 v8::Local<v8::Object> GetJSHookInterface(v8::Local<v8::Context> context); | 61 v8::Local<v8::Object> GetJSHookInterface(v8::Local<v8::Context> context); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 97 | 72 |
| 98 void SetDelegate(std::unique_ptr<APIBindingHooksDelegate> delegate); | 73 void SetDelegate(std::unique_ptr<APIBindingHooksDelegate> delegate); |
| 99 | 74 |
| 100 private: | 75 private: |
| 101 // Updates the |arguments| by running |function| and settings arguments to the | 76 // Updates the |arguments| by running |function| and settings arguments to the |
| 102 // returned result. | 77 // returned result. |
| 103 bool UpdateArguments(v8::Local<v8::Function> function, | 78 bool UpdateArguments(v8::Local<v8::Function> function, |
| 104 v8::Local<v8::Context> context, | 79 v8::Local<v8::Context> context, |
| 105 std::vector<v8::Local<v8::Value>>* arguments); | 80 std::vector<v8::Local<v8::Value>>* arguments); |
| 106 | 81 |
| 107 // Whether we've tried to use any hooks associated with this object. | |
| 108 bool hooks_used_ = false; | |
| 109 | |
| 110 // All registered request handlers. | |
| 111 std::map<std::string, HandleRequestHook> request_hooks_; | |
| 112 | |
| 113 // The name of the associated API. | 82 // The name of the associated API. |
| 114 std::string api_name_; | 83 std::string api_name_; |
| 115 | 84 |
| 116 // We use synchronous JS execution here because at every point we execute JS, | 85 // We use synchronous JS execution here because at every point we execute JS, |
| 117 // it's in direct response to JS calling in. There should be no reason that | 86 // it's in direct response to JS calling in. There should be no reason that |
| 118 // script is disabled. | 87 // script is disabled. |
| 119 binding::RunJSFunctionSync run_js_; | 88 binding::RunJSFunctionSync run_js_; |
| 120 | 89 |
| 121 std::unique_ptr<APIBindingHooksDelegate> delegate_; | 90 std::unique_ptr<APIBindingHooksDelegate> delegate_; |
| 122 | 91 |
| 123 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 92 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 124 }; | 93 }; |
| 125 | 94 |
| 126 } // namespace extensions | 95 } // namespace extensions |
| 127 | 96 |
| 128 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 97 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |