OLD | NEW |
(Empty) | |
| 1 // Copyright 2008 The Native Client 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 #include <stdbool.h> |
| 6 #include <stdio.h> |
| 7 #include <stdlib.h> |
| 8 #include <string.h> |
| 9 |
| 10 #include <nacl/nacl_npapi.h> |
| 11 |
| 12 // These are the method names as JavaScript sees them. |
| 13 static const char* kHelloWorldMethodId = "helloworld"; |
| 14 static const char* kFortyTwoMethodId = "fortytwo"; |
| 15 |
| 16 // This is the module's function that does the work to set the value of the |
| 17 // result variable to '42'. The Invoke() function that called this function |
| 18 // then returns the result back to the browser as a JavaScript value. |
| 19 static bool FortyTwo(NPVariant *result) { |
| 20 if (result) { |
| 21 INT32_TO_NPVARIANT(42, *result); |
| 22 } |
| 23 return true; |
| 24 } |
| 25 |
| 26 // This function creates a string in the browser's memory pool and then returns |
| 27 // a variable containing a pointer to that string. The variable is later |
| 28 // returned back to the browser by the Invoke() function that called this. |
| 29 static bool HelloWorld(NPVariant *result) { |
| 30 if (result) { |
| 31 const char *msg = "hello, world."; |
| 32 const int msg_length = strlen(msg) + 1; |
| 33 // Note: |msg_copy| will be freed later on by the browser, so it needs to |
| 34 // be allocated here with NPN_MemAlloc(). |
| 35 char *msg_copy = reinterpret_cast<char*>(NPN_MemAlloc(msg_length)); |
| 36 strncpy(msg_copy, msg, msg_length); |
| 37 STRINGN_TO_NPVARIANT(msg_copy, msg_length - 1, *result); |
| 38 } |
| 39 return true; |
| 40 } |
| 41 |
| 42 // Creates the plugin-side instance of NPObject. |
| 43 // Called by NPN_CreateObject, declared in npruntime.h |
| 44 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 45 static NPObject* Allocate(NPP npp, NPClass* npclass) { |
| 46 return new NPObject; |
| 47 } |
| 48 |
| 49 // Cleans up the plugin-side instance of an NPObject. |
| 50 // Called by NPN_ReleaseObject, declared in npruntime.h |
| 51 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 52 static void Deallocate(NPObject* object) { |
| 53 delete object; |
| 54 } |
| 55 |
| 56 // Returns |true| if |method_name| is a recognized method. |
| 57 // Called by NPN_HasMethod, declared in npruntime.h |
| 58 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 59 static bool HasMethod(NPObject* obj, NPIdentifier method_name) { |
| 60 char *name = NPN_UTF8FromIdentifier(method_name); |
| 61 bool is_method = false; |
| 62 if (!strcmp((const char *)name, kHelloWorldMethodId)) { |
| 63 is_method = true; |
| 64 } else if (!strcmp((const char*)name, kFortyTwoMethodId)) { |
| 65 is_method = true; |
| 66 } |
| 67 NPN_MemFree(name); |
| 68 return is_method; |
| 69 } |
| 70 |
| 71 // Called by the browser to invoke the default method on an NPObject. |
| 72 // Returns null. |
| 73 // Apparently the plugin won't load properly if we simply |
| 74 // tell the browser we don't have this method. |
| 75 // Called by NPN_InvokeDefault, declared in npruntime.h |
| 76 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 77 static bool InvokeDefault(NPObject *obj, const NPVariant *args, |
| 78 uint32_t argCount, NPVariant *result) { |
| 79 if (result) { |
| 80 NULL_TO_NPVARIANT(*result); |
| 81 } |
| 82 return true; |
| 83 } |
| 84 |
| 85 // Called by the browser to invoke a function object whose name |
| 86 // is |method_name|. |
| 87 // Called by NPN_Invoke, declared in npruntime.h |
| 88 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 89 static bool Invoke(NPObject* obj, |
| 90 NPIdentifier method_name, |
| 91 const NPVariant *args, |
| 92 uint32_t arg_count, |
| 93 NPVariant *result) { |
| 94 NULL_TO_NPVARIANT(*result); |
| 95 char *name = NPN_UTF8FromIdentifier(method_name); |
| 96 if (name == NULL) |
| 97 return false; |
| 98 bool rval = false; |
| 99 |
| 100 // Map the method name to a function call. |result| is filled in by the |
| 101 // called function, then gets returned to the browser when Invoke() returns. |
| 102 if (!strcmp((const char *)name, kHelloWorldMethodId)) { |
| 103 rval = HelloWorld(result); |
| 104 } else if (!strcmp((const char*)name, kFortyTwoMethodId)) { |
| 105 rval = FortyTwo(result); |
| 106 } |
| 107 // Since name was allocated above by NPN_UTF8FromIdentifier, |
| 108 // it needs to be freed here. |
| 109 NPN_MemFree(name); |
| 110 return rval; |
| 111 } |
| 112 |
| 113 // Represents a class's interface, so that the browser knows what functions it |
| 114 // can call on this plugin object. The browser can use the methods in this |
| 115 // class to discover the rest of the plugin's interface. |
| 116 // Documentation URL: https://developer.mozilla.org/en/NPClass |
| 117 static NPClass kHelloWorldClass = { |
| 118 NP_CLASS_STRUCT_VERSION, |
| 119 Allocate, |
| 120 Deallocate, |
| 121 NULL, // Invalidate is not implemented |
| 122 HasMethod, |
| 123 Invoke, |
| 124 InvokeDefault, |
| 125 NULL, // HasProperty is not implemented |
| 126 NULL, // GetProperty is not implemented |
| 127 NULL, // SetProperty is not implemented |
| 128 }; |
| 129 |
| 130 // Called by NPP_GetScriptableInstance to get the scripting interface for |
| 131 // this plugin. |
| 132 NPClass *GetNPSimpleClass() { |
| 133 return &kHelloWorldClass; |
| 134 } |
OLD | NEW |