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
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is Mozilla Plugin App.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef DOM_PLUGINS_PLUGINMESSAGEUTILS_H
40 : #define DOM_PLUGINS_PLUGINMESSAGEUTILS_H
41 :
42 : #include "IPC/IPCMessageUtils.h"
43 : #include "base/message_loop.h"
44 :
45 : #include "mozilla/ipc/RPCChannel.h"
46 : #include "mozilla/ipc/CrossProcessMutex.h"
47 : #include "gfxipc/ShadowLayerUtils.h"
48 :
49 : #include "npapi.h"
50 : #include "npruntime.h"
51 : #include "npfunctions.h"
52 : #include "nsAutoPtr.h"
53 : #include "nsStringGlue.h"
54 : #include "nsTArray.h"
55 : #include "nsThreadUtils.h"
56 : #include "prlog.h"
57 : #include "nsHashKeys.h"
58 : #ifdef MOZ_CRASHREPORTER
59 : # include "nsExceptionHandler.h"
60 : #endif
61 : #ifdef XP_MACOSX
62 : #include "PluginInterposeOSX.h"
63 : #else
64 : namespace mac_plugin_interposing { class NSCursorInfo { }; }
65 : #endif
66 : using mac_plugin_interposing::NSCursorInfo;
67 :
68 : namespace mozilla {
69 : namespace plugins {
70 :
71 : using layers::SurfaceDescriptorX11;
72 :
73 : enum ScriptableObjectType
74 : {
75 : LocalObject,
76 : Proxy
77 : };
78 :
79 : mozilla::ipc::RPCChannel::RacyRPCPolicy
80 : MediateRace(const mozilla::ipc::RPCChannel::Message& parent,
81 : const mozilla::ipc::RPCChannel::Message& child);
82 :
83 : std::string
84 : MungePluginDsoPath(const std::string& path);
85 : std::string
86 : UnmungePluginDsoPath(const std::string& munged);
87 :
88 : extern PRLogModuleInfo* gPluginLog;
89 :
90 : const uint32_t kAllowAsyncDrawing = 0x1;
91 :
92 0 : inline bool IsDrawingModelAsync(int16_t aModel) {
93 0 : return aModel == NPDrawingModelAsyncBitmapSurface
94 : #ifdef XP_WIN
95 : || aModel == NPDrawingModelAsyncWindowsDXGISurface
96 : #endif
97 : ;
98 : }
99 :
100 : #if defined(_MSC_VER)
101 : #define FULLFUNCTION __FUNCSIG__
102 : #elif (__GNUC__ >= 4)
103 : #define FULLFUNCTION __PRETTY_FUNCTION__
104 : #else
105 : #define FULLFUNCTION __FUNCTION__
106 : #endif
107 :
108 : #define PLUGIN_LOG_DEBUG(args) PR_LOG(gPluginLog, PR_LOG_DEBUG, args)
109 : #define PLUGIN_LOG_DEBUG_FUNCTION PR_LOG(gPluginLog, PR_LOG_DEBUG, ("%s", FULLFUNCTION))
110 : #define PLUGIN_LOG_DEBUG_METHOD PR_LOG(gPluginLog, PR_LOG_DEBUG, ("%s [%p]", FULLFUNCTION, (void*) this))
111 :
112 : /**
113 : * This is NPByteRange without the linked list.
114 : */
115 : struct IPCByteRange
116 : {
117 : int32_t offset;
118 : uint32_t length;
119 : };
120 :
121 : typedef std::vector<IPCByteRange> IPCByteRanges;
122 :
123 : typedef nsCString Buffer;
124 :
125 : struct NPRemoteWindow
126 : {
127 : NPRemoteWindow();
128 : uint64_t window;
129 : int32_t x;
130 : int32_t y;
131 : uint32_t width;
132 : uint32_t height;
133 : NPRect clipRect;
134 : NPWindowType type;
135 : #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
136 : VisualID visualID;
137 : Colormap colormap;
138 : #endif /* XP_UNIX */
139 : #if defined(XP_WIN)
140 : base::SharedMemoryHandle surfaceHandle;
141 : #endif
142 : };
143 :
144 : #ifdef XP_WIN
145 : typedef HWND NativeWindowHandle;
146 : #elif defined(MOZ_X11)
147 : typedef XID NativeWindowHandle;
148 : #elif defined(XP_MACOSX) || defined(ANDROID) || defined(MOZ_WIDGET_QT)
149 : typedef intptr_t NativeWindowHandle; // never actually used, will always be 0
150 : #else
151 : #error Need NativeWindowHandle for this platform
152 : #endif
153 :
154 : #ifdef XP_WIN
155 : typedef base::SharedMemoryHandle WindowsSharedMemoryHandle;
156 : typedef HANDLE DXGISharedSurfaceHandle;
157 : #else
158 : typedef mozilla::null_t WindowsSharedMemoryHandle;
159 : typedef mozilla::null_t DXGISharedSurfaceHandle;
160 : #endif
161 :
162 : // XXX maybe not the best place for these. better one?
163 :
164 : #define VARSTR(v_) case v_: return #v_
165 : inline const char* const
166 0 : NPPVariableToString(NPPVariable aVar)
167 : {
168 0 : switch (aVar) {
169 0 : VARSTR(NPPVpluginNameString);
170 0 : VARSTR(NPPVpluginDescriptionString);
171 0 : VARSTR(NPPVpluginWindowBool);
172 0 : VARSTR(NPPVpluginTransparentBool);
173 0 : VARSTR(NPPVjavaClass);
174 0 : VARSTR(NPPVpluginWindowSize);
175 0 : VARSTR(NPPVpluginTimerInterval);
176 :
177 0 : VARSTR(NPPVpluginScriptableInstance);
178 0 : VARSTR(NPPVpluginScriptableIID);
179 :
180 0 : VARSTR(NPPVjavascriptPushCallerBool);
181 :
182 0 : VARSTR(NPPVpluginKeepLibraryInMemory);
183 0 : VARSTR(NPPVpluginNeedsXEmbed);
184 :
185 0 : VARSTR(NPPVpluginScriptableNPObject);
186 :
187 0 : VARSTR(NPPVformValue);
188 :
189 0 : VARSTR(NPPVpluginUrlRequestsDisplayedBool);
190 :
191 0 : VARSTR(NPPVpluginWantsAllNetworkStreams);
192 :
193 : #ifdef XP_MACOSX
194 : VARSTR(NPPVpluginDrawingModel);
195 : VARSTR(NPPVpluginEventModel);
196 : #endif
197 :
198 0 : default: return "???";
199 : }
200 : }
201 :
202 : inline const char*
203 0 : NPNVariableToString(NPNVariable aVar)
204 : {
205 0 : switch(aVar) {
206 0 : VARSTR(NPNVxDisplay);
207 0 : VARSTR(NPNVxtAppContext);
208 0 : VARSTR(NPNVnetscapeWindow);
209 0 : VARSTR(NPNVjavascriptEnabledBool);
210 0 : VARSTR(NPNVasdEnabledBool);
211 0 : VARSTR(NPNVisOfflineBool);
212 :
213 0 : VARSTR(NPNVserviceManager);
214 0 : VARSTR(NPNVDOMElement);
215 0 : VARSTR(NPNVDOMWindow);
216 0 : VARSTR(NPNVToolkit);
217 0 : VARSTR(NPNVSupportsXEmbedBool);
218 :
219 0 : VARSTR(NPNVWindowNPObject);
220 :
221 0 : VARSTR(NPNVPluginElementNPObject);
222 :
223 0 : VARSTR(NPNVSupportsWindowless);
224 :
225 0 : VARSTR(NPNVprivateModeBool);
226 0 : VARSTR(NPNVdocumentOrigin);
227 :
228 0 : default: return "???";
229 : }
230 : }
231 : #undef VARSTR
232 :
233 0 : inline bool IsPluginThread()
234 : {
235 0 : MessageLoop* loop = MessageLoop::current();
236 0 : if (!loop)
237 0 : return false;
238 0 : return (loop->type() == MessageLoop::TYPE_UI);
239 : }
240 :
241 0 : inline void AssertPluginThread()
242 : {
243 0 : NS_ASSERTION(IsPluginThread(), "Should be on the plugin's main thread!");
244 0 : }
245 :
246 : #define ENSURE_PLUGIN_THREAD(retval) \
247 : PR_BEGIN_MACRO \
248 : if (!IsPluginThread()) { \
249 : NS_WARNING("Not running on the plugin's main thread!"); \
250 : return (retval); \
251 : } \
252 : PR_END_MACRO
253 :
254 : #define ENSURE_PLUGIN_THREAD_VOID() \
255 : PR_BEGIN_MACRO \
256 : if (!IsPluginThread()) { \
257 : NS_WARNING("Not running on the plugin's main thread!"); \
258 : return; \
259 : } \
260 : PR_END_MACRO
261 :
262 : void DeferNPObjectLastRelease(const NPNetscapeFuncs* f, NPObject* o);
263 : void DeferNPVariantLastRelease(const NPNetscapeFuncs* f, NPVariant* v);
264 :
265 : // in NPAPI, char* == NULL is sometimes meaningful. the following is
266 : // helper code for dealing with nullable nsCString's
267 : inline nsCString
268 0 : NullableString(const char* aString)
269 : {
270 0 : if (!aString) {
271 0 : nsCString str;
272 0 : str.SetIsVoid(true);
273 0 : return str;
274 : }
275 0 : return nsCString(aString);
276 : }
277 :
278 : inline const char*
279 0 : NullableStringGet(const nsCString& str)
280 : {
281 0 : if (str.IsVoid())
282 0 : return NULL;
283 :
284 0 : return str.get();
285 : }
286 :
287 : struct DeletingObjectEntry : public nsPtrHashKey<NPObject>
288 0 : {
289 0 : DeletingObjectEntry(const NPObject* key)
290 : : nsPtrHashKey<NPObject>(key)
291 0 : , mDeleted(false)
292 0 : { }
293 :
294 : bool mDeleted;
295 : };
296 :
297 : #ifdef XP_WIN
298 : // The private event used for double-pass widgetless plugin rendering.
299 : UINT DoublePassRenderingEvent();
300 : #endif
301 :
302 : } /* namespace plugins */
303 :
304 : } /* namespace mozilla */
305 :
306 : namespace IPC {
307 :
308 : template <>
309 : struct ParamTraits<NPRect>
310 : {
311 : typedef NPRect paramType;
312 :
313 0 : static void Write(Message* aMsg, const paramType& aParam)
314 : {
315 0 : WriteParam(aMsg, aParam.top);
316 0 : WriteParam(aMsg, aParam.left);
317 0 : WriteParam(aMsg, aParam.bottom);
318 0 : WriteParam(aMsg, aParam.right);
319 0 : }
320 :
321 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
322 : {
323 : uint16_t top, left, bottom, right;
324 0 : if (ReadParam(aMsg, aIter, &top) &&
325 0 : ReadParam(aMsg, aIter, &left) &&
326 0 : ReadParam(aMsg, aIter, &bottom) &&
327 0 : ReadParam(aMsg, aIter, &right)) {
328 0 : aResult->top = top;
329 0 : aResult->left = left;
330 0 : aResult->bottom = bottom;
331 0 : aResult->right = right;
332 0 : return true;
333 : }
334 0 : return false;
335 : }
336 :
337 : static void Log(const paramType& aParam, std::wstring* aLog)
338 : {
339 : aLog->append(StringPrintf(L"[%u, %u, %u, %u]", aParam.top, aParam.left,
340 : aParam.bottom, aParam.right));
341 : }
342 : };
343 :
344 : template <>
345 : struct ParamTraits<NPWindowType>
346 : {
347 : typedef NPWindowType paramType;
348 :
349 0 : static void Write(Message* aMsg, const paramType& aParam)
350 : {
351 0 : aMsg->WriteInt16(int16(aParam));
352 0 : }
353 :
354 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
355 : {
356 : int16 result;
357 0 : if (aMsg->ReadInt16(aIter, &result)) {
358 0 : *aResult = paramType(result);
359 0 : return true;
360 : }
361 0 : return false;
362 : }
363 :
364 : static void Log(const paramType& aParam, std::wstring* aLog)
365 : {
366 : aLog->append(StringPrintf(L"%d", int16(aParam)));
367 : }
368 : };
369 :
370 : template <>
371 : struct ParamTraits<NPImageFormat>
372 : {
373 : typedef NPImageFormat paramType;
374 :
375 0 : static void Write(Message* aMsg, const paramType& aParam)
376 : {
377 0 : aMsg->WriteInt16(int16(aParam));
378 0 : }
379 :
380 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
381 : {
382 : int16 result;
383 0 : if (aMsg->ReadInt16(aIter, &result)) {
384 0 : *aResult = paramType(result);
385 0 : return true;
386 : }
387 0 : return false;
388 : }
389 :
390 : static void Log(const paramType& aParam, std::wstring* aLog)
391 : {
392 : aLog->append(StringPrintf(L"%d", int16(aParam)));
393 : }
394 : };
395 :
396 : template <>
397 : struct ParamTraits<mozilla::plugins::NPRemoteWindow>
398 : {
399 : typedef mozilla::plugins::NPRemoteWindow paramType;
400 :
401 0 : static void Write(Message* aMsg, const paramType& aParam)
402 : {
403 0 : aMsg->WriteUInt64(aParam.window);
404 0 : WriteParam(aMsg, aParam.x);
405 0 : WriteParam(aMsg, aParam.y);
406 0 : WriteParam(aMsg, aParam.width);
407 0 : WriteParam(aMsg, aParam.height);
408 0 : WriteParam(aMsg, aParam.clipRect);
409 0 : WriteParam(aMsg, aParam.type);
410 : #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
411 0 : aMsg->WriteULong(aParam.visualID);
412 0 : aMsg->WriteULong(aParam.colormap);
413 : #endif
414 : #if defined(XP_WIN)
415 : WriteParam(aMsg, aParam.surfaceHandle);
416 : #endif
417 0 : }
418 :
419 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
420 : {
421 : uint64 window;
422 : int32_t x, y;
423 : uint32_t width, height;
424 : NPRect clipRect;
425 : NPWindowType type;
426 0 : if (!(aMsg->ReadUInt64(aIter, &window) &&
427 0 : ReadParam(aMsg, aIter, &x) &&
428 0 : ReadParam(aMsg, aIter, &y) &&
429 0 : ReadParam(aMsg, aIter, &width) &&
430 0 : ReadParam(aMsg, aIter, &height) &&
431 0 : ReadParam(aMsg, aIter, &clipRect) &&
432 0 : ReadParam(aMsg, aIter, &type)))
433 0 : return false;
434 :
435 : #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
436 : unsigned long visualID;
437 : unsigned long colormap;
438 0 : if (!(aMsg->ReadULong(aIter, &visualID) &&
439 0 : aMsg->ReadULong(aIter, &colormap)))
440 0 : return false;
441 : #endif
442 :
443 : #if defined(XP_WIN)
444 : base::SharedMemoryHandle surfaceHandle;
445 : if (!ReadParam(aMsg, aIter, &surfaceHandle))
446 : return false;
447 : #endif
448 :
449 0 : aResult->window = window;
450 0 : aResult->x = x;
451 0 : aResult->y = y;
452 0 : aResult->width = width;
453 0 : aResult->height = height;
454 0 : aResult->clipRect = clipRect;
455 0 : aResult->type = type;
456 : #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
457 0 : aResult->visualID = visualID;
458 0 : aResult->colormap = colormap;
459 : #endif
460 : #if defined(XP_WIN)
461 : aResult->surfaceHandle = surfaceHandle;
462 : #endif
463 0 : return true;
464 : }
465 :
466 : static void Log(const paramType& aParam, std::wstring* aLog)
467 : {
468 : aLog->append(StringPrintf(L"[%u, %d, %d, %u, %u, %d",
469 : (unsigned long)aParam.window,
470 : aParam.x, aParam.y, aParam.width,
471 : aParam.height, (long)aParam.type));
472 : }
473 : };
474 :
475 : template <>
476 : struct ParamTraits<NPString>
477 : {
478 : typedef NPString paramType;
479 :
480 : static void Write(Message* aMsg, const paramType& aParam)
481 : {
482 : WriteParam(aMsg, aParam.UTF8Length);
483 : aMsg->WriteBytes(aParam.UTF8Characters,
484 : aParam.UTF8Length * sizeof(NPUTF8));
485 : }
486 :
487 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
488 : {
489 : if (ReadParam(aMsg, aIter, &aResult->UTF8Length)) {
490 : int byteCount = aResult->UTF8Length * sizeof(NPUTF8);
491 : if (!byteCount) {
492 : aResult->UTF8Characters = "\0";
493 : return true;
494 : }
495 :
496 : const char* messageBuffer = nsnull;
497 : nsAutoArrayPtr<char> newBuffer(new char[byteCount]);
498 : if (newBuffer && aMsg->ReadBytes(aIter, &messageBuffer, byteCount )) {
499 : memcpy((void*)messageBuffer, newBuffer.get(), byteCount);
500 : aResult->UTF8Characters = newBuffer.forget();
501 : return true;
502 : }
503 : }
504 : return false;
505 : }
506 :
507 : static void Log(const paramType& aParam, std::wstring* aLog)
508 : {
509 : aLog->append(StringPrintf(L"%s", aParam.UTF8Characters));
510 : }
511 : };
512 :
513 : #ifdef XP_MACOSX
514 : template <>
515 : struct ParamTraits<NPNSString*>
516 : {
517 : typedef NPNSString* paramType;
518 :
519 : // Empty string writes a length of 0 and no buffer.
520 : // We don't write a NULL terminating character in buffers.
521 : static void Write(Message* aMsg, const paramType& aParam)
522 : {
523 : CFStringRef cfString = (CFStringRef)aParam;
524 :
525 : // Write true if we have a string, false represents NULL.
526 : aMsg->WriteBool(!!cfString);
527 : if (!cfString) {
528 : return;
529 : }
530 :
531 : long length = ::CFStringGetLength(cfString);
532 : WriteParam(aMsg, length);
533 : if (length == 0) {
534 : return;
535 : }
536 :
537 : // Attempt to get characters without any allocation/conversion.
538 : if (::CFStringGetCharactersPtr(cfString)) {
539 : aMsg->WriteBytes(::CFStringGetCharactersPtr(cfString), length * sizeof(UniChar));
540 : } else {
541 : UniChar *buffer = (UniChar*)moz_xmalloc(length * sizeof(UniChar));
542 : ::CFStringGetCharacters(cfString, ::CFRangeMake(0, length), buffer);
543 : aMsg->WriteBytes(buffer, length * sizeof(UniChar));
544 : free(buffer);
545 : }
546 : }
547 :
548 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
549 : {
550 : bool haveString = false;
551 : if (!aMsg->ReadBool(aIter, &haveString)) {
552 : return false;
553 : }
554 : if (!haveString) {
555 : *aResult = NULL;
556 : return true;
557 : }
558 :
559 : long length;
560 : if (!ReadParam(aMsg, aIter, &length)) {
561 : return false;
562 : }
563 :
564 : UniChar* buffer = nsnull;
565 : if (length != 0) {
566 : if (!aMsg->ReadBytes(aIter, (const char**)&buffer, length * sizeof(UniChar)) ||
567 : !buffer) {
568 : return false;
569 : }
570 : }
571 :
572 : *aResult = (NPNSString*)::CFStringCreateWithBytes(kCFAllocatorDefault, (UInt8*)buffer,
573 : length * sizeof(UniChar),
574 : kCFStringEncodingUTF16, false);
575 : if (!*aResult) {
576 : return false;
577 : }
578 :
579 : return true;
580 : }
581 : };
582 : #endif
583 :
584 : #ifdef XP_MACOSX
585 : template <>
586 : struct ParamTraits<NSCursorInfo>
587 : {
588 : typedef NSCursorInfo paramType;
589 :
590 : static void Write(Message* aMsg, const paramType& aParam)
591 : {
592 : NSCursorInfo::Type type = aParam.GetType();
593 :
594 : aMsg->WriteInt(type);
595 :
596 : nsPoint hotSpot = aParam.GetHotSpot();
597 : WriteParam(aMsg, hotSpot.x);
598 : WriteParam(aMsg, hotSpot.y);
599 :
600 : uint32_t dataLength = aParam.GetCustomImageDataLength();
601 : WriteParam(aMsg, dataLength);
602 : if (dataLength == 0) {
603 : return;
604 : }
605 :
606 : uint8_t* buffer = (uint8_t*)moz_xmalloc(dataLength);
607 : memcpy(buffer, aParam.GetCustomImageData(), dataLength);
608 : aMsg->WriteBytes(buffer, dataLength);
609 : free(buffer);
610 : }
611 :
612 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
613 : {
614 : NSCursorInfo::Type type;
615 : if (!aMsg->ReadInt(aIter, (int*)&type)) {
616 : return false;
617 : }
618 :
619 : nscoord hotSpotX, hotSpotY;
620 : if (!ReadParam(aMsg, aIter, &hotSpotX) ||
621 : !ReadParam(aMsg, aIter, &hotSpotY)) {
622 : return false;
623 : }
624 :
625 : uint32_t dataLength;
626 : if (!ReadParam(aMsg, aIter, &dataLength)) {
627 : return false;
628 : }
629 :
630 : uint8_t* data = NULL;
631 : if (dataLength != 0) {
632 : if (!aMsg->ReadBytes(aIter, (const char**)&data, dataLength) || !data) {
633 : return false;
634 : }
635 : }
636 :
637 : aResult->SetType(type);
638 : aResult->SetHotSpot(nsPoint(hotSpotX, hotSpotY));
639 : aResult->SetCustomImageData(data, dataLength);
640 :
641 : return true;
642 : }
643 :
644 : static void Log(const paramType& aParam, std::wstring* aLog)
645 : {
646 : const char* typeName = aParam.GetTypeName();
647 : nsPoint hotSpot = aParam.GetHotSpot();
648 : int hotSpotX, hotSpotY;
649 : #ifdef NS_COORD_IS_FLOAT
650 : hotSpotX = rint(hotSpot.x);
651 : hotSpotY = rint(hotSpot.y);
652 : #else
653 : hotSpotX = hotSpot.x;
654 : hotSpotY = hotSpot.y;
655 : #endif
656 : uint32_t dataLength = aParam.GetCustomImageDataLength();
657 : uint8_t* data = aParam.GetCustomImageData();
658 :
659 : aLog->append(StringPrintf(L"[%s, (%i %i), %u, %p]",
660 : typeName, hotSpotX, hotSpotY, dataLength, data));
661 : }
662 : };
663 : #else
664 : template<>
665 : struct ParamTraits<NSCursorInfo>
666 : {
667 : typedef NSCursorInfo paramType;
668 0 : static void Write(Message* aMsg, const paramType& aParam) {
669 0 : NS_RUNTIMEABORT("NSCursorInfo isn't meaningful on this platform");
670 0 : }
671 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult) {
672 0 : NS_RUNTIMEABORT("NSCursorInfo isn't meaningful on this platform");
673 0 : return false;
674 : }
675 : };
676 : #endif // #ifdef XP_MACOSX
677 :
678 : template <>
679 : struct ParamTraits<NPVariant>
680 : {
681 : typedef NPVariant paramType;
682 :
683 : static void Write(Message* aMsg, const paramType& aParam)
684 : {
685 : if (NPVARIANT_IS_VOID(aParam)) {
686 : aMsg->WriteInt(0);
687 : return;
688 : }
689 :
690 : if (NPVARIANT_IS_NULL(aParam)) {
691 : aMsg->WriteInt(1);
692 : return;
693 : }
694 :
695 : if (NPVARIANT_IS_BOOLEAN(aParam)) {
696 : aMsg->WriteInt(2);
697 : WriteParam(aMsg, NPVARIANT_TO_BOOLEAN(aParam));
698 : return;
699 : }
700 :
701 : if (NPVARIANT_IS_INT32(aParam)) {
702 : aMsg->WriteInt(3);
703 : WriteParam(aMsg, NPVARIANT_TO_INT32(aParam));
704 : return;
705 : }
706 :
707 : if (NPVARIANT_IS_DOUBLE(aParam)) {
708 : aMsg->WriteInt(4);
709 : WriteParam(aMsg, NPVARIANT_TO_DOUBLE(aParam));
710 : return;
711 : }
712 :
713 : if (NPVARIANT_IS_STRING(aParam)) {
714 : aMsg->WriteInt(5);
715 : WriteParam(aMsg, NPVARIANT_TO_STRING(aParam));
716 : return;
717 : }
718 :
719 : NS_ERROR("Unsupported type!");
720 : }
721 :
722 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
723 : {
724 : int type;
725 : if (!aMsg->ReadInt(aIter, &type)) {
726 : return false;
727 : }
728 :
729 : switch (type) {
730 : case 0:
731 : VOID_TO_NPVARIANT(*aResult);
732 : return true;
733 :
734 : case 1:
735 : NULL_TO_NPVARIANT(*aResult);
736 : return true;
737 :
738 : case 2: {
739 : bool value;
740 : if (ReadParam(aMsg, aIter, &value)) {
741 : BOOLEAN_TO_NPVARIANT(value, *aResult);
742 : return true;
743 : }
744 : } break;
745 :
746 : case 3: {
747 : int32 value;
748 : if (ReadParam(aMsg, aIter, &value)) {
749 : INT32_TO_NPVARIANT(value, *aResult);
750 : return true;
751 : }
752 : } break;
753 :
754 : case 4: {
755 : double value;
756 : if (ReadParam(aMsg, aIter, &value)) {
757 : DOUBLE_TO_NPVARIANT(value, *aResult);
758 : return true;
759 : }
760 : } break;
761 :
762 : case 5: {
763 : NPString value;
764 : if (ReadParam(aMsg, aIter, &value)) {
765 : STRINGN_TO_NPVARIANT(value.UTF8Characters, value.UTF8Length,
766 : *aResult);
767 : return true;
768 : }
769 : } break;
770 :
771 : default:
772 : NS_ERROR("Unsupported type!");
773 : }
774 :
775 : return false;
776 : }
777 :
778 : static void Log(const paramType& aParam, std::wstring* aLog)
779 : {
780 : if (NPVARIANT_IS_VOID(aParam)) {
781 : aLog->append(L"[void]");
782 : return;
783 : }
784 :
785 : if (NPVARIANT_IS_NULL(aParam)) {
786 : aLog->append(L"[null]");
787 : return;
788 : }
789 :
790 : if (NPVARIANT_IS_BOOLEAN(aParam)) {
791 : LogParam(NPVARIANT_TO_BOOLEAN(aParam), aLog);
792 : return;
793 : }
794 :
795 : if (NPVARIANT_IS_INT32(aParam)) {
796 : LogParam(NPVARIANT_TO_INT32(aParam), aLog);
797 : return;
798 : }
799 :
800 : if (NPVARIANT_IS_DOUBLE(aParam)) {
801 : LogParam(NPVARIANT_TO_DOUBLE(aParam), aLog);
802 : return;
803 : }
804 :
805 : if (NPVARIANT_IS_STRING(aParam)) {
806 : LogParam(NPVARIANT_TO_STRING(aParam), aLog);
807 : return;
808 : }
809 :
810 : NS_ERROR("Unsupported type!");
811 : }
812 : };
813 :
814 : template <>
815 : struct ParamTraits<mozilla::plugins::IPCByteRange>
816 : {
817 : typedef mozilla::plugins::IPCByteRange paramType;
818 :
819 0 : static void Write(Message* aMsg, const paramType& aParam)
820 : {
821 0 : WriteParam(aMsg, aParam.offset);
822 0 : WriteParam(aMsg, aParam.length);
823 0 : }
824 :
825 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
826 : {
827 : paramType p;
828 0 : if (ReadParam(aMsg, aIter, &p.offset) &&
829 0 : ReadParam(aMsg, aIter, &p.length)) {
830 0 : *aResult = p;
831 0 : return true;
832 : }
833 0 : return false;
834 : }
835 : };
836 :
837 : template <>
838 : struct ParamTraits<NPNVariable>
839 : {
840 : typedef NPNVariable paramType;
841 :
842 0 : static void Write(Message* aMsg, const paramType& aParam)
843 : {
844 0 : WriteParam(aMsg, int(aParam));
845 0 : }
846 :
847 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
848 : {
849 : int intval;
850 0 : if (ReadParam(aMsg, aIter, &intval)) {
851 0 : *aResult = paramType(intval);
852 0 : return true;
853 : }
854 0 : return false;
855 : }
856 : };
857 :
858 : template<>
859 : struct ParamTraits<NPNURLVariable>
860 : {
861 : typedef NPNURLVariable paramType;
862 :
863 0 : static void Write(Message* aMsg, const paramType& aParam)
864 : {
865 0 : WriteParam(aMsg, int(aParam));
866 0 : }
867 :
868 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
869 : {
870 : int intval;
871 0 : if (ReadParam(aMsg, aIter, &intval)) {
872 0 : switch (intval) {
873 : case NPNURLVCookie:
874 : case NPNURLVProxy:
875 0 : *aResult = paramType(intval);
876 0 : return true;
877 : }
878 : }
879 0 : return false;
880 : }
881 : };
882 :
883 :
884 : template<>
885 : struct ParamTraits<NPCoordinateSpace>
886 : {
887 : typedef NPCoordinateSpace paramType;
888 :
889 0 : static void Write(Message* aMsg, const paramType& aParam)
890 : {
891 0 : WriteParam(aMsg, int32(aParam));
892 0 : }
893 :
894 0 : static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
895 : {
896 : int32 intval;
897 0 : if (ReadParam(aMsg, aIter, &intval)) {
898 0 : switch (intval) {
899 : case NPCoordinateSpacePlugin:
900 : case NPCoordinateSpaceWindow:
901 : case NPCoordinateSpaceFlippedWindow:
902 : case NPCoordinateSpaceScreen:
903 : case NPCoordinateSpaceFlippedScreen:
904 0 : *aResult = paramType(intval);
905 0 : return true;
906 : }
907 : }
908 0 : return false;
909 : }
910 : };
911 :
912 : } /* namespace IPC */
913 :
914 :
915 : // Serializing NPEvents is completely platform-specific and can be rather
916 : // intricate depending on the platform. So for readability we split it
917 : // into separate files and have the only macro crud live here.
918 : //
919 : // NB: these guards are based on those where struct NPEvent is defined
920 : // in npapi.h. They should be kept in sync.
921 : #if defined(XP_MACOSX)
922 : # include "mozilla/plugins/NPEventOSX.h"
923 : #elif defined(XP_WIN)
924 : # include "mozilla/plugins/NPEventWindows.h"
925 : #elif defined(XP_OS2)
926 : # error Sorry, OS/2 is not supported
927 : #elif defined(ANDROID)
928 : # include "mozilla/plugins/NPEventAndroid.h"
929 : #elif defined(XP_UNIX)
930 : # include "mozilla/plugins/NPEventUnix.h"
931 : #else
932 : # error Unsupported platform
933 : #endif
934 :
935 : #endif /* DOM_PLUGINS_PLUGINMESSAGEUTILS_H */
|