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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_BINDING_HOOKS_H_
6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_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_binding_types.h"
15 #include "v8/include/v8.h"
16
17 namespace gin {
18 class Arguments;
19 }
20
21 namespace extensions {
22
23 // A class to register custom hooks for given API calls that need different
24 // handling. An instance exists for a single API, but can be used across
25 // multiple contexts (but only on the same thread).
26 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows
27 // for registration of C++ handlers. Add JS support.
28 class APIBindingHooks {
29 public:
30 // The callback to handle an API method. We pass in the expected signature
31 // (so the caller can verify arguments, optionally after modifying/"massaging"
32 // them) and the passed arguments. The handler is responsible for returning,
33 // which depending on the API could mean either returning synchronously
34 // through gin::Arguments::Return or asynchronously through a passed callback.
35 // TODO(devlin): As we continue expanding the hooks interface, we should allow
36 // handlers to register a request so that they don't have to maintain a
37 // reference to the callback themselves.
38 using HandleRequestHook =
39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>;
40
41 APIBindingHooks();
42 ~APIBindingHooks();
43
44 // Register a custom binding to handle requests.
45 void RegisterHandleRequest(const std::string& method_name,
46 const HandleRequestHook& hook);
47
48 // Returns the custom hook for the given method, or a null callback if none
49 // exists.
50 HandleRequestHook GetHandleRequest(const std::string& method_name);
51
52 private:
53 // Whether we've tried to use any hooks associated with this object.
54 bool hooks_used_ = false;
55
56 // All registered request handlers.
57 std::map<std::string, HandleRequestHook> request_hooks_;
58
59 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks);
60 };
61
62 } // namespace extensions
63
64 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
OLDNEW
« 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