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

Side by Side Diff: extensions/renderer/api_binding_hooks.h

Issue 2563093002: [Extension Bindings] Add JS custom hook support (Closed)
Patch Set: nits 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
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_BINDING_HOOKS_H_ 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ 6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
7 7
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
(...skipping 20 matching lines...) Expand all
31 // (so the caller can verify arguments, optionally after modifying/"massaging" 31 // (so the caller can verify arguments, optionally after modifying/"massaging"
32 // them) and the passed arguments. The handler is responsible for returning, 32 // them) and the passed arguments. The handler is responsible for returning,
33 // which depending on the API could mean either returning synchronously 33 // which depending on the API could mean either returning synchronously
34 // through gin::Arguments::Return or asynchronously through a passed callback. 34 // through gin::Arguments::Return or asynchronously through a passed callback.
35 // TODO(devlin): As we continue expanding the hooks interface, we should allow 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 36 // handlers to register a request so that they don't have to maintain a
37 // reference to the callback themselves. 37 // reference to the callback themselves.
38 using HandleRequestHook = 38 using HandleRequestHook =
39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>; 39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>;
40 40
41 APIBindingHooks(); 41 explicit APIBindingHooks(const binding::RunJSFunction& run_js);
42 ~APIBindingHooks(); 42 ~APIBindingHooks();
43 43
44 // Register a custom binding to handle requests. 44 // Register a custom binding to handle requests.
45 void RegisterHandleRequest(const std::string& method_name, 45 void RegisterHandleRequest(const std::string& method_name,
46 const HandleRequestHook& hook); 46 const HandleRequestHook& hook);
47 47
48 // Returns the custom hook for the given method, or a null callback if none 48 // Registers a JS script to be compiled and run in order to initialize any JS
49 // exists. 49 // hooks within a v8 context.
50 HandleRequestHook GetHandleRequest(const std::string& method_name); 50 void RegisterJsSource(v8::Global<v8::String> source,
51 v8::Global<v8::String> resource_name);
52
53 // Initializes JS hooks within a context.
54 void InitializeInContext(v8::Local<v8::Context> context,
55 const std::string& api_name);
56
57 // Looks for a custom hook to handle the given request and, if one exists,
58 // runs it. Returns true if a hook was found and run.
59 bool HandleRequest(const std::string& api_name,
60 const std::string& method_name,
61 v8::Local<v8::Context> context,
62 const binding::APISignature* signature,
63 gin::Arguments* arguments);
51 64
52 private: 65 private:
53 // Whether we've tried to use any hooks associated with this object. 66 // Whether we've tried to use any hooks associated with this object.
54 bool hooks_used_ = false; 67 bool hooks_used_ = false;
55 68
56 // All registered request handlers. 69 // All registered request handlers.
57 std::map<std::string, HandleRequestHook> request_hooks_; 70 std::map<std::string, HandleRequestHook> request_hooks_;
58 71
72 // The script to run to initialize JS hooks, if any.
73 v8::Global<v8::String> js_hooks_source_;
74
75 // The name of the JS resource for the hooks. Used to create a ScriptOrigin
76 // to make exception stack traces more readable.
77 v8::Global<v8::String> js_resource_name_;
78
79 binding::RunJSFunction run_js_;
80
59 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); 81 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks);
60 }; 82 };
61 83
62 } // namespace extensions 84 } // namespace extensions
63 85
64 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ 86 #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