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