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