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

Unified Diff: extensions/renderer/api_bindings_system.h

Issue 2438623002: [Extensions Bindings] Add APIBindingsSystem (Closed)
Patch Set: woot Created 4 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: extensions/renderer/api_bindings_system.h
diff --git a/extensions/renderer/api_bindings_system.h b/extensions/renderer/api_bindings_system.h
new file mode 100644
index 0000000000000000000000000000000000000000..88716affe6add64f5a4fe06146976e5aa68da115
--- /dev/null
+++ b/extensions/renderer/api_bindings_system.h
@@ -0,0 +1,93 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_
+#define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "extensions/renderer/argument_spec.h"
+
+namespace base {
+class DictionaryValue;
+class ListValue;
+}
+
+namespace extensions {
+class APIBinding;
+class APIRequestHandler;
+
+// A class encompassing the necessary pieces to construct the JS entry points
+// for Extension APIs. Designed to be used on a single thread, but safe between
+// multiple v8::Contexts.
+class APIBindingsSystem {
+ public:
+ // TODO(devlin): We will probably want to coalesce this with the
+ // ExtensionHostMsg_Request_Params IPC struct.
+ struct Request {
+ Request();
+ ~Request();
+
+ std::string request_id;
+ std::string method_name;
+ std::unique_ptr<base::ListValue> arguments;
+ };
+
+ 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
+ base::Callback<const base::DictionaryValue&(const std::string&)>;
+ using SendRequestMethod = base::Callback<void(std::unique_ptr<Request>)>;
+
+ APIBindingsSystem(std::unique_ptr<APIRequestHandler> request_handler,
+ const GetAPISchemaMethod& get_api_schema,
+ const SendRequestMethod& send_request);
+ ~APIBindingsSystem();
+
+ // Returns a new v8::Object representing the api specified by |api_name|.
+ v8::Local<v8::Object> CreateAPIInstance(const std::string& api_name,
+ v8::Local<v8::Context> context,
+ v8::Isolate* isolate);
+
+ 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
+
+ private:
+ // Creates a new APIBinding for the given |api_name|.
+ std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name);
+
+ // Handles a call into an API, adds a pending request to the request_handler_,
+ // and calls send_request_.
+ void OnAPICall(const std::string& name,
+ std::unique_ptr<base::ListValue> arguments,
+ v8::Isolate* isolate,
+ v8::Local<v8::Context> context,
+ v8::Local<v8::Function> callback);
+
+ // The map of cached API reference types.
+ ArgumentSpec::RefMap type_reference_map_;
+
+ // The request handler associated with the system.
+ std::unique_ptr<APIRequestHandler> request_handler_;
+
+ // A map from api_name -> APIBinding for constructed APIs. APIBindings are
+ // created lazily.
+ std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_;
+
+ // The method to retrieve the DictionaryValue describing a given extension
+ // API. Curried in for testing purposes so we can use fake APIs.
+ GetAPISchemaMethod get_api_schema_;
+
+ // The method to call when a new API call is triggered. Curried in for testing
+ // purposes. Typically, this would send an IPC to the browser to begin the
+ // function work.
+ SendRequestMethod send_request_;
+
+ DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem);
+};
+
+} // namespace
+
+#endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_

Powered by Google App Engine
This is Rietveld 408576698