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_BINDINGS_SYSTEM_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_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 "extensions/renderer/api_binding_types.h" |
| 15 #include "extensions/renderer/api_event_handler.h" |
14 #include "extensions/renderer/api_request_handler.h" | 16 #include "extensions/renderer/api_request_handler.h" |
15 #include "extensions/renderer/argument_spec.h" | 17 #include "extensions/renderer/argument_spec.h" |
16 | 18 |
17 namespace base { | 19 namespace base { |
18 class DictionaryValue; | 20 class DictionaryValue; |
19 class ListValue; | 21 class ListValue; |
20 } | 22 } |
21 | 23 |
22 namespace extensions { | 24 namespace extensions { |
23 class APIBinding; | 25 class APIBinding; |
(...skipping 12 matching lines...) Expand all Loading... |
36 | 38 |
37 std::string request_id; | 39 std::string request_id; |
38 std::string method_name; | 40 std::string method_name; |
39 std::unique_ptr<base::ListValue> arguments; | 41 std::unique_ptr<base::ListValue> arguments; |
40 }; | 42 }; |
41 | 43 |
42 using GetAPISchemaMethod = | 44 using GetAPISchemaMethod = |
43 base::Callback<const base::DictionaryValue&(const std::string&)>; | 45 base::Callback<const base::DictionaryValue&(const std::string&)>; |
44 using SendRequestMethod = base::Callback<void(std::unique_ptr<Request>)>; | 46 using SendRequestMethod = base::Callback<void(std::unique_ptr<Request>)>; |
45 | 47 |
46 APIBindingsSystem(const APIRequestHandler::CallJSFunction& call_js, | 48 APIBindingsSystem(const binding::RunJSFunction& call_js, |
47 const GetAPISchemaMethod& get_api_schema, | 49 const GetAPISchemaMethod& get_api_schema, |
48 const SendRequestMethod& send_request); | 50 const SendRequestMethod& send_request); |
49 ~APIBindingsSystem(); | 51 ~APIBindingsSystem(); |
50 | 52 |
51 // Returns a new v8::Object representing the api specified by |api_name|. | 53 // Returns a new v8::Object representing the api specified by |api_name|. |
52 v8::Local<v8::Object> CreateAPIInstance(const std::string& api_name, | 54 v8::Local<v8::Object> CreateAPIInstance(const std::string& api_name, |
53 v8::Local<v8::Context> context, | 55 v8::Local<v8::Context> context, |
54 v8::Isolate* isolate); | 56 v8::Isolate* isolate); |
55 | 57 |
56 // Responds to the request with the given |request_id|, calling the callback | 58 // Responds to the request with the given |request_id|, calling the callback |
57 // with |response|. | 59 // with |response|. |
58 void CompleteRequest(const std::string& request_id, | 60 void CompleteRequest(const std::string& request_id, |
59 const base::ListValue& response); | 61 const base::ListValue& response); |
60 | 62 |
| 63 // Notifies the APIEventHandler to fire the corresponding event, notifying |
| 64 // listeners. |
| 65 void FireEventInContext(const std::string& event_name, |
| 66 v8::Local<v8::Context> context, |
| 67 const base::ListValue& response); |
| 68 |
61 private: | 69 private: |
62 // Creates a new APIBinding for the given |api_name|. | 70 // Creates a new APIBinding for the given |api_name|. |
63 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); | 71 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); |
64 | 72 |
65 // Handles a call into an API, adds a pending request to the | 73 // Handles a call into an API, adds a pending request to the |
66 // |request_handler_|, and calls |send_request_|. | 74 // |request_handler_|, and calls |send_request_|. |
67 void OnAPICall(const std::string& name, | 75 void OnAPICall(const std::string& name, |
68 std::unique_ptr<base::ListValue> arguments, | 76 std::unique_ptr<base::ListValue> arguments, |
69 v8::Isolate* isolate, | 77 v8::Isolate* isolate, |
70 v8::Local<v8::Context> context, | 78 v8::Local<v8::Context> context, |
71 v8::Local<v8::Function> callback); | 79 v8::Local<v8::Function> callback); |
72 | 80 |
73 // The map of cached API reference types. | 81 // The map of cached API reference types. |
74 ArgumentSpec::RefMap type_reference_map_; | 82 ArgumentSpec::RefMap type_reference_map_; |
75 | 83 |
76 // The request handler associated with the system. | 84 // The request handler associated with the system. |
77 APIRequestHandler request_handler_; | 85 APIRequestHandler request_handler_; |
78 | 86 |
| 87 // The event handler associated with the system. |
| 88 APIEventHandler event_handler_; |
| 89 |
79 // A map from api_name -> APIBinding for constructed APIs. APIBindings are | 90 // A map from api_name -> APIBinding for constructed APIs. APIBindings are |
80 // created lazily. | 91 // created lazily. |
81 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; | 92 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; |
82 | 93 |
83 // The method to retrieve the DictionaryValue describing a given extension | 94 // The method to retrieve the DictionaryValue describing a given extension |
84 // API. Curried in for testing purposes so we can use fake APIs. | 95 // API. Curried in for testing purposes so we can use fake APIs. |
85 GetAPISchemaMethod get_api_schema_; | 96 GetAPISchemaMethod get_api_schema_; |
86 | 97 |
87 // The method to call when a new API call is triggered. Curried in for testing | 98 // The method to call when a new API call is triggered. Curried in for testing |
88 // purposes. Typically, this would send an IPC to the browser to begin the | 99 // purposes. Typically, this would send an IPC to the browser to begin the |
89 // function work. | 100 // function work. |
90 SendRequestMethod send_request_; | 101 SendRequestMethod send_request_; |
91 | 102 |
92 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); | 103 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); |
93 }; | 104 }; |
94 | 105 |
95 } // namespace | 106 } // namespace |
96 | 107 |
97 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 108 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
OLD | NEW |