Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: components/contextual_search/renderer/contextual_search_wrapper.cc

Issue 1385663002: [Contextual Search] Add Mojo-enabled API component. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a RenderFrameObserver to the Wrapper and clear the gin Handle OnDestruct of the render frame. Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 ContextualSearchWrapper* wrapped_object(
49 new ContextualSearchWrapper(render_frame));
50 gin::Handle<ContextualSearchWrapper> wrapper =
51 gin::CreateHandle(isolate, wrapped_object);
52 if (wrapper.IsEmpty())
53 return;
54
55 v8::Local<v8::Object> chrome =
56 content::GetOrCreateChromeObject(isolate, context->Global());
57 chrome->Set(gin::StringToV8(isolate, kContextualSearchObjectName),
58 wrapper.ToV8());
59 wrapped_object->SetGinHandle(wrapper);
jochen (gone - plz use gerrit) 2015/12/04 13:21:36 don't hold on to the wrapper
Donn Denman 2015/12/09 00:57:33 Done.
60 }
61
62 ContextualSearchWrapper::ContextualSearchWrapper(
63 content::RenderFrame* render_frame)
64 : RenderFrameObserver(render_frame) {}
65
66 ContextualSearchWrapper::~ContextualSearchWrapper() {}
67
68 gin::ObjectTemplateBuilder ContextualSearchWrapper::GetObjectTemplateBuilder(
69 v8::Isolate* isolate) {
70 return gin::Wrappable<ContextualSearchWrapper>::GetObjectTemplateBuilder(
71 isolate)
72 .SetMethod(kSetCaptionMethodName, &ContextualSearchWrapper::SetCaption);
73 }
74
75 void ContextualSearchWrapper::EnsureServiceConnected() {
76 if (!contextual_search_js_api_service_ ||
77 !contextual_search_js_api_service_.is_bound()) {
78 render_frame()->GetServiceRegistry()->ConnectToRemoteService(
jochen (gone - plz use gerrit) 2015/12/04 13:21:35 you have to null-check render_frame() before using
Donn Denman 2015/12/09 00:57:33 Done.
79 mojo::GetProxy(&contextual_search_js_api_service_));
80 }
81 }
82
83 void ContextualSearchWrapper::SetGinHandle(
84 gin::Handle<ContextualSearchWrapper> handle) {
85 gin_handle_ = handle;
86 }
87
88 void ContextualSearchWrapper::OnDestruct() {
89 if (!render_frame() && !gin_handle_.IsEmpty()) {
jochen (gone - plz use gerrit) 2015/12/04 13:21:36 this method should be empty, it's just avoid that
Donn Denman 2015/12/09 00:57:33 Done.
90 gin_handle_.Clear();
91 }
92 }
93
94 void ContextualSearchWrapper::SetCaption(const std::string& caption,
95 bool does_answer) {
96 EnsureServiceConnected();
jochen (gone - plz use gerrit) 2015/12/04 13:21:36 if render_frame() is null, you can't SetCaption
Donn Denman 2015/12/09 00:57:33 Done.
97 contextual_search_js_api_service_->HandleSetCaption(caption, does_answer);
98 }
99
100 } // namespace contextual_search
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698