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/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::Local<v8::Context> context) { | |
32 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
33 v8::HandleScope handle_scope(isolate); | |
34 if (context.IsEmpty()) | |
35 return; | |
36 | |
37 v8::Context::Scope context_scope(context); | |
38 | |
39 v8::Local<v8::Object> distiller_obj = | |
40 GetOrCreateDistillerObject(isolate, context->Global()); | |
41 | |
42 distiller_obj->Set( | |
43 gin::StringToSymbol(isolate, "echo"), | |
44 gin::CreateFunctionTemplate( | |
45 isolate, base::Bind(&DistillerNativeJavaScript::DistillerEcho, | |
46 base::Unretained(this))) | |
47 ->GetFunction()); | |
48 } | |
49 | |
50 std::string DistillerNativeJavaScript::DistillerEcho( | |
51 const std::string& message) { | |
52 if (!distiller_js_service_) { | |
53 render_frame_->GetServiceRegistry()->ConnectToRemoteService( | |
54 mojo::GetProxy(&distiller_js_service_)); | |
55 } | |
56 // TODO(mdjones): It is possible and beneficial to have information | |
57 // returned from the browser process with these calls. The problem | |
58 // is waiting blocks this process. | |
59 distiller_js_service_->HandleDistillerEchoCall(message); | |
60 | |
61 return message; | |
62 } | |
63 | |
64 v8::Local<v8::Object> GetOrCreateDistillerObject(v8::Isolate* isolate, | |
65 v8::Local<v8::Object> global) { | |
66 v8::Local<v8::Object> distiller_obj; | |
67 v8::Local<v8::Value> distiller_value = | |
68 global->Get(gin::StringToV8(isolate, "distiller")); | |
69 if (distiller_value.IsEmpty() || !distiller_value->IsObject()) { | |
70 distiller_obj = v8::Object::New(isolate); | |
71 global->Set(gin::StringToSymbol(isolate, "distiller"), distiller_obj); | |
72 } else { | |
73 distiller_obj = v8::Local<v8::Object>::Cast(distiller_value); | |
74 } | |
75 return distiller_obj; | |
76 } | |
77 | |
78 } // namespace dom_distiller | |
OLD | NEW |