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

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

Issue 2947463002: [Extensions Bindings] Add a bindings/ subdirectory under renderer (Closed)
Patch Set: . Created 3 years, 6 months 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/api_binding_unittest.cc ('k') | extensions/renderer/api_bindings_system.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_
6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/macros.h"
14 #include "extensions/renderer/api_binding.h"
15 #include "extensions/renderer/api_binding_types.h"
16 #include "extensions/renderer/api_event_handler.h"
17 #include "extensions/renderer/api_last_error.h"
18 #include "extensions/renderer/api_request_handler.h"
19 #include "extensions/renderer/api_type_reference_map.h"
20 #include "extensions/renderer/binding_access_checker.h"
21
22 namespace base {
23 class DictionaryValue;
24 class ListValue;
25 }
26
27 namespace extensions {
28 class APIBindingHooks;
29
30 // A class encompassing the necessary pieces to construct the JS entry points
31 // for Extension APIs. Designed to be used on a single thread, but safe between
32 // multiple v8::Contexts.
33 class APIBindingsSystem {
34 public:
35 using GetAPISchemaMethod =
36 base::Callback<const base::DictionaryValue&(const std::string&)>;
37 using CustomTypeHandler = base::Callback<v8::Local<v8::Object>(
38 v8::Isolate* isolate,
39 const std::string& property_name,
40 const base::ListValue* property_values,
41 APIRequestHandler* request_handler,
42 APIEventHandler* event_handler,
43 APITypeReferenceMap* type_refs,
44 const BindingAccessChecker* access_checker)>;
45
46 APIBindingsSystem(
47 const binding::RunJSFunction& call_js,
48 const binding::RunJSFunctionSync& call_js_sync,
49 const GetAPISchemaMethod& get_api_schema,
50 const BindingAccessChecker::AvailabilityCallback& is_available,
51 const APIRequestHandler::SendRequestMethod& send_request,
52 const APIEventHandler::EventListenersChangedMethod&
53 event_listeners_changed,
54 APILastError last_error);
55 ~APIBindingsSystem();
56
57 // Returns a new v8::Object representing the api specified by |api_name|.
58 v8::Local<v8::Object> CreateAPIInstance(
59 const std::string& api_name,
60 v8::Local<v8::Context> context,
61 APIBindingHooks** hooks_out);
62
63 // Responds to the request with the given |request_id|, calling the callback
64 // with |response|. If |error| is non-empty, sets the last error.
65 void CompleteRequest(int request_id,
66 const base::ListValue& response,
67 const std::string& error);
68
69 // Notifies the APIEventHandler to fire the corresponding event, notifying
70 // listeners.
71 void FireEventInContext(const std::string& event_name,
72 v8::Local<v8::Context> context,
73 const base::ListValue& response,
74 const EventFilteringInfo& filter);
75
76 // Returns the APIBindingHooks object for the given api to allow for
77 // registering custom hooks. These must be registered *before* the
78 // binding is instantiated.
79 // TODO(devlin): It's a little weird that we don't just expose a
80 // RegisterHooks-type method. Depending on how complex the hook interface
81 // is, maybe we should rethink this. Downside would be that it's less
82 // efficient to register multiple hooks for the same API.
83 APIBindingHooks* GetHooksForAPI(const std::string& api_name);
84
85 // Registers the handler for creating a custom type with the given
86 // |type_name|, where |type_name| is the fully-qualified type (e.g.
87 // storage.StorageArea).
88 void RegisterCustomType(const std::string& type_name,
89 const CustomTypeHandler& function);
90
91 // Handles any cleanup necessary before releasing the given |context|.
92 void WillReleaseContext(v8::Local<v8::Context> context);
93
94 APIRequestHandler* request_handler() { return &request_handler_; }
95 APIEventHandler* event_handler() { return &event_handler_; }
96 APITypeReferenceMap* type_reference_map() { return &type_reference_map_; }
97
98 private:
99 // Creates a new APIBinding for the given |api_name|.
100 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name);
101
102 // Callback for the APITypeReferenceMap in order to initialize an unknown
103 // type.
104 void InitializeType(const std::string& name);
105
106 // Handles creating the type for the specified property.
107 v8::Local<v8::Object> CreateCustomType(
108 v8::Isolate* isolate,
109 const std::string& type_name,
110 const std::string& property_name,
111 const base::ListValue* property_values);
112
113 // The map of cached API reference types.
114 APITypeReferenceMap type_reference_map_;
115
116 // The request handler associated with the system.
117 APIRequestHandler request_handler_;
118
119 // The event handler associated with the system.
120 APIEventHandler event_handler_;
121
122 // The access checker associated with the system.
123 BindingAccessChecker access_checker_;
124
125 // A map from api_name -> APIBinding for constructed APIs. APIBindings are
126 // created lazily.
127 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_;
128
129 // A map from api_name -> APIBindingHooks for registering custom hooks.
130 // TODO(devlin): This map is pretty pointer-y. Is that going to be a
131 // performance concern?
132 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_;
133
134 std::map<std::string, CustomTypeHandler> custom_types_;
135
136 binding::RunJSFunction call_js_;
137
138 binding::RunJSFunctionSync call_js_sync_;
139
140 // The method to retrieve the DictionaryValue describing a given extension
141 // API. Curried in for testing purposes so we can use fake APIs.
142 GetAPISchemaMethod get_api_schema_;
143
144 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem);
145 };
146
147 } // namespace
148
149 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_
OLDNEW
« no previous file with comments | « extensions/renderer/api_binding_unittest.cc ('k') | extensions/renderer/api_bindings_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698