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

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

Issue 2598123002: [Extensions Bindings] Add support for updateArgumentsPreValidate (Closed)
Patch Set: rebase Created 3 years, 11 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 unified diff | Download patch
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 14 matching lines...) Expand all
25 // A class to register custom hooks for given API calls that need different 25 // A class to register custom hooks for given API calls that need different
26 // handling. An instance exists for a single API, but can be used across 26 // handling. An instance exists for a single API, but can be used across
27 // multiple contexts (but only on the same thread). 27 // multiple contexts (but only on the same thread).
28 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows 28 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows
29 // for registration of C++ handlers. Add JS support. 29 // for registration of C++ handlers. Add JS support.
30 class APIBindingHooks { 30 class APIBindingHooks {
31 public: 31 public:
32 // The result of checking for hooks to handle a request. 32 // The result of checking for hooks to handle a request.
33 enum class RequestResult { 33 enum class RequestResult {
34 HANDLED, // A custom hook handled the request. 34 HANDLED, // A custom hook handled the request.
35 THROWN, // An exception was thrown during parsing or handling.
35 INVALID_INVOCATION, // The request was called with invalid arguments. 36 INVALID_INVOCATION, // The request was called with invalid arguments.
36 NOT_HANDLED, // The request was not handled. 37 NOT_HANDLED, // The request was not handled.
37 }; 38 };
38 39
39 // The callback to handle an API method. We pass in the expected signature 40 // The callback to handle an API method. We pass in the expected signature
40 // (so the caller can verify arguments, optionally after modifying/"massaging" 41 // (so the caller can verify arguments, optionally after modifying/"massaging"
41 // them) and the passed arguments. The handler is responsible for returning, 42 // them) and the passed arguments. The handler is responsible for returning,
42 // which depending on the API could mean either returning synchronously 43 // which depending on the API could mean either returning synchronously
43 // through gin::Arguments::Return or asynchronously through a passed callback. 44 // through gin::Arguments::Return or asynchronously through a passed callback.
44 // TODO(devlin): As we continue expanding the hooks interface, we should allow 45 // TODO(devlin): As we continue expanding the hooks interface, we should allow
45 // handlers to register a request so that they don't have to maintain a 46 // handlers to register a request so that they don't have to maintain a
46 // reference to the callback themselves. 47 // reference to the callback themselves.
47 using HandleRequestHook = 48 using HandleRequestHook =
48 base::Callback<RequestResult(const APISignature*, 49 base::Callback<RequestResult(const APISignature*,
49 gin::Arguments*, 50 v8::Local<v8::Context> context,
51 std::vector<v8::Local<v8::Value>>*,
50 const ArgumentSpec::RefMap&)>; 52 const ArgumentSpec::RefMap&)>;
51 53
52 explicit APIBindingHooks(const binding::RunJSFunction& run_js); 54 explicit APIBindingHooks(const binding::RunJSFunctionSync& run_js);
53 ~APIBindingHooks(); 55 ~APIBindingHooks();
54 56
55 // Register a custom binding to handle requests. 57 // Register a custom binding to handle requests.
56 void RegisterHandleRequest(const std::string& method_name, 58 void RegisterHandleRequest(const std::string& method_name,
57 const HandleRequestHook& hook); 59 const HandleRequestHook& hook);
58 60
59 // Registers a JS script to be compiled and run in order to initialize any JS 61 // Registers a JS script to be compiled and run in order to initialize any JS
60 // hooks within a v8 context. 62 // hooks within a v8 context.
61 void RegisterJsSource(v8::Global<v8::String> source, 63 void RegisterJsSource(v8::Global<v8::String> source,
62 v8::Global<v8::String> resource_name); 64 v8::Global<v8::String> resource_name);
63 65
64 // Initializes JS hooks within a context. 66 // Initializes JS hooks within a context.
65 void InitializeInContext(v8::Local<v8::Context> context, 67 void InitializeInContext(v8::Local<v8::Context> context,
66 const std::string& api_name); 68 const std::string& api_name);
67 69
68 // Looks for a custom hook to handle the given request and, if one exists, 70 // Looks for a custom hook to handle the given request and, if one exists,
69 // runs it. Returns the result of trying to run the hook, or NOT_HANDLED if no 71 // runs it. Returns the result of trying to run the hook, or NOT_HANDLED if no
70 // hook was found. 72 // hook was found.
71 RequestResult HandleRequest(const std::string& api_name, 73 RequestResult HandleRequest(const std::string& api_name,
72 const std::string& method_name, 74 const std::string& method_name,
73 v8::Local<v8::Context> context, 75 v8::Local<v8::Context> context,
74 const APISignature* signature, 76 const APISignature* signature,
75 gin::Arguments* arguments, 77 std::vector<v8::Local<v8::Value>>* arguments,
76 const ArgumentSpec::RefMap& type_refs); 78 const ArgumentSpec::RefMap& type_refs);
77 79
78 // Returns a JS interface that can be used to register hooks. 80 // Returns a JS interface that can be used to register hooks.
79 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name, 81 v8::Local<v8::Object> GetJSHookInterface(const std::string& api_name,
80 v8::Local<v8::Context> context); 82 v8::Local<v8::Context> context);
81 83
82 private: 84 private:
85 // Updates the |arguments| by running |function| and settings arguments to the
86 // returned result.
87 bool UpdateArguments(v8::Local<v8::Function> function,
88 v8::Local<v8::Context> context,
89 std::vector<v8::Local<v8::Value>>* arguments);
90
83 // Whether we've tried to use any hooks associated with this object. 91 // Whether we've tried to use any hooks associated with this object.
84 bool hooks_used_ = false; 92 bool hooks_used_ = false;
85 93
86 // All registered request handlers. 94 // All registered request handlers.
87 std::map<std::string, HandleRequestHook> request_hooks_; 95 std::map<std::string, HandleRequestHook> request_hooks_;
88 96
89 // The script to run to initialize JS hooks, if any. 97 // The script to run to initialize JS hooks, if any.
90 v8::Global<v8::String> js_hooks_source_; 98 v8::Global<v8::String> js_hooks_source_;
91 99
92 // The name of the JS resource for the hooks. Used to create a ScriptOrigin 100 // The name of the JS resource for the hooks. Used to create a ScriptOrigin
93 // to make exception stack traces more readable. 101 // to make exception stack traces more readable.
94 v8::Global<v8::String> js_resource_name_; 102 v8::Global<v8::String> js_resource_name_;
95 103
96 binding::RunJSFunction run_js_; 104 // We use synchronous JS execution here because at every point we execute JS,
105 // it's in direct response to JS calling in. There should be no reason that
106 // script is disabled.
107 binding::RunJSFunctionSync run_js_;
97 108
98 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); 109 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks);
99 }; 110 };
100 111
101 } // namespace extensions 112 } // namespace extensions
102 113
103 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ 114 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698