Bug 522299 - Electrolysis: Get windowless plugins drawing on win32. r=jmuizelaar.
[mozilla-central.git] / dom / plugins / PluginMessageUtils.h
blob8157475a3c32468722c40b5cee7420a453241be2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: sw=4 ts=4 et :
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is Mozilla Plugin App.
18 * The Initial Developer of the Original Code is
19 * Chris Jones <jones.chris.g@gmail.com>
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef DOM_PLUGINS_PLUGINMESSAGEUTILS_H
40 #define DOM_PLUGINS_PLUGINMESSAGEUTILS_H
42 #include "IPC/IPCMessageUtils.h"
44 #include "npapi.h"
45 #include "npruntime.h"
46 #include "npfunctions.h"
47 #include "nsAutoPtr.h"
48 #include "nsStringGlue.h"
49 #include "nsThreadUtils.h"
51 namespace mozilla {
53 // XXX might want to move these to nscore.h or something, they can be
54 // generally useful
55 struct void_t { };
56 struct null_t { };
58 namespace ipc {
60 typedef intptr_t NPRemoteIdentifier;
62 } /* namespace ipc */
64 namespace plugins {
66 /**
67 * This is NPByteRange without the linked list.
69 struct IPCByteRange
71 int32_t offset;
72 uint32_t length;
73 };
75 typedef std::vector<IPCByteRange> IPCByteRanges;
77 typedef nsCString Buffer;
79 struct NPRemoteWindow
81 unsigned long window;
82 int32_t x;
83 int32_t y;
84 uint32_t width;
85 uint32_t height;
86 NPRect clipRect;
87 NPWindowType type;
88 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
89 VisualID visualID;
90 Colormap colormap;
91 #endif /* XP_UNIX */
92 #if defined(XP_WIN)
93 base::SharedMemoryHandle surfaceHandle;
94 #endif
97 // XXX maybe not the best place for these. better one?
99 #define VARSTR(v_) case v_: return #v_
100 inline const char* const
101 NPPVariableToString(NPPVariable aVar)
103 switch (aVar) {
104 VARSTR(NPPVpluginNameString);
105 VARSTR(NPPVpluginDescriptionString);
106 VARSTR(NPPVpluginWindowBool);
107 VARSTR(NPPVpluginTransparentBool);
108 VARSTR(NPPVjavaClass);
109 VARSTR(NPPVpluginWindowSize);
110 VARSTR(NPPVpluginTimerInterval);
112 VARSTR(NPPVpluginScriptableInstance);
113 VARSTR(NPPVpluginScriptableIID);
115 VARSTR(NPPVjavascriptPushCallerBool);
117 VARSTR(NPPVpluginKeepLibraryInMemory);
118 VARSTR(NPPVpluginNeedsXEmbed);
120 VARSTR(NPPVpluginScriptableNPObject);
122 VARSTR(NPPVformValue);
124 VARSTR(NPPVpluginUrlRequestsDisplayedBool);
126 VARSTR(NPPVpluginWantsAllNetworkStreams);
128 #ifdef XP_MACOSX
129 VARSTR(NPPVpluginDrawingModel);
130 VARSTR(NPPVpluginEventModel);
131 #endif
133 default: return "???";
137 inline const char*
138 NPNVariableToString(NPNVariable aVar)
140 switch(aVar) {
141 VARSTR(NPNVxDisplay);
142 VARSTR(NPNVxtAppContext);
143 VARSTR(NPNVnetscapeWindow);
144 VARSTR(NPNVjavascriptEnabledBool);
145 VARSTR(NPNVasdEnabledBool);
146 VARSTR(NPNVisOfflineBool);
148 VARSTR(NPNVserviceManager);
149 VARSTR(NPNVDOMElement);
150 VARSTR(NPNVDOMWindow);
151 VARSTR(NPNVToolkit);
152 VARSTR(NPNVSupportsXEmbedBool);
154 VARSTR(NPNVWindowNPObject);
156 VARSTR(NPNVPluginElementNPObject);
158 VARSTR(NPNVSupportsWindowless);
160 VARSTR(NPNVprivateModeBool);
162 default: return "???";
165 #undef VARSTR
168 inline void AssertPluginThread()
170 NS_ASSERTION(NS_IsMainThread(), "should be on the plugin's main thread!");
173 void DeferNPObjectLastRelease(const NPNetscapeFuncs* f, NPObject* o);
174 void DeferNPVariantLastRelease(const NPNetscapeFuncs* f, NPVariant* v);
176 // in NPAPI, char* == NULL is sometimes meaningful. the following is
177 // helper code for dealing with nullable nsCString's
178 inline nsCString
179 NullableString(const char* aString)
181 if (!aString) {
182 nsCString str;
183 str.SetIsVoid(PR_TRUE);
184 return str;
186 return nsCString(aString);
189 inline const char*
190 NullableStringGet(const nsCString& str)
192 if (str.IsVoid())
193 return NULL;
195 return str.get();
198 } /* namespace plugins */
200 } /* namespace mozilla */
202 namespace IPC {
204 template <>
205 struct ParamTraits<NPRect>
207 typedef NPRect paramType;
209 static void Write(Message* aMsg, const paramType& aParam)
211 WriteParam(aMsg, aParam.top);
212 WriteParam(aMsg, aParam.left);
213 WriteParam(aMsg, aParam.bottom);
214 WriteParam(aMsg, aParam.right);
217 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
219 uint16_t top, left, bottom, right;
220 if (ReadParam(aMsg, aIter, &top) &&
221 ReadParam(aMsg, aIter, &left) &&
222 ReadParam(aMsg, aIter, &bottom) &&
223 ReadParam(aMsg, aIter, &right)) {
224 aResult->top = top;
225 aResult->left = left;
226 aResult->bottom = bottom;
227 aResult->right = right;
228 return true;
230 return false;
233 static void Log(const paramType& aParam, std::wstring* aLog)
235 aLog->append(StringPrintf(L"[%u, %u, %u, %u]", aParam.top, aParam.left,
236 aParam.bottom, aParam.right));
240 template <>
241 struct ParamTraits<NPWindowType>
243 typedef NPWindowType paramType;
245 static void Write(Message* aMsg, const paramType& aParam)
247 aMsg->WriteInt16(int16(aParam));
250 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
252 int16 result;
253 if (aMsg->ReadInt16(aIter, &result)) {
254 *aResult = paramType(result);
255 return true;
257 return false;
260 static void Log(const paramType& aParam, std::wstring* aLog)
262 aLog->append(StringPrintf(L"%d", int16(aParam)));
266 template <>
267 struct ParamTraits<mozilla::plugins::NPRemoteWindow>
269 typedef mozilla::plugins::NPRemoteWindow paramType;
271 static void Write(Message* aMsg, const paramType& aParam)
273 aMsg->WriteULong(aParam.window);
274 WriteParam(aMsg, aParam.x);
275 WriteParam(aMsg, aParam.y);
276 WriteParam(aMsg, aParam.width);
277 WriteParam(aMsg, aParam.height);
278 WriteParam(aMsg, aParam.clipRect);
279 WriteParam(aMsg, aParam.type);
280 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
281 aMsg->WriteULong(aParam.visualID);
282 aMsg->WriteULong(aParam.colormap);
283 #endif
284 #if defined(XP_WIN)
285 WriteParam(aMsg, aParam.surfaceHandle);
286 #endif
289 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
291 unsigned long window;
292 int32_t x, y;
293 uint32_t width, height;
294 NPRect clipRect;
295 NPWindowType type;
296 if (!(aMsg->ReadULong(aIter, &window) &&
297 ReadParam(aMsg, aIter, &x) &&
298 ReadParam(aMsg, aIter, &y) &&
299 ReadParam(aMsg, aIter, &width) &&
300 ReadParam(aMsg, aIter, &height) &&
301 ReadParam(aMsg, aIter, &clipRect) &&
302 ReadParam(aMsg, aIter, &type)))
303 return false;
305 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
306 unsigned long visualID;
307 unsigned long colormap;
308 if (!(aMsg->ReadULong(aIter, &visualID) &&
309 aMsg->ReadULong(aIter, &colormap)))
310 return false;
311 #endif
313 #if defined(XP_WIN)
314 base::SharedMemoryHandle surfaceHandle;
315 if (!ReadParam(aMsg, aIter, &surfaceHandle))
316 return false;
317 #endif
319 aResult->window = window;
320 aResult->x = x;
321 aResult->y = y;
322 aResult->width = width;
323 aResult->height = height;
324 aResult->clipRect = clipRect;
325 aResult->type = type;
326 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
327 aResult->visualID = visualID;
328 aResult->colormap = colormap;
329 #endif
330 #if defined(XP_WIN)
331 aResult->surfaceHandle = surfaceHandle;
332 #endif
333 return true;
336 static void Log(const paramType& aParam, std::wstring* aLog)
338 aLog->append(StringPrintf(L"[%u, %d, %d, %u, %u, %d",
339 (unsigned long)aParam.window,
340 aParam.x, aParam.y, aParam.width,
341 aParam.height, (long)aParam.type));
345 template <>
346 struct ParamTraits<NPString>
348 typedef NPString paramType;
350 static void Write(Message* aMsg, const paramType& aParam)
352 WriteParam(aMsg, aParam.UTF8Length);
353 aMsg->WriteBytes(aParam.UTF8Characters,
354 aParam.UTF8Length * sizeof(NPUTF8));
357 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
359 if (ReadParam(aMsg, aIter, &aResult->UTF8Length)) {
360 int byteCount = aResult->UTF8Length * sizeof(NPUTF8);
361 if (!byteCount) {
362 aResult->UTF8Characters = "\0";
363 return true;
366 const char* messageBuffer = nsnull;
367 nsAutoArrayPtr<char> newBuffer(new char[byteCount]);
368 if (newBuffer && aMsg->ReadBytes(aIter, &messageBuffer, byteCount )) {
369 memcpy((void*)messageBuffer, newBuffer.get(), byteCount);
370 aResult->UTF8Characters = newBuffer.forget();
371 return true;
374 return false;
377 static void Log(const paramType& aParam, std::wstring* aLog)
379 aLog->append(StringPrintf(L"%s", aParam.UTF8Characters));
383 template <>
384 struct ParamTraits<NPVariant>
386 typedef NPVariant paramType;
388 static void Write(Message* aMsg, const paramType& aParam)
390 if (NPVARIANT_IS_VOID(aParam)) {
391 aMsg->WriteInt(0);
392 return;
395 if (NPVARIANT_IS_NULL(aParam)) {
396 aMsg->WriteInt(1);
397 return;
400 if (NPVARIANT_IS_BOOLEAN(aParam)) {
401 aMsg->WriteInt(2);
402 WriteParam(aMsg, NPVARIANT_TO_BOOLEAN(aParam));
403 return;
406 if (NPVARIANT_IS_INT32(aParam)) {
407 aMsg->WriteInt(3);
408 WriteParam(aMsg, NPVARIANT_TO_INT32(aParam));
409 return;
412 if (NPVARIANT_IS_DOUBLE(aParam)) {
413 aMsg->WriteInt(4);
414 WriteParam(aMsg, NPVARIANT_TO_DOUBLE(aParam));
415 return;
418 if (NPVARIANT_IS_STRING(aParam)) {
419 aMsg->WriteInt(5);
420 WriteParam(aMsg, NPVARIANT_TO_STRING(aParam));
421 return;
424 NS_ERROR("Unsupported type!");
427 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
429 int type;
430 if (!aMsg->ReadInt(aIter, &type)) {
431 return false;
434 switch (type) {
435 case 0:
436 VOID_TO_NPVARIANT(*aResult);
437 return true;
439 case 1:
440 NULL_TO_NPVARIANT(*aResult);
441 return true;
443 case 2: {
444 bool value;
445 if (ReadParam(aMsg, aIter, &value)) {
446 BOOLEAN_TO_NPVARIANT(value, *aResult);
447 return true;
449 } break;
451 case 3: {
452 int32 value;
453 if (ReadParam(aMsg, aIter, &value)) {
454 INT32_TO_NPVARIANT(value, *aResult);
455 return true;
457 } break;
459 case 4: {
460 double value;
461 if (ReadParam(aMsg, aIter, &value)) {
462 DOUBLE_TO_NPVARIANT(value, *aResult);
463 return true;
465 } break;
467 case 5: {
468 NPString value;
469 if (ReadParam(aMsg, aIter, &value)) {
470 STRINGN_TO_NPVARIANT(value.UTF8Characters, value.UTF8Length,
471 *aResult);
472 return true;
474 } break;
476 default:
477 NS_ERROR("Unsupported type!");
480 return false;
483 static void Log(const paramType& aParam, std::wstring* aLog)
485 if (NPVARIANT_IS_VOID(aParam)) {
486 aLog->append(L"[void]");
487 return;
490 if (NPVARIANT_IS_NULL(aParam)) {
491 aLog->append(L"[null]");
492 return;
495 if (NPVARIANT_IS_BOOLEAN(aParam)) {
496 LogParam(NPVARIANT_TO_BOOLEAN(aParam), aLog);
497 return;
500 if (NPVARIANT_IS_INT32(aParam)) {
501 LogParam(NPVARIANT_TO_INT32(aParam), aLog);
502 return;
505 if (NPVARIANT_IS_DOUBLE(aParam)) {
506 LogParam(NPVARIANT_TO_DOUBLE(aParam), aLog);
507 return;
510 if (NPVARIANT_IS_STRING(aParam)) {
511 LogParam(NPVARIANT_TO_STRING(aParam), aLog);
512 return;
515 NS_ERROR("Unsupported type!");
519 template<>
520 struct ParamTraits<mozilla::void_t>
522 typedef mozilla::void_t paramType;
523 static void Write(Message* aMsg, const paramType& aParam) { }
524 static bool
525 Read(const Message* aMsg, void** aIter, paramType* aResult)
527 *aResult = paramType();
528 return true;
532 template<>
533 struct ParamTraits<mozilla::null_t>
535 typedef mozilla::null_t paramType;
536 static void Write(Message* aMsg, const paramType& aParam) { }
537 static bool
538 Read(const Message* aMsg, void** aIter, paramType* aResult)
540 *aResult = paramType();
541 return true;
545 template <>
546 struct ParamTraits<mozilla::plugins::IPCByteRange>
548 typedef mozilla::plugins::IPCByteRange paramType;
550 static void Write(Message* aMsg, const paramType& aParam)
552 WriteParam(aMsg, aParam.offset);
553 WriteParam(aMsg, aParam.length);
556 static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
558 paramType p;
559 if (ReadParam(aMsg, aIter, &p.offset) &&
560 ReadParam(aMsg, aIter, &p.length)) {
561 *aResult = p;
562 return true;
564 return false;
569 } /* namespace IPC */
572 // Serializing NPEvents is completely platform-specific and can be rather
573 // intricate depending on the platform. So for readability we split it
574 // into separate files and have the only macro crud live here.
576 // NB: these guards are based on those where struct NPEvent is defined
577 // in npapi.h. They should be kept in sync.
578 #if defined(XP_MACOSX)
579 # include "mozilla/plugins/NPEventOSX.h"
580 #elif defined(XP_WIN)
581 # include "mozilla/plugins/NPEventWindows.h"
582 #elif defined(XP_OS2)
583 # error Sorry, OS/2 is not supported
584 #elif defined(XP_UNIX) && defined(MOZ_X11)
585 # include "mozilla/plugins/NPEventX11.h"
586 #else
587 # error Unsupported platform
588 #endif
591 #endif /* DOM_PLUGINS_PLUGINMESSAGEUTILS_H */