Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / tests / extensions / background_keepalive / background.cc
blobf4b3b7a55478f302d20432cc202c354a9481fc68
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.
5 #include <cstdio>
6 #include <string>
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"
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);
23 virtual ~Instance() {}
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.");
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_);
43 pp::CompletionCallbackFactory<Instance> callback_factory_;
44 int delay_milliseconds_;
45 bool active_;
48 class Module : public pp::Module {
49 public:
50 Module() : pp::Module() {}
51 virtual ~Module() {}
53 virtual pp::Instance* CreateInstance(PP_Instance instance) {
54 return new Instance(instance);
58 namespace pp {
59 Module* CreateModule() {
60 return new ::Module();
62 } // namespace pp