Chromium Code Reviews| 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/contextual_search_wrapper.h" | |
| 6 | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "components/contextual_search/contextual_search_api_service.mojom.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 contextual_search { | |
| 20 | |
| 21 static const char kContextualSearchObjectName[] = "contextualSearch"; | |
|
jochen (gone - plz use gerrit)
2015/11/17 14:58:12
prefer anonymous namespace over file-local static
Donn Denman
2015/11/19 01:11:29
Done.
| |
| 22 static const char kSetCaptionMethodName[] = "setCaption"; | |
| 23 | |
| 24 // TODO(donnd): merge with the identical code in | |
|
Donn Denman
2015/11/17 00:24:24
I tried your suggestion to "move this to content/p
jochen (gone - plz use gerrit)
2015/11/17 14:58:12
what's the link error you got?
Donn Denman
2015/11/19 01:11:29
An undefined reference here to content::GetOrCreat
jochen (gone - plz use gerrit)
2015/11/19 09:22:17
Sounds like you forgot to put CONTENT_EXPORT in fr
Donn Denman
2015/11/19 17:48:04
LOL! I'll give that a try!
| |
| 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 gin::WrapperInfo ContextualSearchWrapper::kWrapperInfo = { | |
| 41 gin::kEmbedderNativeGin}; | |
| 42 | |
| 43 // static | |
| 44 void ContextualSearchWrapper::Install(content::RenderFrame* render_frame) { | |
| 45 // NOTE: Installing new v8 functions that can access Chrome native code | |
| 46 // requires a security review! We did an exahustive search for a better | |
| 47 // way to implement a communication channel between the page and Chrome, | |
| 48 // but found nothing better. | |
| 49 // TODO(donnd): use a better communication channel once that becomes | |
| 50 // available, e.g. navigator.connect API. See crbug.com/541683. | |
| 51 // TODO(donnd): refactor some of this boilerplate into a reusable | |
| 52 // method. This was cribbed from MemoryBenchmarkingExtension. | |
| 53 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 54 v8::HandleScope handle_scope(isolate); | |
| 55 v8::Local<v8::Context> context = | |
| 56 render_frame->GetWebFrame()->mainWorldScriptContext(); | |
| 57 if (context.IsEmpty()) | |
| 58 return; | |
| 59 | |
| 60 v8::Context::Scope context_scope(context); | |
| 61 gin::Handle<ContextualSearchWrapper> wrapper = | |
| 62 gin::CreateHandle(isolate, new ContextualSearchWrapper(render_frame)); | |
| 63 if (wrapper.IsEmpty()) | |
| 64 return; | |
| 65 | |
| 66 v8::Local<v8::Object> chrome = | |
| 67 GetOrCreateChromeObject(isolate, context->Global()); | |
| 68 chrome->Set(gin::StringToV8(isolate, kContextualSearchObjectName), | |
| 69 wrapper.ToV8()); | |
| 70 } | |
| 71 | |
| 72 ContextualSearchWrapper::ContextualSearchWrapper( | |
| 73 content::RenderFrame* render_frame) | |
| 74 : render_frame_(render_frame) {} | |
| 75 | |
| 76 ContextualSearchWrapper::~ContextualSearchWrapper() {} | |
| 77 | |
| 78 gin::ObjectTemplateBuilder ContextualSearchWrapper::GetObjectTemplateBuilder( | |
| 79 v8::Isolate* isolate) { | |
| 80 return gin::Wrappable<ContextualSearchWrapper>::GetObjectTemplateBuilder( | |
| 81 isolate) | |
| 82 .SetMethod(kSetCaptionMethodName, &ContextualSearchWrapper::SetCaption); | |
| 83 } | |
| 84 | |
| 85 void ContextualSearchWrapper::EnsureServiceConnected() { | |
| 86 if (!contextual_search_api_service_ || | |
| 87 !contextual_search_api_service_.is_bound()) { | |
| 88 render_frame_->GetServiceRegistry()->ConnectToRemoteService( | |
| 89 mojo::GetProxy(&contextual_search_api_service_)); | |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void ContextualSearchWrapper::SetCaption(const std::string& caption) { | |
| 94 EnsureServiceConnected(); | |
| 95 DVLOG(0) << "ctxs SetCaption: caption: " << caption; | |
| 96 contextual_search_api_service_->HandleSetCaption(caption); | |
| 97 } | |
| 98 | |
| 99 } // namespace contextual_search | |
| OLD | NEW |