OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/content/renderer/distiller_native_javascript.
h" | 5 #include "components/dom_distiller/content/renderer/distiller_native_javascript.
h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 v8::Isolate* isolate = blink::mainThreadIsolate(); | 32 v8::Isolate* isolate = blink::mainThreadIsolate(); |
33 v8::HandleScope handle_scope(isolate); | 33 v8::HandleScope handle_scope(isolate); |
34 if (context.IsEmpty()) | 34 if (context.IsEmpty()) |
35 return; | 35 return; |
36 | 36 |
37 v8::Context::Scope context_scope(context); | 37 v8::Context::Scope context_scope(context); |
38 | 38 |
39 v8::Local<v8::Object> distiller_obj = | 39 v8::Local<v8::Object> distiller_obj = |
40 GetOrCreateDistillerObject(isolate, context->Global()); | 40 GetOrCreateDistillerObject(isolate, context->Global()); |
41 | 41 |
| 42 EnsureServiceConnected(); |
| 43 |
| 44 // Some of the JavaScript functions require extra work to be done when it is |
| 45 // called, so they have wrapper functions maintained in this class. |
42 BindFunctionToObject( | 46 BindFunctionToObject( |
43 distiller_obj, | 47 distiller_obj, |
44 "echo", | 48 "echo", |
45 base::Bind( | 49 base::Bind( |
46 &DistillerNativeJavaScript::DistillerEcho, base::Unretained(this))); | 50 &DistillerNativeJavaScript::DistillerEcho, base::Unretained(this))); |
47 | 51 |
| 52 // Many functions can simply call the Mojo interface directly and have no |
| 53 // wrapper function for binding. Note that calling distiller_js_service.get() |
| 54 // does not transfer ownership of the interface. |
48 BindFunctionToObject( | 55 BindFunctionToObject( |
49 distiller_obj, | 56 distiller_obj, |
50 "sendFeedback", | 57 "sendFeedback", |
51 base::Bind( | 58 base::Bind( |
52 &DistillerNativeJavaScript::DistillerSendFeedback, | 59 &DistillerJavaScriptService::HandleDistillerFeedbackCall, |
53 base::Unretained(this))); | 60 base::Unretained(distiller_js_service_.get()))); |
54 | 61 |
55 BindFunctionToObject( | 62 BindFunctionToObject( |
56 distiller_obj, | 63 distiller_obj, |
57 "closePanel", | 64 "closePanel", |
58 base::Bind( | 65 base::Bind( |
59 &DistillerNativeJavaScript::DistillerClosePanel, | 66 &DistillerJavaScriptService::HandleDistillerClosePanelCall, |
60 base::Unretained(this))); | 67 base::Unretained(distiller_js_service_.get()))); |
| 68 |
| 69 BindFunctionToObject( |
| 70 distiller_obj, |
| 71 "openSettings", |
| 72 base::Bind( |
| 73 &DistillerJavaScriptService::HandleDistillerOpenSettingsCall, |
| 74 base::Unretained(distiller_js_service_.get()))); |
61 } | 75 } |
62 | 76 |
63 template<typename Sig> | 77 template<typename Sig> |
64 void DistillerNativeJavaScript::BindFunctionToObject( | 78 void DistillerNativeJavaScript::BindFunctionToObject( |
65 v8::Local<v8::Object> javascript_object, | 79 v8::Local<v8::Object> javascript_object, |
66 const std::string& name, | 80 const std::string& name, |
67 const base::Callback<Sig> callback) { | 81 const base::Callback<Sig> callback) { |
68 // Get the isolate associated with this object. | 82 // Get the isolate associated with this object. |
69 v8::Isolate* isolate = javascript_object->GetIsolate(); | 83 v8::Isolate* isolate = javascript_object->GetIsolate(); |
70 javascript_object->Set( | 84 javascript_object->Set( |
71 gin::StringToSymbol(isolate, name), | 85 gin::StringToSymbol(isolate, name), |
72 gin::CreateFunctionTemplate(isolate, callback)->GetFunction()); | 86 gin::CreateFunctionTemplate(isolate, callback)->GetFunction()); |
73 } | 87 } |
74 | 88 |
75 void DistillerNativeJavaScript::EnsureServiceConnected() { | 89 void DistillerNativeJavaScript::EnsureServiceConnected() { |
76 if (!distiller_js_service_) { | 90 if (!distiller_js_service_ || !distiller_js_service_.is_bound()) { |
77 render_frame_->GetServiceRegistry()->ConnectToRemoteService( | 91 render_frame_->GetServiceRegistry()->ConnectToRemoteService( |
78 mojo::GetProxy(&distiller_js_service_)); | 92 mojo::GetProxy(&distiller_js_service_)); |
79 } | 93 } |
80 } | 94 } |
81 | 95 |
82 void DistillerNativeJavaScript::DistillerSendFeedback(bool good) { | |
83 EnsureServiceConnected(); | |
84 distiller_js_service_->HandleDistillerFeedbackCall(good); | |
85 } | |
86 | |
87 void DistillerNativeJavaScript::DistillerClosePanel() { | |
88 EnsureServiceConnected(); | |
89 distiller_js_service_->HandleDistillerClosePanelCall(); | |
90 } | |
91 | |
92 std::string DistillerNativeJavaScript::DistillerEcho( | 96 std::string DistillerNativeJavaScript::DistillerEcho( |
93 const std::string& message) { | 97 const std::string& message) { |
94 EnsureServiceConnected(); | 98 EnsureServiceConnected(); |
95 // TODO(mdjones): It is possible and beneficial to have information | 99 // TODO(mdjones): It is possible and beneficial to have information |
96 // returned from the browser process with these calls. The problem | 100 // returned from the browser process with these calls. The problem |
97 // is waiting blocks this process. | 101 // is waiting blocks this process. |
98 distiller_js_service_->HandleDistillerEchoCall(message); | 102 distiller_js_service_->HandleDistillerEchoCall(message); |
99 | 103 |
100 return message; | 104 return message; |
101 } | 105 } |
102 | 106 |
103 v8::Local<v8::Object> GetOrCreateDistillerObject(v8::Isolate* isolate, | 107 v8::Local<v8::Object> GetOrCreateDistillerObject(v8::Isolate* isolate, |
104 v8::Local<v8::Object> global) { | 108 v8::Local<v8::Object> global) { |
105 v8::Local<v8::Object> distiller_obj; | 109 v8::Local<v8::Object> distiller_obj; |
106 v8::Local<v8::Value> distiller_value = | 110 v8::Local<v8::Value> distiller_value = |
107 global->Get(gin::StringToV8(isolate, "distiller")); | 111 global->Get(gin::StringToV8(isolate, "distiller")); |
108 if (distiller_value.IsEmpty() || !distiller_value->IsObject()) { | 112 if (distiller_value.IsEmpty() || !distiller_value->IsObject()) { |
109 distiller_obj = v8::Object::New(isolate); | 113 distiller_obj = v8::Object::New(isolate); |
110 global->Set(gin::StringToSymbol(isolate, "distiller"), distiller_obj); | 114 global->Set(gin::StringToSymbol(isolate, "distiller"), distiller_obj); |
111 } else { | 115 } else { |
112 distiller_obj = v8::Local<v8::Object>::Cast(distiller_value); | 116 distiller_obj = v8::Local<v8::Object>::Cast(distiller_value); |
113 } | 117 } |
114 return distiller_obj; | 118 return distiller_obj; |
115 } | 119 } |
116 | 120 |
117 } // namespace dom_distiller | 121 } // namespace dom_distiller |
OLD | NEW |