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 <algorithm> |
| 6 |
| 7 #include "ppapi/cpp/instance.h" |
| 8 #include "ppapi/cpp/module.h" |
| 9 #include "ppapi/cpp/var.h" |
| 10 #include "ppapi/cpp/var_dictionary.h" |
| 11 #include "ppapi/tests/test_utils.h" |
| 12 |
| 13 // Windows defines 'PostMessage', so we have to undef it. |
| 14 #ifdef PostMessage |
| 15 #undef PostMessage |
| 16 #endif |
| 17 |
| 18 // This is a simple C++ Pepper plugin that enables Plugin Power Saver tests. |
| 19 class PowerSaverTestInstance : public pp::Instance { |
| 20 public: |
| 21 explicit PowerSaverTestInstance(PP_Instance instance) |
| 22 : pp::Instance(instance), received_first_did_change_view_(false) {} |
| 23 ~PowerSaverTestInstance() override {} |
| 24 |
| 25 // For browser tests, responds to: |
| 26 // - When postMessage("isPeripheral") is called on the plugin DOM element. |
| 27 // - When the plugin throttler posts a message notifying us that our |
| 28 // peripheral status has changed. |
| 29 void HandleMessage(const pp::Var& message_data) override { |
| 30 if (message_data.is_string()) { |
| 31 if (message_data.AsString() == "getPeripheralStatus") |
| 32 BroadcastIsPeripheralStatus("getPeripheralStatusResponse"); |
| 33 else if (message_data.AsString() == "peripheralStatusChange") |
| 34 BroadcastIsPeripheralStatus("peripheralStatusChange"); |
| 35 } |
| 36 } |
| 37 |
| 38 // Broadcast our peripheral status after the initial view data. This is for |
| 39 // tests that await initial plugin creation. |
| 40 void DidChangeView(const pp::View& view) override { |
| 41 if (!received_first_did_change_view_) { |
| 42 BroadcastIsPeripheralStatus("initial"); |
| 43 received_first_did_change_view_ = true; |
| 44 } |
| 45 } |
| 46 |
| 47 private: |
| 48 void BroadcastIsPeripheralStatus(const std::string& source) { |
| 49 pp::VarDictionary message; |
| 50 message.Set( |
| 51 "isPeripheral", |
| 52 pp::Var(PP_ToBool(GetTestingInterface()->IsPeripheral(pp_instance())))); |
| 53 message.Set("source", pp::Var(source)); |
| 54 PostMessage(message); |
| 55 } |
| 56 |
| 57 bool received_first_did_change_view_; |
| 58 }; |
| 59 |
| 60 class PowerSaverTestModule : public pp::Module { |
| 61 public: |
| 62 PowerSaverTestModule() : pp::Module() {} |
| 63 virtual ~PowerSaverTestModule() {} |
| 64 |
| 65 virtual pp::Instance* CreateInstance(PP_Instance instance) { |
| 66 return new PowerSaverTestInstance(instance); |
| 67 } |
| 68 }; |
| 69 |
| 70 namespace pp { |
| 71 |
| 72 Module* CreateModule() { |
| 73 return new PowerSaverTestModule(); |
| 74 } |
| 75 |
| 76 } // namespace pp |
OLD | NEW |