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

Side by Side Diff: components/dom_distiller/content/renderer/distiller_native_javascript.cc

Issue 1231083007: Expose distiller functions to JavaScript (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@refactor-content
Patch Set: cl format Created 5 years, 4 months 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/dom_distiller/content/renderer/distiller_native_javascript. h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "components/dom_distiller/content/common/distiller_javascript_service.m ojom.h"
12 #include "content/public/common/service_registry.h"
13 #include "content/public/renderer/render_frame.h"
14 #include "gin/arguments.h"
15 #include "gin/function_template.h"
16 #include "third_party/WebKit/public/web/WebKit.h"
17 #include "third_party/WebKit/public/web/WebLocalFrame.h"
18 #include "v8/include/v8.h"
19
20 using blink::WebLocalFrame;
21
22 namespace dom_distiller {
23
24 DistillerNativeJavaScript::DistillerNativeJavaScript(
25 content::RenderFrame* render_frame)
26 : render_frame_(render_frame) {}
27
28 DistillerNativeJavaScript::~DistillerNativeJavaScript() {}
29
30 void DistillerNativeJavaScript::AddJavaScriptObjectToFrame() {
31 v8::Isolate* isolate = blink::mainThreadIsolate();
32 v8::HandleScope handle_scope(isolate);
33 v8::Local<v8::Context> context =
34 render_frame_->GetWebFrame()->mainWorldScriptContext();
35 if (context.IsEmpty())
36 return;
37
38 v8::Context::Scope context_scope(context);
39
40 v8::Local<v8::Object> distiller_obj =
41 GetOrCreateDistillerObject(isolate, context->Global());
42
43 distiller_obj->Set(
44 gin::StringToSymbol(isolate, "echo"),
45 gin::CreateFunctionTemplate(
46 isolate, base::Bind(&DistillerNativeJavaScript::distillerEcho,
47 base::Unretained(this)))
48 ->GetFunction());
49 }
50
51 std::string DistillerNativeJavaScript::distillerEcho(
52 const std::string& message) {
53 if (!distiller_js_service_) {
54 render_frame_->GetServiceRegistry()->ConnectToRemoteService(
55 mojo::GetProxy(&distiller_js_service_));
56 }
57 // TODO(mdjones): It is possible and beneficial to have information
58 // returned from the browser process with these calls. The problem
59 // is waiting blocks this process.
60 distiller_js_service_->HandleDistillerEchoCall(message);
61
62 return message;
63 }
64
65 v8::Local<v8::Object> GetOrCreateDistillerObject(v8::Isolate* isolate,
66 v8::Local<v8::Object> global) {
67 v8::Local<v8::Object> distiller_obj;
68 v8::Local<v8::Value> distiller_value =
69 global->Get(gin::StringToV8(isolate, "distiller"));
70 if (distiller_value.IsEmpty() || !distiller_value->IsObject()) {
71 distiller_obj = v8::Object::New(isolate);
72 global->Set(gin::StringToSymbol(isolate, "distiller"), distiller_obj);
73 } else {
74 distiller_obj = v8::Local<v8::Object>::Cast(distiller_value);
75 }
76 return distiller_obj;
77 }
78
79 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698