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/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 } // namespace | |
25 | |
26 namespace contextual_search { | |
27 | |
28 gin::WrapperInfo ContextualSearchWrapper::kWrapperInfo = { | |
29 gin::kEmbedderNativeGin}; | |
30 | |
31 // static | |
32 void ContextualSearchWrapper::Install(content::RenderFrame* render_frame) { | |
33 // NOTE: Installing new v8 functions that can access Chrome native code | |
34 // requires a security review! We did an exahustive search for a better | |
35 // way to implement a communication channel between the page and Chrome, | |
36 // but found nothing better. | |
37 // TODO(donnd): use a better communication channel once that becomes | |
38 // available, e.g. navigator.connect API. See crbug.com/541683. | |
39 // TODO(donnd): refactor some of this boilerplate into a reusable | |
40 // method. This was cribbed from MemoryBenchmarkingExtension. | |
41 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
42 v8::HandleScope handle_scope(isolate); | |
43 v8::Local<v8::Context> context = | |
44 render_frame->GetWebFrame()->mainWorldScriptContext(); | |
45 if (context.IsEmpty()) | |
46 return; | |
47 | |
48 v8::Context::Scope context_scope(context); | |
49 gin::Handle<ContextualSearchWrapper> wrapper = | |
50 gin::CreateHandle(isolate, new ContextualSearchWrapper(render_frame)); | |
51 if (wrapper.IsEmpty()) | |
52 return; | |
53 | |
54 v8::Local<v8::Object> chrome = | |
55 content::GetOrCreateChromeObject(isolate, context->Global()); | |
56 chrome->Set(gin::StringToV8(isolate, kContextualSearchObjectName), | |
57 wrapper.ToV8()); | |
58 } | |
59 | |
60 ContextualSearchWrapper::ContextualSearchWrapper( | |
61 content::RenderFrame* render_frame) | |
62 : render_frame_(render_frame) {} | |
jochen (gone - plz use gerrit)
2015/11/24 10:43:35
the wrapper might outlive the render frame, you ne
Donn Denman
2015/12/02 03:32:48
Ah, thanks for catching that.
How does this look?
| |
63 | |
64 ContextualSearchWrapper::~ContextualSearchWrapper() {} | |
65 | |
66 gin::ObjectTemplateBuilder ContextualSearchWrapper::GetObjectTemplateBuilder( | |
67 v8::Isolate* isolate) { | |
68 return gin::Wrappable<ContextualSearchWrapper>::GetObjectTemplateBuilder( | |
69 isolate) | |
70 .SetMethod(kSetCaptionMethodName, &ContextualSearchWrapper::SetCaption); | |
71 } | |
72 | |
73 void ContextualSearchWrapper::EnsureServiceConnected() { | |
74 if (!contextual_search_js_api_service_ || | |
75 !contextual_search_js_api_service_.is_bound()) { | |
76 render_frame_->GetServiceRegistry()->ConnectToRemoteService( | |
77 mojo::GetProxy(&contextual_search_js_api_service_)); | |
78 } | |
79 } | |
80 | |
81 void ContextualSearchWrapper::SetCaption(const std::string& caption, | |
82 bool does_answer) { | |
83 EnsureServiceConnected(); | |
84 contextual_search_js_api_service_->HandleSetCaption(caption, does_answer); | |
85 } | |
86 | |
87 } // namespace contextual_search | |
OLD | NEW |