OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "components/contextual_search/renderer/contextual_search_wrapper.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "content/public/common/service_registry.h" |
| 9 #include "content/public/renderer/chrome_object_extensions_utils.h" |
| 10 #include "content/public/renderer/render_frame.h" |
| 11 #include "gin/arguments.h" |
| 12 #include "gin/object_template_builder.h" |
| 13 #include "third_party/WebKit/public/web/WebFrame.h" |
| 14 #include "third_party/WebKit/public/web/WebKit.h" |
| 15 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 16 #include "v8/include/v8.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 static const char kContextualSearchObjectName[] = "contextualSearch"; |
| 21 static const char kSetCaptionMethodName[] = "setCaption"; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace contextual_search { |
| 26 |
| 27 gin::WrapperInfo ContextualSearchWrapper::kWrapperInfo = { |
| 28 gin::kEmbedderNativeGin}; |
| 29 |
| 30 // static |
| 31 void ContextualSearchWrapper::Install(content::RenderFrame* render_frame) { |
| 32 // NOTE: Installing new v8 functions that can access Chrome native code |
| 33 // requires a security review! We did an exhaustive search for a better |
| 34 // way to implement a communication channel between the page and Chrome, |
| 35 // but found nothing better. |
| 36 // TODO(donnd): use a better communication channel once that becomes |
| 37 // available, e.g. navigator.connect API. See crbug.com/541683. |
| 38 // TODO(donnd): refactor some of this boilerplate into a reusable |
| 39 // method. This was cribbed from MemoryBenchmarkingExtension. |
| 40 v8::Isolate* isolate = blink::mainThreadIsolate(); |
| 41 v8::HandleScope handle_scope(isolate); |
| 42 v8::Local<v8::Context> context = |
| 43 render_frame->GetWebFrame()->mainWorldScriptContext(); |
| 44 if (context.IsEmpty()) |
| 45 return; |
| 46 |
| 47 v8::Context::Scope context_scope(context); |
| 48 gin::Handle<ContextualSearchWrapper> wrapper = |
| 49 gin::CreateHandle(isolate, new ContextualSearchWrapper(render_frame)); |
| 50 if (wrapper.IsEmpty()) |
| 51 return; |
| 52 |
| 53 v8::Local<v8::Object> chrome = |
| 54 content::GetOrCreateChromeObject(isolate, context->Global()); |
| 55 chrome->Set(gin::StringToV8(isolate, kContextualSearchObjectName), |
| 56 wrapper.ToV8()); |
| 57 } |
| 58 |
| 59 ContextualSearchWrapper::ContextualSearchWrapper( |
| 60 content::RenderFrame* render_frame) |
| 61 : RenderFrameObserver(render_frame) {} |
| 62 |
| 63 ContextualSearchWrapper::~ContextualSearchWrapper() {} |
| 64 |
| 65 gin::ObjectTemplateBuilder ContextualSearchWrapper::GetObjectTemplateBuilder( |
| 66 v8::Isolate* isolate) { |
| 67 return gin::Wrappable<ContextualSearchWrapper>::GetObjectTemplateBuilder( |
| 68 isolate) |
| 69 .SetMethod(kSetCaptionMethodName, &ContextualSearchWrapper::SetCaption); |
| 70 } |
| 71 |
| 72 bool ContextualSearchWrapper::EnsureServiceConnected() { |
| 73 if (render_frame() && (!contextual_search_js_api_service_ || |
| 74 !contextual_search_js_api_service_.is_bound())) { |
| 75 render_frame()->GetServiceRegistry()->ConnectToRemoteService( |
| 76 mojo::GetProxy(&contextual_search_js_api_service_)); |
| 77 return true; |
| 78 } else { |
| 79 return false; |
| 80 } |
| 81 } |
| 82 |
| 83 void ContextualSearchWrapper::OnDestruct() {} |
| 84 |
| 85 void ContextualSearchWrapper::SetCaption(const std::string& caption, |
| 86 bool does_answer) { |
| 87 if (EnsureServiceConnected()) { |
| 88 contextual_search_js_api_service_->HandleSetCaption(caption, does_answer); |
| 89 } |
| 90 } |
| 91 |
| 92 } // namespace contextual_search |
OLD | NEW |