| OLD | NEW |
| (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 <memory> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/macros.h" | |
| 12 #include "extensions/renderer/api_binding_types.h" | |
| 13 #include "v8/include/v8.h" | |
| 14 | |
| 15 namespace extensions { | |
| 16 class APIBindingHooksDelegate; | |
| 17 class APITypeReferenceMap; | |
| 18 class APISignature; | |
| 19 | |
| 20 // A class to register custom hooks for given API calls that need different | |
| 21 // handling. An instance exists for a single API, but can be used across | |
| 22 // multiple contexts (but only on the same thread). | |
| 23 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows | |
| 24 // for registration of C++ handlers. Add JS support. | |
| 25 class APIBindingHooks { | |
| 26 public: | |
| 27 // The result of checking for hooks to handle a request. | |
| 28 struct RequestResult { | |
| 29 enum ResultCode { | |
| 30 HANDLED, // A custom hook handled the request. | |
| 31 ARGUMENTS_UPDATED, // The arguments were updated post-validation. | |
| 32 THROWN, // An exception was thrown during parsing or | |
| 33 // handling. | |
| 34 INVALID_INVOCATION, // The request was called with invalid arguments. | |
| 35 // |error| will contain the invocation error. | |
| 36 NOT_HANDLED, // The request was not handled. | |
| 37 }; | |
| 38 | |
| 39 explicit RequestResult(ResultCode code); | |
| 40 RequestResult(ResultCode code, v8::Local<v8::Function> custom_callback); | |
| 41 RequestResult(std::string invocation_error); | |
| 42 RequestResult(const RequestResult& other); | |
| 43 ~RequestResult(); | |
| 44 | |
| 45 ResultCode code; | |
| 46 v8::Local<v8::Function> custom_callback; | |
| 47 v8::Local<v8::Value> return_value; // Only valid if code == HANDLED. | |
| 48 std::string error; | |
| 49 }; | |
| 50 | |
| 51 APIBindingHooks(const std::string& api_name, | |
| 52 const binding::RunJSFunctionSync& run_js); | |
| 53 ~APIBindingHooks(); | |
| 54 | |
| 55 // Looks for any custom hooks associated with the given request, and, if any | |
| 56 // are found, runs them. Returns the result of running the hooks, if any. | |
| 57 RequestResult RunHooks(const std::string& method_name, | |
| 58 v8::Local<v8::Context> context, | |
| 59 const APISignature* signature, | |
| 60 std::vector<v8::Local<v8::Value>>* arguments, | |
| 61 const APITypeReferenceMap& type_refs); | |
| 62 | |
| 63 // Returns a JS interface that can be used to register hooks. | |
| 64 v8::Local<v8::Object> GetJSHookInterface(v8::Local<v8::Context> context); | |
| 65 | |
| 66 // Gets the custom-set JS callback for the given method, if one exists. | |
| 67 v8::Local<v8::Function> GetCustomJSCallback(const std::string& method_name, | |
| 68 v8::Local<v8::Context> context); | |
| 69 | |
| 70 // Creates a new JS event for the given |event_name|, if a custom event is | |
| 71 // provided. Returns true if an event was created. | |
| 72 bool CreateCustomEvent(v8::Local<v8::Context> context, | |
| 73 const std::string& event_name, | |
| 74 v8::Local<v8::Value>* event_out); | |
| 75 | |
| 76 // Performs any extra initialization on the template. | |
| 77 void InitializeTemplate(v8::Isolate* isolate, | |
| 78 v8::Local<v8::ObjectTemplate> object_template, | |
| 79 const APITypeReferenceMap& type_refs); | |
| 80 | |
| 81 void SetDelegate(std::unique_ptr<APIBindingHooksDelegate> delegate); | |
| 82 | |
| 83 private: | |
| 84 // Updates the |arguments| by running |function| and settings arguments to the | |
| 85 // returned result. | |
| 86 bool UpdateArguments(v8::Local<v8::Function> function, | |
| 87 v8::Local<v8::Context> context, | |
| 88 std::vector<v8::Local<v8::Value>>* arguments); | |
| 89 | |
| 90 // The name of the associated API. | |
| 91 std::string api_name_; | |
| 92 | |
| 93 // We use synchronous JS execution here because at every point we execute JS, | |
| 94 // it's in direct response to JS calling in. There should be no reason that | |
| 95 // script is disabled. | |
| 96 binding::RunJSFunctionSync run_js_; | |
| 97 | |
| 98 std::unique_ptr<APIBindingHooksDelegate> delegate_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); | |
| 101 }; | |
| 102 | |
| 103 } // namespace extensions | |
| 104 | |
| 105 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ | |
| OLD | NEW |