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