Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1353)

Side by Side Diff: extensions/renderer/api_binding.h

Issue 2469593002: [Extensions Bindings] Add Events support (Closed)
Patch Set: asantest Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/BUILD.gn ('k') | extensions/renderer/api_binding.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 for determining if a given API method (specified by |name|) 50 // The callback for determining if a given API method (specified by |name|)
50 // is available. 51 // is available.
51 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; 52 using AvailabilityCallback = base::Callback<bool(const std::string& name)>;
52 53
53 // The callback type for handling an API call. 54 // The callback type for handling an API call.
54 using HandlerCallback = base::Callback<void(gin::Arguments*)>; 55 using HandlerCallback = base::Callback<void(gin::Arguments*)>;
55 56
56 // The ArgumentSpec::RefMap is required to outlive this object. 57 // The ArgumentSpec::RefMap is required to outlive this object.
57 // |type_definitions| may be null if the API does not specify any types. 58 // |type_definitions| and |event_definitions| may be null if the API does not
59 // specify any types or events.
58 APIBinding(const std::string& name, 60 APIBinding(const std::string& name,
59 const base::ListValue& function_definitions, 61 const base::ListValue& function_definitions,
60 const base::ListValue* type_definitions, 62 const base::ListValue* type_definitions,
63 const base::ListValue* event_definitions,
61 const APIMethodCallback& callback, 64 const APIMethodCallback& callback,
62 ArgumentSpec::RefMap* type_refs); 65 ArgumentSpec::RefMap* type_refs);
63 ~APIBinding(); 66 ~APIBinding();
64 67
65 // Returns a new v8::Object for the API this APIBinding represents. 68 // Returns a new v8::Object for the API this APIBinding represents.
66 v8::Local<v8::Object> CreateInstance( 69 v8::Local<v8::Object> CreateInstance(
67 v8::Local<v8::Context> context, 70 v8::Local<v8::Context> context,
68 v8::Isolate* isolate, 71 v8::Isolate* isolate,
72 APIEventHandler* event_handler,
69 const AvailabilityCallback& is_available); 73 const AvailabilityCallback& is_available);
70 74
71 private: 75 private:
72 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>; 76 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>;
73 77
74 // Per-context data that stores the callbacks that are used within the 78 // Per-context data that stores the callbacks that are used within the
75 // context. Since these callbacks are used within v8::Externals, v8 itself 79 // context. Since these callbacks are used within v8::Externals, v8 itself
76 // does not clean them up. 80 // does not clean them up.
77 struct APIPerContextData : public base::SupportsUserData::Data { 81 struct APIPerContextData : public base::SupportsUserData::Data {
78 APIPerContextData(); 82 APIPerContextData();
79 ~APIPerContextData() override; 83 ~APIPerContextData() override;
80 84
81 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks; 85 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks;
82 }; 86 };
83 87
84 // Handles a call an API method with the given |name| and matches the 88 // Handles a call an API method with the given |name| and matches the
85 // arguments against |signature|. 89 // arguments against |signature|.
86 void HandleCall(const std::string& name, 90 void HandleCall(const std::string& name,
87 const APISignature* signature, 91 const APISignature* signature,
88 gin::Arguments* args); 92 gin::Arguments* args);
89 93
90 // The root name of the API, e.g. "tabs" for chrome.tabs. 94 // The root name of the API, e.g. "tabs" for chrome.tabs.
91 std::string api_name_; 95 std::string api_name_;
92 96
93 // A map from method name to expected signature. 97 // A map from method name to expected signature.
94 std::map<std::string, std::unique_ptr<APISignature>> signatures_; 98 std::map<std::string, std::unique_ptr<APISignature>> signatures_;
95 99
100 // The names of all events associated with this API.
101 std::vector<std::string> event_names_;
102
96 // The callback to use when an API is invoked with valid arguments. 103 // The callback to use when an API is invoked with valid arguments.
97 APIMethodCallback method_callback_; 104 APIMethodCallback method_callback_;
98 105
99 // The reference map for all known types; required to outlive this object. 106 // The reference map for all known types; required to outlive this object.
100 const ArgumentSpec::RefMap* type_refs_; 107 const ArgumentSpec::RefMap* type_refs_;
101 108
102 base::WeakPtrFactory<APIBinding> weak_factory_; 109 base::WeakPtrFactory<APIBinding> weak_factory_;
103 110
104 DISALLOW_COPY_AND_ASSIGN(APIBinding); 111 DISALLOW_COPY_AND_ASSIGN(APIBinding);
105 }; 112 };
106 113
107 } // namespace extensions 114 } // namespace extensions
108 115
109 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ 116 #endif // EXTENSIONS_RENDERER_API_BINDING_H_
OLDNEW
« no previous file with comments | « extensions/renderer/BUILD.gn ('k') | extensions/renderer/api_binding.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698