Chromium Code Reviews| 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> |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 // them) and the passed arguments. The handler is responsible for returning, | 42 // them) and the passed arguments. The handler is responsible for returning, |
| 43 // which depending on the API could mean either returning synchronously | 43 // which depending on the API could mean either returning synchronously |
| 44 // through gin::Arguments::Return or asynchronously through a passed callback. | 44 // through gin::Arguments::Return or asynchronously through a passed callback. |
| 45 // TODO(devlin): As we continue expanding the hooks interface, we should allow | 45 // TODO(devlin): As we continue expanding the hooks interface, we should allow |
| 46 // handlers to register a request so that they don't have to maintain a | 46 // handlers to register a request so that they don't have to maintain a |
| 47 // reference to the callback themselves. | 47 // reference to the callback themselves. |
| 48 using HandleRequestHook = | 48 using HandleRequestHook = |
| 49 base::Callback<RequestResult(const APISignature*, | 49 base::Callback<RequestResult(const APISignature*, |
| 50 v8::Local<v8::Context> context, | 50 v8::Local<v8::Context> context, |
| 51 std::vector<v8::Local<v8::Value>>*, | 51 std::vector<v8::Local<v8::Value>>*, |
| 52 const ArgumentSpec::RefMap&)>; | 52 const ArgumentSpec::RefMap&, |
| 53 v8::Local<v8::Value>* return_value)>; | |
| 53 | 54 |
| 54 explicit APIBindingHooks(const binding::RunJSFunctionSync& run_js); | 55 explicit APIBindingHooks(const binding::RunJSFunctionSync& run_js); |
| 55 ~APIBindingHooks(); | 56 ~APIBindingHooks(); |
| 56 | 57 |
| 57 // Register a custom binding to handle requests. | 58 // Register a custom binding to handle requests. |
| 58 void RegisterHandleRequest(const std::string& method_name, | 59 void RegisterHandleRequest(const std::string& method_name, |
| 59 const HandleRequestHook& hook); | 60 const HandleRequestHook& hook); |
| 60 | 61 |
| 61 // Registers a JS script to be compiled and run in order to initialize any JS | 62 // Registers a JS script to be compiled and run in order to initialize any JS |
| 62 // hooks within a v8 context. | 63 // hooks within a v8 context. |
| 63 void RegisterJsSource(v8::Global<v8::String> source, | 64 void RegisterJsSource(v8::Global<v8::String> source, |
| 64 v8::Global<v8::String> resource_name); | 65 v8::Global<v8::String> resource_name); |
| 65 | 66 |
| 66 // Initializes JS hooks within a context. | 67 // Initializes JS hooks within a context. |
| 67 void InitializeInContext(v8::Local<v8::Context> context, | 68 void InitializeInContext(v8::Local<v8::Context> context, |
| 68 const std::string& api_name); | 69 const std::string& api_name); |
| 69 | 70 |
| 70 // Looks for a custom hook to handle the given request and, if one exists, | 71 // Looks for a custom hook to handle the given request and, if one exists, |
| 71 // runs it. Returns the result of trying to run the hook, or NOT_HANDLED if no | 72 // runs it. Returns the result of running the hook, if any. Populates |
| 72 // hook was found. | 73 // |return_value| with a value to return, if any. |
| 73 RequestResult HandleRequest(const std::string& api_name, | 74 RequestResult HandleRequest(const std::string& api_name, |
| 74 const std::string& method_name, | 75 const std::string& method_name, |
| 75 v8::Local<v8::Context> context, | 76 v8::Local<v8::Context> context, |
| 76 const APISignature* signature, | 77 const APISignature* signature, |
| 77 std::vector<v8::Local<v8::Value>>* arguments, | 78 std::vector<v8::Local<v8::Value>>* arguments, |
| 78 const ArgumentSpec::RefMap& type_refs); | 79 const ArgumentSpec::RefMap& type_refs, |
| 80 v8::Local<v8::Value>* return_value); | |
|
jbroman
2017/01/02 20:05:46
nit: This argument list is getting a little long.
Devlin
2017/01/04 18:38:07
Hmm... it makes it seem a little more verbose to m
| |
| 79 | 81 |
| 80 // Returns a JS interface that can be used to register hooks. | 82 // Returns a JS interface that can be used to register hooks. |
| 81 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, | 83 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, |
| 82 v8::Local<v8::Context> context); | 84 v8::Local<v8::Context> context); |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 // Updates the |arguments| by running |function| and settings arguments to the | 87 // Updates the |arguments| by running |function| and settings arguments to the |
| 86 // returned result. | 88 // returned result. |
| 87 bool UpdateArguments(v8::Local<v8::Function> function, | 89 bool UpdateArguments(v8::Local<v8::Function> function, |
| 88 v8::Local<v8::Context> context, | 90 v8::Local<v8::Context> context, |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 105 // it's in direct response to JS calling in. There should be no reason that | 107 // it's in direct response to JS calling in. There should be no reason that |
| 106 // script is disabled. | 108 // script is disabled. |
| 107 binding::RunJSFunctionSync run_js_; | 109 binding::RunJSFunctionSync run_js_; |
| 108 | 110 |
| 109 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 111 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 110 }; | 112 }; |
| 111 | 113 |
| 112 } // namespace extensions | 114 } // namespace extensions |
| 113 | 115 |
| 114 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 116 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |