| OLD | NEW |
| 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 #include "extensions/renderer/api_binding_hooks.h" | 5 #include "extensions/renderer/api_binding_hooks.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/supports_user_data.h" | 9 #include "base/supports_user_data.h" |
| 10 #include "extensions/renderer/api_binding_hooks_delegate.h" | 10 #include "extensions/renderer/api_binding_hooks_delegate.h" |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 : code(code), custom_callback(custom_callback) {} | 200 : code(code), custom_callback(custom_callback) {} |
| 201 APIBindingHooks::RequestResult::~RequestResult() {} | 201 APIBindingHooks::RequestResult::~RequestResult() {} |
| 202 APIBindingHooks::RequestResult::RequestResult(const RequestResult& other) = | 202 APIBindingHooks::RequestResult::RequestResult(const RequestResult& other) = |
| 203 default; | 203 default; |
| 204 | 204 |
| 205 APIBindingHooks::APIBindingHooks(const std::string& api_name, | 205 APIBindingHooks::APIBindingHooks(const std::string& api_name, |
| 206 const binding::RunJSFunctionSync& run_js) | 206 const binding::RunJSFunctionSync& run_js) |
| 207 : api_name_(api_name), run_js_(run_js) {} | 207 : api_name_(api_name), run_js_(run_js) {} |
| 208 APIBindingHooks::~APIBindingHooks() {} | 208 APIBindingHooks::~APIBindingHooks() {} |
| 209 | 209 |
| 210 void APIBindingHooks::RegisterHandleRequest(const std::string& method_name, | |
| 211 const HandleRequestHook& hook) { | |
| 212 DCHECK(!hooks_used_) << "Hooks must be registered before the first use!"; | |
| 213 request_hooks_[method_name] = hook; | |
| 214 } | |
| 215 | |
| 216 APIBindingHooks::RequestResult APIBindingHooks::RunHooks( | 210 APIBindingHooks::RequestResult APIBindingHooks::RunHooks( |
| 217 const std::string& method_name, | 211 const std::string& method_name, |
| 218 v8::Local<v8::Context> context, | 212 v8::Local<v8::Context> context, |
| 219 const APISignature* signature, | 213 const APISignature* signature, |
| 220 std::vector<v8::Local<v8::Value>>* arguments, | 214 std::vector<v8::Local<v8::Value>>* arguments, |
| 221 const APITypeReferenceMap& type_refs) { | 215 const APITypeReferenceMap& type_refs) { |
| 222 // Easy case: a native custom hook. | 216 // Easy case: a native custom hook. |
| 223 auto request_hooks_iter = request_hooks_.find(method_name); | 217 if (delegate_) { |
| 224 if (request_hooks_iter != request_hooks_.end()) { | 218 RequestResult result = delegate_->HandleRequest( |
| 225 RequestResult result = | 219 method_name, signature, context, arguments, type_refs); |
| 226 request_hooks_iter->second.Run( | 220 if (result.code != RequestResult::NOT_HANDLED) |
| 227 signature, context, arguments, type_refs); | 221 return result; |
| 228 // Right now, it doesn't make sense to register a request handler that | |
| 229 // doesn't handle the request. | |
| 230 DCHECK_NE(RequestResult::NOT_HANDLED, result.code); | |
| 231 return result; | |
| 232 } | 222 } |
| 233 | 223 |
| 234 // Harder case: looking up a custom hook registered on the context (since | 224 // Harder case: looking up a custom hook registered on the context (since |
| 235 // these are JS, each context has a separate instance). | 225 // these are JS, each context has a separate instance). |
| 236 v8::Local<v8::Object> hook_interface_object = | 226 v8::Local<v8::Object> hook_interface_object = |
| 237 GetJSHookInterfaceObject(api_name_, context, false); | 227 GetJSHookInterfaceObject(api_name_, context, false); |
| 238 if (hook_interface_object.IsEmpty()) | 228 if (hook_interface_object.IsEmpty()) |
| 239 return RequestResult(RequestResult::NOT_HANDLED); | 229 return RequestResult(RequestResult::NOT_HANDLED); |
| 240 | 230 |
| 241 v8::Isolate* isolate = context->GetIsolate(); | 231 v8::Isolate* isolate = context->GetIsolate(); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (result.IsEmpty() || | 358 if (result.IsEmpty() || |
| 369 !gin::Converter<std::vector<v8::Local<v8::Value>>>::FromV8( | 359 !gin::Converter<std::vector<v8::Local<v8::Value>>>::FromV8( |
| 370 context->GetIsolate(), result, &new_args)) { | 360 context->GetIsolate(), result, &new_args)) { |
| 371 return false; | 361 return false; |
| 372 } | 362 } |
| 373 arguments->swap(new_args); | 363 arguments->swap(new_args); |
| 374 return true; | 364 return true; |
| 375 } | 365 } |
| 376 | 366 |
| 377 } // namespace extensions | 367 } // namespace extensions |
| OLD | NEW |