OLD | NEW |
(Empty) | |
| 1 // Copyright 2010 The Native Client SDK Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can |
| 3 // be found in the LICENSE file. |
| 4 |
| 5 #ifndef EXAMPLES_SRPC_DUALITY_SCRIPTING_BRIDGE_H_ |
| 6 #define EXAMPLES_SRPC_DUALITY_SCRIPTING_BRIDGE_H_ |
| 7 |
| 8 #include <examples/srpc/duality/scriptable.h> |
| 9 |
| 10 #include <nacl/nacl_npapi.h> |
| 11 |
| 12 #include <map> |
| 13 |
| 14 |
| 15 // General scripting bridge class that uses an implementable interface to |
| 16 // provide NaCl scriptability. |
| 17 class ScriptingBridge : public NPObject { |
| 18 public: |
| 19 explicit ScriptingBridge(NPP npp, Scriptable * scriptable_instance); |
| 20 virtual ~ScriptingBridge(); |
| 21 |
| 22 // Called by NPP_GetScriptableInstance to get the scripting interface for |
| 23 // a plugin object of ScriptableType. The browser may dereference the |
| 24 // returned pointer any time after it gets it, until the plugin is cleaned |
| 25 // up. It should clean it up whenever it decides to collect garbage and |
| 26 // there are no references remaining. |
| 27 template<typename ScriptableType> |
| 28 static NPClass* GetNPSimpleClass(); |
| 29 |
| 30 private: |
| 31 |
| 32 // Creates the plugin-side instance of NPObject. |
| 33 // Called by NPN_CreateObject, declared in npruntime.h |
| 34 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 35 template<typename ScriptableType> |
| 36 static NPObject* AllocateCallback(NPP npp, NPClass* npclass); |
| 37 |
| 38 // Cleans up the plugin-side instance of an NPObject. |
| 39 // Called by NPN_ReleaseObject, declared in npruntime.h |
| 40 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 41 static void DeallocateCallback(NPObject* object); |
| 42 |
| 43 // Returns the value of the property called |name| in |result| and true. |
| 44 // Returns false if |name| is not a property on this object or something else |
| 45 // goes wrong. |
| 46 // Called by NPN_GetProperty, declared in npruntime.h |
| 47 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 48 static bool GetPropertyCallback(NPObject* object, |
| 49 NPIdentifier name, |
| 50 NPVariant* result); |
| 51 |
| 52 // Returns |true| if |method_name| is a recognized method. |
| 53 // Called by NPN_HasMethod, declared in npruntime.h |
| 54 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 55 static bool HasMethodCallback(NPObject* object, NPIdentifier name); |
| 56 |
| 57 // Returns true if |name| is actually the name of a public property on the |
| 58 // plugin class being queried. |
| 59 // Called by NPN_HasProperty, declared in npruntime.h |
| 60 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 61 static bool HasPropertyCallback(NPObject* object, NPIdentifier name); |
| 62 |
| 63 // Not public because it is called by Allocate |
| 64 void Init(); |
| 65 |
| 66 // Called by the browser when a plugin is being destroyed to clean up any |
| 67 // remaining instances of NPClass. |
| 68 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 69 static void InvalidateCallback(NPObject* object); |
| 70 void Invalidate(); |
| 71 |
| 72 // Called by the browser to invoke a function object whose name is |name|. |
| 73 // Called by NPN_Invoke, declared in npruntime.h |
| 74 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 75 static bool InvokeCallback(NPObject* object, |
| 76 NPIdentifier name, |
| 77 const NPVariant* args, |
| 78 uint32_t arg_count, |
| 79 NPVariant* result); |
| 80 |
| 81 // Called by the browser to invoke the default method on an NPObject. |
| 82 // In this case the default method just returns false. |
| 83 // Apparently the plugin won't load properly if we simply |
| 84 // tell the browser we don't have this method. |
| 85 // Called by NPN_InvokeDefault, declared in npruntime.h |
| 86 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 87 static bool InvokeDefaultCallback(NPObject* object, |
| 88 const NPVariant* args, |
| 89 uint32_t arg_count, |
| 90 NPVariant* result); |
| 91 |
| 92 // Removes the property |name| from |object| and returns true. |
| 93 // Returns false if it can't be removed for some reason. |
| 94 // Called by NPN_RemoveProperty, declared in npruntime.h |
| 95 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 96 static bool RemovePropertyCallback(NPObject* object, NPIdentifier name); |
| 97 |
| 98 // Sets the property |name| of |object| to |value| and return true. |
| 99 // Returns false if |name| is not the name of a settable property on |object| |
| 100 // or if something else goes wrong. |
| 101 // Called by NPN_SetProperty, declared in npruntime.h |
| 102 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 103 static bool SetPropertyCallback(NPObject* object, |
| 104 NPIdentifier name, |
| 105 const NPVariant* value); |
| 106 |
| 107 // Not called GetInstance because that would be misleading. |
| 108 // GetInstance would imply a singleton design pattern. ToTYPE implies |
| 109 // the type cast which is appropriate. |
| 110 static ScriptingBridge * ToInstance(NPObject * object); |
| 111 |
| 112 |
| 113 NPP npp_; |
| 114 |
| 115 Scriptable * scriptable_instance_; |
| 116 }; |
| 117 |
| 118 template<typename ScriptableType> |
| 119 NPClass* ScriptingBridge::GetNPSimpleClass() { |
| 120 printf("Duality: ScriptingBridge::GetNPSimpleClass was called!\n"); |
| 121 fflush(stdout); |
| 122 void* np_class_pointer = NPN_MemAlloc(sizeof(NPClass)); |
| 123 NPClass* np_class = static_cast<NPClass *>(np_class_pointer); |
| 124 np_class->structVersion = NP_CLASS_STRUCT_VERSION; |
| 125 np_class->allocate = ScriptingBridge::AllocateCallback<ScriptableType>; |
| 126 np_class->deallocate = ScriptingBridge::DeallocateCallback; |
| 127 np_class->invalidate = ScriptingBridge::InvalidateCallback; |
| 128 np_class->hasMethod = ScriptingBridge::HasMethodCallback; |
| 129 np_class->invoke = ScriptingBridge::InvokeCallback; |
| 130 np_class->invokeDefault = ScriptingBridge::InvokeDefaultCallback; |
| 131 np_class->hasProperty = ScriptingBridge::HasPropertyCallback; |
| 132 np_class->getProperty = ScriptingBridge::GetPropertyCallback; |
| 133 np_class->setProperty = ScriptingBridge::SetPropertyCallback; |
| 134 np_class->removeProperty = ScriptingBridge::RemovePropertyCallback; |
| 135 np_class->enumerate = NULL; |
| 136 np_class->construct = NULL; |
| 137 return np_class; |
| 138 } |
| 139 |
| 140 template<typename ScriptableType> |
| 141 NPObject* ScriptingBridge::AllocateCallback(NPP npp, NPClass* npclass) { |
| 142 printf("Duality: ScriptingBridge::Allocate was called!\n"); |
| 143 fflush(stdout); |
| 144 ScriptableType* scriptable_object = new ScriptableType(); |
| 145 ScriptingBridge* bridge = new ScriptingBridge(npp, scriptable_object); |
| 146 bridge->Init(); |
| 147 return bridge; |
| 148 } |
| 149 |
| 150 |
| 151 #endif // EXAMPLES_SRPC_DUALITY_SCRIPTING_BRIDGE_H_ |
OLD | NEW |