| 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> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/supports_user_data.h" | 15 #include "base/supports_user_data.h" |
| 16 #include "extensions/renderer/argument_spec.h" | 16 #include "extensions/renderer/argument_spec.h" |
| 17 #include "v8/include/v8.h" | 17 #include "v8/include/v8.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class ListValue; | 20 class ListValue; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace gin { | 23 namespace gin { |
| 24 class Arguments; | 24 class Arguments; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 class APIEventHandler; |
| 28 | 29 |
| 29 // A class that vends v8::Objects for extension APIs. These APIs have function | 30 // A class that vends v8::Objects for extension APIs. These APIs have function |
| 30 // interceptors for all exposed methods, which call back into the APIBinding. | 31 // interceptors for all exposed methods, which call back into the APIBinding. |
| 31 // The APIBinding then matches the calling arguments against an expected method | 32 // The APIBinding then matches the calling arguments against an expected method |
| 32 // signature, throwing an error if they don't match. | 33 // signature, throwing an error if they don't match. |
| 33 // There should only need to be a single APIBinding object for each API, and | 34 // There should only need to be a single APIBinding object for each API, and |
| 34 // each can vend multiple v8::Objects for different contexts. | 35 // each can vend multiple v8::Objects for different contexts. |
| 35 // TODO(devlin): What's the lifetime of this object? | 36 // TODO(devlin): What's the lifetime of this object? |
| 36 class APIBinding { | 37 class APIBinding { |
| 37 public: | 38 public: |
| 38 // The callback to called when an API method is invoked with matching | 39 // The callback to called when an API method is invoked with matching |
| 39 // arguments. This passes the name of the api method and the arguments it | 40 // arguments. This passes the name of the api method and the arguments it |
| 40 // was passed, as well as the current isolate, context, and callback value. | 41 // was passed, as well as the current isolate, context, and callback value. |
| 41 // Note that the callback can be empty if none was passed. | 42 // Note that the callback can be empty if none was passed. |
| 42 using APIMethodCallback = | 43 using APIMethodCallback = |
| 43 base::Callback<void(const std::string& name, | 44 base::Callback<void(const std::string& name, |
| 44 std::unique_ptr<base::ListValue> arguments, | 45 std::unique_ptr<base::ListValue> arguments, |
| 45 v8::Isolate*, | 46 v8::Isolate*, |
| 46 v8::Local<v8::Context>, | 47 v8::Local<v8::Context>, |
| 47 v8::Local<v8::Function>)>; | 48 v8::Local<v8::Function>)>; |
| 48 | 49 |
| 49 // The callback type for handling an API call. | 50 // The callback type for handling an API call. |
| 50 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 51 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
| 51 | 52 |
| 52 // The ArgumentSpec::RefMap is required to outlive this object. | 53 // The ArgumentSpec::RefMap is required to outlive this object. |
| 53 // |type_definitions| may be null if the API does not specify any types. | 54 // |type_definitions| and |event_definitions| may be null if the API does not |
| 55 // specify any types or events. |
| 54 APIBinding(const std::string& name, | 56 APIBinding(const std::string& name, |
| 55 const base::ListValue& function_definitions, | 57 const base::ListValue& function_definitions, |
| 56 const base::ListValue* type_definitions, | 58 const base::ListValue* type_definitions, |
| 59 const base::ListValue* event_definitions, |
| 57 const APIMethodCallback& callback, | 60 const APIMethodCallback& callback, |
| 58 ArgumentSpec::RefMap* type_refs); | 61 ArgumentSpec::RefMap* type_refs); |
| 59 ~APIBinding(); | 62 ~APIBinding(); |
| 60 | 63 |
| 61 // Returns a new v8::Object for the API this APIBinding represents. | 64 // Returns a new v8::Object for the API this APIBinding represents. |
| 62 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context, | 65 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context, |
| 63 v8::Isolate* isolate); | 66 v8::Isolate* isolate, |
| 67 APIEventHandler* event_handler); |
| 64 | 68 |
| 65 private: | 69 private: |
| 66 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>; | 70 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>; |
| 67 | 71 |
| 68 // Per-context data that stores the callbacks that are used within the | 72 // Per-context data that stores the callbacks that are used within the |
| 69 // context. Since these callbacks are used within v8::Externals, v8 itself | 73 // context. Since these callbacks are used within v8::Externals, v8 itself |
| 70 // does not clean them up. | 74 // does not clean them up. |
| 71 struct APIPerContextData : public base::SupportsUserData::Data { | 75 struct APIPerContextData : public base::SupportsUserData::Data { |
| 72 APIPerContextData(); | 76 APIPerContextData(); |
| 73 ~APIPerContextData() override; | 77 ~APIPerContextData() override; |
| 74 | 78 |
| 75 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks; | 79 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks; |
| 76 }; | 80 }; |
| 77 | 81 |
| 78 // Handles a call an API method with the given |name| and matches the | 82 // Handles a call an API method with the given |name| and matches the |
| 79 // arguments against |signature|. | 83 // arguments against |signature|. |
| 80 void HandleCall(const std::string& name, | 84 void HandleCall(const std::string& name, |
| 81 const APISignature* signature, | 85 const APISignature* signature, |
| 82 gin::Arguments* args); | 86 gin::Arguments* args); |
| 83 | 87 |
| 84 // The root name of the API, e.g. "tabs" for chrome.tabs. | 88 // The root name of the API, e.g. "tabs" for chrome.tabs. |
| 85 std::string api_name_; | 89 std::string api_name_; |
| 86 | 90 |
| 87 // A map from method name to expected signature. | 91 // A map from method name to expected signature. |
| 88 std::map<std::string, std::unique_ptr<APISignature>> signatures_; | 92 std::map<std::string, std::unique_ptr<APISignature>> signatures_; |
| 89 | 93 |
| 94 // The names of all events associated with this API. |
| 95 std::vector<std::string> event_names_; |
| 96 |
| 90 // The callback to use when an API is invoked with valid arguments. | 97 // The callback to use when an API is invoked with valid arguments. |
| 91 APIMethodCallback method_callback_; | 98 APIMethodCallback method_callback_; |
| 92 | 99 |
| 93 // The reference map for all known types; required to outlive this object. | 100 // The reference map for all known types; required to outlive this object. |
| 94 const ArgumentSpec::RefMap* type_refs_; | 101 const ArgumentSpec::RefMap* type_refs_; |
| 95 | 102 |
| 96 base::WeakPtrFactory<APIBinding> weak_factory_; | 103 base::WeakPtrFactory<APIBinding> weak_factory_; |
| 97 | 104 |
| 98 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 105 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
| 99 }; | 106 }; |
| 100 | 107 |
| 101 } // namespace extensions | 108 } // namespace extensions |
| 102 | 109 |
| 103 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 110 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
| OLD | NEW |