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

Unified Diff: extensions/renderer/api_binding_hooks.h

Issue 2552343006: [Extensions Binding] Allow for registering custom hooks (Closed)
Patch Set: . Created 4 years 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
« no previous file with comments | « extensions/renderer/api_binding.cc ('k') | extensions/renderer/api_binding_hooks.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: extensions/renderer/api_binding_hooks.h
diff --git a/extensions/renderer/api_binding_hooks.h b/extensions/renderer/api_binding_hooks.h
new file mode 100644
index 0000000000000000000000000000000000000000..c075c1d175edf4de4239d73b6c1a42f9c9061816
--- /dev/null
+++ b/extensions/renderer/api_binding_hooks.h
@@ -0,0 +1,64 @@
+// 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_BINDING_HOOKS_H_
+#define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
+
+#include <map>
+#include <memory>
+#include <string>
+
+#include "base/callback.h"
+#include "base/macros.h"
+#include "extensions/renderer/api_binding_types.h"
+#include "v8/include/v8.h"
+
+namespace gin {
+class Arguments;
+}
+
+namespace extensions {
+
+// A class to register custom hooks for given API calls that need different
+// handling. An instance exists for a single API, but can be used across
+// multiple contexts (but only on the same thread).
+// TODO(devlin): We have both C++ and JS custom bindings, but this only allows
+// for registration of C++ handlers. Add JS support.
+class APIBindingHooks {
+ public:
+ // The callback to handle an API method. We pass in the expected signature
+ // (so the caller can verify arguments, optionally after modifying/"massaging"
+ // them) and the passed arguments. The handler is responsible for returning,
+ // which depending on the API could mean either returning synchronously
+ // through gin::Arguments::Return or asynchronously through a passed callback.
+ // TODO(devlin): As we continue expanding the hooks interface, we should allow
+ // handlers to register a request so that they don't have to maintain a
+ // reference to the callback themselves.
+ using HandleRequestHook =
+ base::Callback<void(const binding::APISignature*, gin::Arguments*)>;
+
+ APIBindingHooks();
+ ~APIBindingHooks();
+
+ // Register a custom binding to handle requests.
+ void RegisterHandleRequest(const std::string& method_name,
+ const HandleRequestHook& hook);
+
+ // Returns the custom hook for the given method, or a null callback if none
+ // exists.
+ HandleRequestHook GetHandleRequest(const std::string& method_name);
+
+ private:
+ // Whether we've tried to use any hooks associated with this object.
+ bool hooks_used_ = false;
+
+ // All registered request handlers.
+ std::map<std::string, HandleRequestHook> request_hooks_;
+
+ DISALLOW_COPY_AND_ASSIGN(APIBindingHooks);
+};
+
+} // namespace extensions
+
+#endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
« no previous file with comments | « extensions/renderer/api_binding.cc ('k') | extensions/renderer/api_binding_hooks.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698