1 /* Copyright (c) 2012 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.
7 * This file defines the <code>PPB_Messaging</code> interface implemented
8 * by the browser for sending messages to DOM elements associated with a
9 * specific module instance.
18 * The <code>PPB_Messaging</code> interface is implemented by the browser
19 * and is related to sending messages to JavaScript message event listeners on
20 * the DOM element associated with specific module instance.
22 interface PPB_Messaging
{
24 * PostMessage() asynchronously invokes any listeners for message events on
25 * the DOM element for the given module instance. A call to PostMessage()
26 * will not block while the message is processed.
28 * @param[in] instance A <code>PP_Instance</code> identifying one instance
30 * @param[in] message A <code>PP_Var</code> containing the data to be sent to
32 * <code>message</code> can be any <code>PP_Var</code> type except
33 * <code>PP_VARTYPE_OBJECT</code>. Array/Dictionary types are supported from
34 * Chrome M29 onward. All var types are copied when passing them to
37 * When passing array or dictionary <code>PP_Var</code>s, the entire reference
38 * graph will be converted and transferred. If the reference graph has cycles,
39 * the message will not be sent and an error will be logged to the console.
41 * Listeners for message events in JavaScript code will receive an object
42 * conforming to the HTML 5 <code>MessageEvent</code> interface.
43 * Specifically, the value of message will be contained as a property called
44 * data in the received <code>MessageEvent</code>.
46 * This messaging system is similar to the system used for listening for
47 * messages from Web Workers. Refer to
48 * <code>http://www.whatwg.org/specs/web-workers/current-work/</code> for
49 * further information.
51 * <strong>Example:</strong>
57 * type="application/x-ppapi-postMessage-example"/>
58 * <script type="text/javascript">
59 * var plugin = document.getElementById('plugin');
60 * plugin.addEventListener("message",
61 * function(message) { alert(message.data); },
68 * The module instance then invokes PostMessage() as follows:
72 * char hello_world[] = "Hello world!";
73 * PP_Var hello_var = ppb_var_interface->VarFromUtf8(instance,
75 * sizeof(hello_world));
76 * ppb_messaging_interface->PostMessage(instance, hello_var); // Copies var.
77 * ppb_var_interface->Release(hello_var);
81 * The browser will pop-up an alert saying "Hello world!"
84 void PostMessage
([in] PP_Instance instance
, [in] PP_Var
message);
87 * Registers a handler for receiving messages from JavaScript. If a handler
88 * is registered this way, it will replace PPP_Messaging, and all messages
89 * sent from JavaScript via postMessage and postMessageAndAwaitResponse will
90 * be dispatched to <code>handler</code>.
92 * The function calls will be dispatched via <code>message_loop</code>. This
93 * means that the functions will be invoked on the thread to which
94 * <code>message_loop</code> is attached, when <code>message_loop</code> is
95 * run. It is illegal to pass the main thread message loop;
96 * RegisterMessageHandler will return PP_ERROR_WRONG_THREAD in that case.
97 * If you quit <code>message_loop</code> before calling Unregister(),
98 * the browser will not be able to call functions in the plugin's message
99 * handler any more. That could mean missing some messages or could cause a
100 * leak if you depend on Destroy() to free hander data. So you should,
101 * whenever possible, Unregister() the handler prior to quitting its event
104 * Attempting to register a message handler when one is already registered
105 * will cause the current MessageHandler to be unregistered and replaced. In
106 * that case, no messages will be sent to the "default" message handler
107 * (PPP_Messaging). Messages will stop arriving at the prior message handler
108 * and will begin to be dispatched at the new message handler.
110 * @param[in] instance A <code>PP_Instance</code> identifying one instance
112 * @param[in] user_data A pointer the plugin may choose to use when handling
113 * calls to functions within PPP_MessageHandler. The browser will pass this
114 * same pointer when invoking functions within PPP_MessageHandler.
115 * @param[in] handler The plugin-provided set of functions for handling
117 * @param[in] message_loop Represents the message loop on which
118 * PPP_MessageHandler functions should be invoked.
119 * @return PP_OK on success, or an error from pp_errors.h.
122 int32_t RegisterMessageHandler
([in] PP_Instance instance
,
123 [inout
] mem_t user_data
,
124 [in] PPP_MessageHandler handler
,
125 [in] PP_Resource message_loop
);
127 * Unregisters the current message handler for <code>instance</code> if one
128 * is registered. After this call, the message handler (if one was
129 * registered) will have "Destroy" called on it and will receive no further
130 * messages after that point. After that point, all messages sent from
131 * JavaScript using postMessage() will be dispatched to PPP_Messaging (if
132 * the plugin supports PPP_MESSAGING_INTERFACE). Attempts to call
133 * postMessageAndAwaitResponse() from JavaScript will fail.
135 * Attempting to unregister a message handler when none is registered has no
138 * @param[in] instance A <code>PP_Instance</code> identifying one instance
142 void UnregisterMessageHandler
([in] PP_Instance instance
);