| 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_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDING_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDING_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 // A class that vends v8::Objects for extension APIs. These APIs have function | 34 // A class that vends v8::Objects for extension APIs. These APIs have function |
| 35 // interceptors for all exposed methods, which call back into the APIBinding. | 35 // interceptors for all exposed methods, which call back into the APIBinding. |
| 36 // The APIBinding then matches the calling arguments against an expected method | 36 // The APIBinding then matches the calling arguments against an expected method |
| 37 // signature, throwing an error if they don't match. | 37 // signature, throwing an error if they don't match. |
| 38 // There should only need to be a single APIBinding object for each API, and | 38 // There should only need to be a single APIBinding object for each API, and |
| 39 // each can vend multiple v8::Objects for different contexts. | 39 // each can vend multiple v8::Objects for different contexts. |
| 40 // This object is designed to be one-per-isolate, but used across separate | 40 // This object is designed to be one-per-isolate, but used across separate |
| 41 // contexts. | 41 // contexts. |
| 42 class APIBinding { | 42 class APIBinding { |
| 43 public: | 43 public: |
| 44 // TODO(devlin): We may want to coalesce this with the | |
| 45 // ExtensionHostMsg_Request_Params IPC struct. | |
| 46 struct Request { | |
| 47 Request(); | |
| 48 ~Request(); | |
| 49 | |
| 50 int request_id = -1; | |
| 51 std::string method_name; | |
| 52 bool has_callback = false; | |
| 53 bool has_user_gesture = false; | |
| 54 std::unique_ptr<base::ListValue> arguments; | |
| 55 | |
| 56 private: | |
| 57 DISALLOW_COPY_AND_ASSIGN(Request); | |
| 58 }; | |
| 59 | |
| 60 using SendRequestMethod = | |
| 61 base::Callback<void(std::unique_ptr<Request>, v8::Local<v8::Context>)>; | |
| 62 | |
| 63 // The callback for determining if a given API method (specified by |name|) | 44 // The callback for determining if a given API method (specified by |name|) |
| 64 // is available. | 45 // is available. |
| 65 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; | 46 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; |
| 66 | 47 |
| 67 // The callback type for handling an API call. | 48 // The callback type for handling an API call. |
| 68 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 49 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
| 69 | 50 |
| 70 // The APITypeReferenceMap is required to outlive this object. | 51 // The APITypeReferenceMap is required to outlive this object. |
| 71 // |function_definitions|, |type_definitions| and |event_definitions| | 52 // |function_definitions|, |type_definitions| and |event_definitions| |
| 72 // may be null if the API does not specify any of that category. | 53 // may be null if the API does not specify any of that category. |
| 73 APIBinding(const std::string& name, | 54 APIBinding(const std::string& name, |
| 74 const base::ListValue* function_definitions, | 55 const base::ListValue* function_definitions, |
| 75 const base::ListValue* type_definitions, | 56 const base::ListValue* type_definitions, |
| 76 const base::ListValue* event_definitions, | 57 const base::ListValue* event_definitions, |
| 77 const base::DictionaryValue* property_definitions, | 58 const base::DictionaryValue* property_definitions, |
| 78 const SendRequestMethod& callback, | |
| 79 std::unique_ptr<APIBindingHooks> binding_hooks, | 59 std::unique_ptr<APIBindingHooks> binding_hooks, |
| 80 APITypeReferenceMap* type_refs, | 60 APITypeReferenceMap* type_refs, |
| 81 APIRequestHandler* request_handler, | 61 APIRequestHandler* request_handler, |
| 82 APIEventHandler* event_handler); | 62 APIEventHandler* event_handler); |
| 83 ~APIBinding(); | 63 ~APIBinding(); |
| 84 | 64 |
| 85 // Returns a new v8::Object for the API this APIBinding represents. | 65 // Returns a new v8::Object for the API this APIBinding represents. |
| 86 v8::Local<v8::Object> CreateInstance( | 66 v8::Local<v8::Object> CreateInstance( |
| 87 v8::Local<v8::Context> context, | 67 v8::Local<v8::Context> context, |
| 88 v8::Isolate* isolate, | 68 v8::Isolate* isolate, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 | 100 |
| 121 // The pair for enum entry is <original, js-ified>. JS enum entries use | 101 // The pair for enum entry is <original, js-ified>. JS enum entries use |
| 122 // SCREAMING_STYLE (whereas our API enums are just inconsistent). | 102 // SCREAMING_STYLE (whereas our API enums are just inconsistent). |
| 123 using EnumEntry = std::pair<std::string, std::string>; | 103 using EnumEntry = std::pair<std::string, std::string>; |
| 124 // A map of <name, values> for the enums on this API. | 104 // A map of <name, values> for the enums on this API. |
| 125 std::map<std::string, std::vector<EnumEntry>> enums_; | 105 std::map<std::string, std::vector<EnumEntry>> enums_; |
| 126 | 106 |
| 127 // The associated properties of the API, if any. | 107 // The associated properties of the API, if any. |
| 128 const base::DictionaryValue* property_definitions_; | 108 const base::DictionaryValue* property_definitions_; |
| 129 | 109 |
| 130 // The callback to use when an API is invoked with valid arguments. | |
| 131 SendRequestMethod method_callback_; | |
| 132 | |
| 133 // The registered hooks for this API. | 110 // The registered hooks for this API. |
| 134 std::unique_ptr<APIBindingHooks> binding_hooks_; | 111 std::unique_ptr<APIBindingHooks> binding_hooks_; |
| 135 | 112 |
| 136 // The reference map for all known types; required to outlive this object. | 113 // The reference map for all known types; required to outlive this object. |
| 137 const APITypeReferenceMap* type_refs_; | 114 const APITypeReferenceMap* type_refs_; |
| 138 | 115 |
| 139 // The associated request handler, shared between this and other bindings. | 116 // The associated request handler, shared between this and other bindings. |
| 140 // Required to outlive this object. | 117 // Required to outlive this object. |
| 141 APIRequestHandler* request_handler_; | 118 APIRequestHandler* request_handler_; |
| 142 | 119 |
| 143 // The template for this API. Note: some methods may only be available in | 120 // The template for this API. Note: some methods may only be available in |
| 144 // certain contexts, but this template contains all methods. Those that are | 121 // certain contexts, but this template contains all methods. Those that are |
| 145 // unavailable are removed after object instantiation. | 122 // unavailable are removed after object instantiation. |
| 146 v8::Eternal<v8::ObjectTemplate> object_template_; | 123 v8::Eternal<v8::ObjectTemplate> object_template_; |
| 147 | 124 |
| 148 base::WeakPtrFactory<APIBinding> weak_factory_; | 125 base::WeakPtrFactory<APIBinding> weak_factory_; |
| 149 | 126 |
| 150 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 127 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
| 151 }; | 128 }; |
| 152 | 129 |
| 153 } // namespace extensions | 130 } // namespace extensions |
| 154 | 131 |
| 155 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 132 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
| OLD | NEW |