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.
7 #include "ppapi/cpp/instance.h"
8 #include "ppapi/cpp/module.h"
9 #include "ppapi/cpp/var.h"
10 #include "ppapi/utility/completion_callback_factory.h"
14 // Allow 'this' in initializer list
15 #pragma warning(disable : 4355)
18 /// The Instance class. One of these exists for each instance of your NaCl
19 /// module on the web page. The browser will ask the Module object to create
20 /// a new Instance for each occurrence of the <embed> tag that has these
22 /// type="application/x-nacl"
23 /// src="file_histogram.nmf"
24 class CoreInstance : public pp::Instance {
26 /// The constructor creates the plugin-side instance.
27 /// @param[in] instance the handle to the browser-side plugin instance.
28 explicit CoreInstance(PP_Instance instance)
29 : pp::Instance(instance), callback_factory_(this) {}
32 /// Handler for messages coming in from the browser via postMessage(). The
33 /// @a var_message will contain the requested delay time.
35 /// @param[in] var_message The message posted by the browser.
36 virtual void HandleMessage(const pp::Var& var_message) {
37 int32_t delay = var_message.AsInt();
39 // If a delay is requested, issue a callback after delay ms.
40 last_receive_time_ = pp::Module::Get()->core()->GetTimeTicks();
41 pp::Module::Get()->core()->CallOnMainThread(
42 delay, callback_factory_.NewCallback(&CoreInstance::DelayedPost), 0);
44 // If no delay is requested, reply immediately with zero time elapsed.
50 void DelayedPost(int32_t) {
51 // Send the time elapsed until the callbacked fired.
52 pp::Var msg(pp::Module::Get()->core()->GetTimeTicks() - last_receive_time_);
57 pp::CompletionCallbackFactory<CoreInstance> callback_factory_;
58 PP_TimeTicks last_receive_time_;
61 class CoreModule : public pp::Module {
63 CoreModule() : pp::Module() {}
64 virtual ~CoreModule() {}
66 virtual pp::Instance* CreateInstance(PP_Instance instance) {
67 return new CoreInstance(instance);
72 Module* CreateModule() { return new CoreModule(); }