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

Side by Side Diff: ppapi/tests/power_saver_test_plugin.cc

Issue 1114623002: Plugin Power Saver: Make PPS work well with prerendered pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: update comments Created 5 years, 7 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 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 <algorithm> 5 #include <algorithm>
6 6
7 #include "ppapi/cpp/graphics_2d.h"
8 #include "ppapi/cpp/image_data.h"
7 #include "ppapi/cpp/instance.h" 9 #include "ppapi/cpp/instance.h"
8 #include "ppapi/cpp/module.h" 10 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h" 11 #include "ppapi/cpp/var.h"
10 #include "ppapi/cpp/var_dictionary.h" 12 #include "ppapi/cpp/var_dictionary.h"
11 #include "ppapi/tests/test_utils.h" 13 #include "ppapi/tests/test_utils.h"
12 14
13 // Windows defines 'PostMessage', so we have to undef it. 15 // Windows defines 'PostMessage', so we have to undef it.
14 #ifdef PostMessage 16 #ifdef PostMessage
15 #undef PostMessage 17 #undef PostMessage
16 #endif 18 #endif
17 19
18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. 20 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests.
19 class PowerSaverTestInstance : public pp::Instance { 21 class PowerSaverTestInstance : public pp::Instance {
20 public: 22 public:
21 explicit PowerSaverTestInstance(PP_Instance instance) 23 explicit PowerSaverTestInstance(PP_Instance instance)
22 : pp::Instance(instance), received_first_did_change_view_(false) {} 24 : pp::Instance(instance),
25 received_first_did_change_view_(false),
26 callback_factory_(this) {}
23 ~PowerSaverTestInstance() override {} 27 ~PowerSaverTestInstance() override {}
24 28
29 bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
30 GetTestingInterface()->SubscribeToPowerSaverNotifications(pp_instance());
31 return true;
32 }
33
25 // For browser tests, responds to: 34 // For browser tests, responds to:
26 // - When postMessage("isPeripheral") is called on the plugin DOM element. 35 // - When postMessage("isPeripheral") is called on the plugin DOM element.
27 // - When the plugin throttler posts a message notifying us that our 36 // - When the plugin throttler posts a message notifying us that our
28 // peripheral status has changed. 37 // peripheral status has changed.
29 void HandleMessage(const pp::Var& message_data) override { 38 void HandleMessage(const pp::Var& message_data) override {
30 if (message_data.is_string()) { 39 if (message_data.is_string())
31 if (message_data.AsString() == "getPeripheralStatus") 40 BroadcastStatus(message_data.AsString());
32 BroadcastIsPeripheralStatus("getPeripheralStatusResponse");
33 else if (message_data.AsString() == "peripheralStatusChange")
34 BroadcastIsPeripheralStatus("peripheralStatusChange");
35 }
36 } 41 }
37 42
38 // Broadcast our peripheral status after the initial view data. This is for 43 // Broadcast our peripheral status after the initial view data. This is for
39 // tests that await initial plugin creation. 44 // tests that await initial plugin creation.
40 void DidChangeView(const pp::View& view) override { 45 void DidChangeView(const pp::View& view) override {
41 if (!received_first_did_change_view_) { 46 if (!received_first_did_change_view_) {
42 BroadcastIsPeripheralStatus("initial"); 47 BroadcastStatus("initial");
43 received_first_did_change_view_ = true; 48 received_first_did_change_view_ = true;
44 } 49 }
50
51 view_ = view;
52 device_context_ = pp::Graphics2D(this, view_.GetRect().size(), true);
53 if (!BindGraphics(device_context_))
54 return;
55
56 Paint();
45 } 57 }
46 58
59 void OnFlush(int32_t) { Paint(); }
60
47 private: 61 private:
48 void BroadcastIsPeripheralStatus(const std::string& source) { 62 void Paint() {
63 pp::ImageData image(this, PP_IMAGEDATAFORMAT_BGRA_PREMUL,
64 view_.GetRect().size(), true);
65 if (image.is_null())
66 return;
67
68 // Draw black and white stripes to present an "interesting" keyframe.
69 for (int y = 0; y < view_.GetRect().size().height(); ++y) {
70 for (int x = 0; x < view_.GetRect().size().width(); ++x) {
71 uint32_t color = x % 2 ? 0xFF0000FF : 0xFFFFFFFF;
72 *image.GetAddr32(pp::Point(x, y)) = color;
73 }
74 }
75
76 device_context_.ReplaceContents(&image);
77 device_context_.Flush(
78 callback_factory_.NewCallback(&PowerSaverTestInstance::OnFlush));
79 }
80
81 void BroadcastStatus(const std::string& source) {
49 pp::VarDictionary message; 82 pp::VarDictionary message;
50 message.Set( 83 message.Set(
51 "isPeripheral", 84 "status",
52 pp::Var(PP_ToBool(GetTestingInterface()->IsPeripheral(pp_instance())))); 85 pp::Var(GetTestingInterface()->GetPowerSaverStatus(pp_instance())));
53 message.Set("source", pp::Var(source)); 86 message.Set("source", pp::Var(source));
54 PostMessage(message); 87 PostMessage(message);
55 } 88 }
56 89
57 bool received_first_did_change_view_; 90 bool received_first_did_change_view_;
91 pp::View view_;
92 pp::Graphics2D device_context_;
93
94 pp::CompletionCallbackFactory<PowerSaverTestInstance> callback_factory_;
58 }; 95 };
59 96
60 class PowerSaverTestModule : public pp::Module { 97 class PowerSaverTestModule : public pp::Module {
61 public: 98 public:
62 PowerSaverTestModule() : pp::Module() {} 99 PowerSaverTestModule() : pp::Module() {}
63 virtual ~PowerSaverTestModule() {} 100 virtual ~PowerSaverTestModule() {}
64 101
65 virtual pp::Instance* CreateInstance(PP_Instance instance) { 102 virtual pp::Instance* CreateInstance(PP_Instance instance) {
66 return new PowerSaverTestInstance(instance); 103 return new PowerSaverTestInstance(instance);
67 } 104 }
68 }; 105 };
69 106
70 namespace pp { 107 namespace pp {
71 108
72 Module* CreateModule() { 109 Module* CreateModule() {
73 return new PowerSaverTestModule(); 110 return new PowerSaverTestModule();
74 } 111 }
75 112
76 } // namespace pp 113 } // namespace pp
OLDNEW
« content/renderer/pepper/plugin_module.cc ('K') | « ppapi/proxy/ppb_testing_proxy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698