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 12 matching lines...) Expand all Loading... | |
| 23 class APISignature; | 23 class APISignature; |
| 24 | 24 |
| 25 // 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 |
| 26 // 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 |
| 27 // multiple contexts (but only on the same thread). | 27 // multiple contexts (but only on the same thread). |
| 28 // 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 |
| 29 // for registration of C++ handlers. Add JS support. | 29 // for registration of C++ handlers. Add JS support. |
| 30 class APIBindingHooks { | 30 class APIBindingHooks { |
| 31 public: | 31 public: |
| 32 // The result of checking for hooks to handle a request. | 32 // The result of checking for hooks to handle a request. |
| 33 enum class RequestResult { | 33 struct RequestResult { |
| 34 HANDLED, // A custom hook handled the request. | 34 enum ResultCode { |
| 35 THROWN, // An exception was thrown during parsing or handling. | 35 HANDLED, // A custom hook handled the request. |
| 36 INVALID_INVOCATION, // The request was called with invalid arguments. | 36 THROWN, // An exception was thrown during parsing or |
| 37 NOT_HANDLED, // The request was not handled. | 37 // handling. |
| 38 INVALID_INVOCATION, // The request was called with invalid arguments. | |
| 39 NOT_HANDLED, // The request was not handled. | |
| 40 }; | |
| 41 | |
| 42 explicit RequestResult(ResultCode code); | |
| 43 RequestResult(const RequestResult& other); | |
| 44 ~RequestResult(); | |
|
jbroman
2017/01/04 21:57:49
Is the [chromium-style] plugin forcing these to be
Devlin
2017/01/04 23:29:29
Yeah, this was the result of being bullied by [chr
| |
| 45 | |
| 46 ResultCode code; | |
| 47 v8::Local<v8::Value> return_value; // Only valid if code == HANDLED. | |
| 38 }; | 48 }; |
| 39 | 49 |
| 40 // The callback to handle an API method. We pass in the expected signature | 50 // The callback to handle an API method. We pass in the expected signature |
| 41 // (so the caller can verify arguments, optionally after modifying/"massaging" | 51 // (so the caller can verify arguments, optionally after modifying/"massaging" |
| 42 // them) and the passed arguments. The handler is responsible for returning, | 52 // them) and the passed arguments. The handler is responsible for returning, |
| 43 // which depending on the API could mean either returning synchronously | 53 // which depending on the API could mean either returning synchronously |
| 44 // through gin::Arguments::Return or asynchronously through a passed callback. | 54 // through gin::Arguments::Return or asynchronously through a passed callback. |
| 45 // TODO(devlin): As we continue expanding the hooks interface, we should allow | 55 // 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 | 56 // handlers to register a request so that they don't have to maintain a |
| 47 // reference to the callback themselves. | 57 // reference to the callback themselves. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 61 // Registers a JS script to be compiled and run in order to initialize any JS | 71 // Registers a JS script to be compiled and run in order to initialize any JS |
| 62 // hooks within a v8 context. | 72 // hooks within a v8 context. |
| 63 void RegisterJsSource(v8::Global<v8::String> source, | 73 void RegisterJsSource(v8::Global<v8::String> source, |
| 64 v8::Global<v8::String> resource_name); | 74 v8::Global<v8::String> resource_name); |
| 65 | 75 |
| 66 // Initializes JS hooks within a context. | 76 // Initializes JS hooks within a context. |
| 67 void InitializeInContext(v8::Local<v8::Context> context, | 77 void InitializeInContext(v8::Local<v8::Context> context, |
| 68 const std::string& api_name); | 78 const std::string& api_name); |
| 69 | 79 |
| 70 // Looks for a custom hook to handle the given request and, if one exists, | 80 // 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 | 81 // runs it. Returns the result of running the hook, if any. Populates |
|
jbroman
2017/01/04 21:57:49
If the |return_value| argument is removed, then th
Devlin
2017/01/04 23:29:29
Whoops, done.
| |
| 72 // hook was found. | 82 // |return_value| with a value to return, if any. |
| 73 RequestResult HandleRequest(const std::string& api_name, | 83 RequestResult HandleRequest(const std::string& api_name, |
| 74 const std::string& method_name, | 84 const std::string& method_name, |
| 75 v8::Local<v8::Context> context, | 85 v8::Local<v8::Context> context, |
| 76 const APISignature* signature, | 86 const APISignature* signature, |
| 77 std::vector<v8::Local<v8::Value>>* arguments, | 87 std::vector<v8::Local<v8::Value>>* arguments, |
| 78 const ArgumentSpec::RefMap& type_refs); | 88 const ArgumentSpec::RefMap& type_refs); |
| 79 | 89 |
| 80 // Returns a JS interface that can be used to register hooks. | 90 // Returns a JS interface that can be used to register hooks. |
| 81 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, | 91 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, |
| 82 v8::Local<v8::Context> context); | 92 v8::Local<v8::Context> context); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 105 // it's in direct response to JS calling in. There should be no reason that | 115 // it's in direct response to JS calling in. There should be no reason that |
| 106 // script is disabled. | 116 // script is disabled. |
| 107 binding::RunJSFunctionSync run_js_; | 117 binding::RunJSFunctionSync run_js_; |
| 108 | 118 |
| 109 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 119 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 110 }; | 120 }; |
| 111 | 121 |
| 112 } // namespace extensions | 122 } // namespace extensions |
| 113 | 123 |
| 114 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 124 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |