| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_JS_UTIL_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_JS_UTIL_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDING_JS_UTIL_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDING_JS_UTIL_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "extensions/renderer/api_binding_types.h" | 11 #include "extensions/renderer/api_binding_types.h" |
| 12 #include "gin/wrappable.h" | 12 #include "gin/wrappable.h" |
| 13 #include "v8/include/v8.h" | 13 #include "v8/include/v8.h" |
| 14 | 14 |
| 15 namespace gin { | 15 namespace gin { |
| 16 class Arguments; | 16 class Arguments; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace extensions { | 19 namespace extensions { |
| 20 class APIEventHandler; | 20 class APIEventHandler; |
| 21 class APIRequestHandler; | 21 class APIRequestHandler; |
| 22 class APITypeReferenceMap; | 22 class APITypeReferenceMap; |
| 23 | 23 |
| 24 // An object that exposes utility methods to the existing JS bindings, such as | 24 // An object that exposes utility methods to the existing JS bindings, such as |
| 25 // sendRequest and registering event argument massagers. If/when we get rid of | 25 // sendRequest and registering event argument massagers. If/when we get rid of |
| 26 // some of our JS bindings, we can reduce or remove this class. | 26 // some of our JS bindings, we can reduce or remove this class. |
| 27 class APIBindingJSUtil final : public gin::Wrappable<APIBindingJSUtil> { | 27 class APIBindingJSUtil final : public gin::Wrappable<APIBindingJSUtil> { |
| 28 public: | 28 public: |
| 29 APIBindingJSUtil(const APITypeReferenceMap* type_refs, | 29 APIBindingJSUtil(APITypeReferenceMap* type_refs, |
| 30 APIRequestHandler* request_handler, | 30 APIRequestHandler* request_handler, |
| 31 APIEventHandler* event_handler, | 31 APIEventHandler* event_handler, |
| 32 const binding::RunJSFunction& run_js); | 32 const binding::RunJSFunction& run_js); |
| 33 ~APIBindingJSUtil() override; | 33 ~APIBindingJSUtil() override; |
| 34 | 34 |
| 35 static gin::WrapperInfo kWrapperInfo; | 35 static gin::WrapperInfo kWrapperInfo; |
| 36 | 36 |
| 37 // gin::Wrappable: | 37 // gin::Wrappable: |
| 38 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | 38 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 39 v8::Isolate* isolate) final; | 39 v8::Isolate* isolate) final; |
| 40 | 40 |
| 41 private: | 41 private: |
| 42 // A handler to initiate an API request through the APIRequestHandler. A | 42 // A handler to initiate an API request through the APIRequestHandler. A |
| 43 // replacement for custom bindings that utilize require('sendRequest'). | 43 // replacement for custom bindings that utilize require('sendRequest'). |
| 44 void SendRequest(gin::Arguments* arguments, | 44 void SendRequest(gin::Arguments* arguments, |
| 45 const std::string& name, | 45 const std::string& name, |
| 46 const std::vector<v8::Local<v8::Value>>& request_args, | 46 const std::vector<v8::Local<v8::Value>>& request_args, |
| 47 v8::Local<v8::Value> schemas_unused, | 47 v8::Local<v8::Value> schemas_unused, |
| 48 v8::Local<v8::Value> options); | 48 v8::Local<v8::Value> options); |
| 49 | 49 |
| 50 // A handler to register an argument massager for a specific event. | 50 // A handler to register an argument massager for a specific event. |
| 51 // Replacement for event_bindings.registerArgumentMassager. | 51 // Replacement for event_bindings.registerArgumentMassager. |
| 52 void RegisterEventArgumentMassager(gin::Arguments* arguments, | 52 void RegisterEventArgumentMassager(gin::Arguments* arguments, |
| 53 const std::string& event_name, | 53 const std::string& event_name, |
| 54 v8::Local<v8::Function> massager); | 54 v8::Local<v8::Function> massager); |
| 55 | 55 |
| 56 // A handler to allow custom bindings to create custom extension API event | 56 // A handler to allow custom bindings to create custom extension API event |
| 57 // objects (e.g. foo.onBar). | 57 // objects (e.g. foo.onBar). |
| 58 // Note: The JS version allows for constructing declarative events; it's | |
| 59 // unclear if we'll need to support this. | |
| 60 // TODO(devlin): Currently, we ignore schema. We may want to take it into | 58 // TODO(devlin): Currently, we ignore schema. We may want to take it into |
| 61 // account. | 59 // account. |
| 62 void CreateCustomEvent(gin::Arguments* arguments, | 60 void CreateCustomEvent(gin::Arguments* arguments, |
| 63 v8::Local<v8::Value> v8_event_name, | 61 v8::Local<v8::Value> v8_event_name, |
| 64 v8::Local<v8::Value> unused_schema, | 62 v8::Local<v8::Value> unused_schema, |
| 65 bool supports_filters); | 63 bool supports_filters); |
| 66 | 64 |
| 65 // Creates a new declarative event. |
| 66 void CreateCustomDeclarativeEvent( |
| 67 gin::Arguments* arguments, |
| 68 const std::string& event_name, |
| 69 const std::vector<std::string>& actions_list, |
| 70 const std::vector<std::string>& conditions_list, |
| 71 int webview_instance_id); |
| 72 |
| 67 // Invalidates an event, removing its listeners and preventing any more from | 73 // Invalidates an event, removing its listeners and preventing any more from |
| 68 // being added. | 74 // being added. |
| 69 void InvalidateEvent(gin::Arguments* arguments, v8::Local<v8::Object> event); | 75 void InvalidateEvent(gin::Arguments* arguments, v8::Local<v8::Object> event); |
| 70 | 76 |
| 71 // Sets the last error in the context. | 77 // Sets the last error in the context. |
| 72 void SetLastError(gin::Arguments* arguments, const std::string& error); | 78 void SetLastError(gin::Arguments* arguments, const std::string& error); |
| 73 | 79 |
| 74 // Clears the last error in the context. | 80 // Clears the last error in the context. |
| 75 void ClearLastError(gin::Arguments* arguments); | 81 void ClearLastError(gin::Arguments* arguments); |
| 76 | 82 |
| 77 // Returns true if there is a set lastError in the given context. | 83 // Returns true if there is a set lastError in the given context. |
| 78 void HasLastError(gin::Arguments* arguments); | 84 void HasLastError(gin::Arguments* arguments); |
| 79 | 85 |
| 80 // Sets the lastError in the given context, runs the provided callback, and | 86 // Sets the lastError in the given context, runs the provided callback, and |
| 81 // then clears the last error. | 87 // then clears the last error. |
| 82 void RunCallbackWithLastError(gin::Arguments* arguments, | 88 void RunCallbackWithLastError(gin::Arguments* arguments, |
| 83 const std::string& error, | 89 const std::string& error, |
| 84 v8::Local<v8::Function> callback); | 90 v8::Local<v8::Function> callback); |
| 85 | 91 |
| 86 // Type references. Guaranteed to outlive this object. | 92 // Type references. Guaranteed to outlive this object. |
| 87 const APITypeReferenceMap* type_refs_; | 93 APITypeReferenceMap* type_refs_; |
| 88 | 94 |
| 89 // The request handler. Guaranteed to outlive this object. | 95 // The request handler. Guaranteed to outlive this object. |
| 90 APIRequestHandler* request_handler_; | 96 APIRequestHandler* request_handler_; |
| 91 | 97 |
| 92 // The event handler. Guaranteed to outlive this object. | 98 // The event handler. Guaranteed to outlive this object. |
| 93 APIEventHandler* event_handler_; | 99 APIEventHandler* event_handler_; |
| 94 | 100 |
| 95 binding::RunJSFunction run_js_; | 101 binding::RunJSFunction run_js_; |
| 96 | 102 |
| 97 DISALLOW_COPY_AND_ASSIGN(APIBindingJSUtil); | 103 DISALLOW_COPY_AND_ASSIGN(APIBindingJSUtil); |
| 98 }; | 104 }; |
| 99 | 105 |
| 100 } // namespace extensions | 106 } // namespace extensions |
| 101 | 107 |
| 102 #endif // EXTENSIONS_RENDERER_API_BINDING_JS_UTIL_H_ | 108 #endif // EXTENSIONS_RENDERER_API_BINDING_JS_UTIL_H_ |
| OLD | NEW |