1 // Copyright (c) 2013 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 <cstdio> 6 #include <string> 7 8 #include "ppapi/cpp/instance.h" 9 #include "ppapi/cpp/message_loop.h" 10 #include "ppapi/cpp/module.h" 11 #include "ppapi/cpp/var.h" 12 #include "ppapi/utility/completion_callback_factory.h" 13 14 class Instance : public pp::Instance { 15 public: 16 explicit Instance(PP_Instance instance) : 17 pp::Instance(instance), 18 callback_factory_(this), 19 delay_milliseconds_(10), 20 active_(true) { 21 DoSomething(PP_OK); 22 } 23 virtual ~Instance() {} 24 25 virtual void HandleMessage(const pp::Var& message_var) { 26 std::string message_string = message_var.AsString(); 27 if (message_string == "be idle") { 28 active_ = false; 29 } else { 30 PostMessage("Unhandled control message."); 31 } 32 } 33 34 void DoSomething(int32_t result) { 35 if (active_) { 36 pp::MessageLoop loop = pp::MessageLoop::GetCurrent(); 37 pp::CompletionCallback c = callback_factory_.NewCallback( 38 &Instance::DoSomething); 39 loop.PostWork(c, delay_milliseconds_); 40 } 41 } 42 43 pp::CompletionCallbackFactory<Instance> callback_factory_; 44 int delay_milliseconds_; 45 bool active_; 46 }; 47 48 class Module : public pp::Module { 49 public: 50 Module() : pp::Module() {} 51 virtual ~Module() {} 52 53 virtual pp::Instance* CreateInstance(PP_Instance instance) { 54 return new Instance(instance); 55 } 56 }; 57 58 namespace pp { 59 Module* CreateModule() { 60 return new ::Module(); 61 } 62 } // namespace pp 63 64