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