Miyoung Shin | 7c787404 | 2020-10-01 07:47:41 | [diff] [blame] | 1 | // Copyright 2020 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 | #include "extensions/renderer/dom_hooks_delegate.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "extensions/renderer/bindings/api_signature.h" |
| 10 | #include "extensions/renderer/dispatcher.h" |
| 11 | #include "extensions/renderer/script_context.h" |
| 12 | #include "third_party/blink/public/web/web_element.h" |
| 13 | |
| 14 | namespace extensions { |
| 15 | |
| 16 | DOMHooksDelegate::DOMHooksDelegate() = default; |
| 17 | DOMHooksDelegate::~DOMHooksDelegate() = default; |
| 18 | |
| 19 | APIBindingHooks::RequestResult DOMHooksDelegate::HandleRequest( |
| 20 | const std::string& method_name, |
| 21 | const APISignature* signature, |
| 22 | v8::Local<v8::Context> context, |
| 23 | std::vector<v8::Local<v8::Value>>* arguments, |
| 24 | const APITypeReferenceMap& refs) { |
| 25 | using RequestResult = APIBindingHooks::RequestResult; |
| 26 | |
| 27 | v8::Isolate* isolate = context->GetIsolate(); |
| 28 | v8::TryCatch try_catch(isolate); |
| 29 | APISignature::V8ParseResult parse_result = |
| 30 | signature->ParseArgumentsToV8(context, *arguments, refs); |
| 31 | if (!parse_result.succeeded()) { |
| 32 | if (try_catch.HasCaught()) { |
| 33 | try_catch.ReThrow(); |
| 34 | return RequestResult(RequestResult::THROWN); |
| 35 | } |
| 36 | return RequestResult(RequestResult::INVALID_INVOCATION); |
| 37 | } |
| 38 | |
| 39 | ScriptContext* script_context = |
| 40 | ScriptContextSet::GetContextByV8Context(context); |
| 41 | DCHECK(script_context); |
| 42 | |
| 43 | APIBindingHooks::RequestResult result( |
| 44 | APIBindingHooks::RequestResult::HANDLED); |
| 45 | if (method_name == "dom.openOrClosedShadowRoot") { |
| 46 | DCHECK(parse_result.arguments.has_value()); |
| 47 | result.return_value = |
| 48 | OpenOrClosedShadowRoot(script_context, *parse_result.arguments); |
| 49 | } else { |
| 50 | NOTREACHED(); |
| 51 | } |
| 52 | |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | v8::Local<v8::Value> DOMHooksDelegate::OpenOrClosedShadowRoot( |
| 57 | ScriptContext* script_context, |
| 58 | const std::vector<v8::Local<v8::Value>>& parsed_arguments) { |
| 59 | DCHECK(script_context->extension()); |
| 60 | DCHECK(parsed_arguments[0]->IsObject()); |
| 61 | |
| 62 | blink::WebElement element = |
| 63 | blink::WebElement::FromV8Value(parsed_arguments[0]); |
| 64 | if (element.IsNull()) |
| 65 | return v8::Null(script_context->isolate()); |
| 66 | |
| 67 | blink::WebNode shadow_root = element.OpenOrClosedShadowRoot(); |
| 68 | if (shadow_root.IsNull()) |
| 69 | return v8::Null(script_context->isolate()); |
| 70 | |
| 71 | return shadow_root.ToV8Value(script_context->v8_context()->Global(), |
| 72 | script_context->isolate()); |
| 73 | } |
| 74 | |
| 75 | } // namespace extensions |