Home | History | Annotate | Download | only in manual
      1 // Copyright (c) 2011 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 "ppapi/c/pp_errors.h"
      6 #include "ppapi/c/pp_input_event.h"
      7 #include "ppapi/c/ppb_var.h"
      8 #include "ppapi/c/trusted/ppb_instance_trusted.h"
      9 #include "ppapi/cpp/completion_callback.h"
     10 #include "ppapi/cpp/instance.h"
     11 #include "ppapi/cpp/module.h"
     12 #include "ppapi/cpp/private/var_private.h"
     13 
     14 class MyInstance : public pp::Instance {
     15  public:
     16   MyInstance(PP_Instance instance) : pp::Instance(instance) {
     17     factory_.Initialize(this);
     18   }
     19 
     20   virtual ~MyInstance() {
     21   }
     22 
     23   virtual bool Init(uint32_t argc, const char* argn[], const char* argv[]) {
     24     return true;
     25   }
     26 
     27   virtual bool HandleInputEvent(const PP_InputEvent& event) {
     28     switch (event.type) {
     29       case PP_INPUTEVENT_TYPE_MOUSEDOWN:
     30         pp::Module::Get()->core()->CallOnMainThread(100,
     31             factory_.NewCallback(&MyInstance::SayHello));
     32         return true;
     33       case PP_INPUTEVENT_TYPE_MOUSEMOVE:
     34         return true;
     35       case PP_INPUTEVENT_TYPE_KEYDOWN:
     36         return true;
     37       default:
     38         return false;
     39     }
     40   }
     41 
     42  private:
     43   void SayHello(int32_t) {
     44     pp::Var code("deletePlugin()");
     45     /*
     46     When scripting is removed from instance, this is the code that will do the
     47     same thing:
     48     const PPB_Instance_Trusted* inst =
     49         (const PPB_Instance_Trusted*)pp::Module::Get()->GetBrowserInterface(
     50             PPB_INSTANCE_TRUSTED_INTERFACE);
     51     inst->ExecuteScript(pp_instance(), code.pp_var(), NULL);
     52     */
     53     ExecuteScript(code);
     54   }
     55 
     56   pp::CompletionCallbackFactory<MyInstance> factory_;
     57 };
     58 
     59 class MyModule : public pp::Module {
     60  public:
     61   MyModule() : pp::Module() {}
     62   virtual ~MyModule() {}
     63 
     64   virtual pp::Instance* CreateInstance(PP_Instance instance) {
     65     return new MyInstance(instance);
     66   }
     67 };
     68 
     69 namespace pp {
     70 
     71 // Factory function for your specialization of the Module object.
     72 Module* CreateModule() {
     73   return new MyModule();
     74 }
     75 
     76 }  // namespace pp
     77