OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 * Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "native_client/src/include/portability.h" | 10 #include "native_client/src/include/portability.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 /* Global variables */ | 25 /* Global variables */ |
26 PPB_GetInterface get_browser_interface_func = NULL; | 26 PPB_GetInterface get_browser_interface_func = NULL; |
27 PP_Instance instance = 0; | 27 PP_Instance instance = 0; |
28 PP_Module module = 0; | 28 PP_Module module = 0; |
29 | 29 |
30 struct MessageInfo { | 30 struct MessageInfo { |
31 PP_Instance instance; | 31 PP_Instance instance; |
32 struct PP_Var message; | 32 struct PP_Var message; |
33 }; | 33 }; |
34 | 34 |
35 static struct PPB_Var* GetPPB_Var() { | 35 static PPB_Var* GetPPB_Var() { |
36 return (struct PPB_Var*)(*get_browser_interface_func)(PPB_VAR_INTERFACE); | 36 return (PPB_Var*)(*get_browser_interface_func)(PPB_VAR_INTERFACE); |
37 } | 37 } |
38 | 38 |
39 static void SendOnMessageEventCallback(void* data, int32_t result) { | 39 static void SendOnMessageEventCallback(void* data, int32_t result) { |
40 struct MessageInfo* message_to_send = (struct MessageInfo*)data; | 40 struct MessageInfo* message_to_send = (struct MessageInfo*)data; |
41 struct PPB_Messaging* ppb_messaging = | 41 PPB_Messaging* ppb_messaging = |
42 (struct PPB_Messaging*)(*get_browser_interface_func)( | 42 (PPB_Messaging*)(*get_browser_interface_func)( |
43 PPB_MESSAGING_INTERFACE); | 43 PPB_MESSAGING_INTERFACE); |
44 | 44 |
45 UNREFERENCED_PARAMETER(result); | 45 UNREFERENCED_PARAMETER(result); |
46 CHECK(ppb_messaging); | 46 CHECK(ppb_messaging); |
47 | 47 |
48 ppb_messaging->PostMessage(message_to_send->instance, | 48 ppb_messaging->PostMessage(message_to_send->instance, |
49 message_to_send->message); | 49 message_to_send->message); |
50 | 50 |
51 /* Since the message we're sending originally was sent from the browser, | 51 /* Since the message we're sending originally was sent from the browser, |
52 * and subequently copied, if sending a string message we need to | 52 * and subequently copied, if sending a string message we need to |
53 * dereference it. | 53 * dereference it. |
54 */ | 54 */ |
55 if (message_to_send->message.type == PP_VARTYPE_STRING) { | 55 if (message_to_send->message.type == PP_VARTYPE_STRING) { |
56 struct PPB_Var* ppb_var = GetPPB_Var(); | 56 PPB_Var* ppb_var = GetPPB_Var(); |
57 ppb_var->Release(message_to_send->message); | 57 ppb_var->Release(message_to_send->message); |
58 } | 58 } |
59 free(message_to_send); | 59 free(message_to_send); |
60 } | 60 } |
61 | 61 |
62 /* TODO(dspringer): We need to add a test that calls PostMessage directly from | 62 /* TODO(dspringer): We need to add a test that calls PostMessage directly from |
63 * HandleMessage to ensure that this is all asynchronous. | 63 * HandleMessage to ensure that this is all asynchronous. |
64 */ | 64 */ |
65 void HandleMessage(PP_Instance instance, struct PP_Var message) { | 65 void HandleMessage(PP_Instance instance, struct PP_Var message) { |
66 struct PPB_Core* ppb_core = | 66 PPB_Core* ppb_core = |
67 (struct PPB_Core*)(*get_browser_interface_func)(PPB_CORE_INTERFACE); | 67 (PPB_Core*)(*get_browser_interface_func)(PPB_CORE_INTERFACE); |
68 struct MessageInfo* message_to_send = malloc(sizeof(struct MessageInfo)); | 68 struct MessageInfo* message_to_send = malloc(sizeof(struct MessageInfo)); |
69 message_to_send->instance = instance; | 69 message_to_send->instance = instance; |
70 message_to_send->message = message; | 70 message_to_send->message = message; |
71 | 71 |
72 if (message.type == PP_VARTYPE_STRING) { | 72 if (message.type == PP_VARTYPE_STRING) { |
73 struct PPB_Var* ppb_var = GetPPB_Var(); | 73 PPB_Var* ppb_var = GetPPB_Var(); |
74 /* If the message is a string, add reference to go with the copy we did | 74 /* If the message is a string, add reference to go with the copy we did |
75 * above. | 75 * above. |
76 */ | 76 */ |
77 ppb_var->AddRef(message); | 77 ppb_var->AddRef(message); |
78 } | 78 } |
79 /* Echo message back to browser */ | 79 /* Echo message back to browser */ |
80 ppb_core->CallOnMainThread( | 80 ppb_core->CallOnMainThread( |
81 0, /* I don't care about delay */ | 81 0, /* I don't care about delay */ |
82 PP_MakeCompletionCallback(SendOnMessageEventCallback, message_to_send), | 82 PP_MakeCompletionCallback(SendOnMessageEventCallback, message_to_send), |
83 PP_OK); /* Dummy value for result */ | 83 PP_OK); /* Dummy value for result */ |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 get_browser_interface_func = get_browser_interface; | 126 get_browser_interface_func = get_browser_interface; |
127 return PP_OK; | 127 return PP_OK; |
128 } | 128 } |
129 | 129 |
130 PP_EXPORT void PPP_ShutdownModule() { | 130 PP_EXPORT void PPP_ShutdownModule() { |
131 } | 131 } |
132 | 132 |
133 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { | 133 PP_EXPORT const void* PPP_GetInterface(const char* interface_name) { |
134 if (0 == strncmp(PPP_INSTANCE_INTERFACE, interface_name, | 134 if (0 == strncmp(PPP_INSTANCE_INTERFACE, interface_name, |
135 strlen(PPP_INSTANCE_INTERFACE))) { | 135 strlen(PPP_INSTANCE_INTERFACE))) { |
136 static struct PPP_Instance instance_interface = { | 136 static PPP_Instance instance_interface = { |
137 DidCreate, | 137 DidCreate, |
138 DidDestroy, | 138 DidDestroy, |
139 DidChangeView, | 139 DidChangeView, |
140 DidChangeFocus, | 140 DidChangeFocus, |
141 HandleDocumentLoad | 141 HandleDocumentLoad |
142 }; | 142 }; |
143 return &instance_interface; | 143 return &instance_interface; |
144 } else if (0 == strncmp(PPP_MESSAGING_INTERFACE, interface_name, | 144 } else if (0 == strncmp(PPP_MESSAGING_INTERFACE, interface_name, |
145 strlen(PPP_MESSAGING_INTERFACE))) { | 145 strlen(PPP_MESSAGING_INTERFACE))) { |
146 static struct PPP_Messaging messaging_interface = { | 146 static PPP_Messaging messaging_interface = { |
147 HandleMessage | 147 HandleMessage |
148 }; | 148 }; |
149 return &messaging_interface; | 149 return &messaging_interface; |
150 } | 150 } |
151 return NULL; | 151 return NULL; |
152 } | 152 } |
OLD | NEW |