Home | History | Annotate | Download | only in core
      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 <string>
      6 
      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"
     11 
     12 #ifdef WIN32
     13 #undef PostMessage
     14 // Allow 'this' in initializer list
     15 #pragma warning(disable : 4355)
     16 #endif
     17 
     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
     21 /// attributes:
     22 ///     type="application/x-nacl"
     23 ///     src="file_histogram.nmf"
     24 class CoreInstance : public pp::Instance {
     25  public:
     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) {}
     30 
     31  private:
     32   /// Handler for messages coming in from the browser via postMessage().  The
     33   /// @a var_message will contain the requested delay time.
     34   ///
     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();
     38     if (delay) {
     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);
     43     } else {
     44       // If no delay is requested, reply immediately with zero time elapsed.
     45       pp::Var msg(0);
     46       PostMessage(msg);
     47     }
     48   }
     49 
     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_);
     53     PostMessage(msg);
     54   }
     55 
     56  private:
     57   pp::CompletionCallbackFactory<CoreInstance> callback_factory_;
     58   PP_TimeTicks last_receive_time_;
     59 };
     60 
     61 class CoreModule : public pp::Module {
     62  public:
     63   CoreModule() : pp::Module() {}
     64   virtual ~CoreModule() {}
     65 
     66   virtual pp::Instance* CreateInstance(PP_Instance instance) {
     67     return new CoreInstance(instance);
     68   }
     69 };
     70 
     71 namespace pp {
     72 Module* CreateModule() { return new CoreModule(); }
     73 }  // namespace pp
     74