OLD | NEW |
---|---|
(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/argument_spec.h" | |
15 | |
16 namespace base { | |
17 class DictionaryValue; | |
18 class ListValue; | |
19 } | |
20 | |
21 namespace extensions { | |
22 class APIBinding; | |
23 class APIRequestHandler; | |
24 | |
25 // A class encompassing the necessary pieces to construct the JS entry points | |
26 // for Extension APIs. Designed to be used on a single thread, but safe between | |
27 // multiple v8::Contexts. | |
28 class APIBindingsSystem { | |
29 public: | |
30 // TODO(devlin): We will probably want to coalesce this with the | |
31 // ExtensionHostMsg_Request_Params IPC struct. | |
32 struct Request { | |
33 Request(); | |
34 ~Request(); | |
35 | |
36 std::string request_id; | |
37 std::string method_name; | |
38 std::unique_ptr<base::ListValue> arguments; | |
39 }; | |
40 | |
41 using GetAPISchemaMethod = | |
jbroman
2016/10/21 22:53:59
I take it you expect the real implementation to be
Devlin
2016/10/22 00:49:15
You can see the current "real" implementation in t
| |
42 base::Callback<const base::DictionaryValue&(const std::string&)>; | |
43 using SendRequestMethod = base::Callback<void(std::unique_ptr<Request>)>; | |
44 | |
45 APIBindingsSystem(std::unique_ptr<APIRequestHandler> request_handler, | |
46 const GetAPISchemaMethod& get_api_schema, | |
47 const SendRequestMethod& send_request); | |
48 ~APIBindingsSystem(); | |
49 | |
50 // Returns a new v8::Object representing the api specified by |api_name|. | |
51 v8::Local<v8::Object> CreateAPIInstance(const std::string& api_name, | |
52 v8::Local<v8::Context> context, | |
53 v8::Isolate* isolate); | |
54 | |
55 APIRequestHandler* request_handler() { return request_handler_.get(); } | |
jbroman
2016/10/21 22:53:59
nit: It seems a little asymmetric that users of th
Devlin
2016/10/22 00:49:15
I was a little torn on this. This works, but mean
| |
56 | |
57 private: | |
58 // Creates a new APIBinding for the given |api_name|. | |
59 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); | |
60 | |
61 // Handles a call into an API, adds a pending request to the request_handler_, | |
62 // and calls send_request_. | |
63 void OnAPICall(const std::string& name, | |
64 std::unique_ptr<base::ListValue> arguments, | |
65 v8::Isolate* isolate, | |
66 v8::Local<v8::Context> context, | |
67 v8::Local<v8::Function> callback); | |
68 | |
69 // The map of cached API reference types. | |
70 ArgumentSpec::RefMap type_reference_map_; | |
71 | |
72 // The request handler associated with the system. | |
73 std::unique_ptr<APIRequestHandler> request_handler_; | |
74 | |
75 // A map from api_name -> APIBinding for constructed APIs. APIBindings are | |
76 // created lazily. | |
77 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; | |
78 | |
79 // The method to retrieve the DictionaryValue describing a given extension | |
80 // API. Curried in for testing purposes so we can use fake APIs. | |
81 GetAPISchemaMethod get_api_schema_; | |
82 | |
83 // The method to call when a new API call is triggered. Curried in for testing | |
84 // purposes. Typically, this would send an IPC to the browser to begin the | |
85 // function work. | |
86 SendRequestMethod send_request_; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); | |
89 }; | |
90 | |
91 } // namespace | |
92 | |
93 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | |
OLD | NEW |