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(); |
| 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. |
72 // hook was found. | |
73 RequestResult HandleRequest(const std::string& api_name, | 82 RequestResult HandleRequest(const std::string& api_name, |
74 const std::string& method_name, | 83 const std::string& method_name, |
75 v8::Local<v8::Context> context, | 84 v8::Local<v8::Context> context, |
76 const APISignature* signature, | 85 const APISignature* signature, |
77 std::vector<v8::Local<v8::Value>>* arguments, | 86 std::vector<v8::Local<v8::Value>>* arguments, |
78 const ArgumentSpec::RefMap& type_refs); | 87 const ArgumentSpec::RefMap& type_refs); |
79 | 88 |
80 // Returns a JS interface that can be used to register hooks. | 89 // Returns a JS interface that can be used to register hooks. |
81 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, | 90 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, |
82 v8::Local<v8::Context> context); | 91 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 | 114 // it's in direct response to JS calling in. There should be no reason that |
106 // script is disabled. | 115 // script is disabled. |
107 binding::RunJSFunctionSync run_js_; | 116 binding::RunJSFunctionSync run_js_; |
108 | 117 |
109 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | 118 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
110 }; | 119 }; |
111 | 120 |
112 } // namespace extensions | 121 } // namespace extensions |
113 | 122 |
114 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | 123 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
OLD | NEW |