| 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 20 matching lines...) Expand all Loading... |
| 31 // (so the caller can verify arguments, optionally after modifying/"massaging" | 31 // (so the caller can verify arguments, optionally after modifying/"massaging" |
| 32 // them) and the passed arguments. The handler is responsible for returning, | 32 // them) and the passed arguments. The handler is responsible for returning, |
| 33 // which depending on the API could mean either returning synchronously | 33 // which depending on the API could mean either returning synchronously |
| 34 // through gin::Arguments::Return or asynchronously through a passed callback. | 34 // through gin::Arguments::Return or asynchronously through a passed callback. |
| 35 // TODO(devlin): As we continue expanding the hooks interface, we should allow | 35 // TODO(devlin): As we continue expanding the hooks interface, we should allow |
| 36 // handlers to register a request so that they don't have to maintain a | 36 // handlers to register a request so that they don't have to maintain a |
| 37 // reference to the callback themselves. | 37 // reference to the callback themselves. |
| 38 using HandleRequestHook = | 38 using HandleRequestHook = |
| 39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>; | 39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>; |
| 40 | 40 |
| 41 APIBindingHooks(); | 41 explicit APIBindingHooks(const binding::RunJSFunction& run_js); |
| 42 ~APIBindingHooks(); | 42 ~APIBindingHooks(); |
| 43 | 43 |
| 44 // Register a custom binding to handle requests. | 44 // Register a custom binding to handle requests. |
| 45 void RegisterHandleRequest(const std::string& method_name, | 45 void RegisterHandleRequest(const std::string& method_name, |
| 46 const HandleRequestHook& hook); | 46 const HandleRequestHook& hook); |
| 47 | 47 |
| 48 // Registers a JS script to be compiled and run in order to initialize any JS |
| 49 // hooks within a v8 context. |
| 50 void RegisterJsSource(v8::Global<v8::String> source); |
| 51 |
| 52 // Initializes JS hooks within a context. |
| 53 void InitializeInContext(v8::Local<v8::Context> context, |
| 54 const std::string& api_name); |
| 55 |
| 48 // Returns the custom hook for the given method, or a null callback if none | 56 // Returns the custom hook for the given method, or a null callback if none |
| 49 // exists. | 57 // exists. |
| 50 HandleRequestHook GetHandleRequest(const std::string& method_name); | 58 HandleRequestHook GetHandleRequest(const std::string& api_name, |
| 59 const std::string& method_name, |
| 60 v8::Local<v8::Context> context); |
| 51 | 61 |
| 52 private: | 62 private: |
| 53 // All registered request handlers. | 63 // All registered request handlers. |
| 54 std::map<std::string, HandleRequestHook> request_hooks_; | 64 std::map<std::string, HandleRequestHook> request_hooks_; |
| 55 | 65 |
| 66 // The script to run to initialize JS hooks, if any. |
| 67 v8::Global<v8::String> js_hooks_source_; |
| 68 |
| 69 binding::RunJSFunction run_js_; |
| 70 |
| 56 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 71 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 57 }; | 72 }; |
| 58 | 73 |
| 59 } // namespace extensions | 74 } // namespace extensions |
| 60 | 75 |
| 61 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 76 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |