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

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

Powered by Google App Engine
This is Rietveld 408576698