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