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

Side by Side Diff: content/renderer/pepper/message_channel.cc

Issue 1088763002: Plugin Power Saver: Add comprehensive browser tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@0260-plugins-overhaul-prerender-tests
Patch Set: Created 5 years, 8 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/renderer/pepper/message_channel.h" 5 #include "content/renderer/pepper/message_channel.h"
6 6
7 #include <cstdlib> 7 #include <cstdlib>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
13 #include "content/renderer/pepper/host_array_buffer_var.h" 13 #include "content/renderer/pepper/host_array_buffer_var.h"
14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h" 14 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
15 #include "content/renderer/pepper/pepper_try_catch.h" 15 #include "content/renderer/pepper/pepper_try_catch.h"
16 #include "content/renderer/pepper/plugin_instance_throttler_impl.h"
16 #include "content/renderer/pepper/plugin_module.h" 17 #include "content/renderer/pepper/plugin_module.h"
17 #include "content/renderer/pepper/plugin_object.h" 18 #include "content/renderer/pepper/plugin_object.h"
18 #include "gin/arguments.h" 19 #include "gin/arguments.h"
19 #include "gin/converter.h" 20 #include "gin/converter.h"
20 #include "gin/function_template.h" 21 #include "gin/function_template.h"
21 #include "gin/object_template_builder.h" 22 #include "gin/object_template_builder.h"
22 #include "gin/public/gin_embedders.h" 23 #include "gin/public/gin_embedders.h"
24 #include "ppapi/shared_impl/ppapi_constants.h"
23 #include "ppapi/shared_impl/ppapi_globals.h" 25 #include "ppapi/shared_impl/ppapi_globals.h"
24 #include "ppapi/shared_impl/scoped_pp_var.h" 26 #include "ppapi/shared_impl/scoped_pp_var.h"
25 #include "ppapi/shared_impl/var.h" 27 #include "ppapi/shared_impl/var.h"
26 #include "ppapi/shared_impl/var_tracker.h" 28 #include "ppapi/shared_impl/var_tracker.h"
27 #include "third_party/WebKit/public/web/WebBindings.h" 29 #include "third_party/WebKit/public/web/WebBindings.h"
28 #include "third_party/WebKit/public/web/WebDocument.h" 30 #include "third_party/WebKit/public/web/WebDocument.h"
29 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h" 31 #include "third_party/WebKit/public/web/WebDOMMessageEvent.h"
30 #include "third_party/WebKit/public/web/WebElement.h" 32 #include "third_party/WebKit/public/web/WebElement.h"
31 #include "third_party/WebKit/public/web/WebLocalFrame.h" 33 #include "third_party/WebKit/public/web/WebLocalFrame.h"
32 #include "third_party/WebKit/public/web/WebNode.h" 34 #include "third_party/WebKit/public/web/WebNode.h"
33 #include "third_party/WebKit/public/web/WebPluginContainer.h" 35 #include "third_party/WebKit/public/web/WebPluginContainer.h"
34 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h" 36 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
35 #include "v8/include/v8.h" 37 #include "v8/include/v8.h"
36 38
37 using ppapi::ArrayBufferVar; 39 using ppapi::ArrayBufferVar;
38 using ppapi::PpapiGlobals; 40 using ppapi::PpapiGlobals;
39 using ppapi::ScopedPPVar; 41 using ppapi::ScopedPPVar;
40 using ppapi::StringVar; 42 using ppapi::StringVar;
41 using blink::WebBindings; 43 using blink::WebBindings;
42 using blink::WebElement; 44 using blink::WebElement;
43 using blink::WebDOMEvent; 45 using blink::WebDOMEvent;
44 using blink::WebDOMMessageEvent; 46 using blink::WebDOMMessageEvent;
45 using blink::WebPluginContainer; 47 using blink::WebPluginContainer;
46 using blink::WebSerializedScriptValue; 48 using blink::WebSerializedScriptValue;
47 49
48 namespace content { 50 namespace content {
49 51
50 namespace { 52 namespace {
51 53
54 const char kIsPeripheral[] = "isPeripheral";
52 const char kPostMessage[] = "postMessage"; 55 const char kPostMessage[] = "postMessage";
53 const char kPostMessageAndAwaitResponse[] = "postMessageAndAwaitResponse"; 56 const char kPostMessageAndAwaitResponse[] = "postMessageAndAwaitResponse";
54 const char kV8ToVarConversionError[] = 57 const char kV8ToVarConversionError[] =
55 "Failed to convert a PostMessage " 58 "Failed to convert a PostMessage "
56 "argument from a JavaScript value to a PP_Var. It may have cycles or be of " 59 "argument from a JavaScript value to a PP_Var. It may have cycles or be of "
57 "an unsupported type."; 60 "an unsupported type.";
58 const char kVarToV8ConversionError[] = 61 const char kVarToV8ConversionError[] =
59 "Failed to convert a PostMessage " 62 "Failed to convert a PostMessage "
60 "argument from a PP_Var to a Javascript value. It may have cycles or be of " 63 "argument from a PP_Var to a Javascript value. It may have cycles or be of "
61 "an unsupported type."; 64 "an unsupported type.";
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 221
219 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate); 222 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate);
220 if (identifier == kPostMessage) { 223 if (identifier == kPostMessage) {
221 return GetFunctionTemplate(isolate, identifier, 224 return GetFunctionTemplate(isolate, identifier,
222 &MessageChannel::PostMessageToNative) 225 &MessageChannel::PostMessageToNative)
223 ->GetFunction(); 226 ->GetFunction();
224 } else if (identifier == kPostMessageAndAwaitResponse) { 227 } else if (identifier == kPostMessageAndAwaitResponse) {
225 return GetFunctionTemplate(isolate, identifier, 228 return GetFunctionTemplate(isolate, identifier,
226 &MessageChannel::PostBlockingMessageToNative) 229 &MessageChannel::PostBlockingMessageToNative)
227 ->GetFunction(); 230 ->GetFunction();
231 } else if (instance_->module()->name() == ppapi::kPpapiTestLibraryName &&
232 identifier == kIsPeripheral) {
233 return GetFunctionTemplate(isolate, identifier,
234 &MessageChannel::IsPeripheralToNative)
235 ->GetFunction();
228 } 236 }
229 237
230 std::map<std::string, ScopedPPVar>::const_iterator it = 238 std::map<std::string, ScopedPPVar>::const_iterator it =
231 internal_named_properties_.find(identifier); 239 internal_named_properties_.find(identifier);
232 if (it != internal_named_properties_.end()) { 240 if (it != internal_named_properties_.end()) {
233 v8::Handle<v8::Value> result = try_catch.ToV8(it->second.get()); 241 v8::Handle<v8::Value> result = try_catch.ToV8(it->second.get());
234 if (try_catch.ThrowException()) 242 if (try_catch.ThrowException())
235 return v8::Local<v8::Value>(); 243 return v8::Local<v8::Value>();
236 return result; 244 return result;
237 } 245 }
238 246
239 PluginObject* plugin_object = GetPluginObject(isolate); 247 PluginObject* plugin_object = GetPluginObject(isolate);
240 if (plugin_object) 248 if (plugin_object)
241 return plugin_object->GetNamedProperty(isolate, identifier); 249 return plugin_object->GetNamedProperty(isolate, identifier);
242 return v8::Local<v8::Value>(); 250 return v8::Local<v8::Value>();
243 } 251 }
244 252
245 bool MessageChannel::SetNamedProperty(v8::Isolate* isolate, 253 bool MessageChannel::SetNamedProperty(v8::Isolate* isolate,
246 const std::string& identifier, 254 const std::string& identifier,
247 v8::Local<v8::Value> value) { 255 v8::Local<v8::Value> value) {
248 if (!instance_) 256 if (!instance_)
249 return false; 257 return false;
250 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate); 258 PepperTryCatchV8 try_catch(instance_, &var_converter_, isolate);
259 if (instance_->module()->name() == ppapi::kPpapiTestLibraryName &&
260 identifier == kIsPeripheral) {
261 try_catch.ThrowException("Cannot set property with the name isPeripheral");
262 return true;
263 }
251 if (identifier == kPostMessage || 264 if (identifier == kPostMessage ||
252 identifier == kPostMessageAndAwaitResponse) { 265 identifier == kPostMessageAndAwaitResponse) {
253 try_catch.ThrowException("Cannot set properties with the name postMessage" 266 try_catch.ThrowException("Cannot set properties with the name postMessage"
254 "or postMessageAndAwaitResponse"); 267 "or postMessageAndAwaitResponse");
255 return true; 268 return true;
256 } 269 }
257 270
258 // TODO(raymes): This is only used by the gTalk plugin which is deprecated. 271 // TODO(raymes): This is only used by the gTalk plugin which is deprecated.
259 // Remove passthrough of SetProperty calls as soon as it is removed. 272 // Remove passthrough of SetProperty calls as soon as it is removed.
260 PluginObject* plugin_object = GetPluginObject(isolate); 273 PluginObject* plugin_object = GetPluginObject(isolate);
261 if (plugin_object) 274 if (plugin_object)
262 return plugin_object->SetNamedProperty(isolate, identifier, value); 275 return plugin_object->SetNamedProperty(isolate, identifier, value);
263 276
264 return false; 277 return false;
265 } 278 }
266 279
267 std::vector<std::string> MessageChannel::EnumerateNamedProperties( 280 std::vector<std::string> MessageChannel::EnumerateNamedProperties(
268 v8::Isolate* isolate) { 281 v8::Isolate* isolate) {
269 std::vector<std::string> result; 282 std::vector<std::string> result;
270 PluginObject* plugin_object = GetPluginObject(isolate); 283 PluginObject* plugin_object = GetPluginObject(isolate);
271 if (plugin_object) 284 if (plugin_object)
272 result = plugin_object->EnumerateNamedProperties(isolate); 285 result = plugin_object->EnumerateNamedProperties(isolate);
286
287 if (instance_->module()->name() == ppapi::kPpapiTestLibraryName)
288 result.push_back(kIsPeripheral);
289
273 result.push_back(kPostMessage); 290 result.push_back(kPostMessage);
274 result.push_back(kPostMessageAndAwaitResponse); 291 result.push_back(kPostMessageAndAwaitResponse);
275 return result; 292 return result;
276 } 293 }
277 294
278 void MessageChannel::PostMessageToNative(gin::Arguments* args) { 295 void MessageChannel::PostMessageToNative(gin::Arguments* args) {
279 if (!instance_) 296 if (!instance_)
280 return; 297 return;
281 if (args->Length() != 1) { 298 if (args->Length() != 1) {
282 // TODO(raymes): Consider throwing an exception here. We don't now for 299 // TODO(raymes): Consider throwing an exception here. We don't now for
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 "and PPP_MessageHandler."); 362 "and PPP_MessageHandler.");
346 return; 363 return;
347 } 364 }
348 v8::Handle<v8::Value> v8_result = try_catch.ToV8(pp_result.get()); 365 v8::Handle<v8::Value> v8_result = try_catch.ToV8(pp_result.get());
349 if (try_catch.ThrowException()) 366 if (try_catch.ThrowException())
350 return; 367 return;
351 368
352 args->Return(v8_result); 369 args->Return(v8_result);
353 } 370 }
354 371
372 void MessageChannel::IsPeripheralToNative(gin::Arguments* args) {
373 if (!instance_)
374 return;
375 PepperTryCatchV8 try_catch(instance_, &var_converter_, args->isolate());
376 if (args->Length() != 0) {
377 try_catch.ThrowException("isPeripheral takes no arguments");
378 return;
379 }
380
381 bool is_peripheral =
382 instance_->throttler() && instance_->throttler()->power_saver_enabled();
383
384 v8::Handle<v8::Value> v8_result =
385 v8::Boolean::New(args->isolate(), is_peripheral);
386 args->Return(v8_result);
387 }
388
355 void MessageChannel::PostMessageToJavaScriptImpl( 389 void MessageChannel::PostMessageToJavaScriptImpl(
356 const WebSerializedScriptValue& message_data) { 390 const WebSerializedScriptValue& message_data) {
357 DCHECK(instance_); 391 DCHECK(instance_);
358 392
359 WebPluginContainer* container = instance_->container(); 393 WebPluginContainer* container = instance_->container();
360 // It's possible that container() is NULL if the plugin has been removed from 394 // It's possible that container() is NULL if the plugin has been removed from
361 // the DOM (but the PluginInstance is not destroyed yet). 395 // the DOM (but the PluginInstance is not destroyed yet).
362 if (!container) 396 if (!container)
363 return; 397 return;
364 398
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name); 510 v8::Local<v8::FunctionTemplate> function_template = template_cache_.Get(name);
477 if (!function_template.IsEmpty()) 511 if (!function_template.IsEmpty())
478 return function_template; 512 return function_template;
479 function_template = gin::CreateFunctionTemplate( 513 function_template = gin::CreateFunctionTemplate(
480 isolate, base::Bind(memberFuncPtr, weak_ptr_factory_.GetWeakPtr())); 514 isolate, base::Bind(memberFuncPtr, weak_ptr_factory_.GetWeakPtr()));
481 template_cache_.Set(name, function_template); 515 template_cache_.Set(name, function_template);
482 return function_template; 516 return function_template;
483 } 517 }
484 518
485 } // namespace content 519 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698