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