1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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
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.
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"
45 #include "npruntime.h"
46 #include "npfunctions.h"
47 #include "nsAutoPtr.h"
48 #include "nsStringGlue.h"
49 #include "nsThreadUtils.h"
53 // XXX might want to move these to nscore.h or something, they can be
60 typedef intptr_t NPRemoteIdentifier
;
67 * This is NPByteRange without the linked list.
75 typedef std::vector
<IPCByteRange
> IPCByteRanges
;
77 typedef nsCString Buffer
;
88 #if defined(MOZ_X11) && defined(XP_UNIX) && !defined(XP_MACOSX)
93 base::SharedMemoryHandle surfaceHandle
;
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
)
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
);
129 VARSTR(NPPVpluginDrawingModel
);
130 VARSTR(NPPVpluginEventModel
);
133 default: return "???";
138 NPNVariableToString(NPNVariable 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
);
152 VARSTR(NPNVSupportsXEmbedBool
);
154 VARSTR(NPNVWindowNPObject
);
156 VARSTR(NPNVPluginElementNPObject
);
158 VARSTR(NPNVSupportsWindowless
);
160 VARSTR(NPNVprivateModeBool
);
162 default: return "???";
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
179 NullableString(const char* aString
)
183 str
.SetIsVoid(PR_TRUE
);
186 return nsCString(aString
);
190 NullableStringGet(const nsCString
& str
)
198 } /* namespace plugins */
200 } /* namespace mozilla */
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
)) {
225 aResult
->left
= left
;
226 aResult
->bottom
= bottom
;
227 aResult
->right
= right
;
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
));
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
)
253 if (aMsg
->ReadInt16(aIter
, &result
)) {
254 *aResult
= paramType(result
);
260 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
262 aLog
->append(StringPrintf(L
"%d", int16(aParam
)));
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
);
285 WriteParam(aMsg
, aParam
.surfaceHandle
);
289 static bool Read(const Message
* aMsg
, void** aIter
, paramType
* aResult
)
291 unsigned long window
;
293 uint32_t width
, height
;
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
)))
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
)))
314 base::SharedMemoryHandle surfaceHandle
;
315 if (!ReadParam(aMsg
, aIter
, &surfaceHandle
))
319 aResult
->window
= window
;
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
;
331 aResult
->surfaceHandle
= surfaceHandle
;
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
));
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
);
362 aResult
->UTF8Characters
= "\0";
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();
377 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
379 aLog
->append(StringPrintf(L
"%s", aParam
.UTF8Characters
));
384 struct ParamTraits
<NPVariant
>
386 typedef NPVariant paramType
;
388 static void Write(Message
* aMsg
, const paramType
& aParam
)
390 if (NPVARIANT_IS_VOID(aParam
)) {
395 if (NPVARIANT_IS_NULL(aParam
)) {
400 if (NPVARIANT_IS_BOOLEAN(aParam
)) {
402 WriteParam(aMsg
, NPVARIANT_TO_BOOLEAN(aParam
));
406 if (NPVARIANT_IS_INT32(aParam
)) {
408 WriteParam(aMsg
, NPVARIANT_TO_INT32(aParam
));
412 if (NPVARIANT_IS_DOUBLE(aParam
)) {
414 WriteParam(aMsg
, NPVARIANT_TO_DOUBLE(aParam
));
418 if (NPVARIANT_IS_STRING(aParam
)) {
420 WriteParam(aMsg
, NPVARIANT_TO_STRING(aParam
));
424 NS_ERROR("Unsupported type!");
427 static bool Read(const Message
* aMsg
, void** aIter
, paramType
* aResult
)
430 if (!aMsg
->ReadInt(aIter
, &type
)) {
436 VOID_TO_NPVARIANT(*aResult
);
440 NULL_TO_NPVARIANT(*aResult
);
445 if (ReadParam(aMsg
, aIter
, &value
)) {
446 BOOLEAN_TO_NPVARIANT(value
, *aResult
);
453 if (ReadParam(aMsg
, aIter
, &value
)) {
454 INT32_TO_NPVARIANT(value
, *aResult
);
461 if (ReadParam(aMsg
, aIter
, &value
)) {
462 DOUBLE_TO_NPVARIANT(value
, *aResult
);
469 if (ReadParam(aMsg
, aIter
, &value
)) {
470 STRINGN_TO_NPVARIANT(value
.UTF8Characters
, value
.UTF8Length
,
477 NS_ERROR("Unsupported type!");
483 static void Log(const paramType
& aParam
, std::wstring
* aLog
)
485 if (NPVARIANT_IS_VOID(aParam
)) {
486 aLog
->append(L
"[void]");
490 if (NPVARIANT_IS_NULL(aParam
)) {
491 aLog
->append(L
"[null]");
495 if (NPVARIANT_IS_BOOLEAN(aParam
)) {
496 LogParam(NPVARIANT_TO_BOOLEAN(aParam
), aLog
);
500 if (NPVARIANT_IS_INT32(aParam
)) {
501 LogParam(NPVARIANT_TO_INT32(aParam
), aLog
);
505 if (NPVARIANT_IS_DOUBLE(aParam
)) {
506 LogParam(NPVARIANT_TO_DOUBLE(aParam
), aLog
);
510 if (NPVARIANT_IS_STRING(aParam
)) {
511 LogParam(NPVARIANT_TO_STRING(aParam
), aLog
);
515 NS_ERROR("Unsupported type!");
520 struct ParamTraits
<mozilla::void_t
>
522 typedef mozilla::void_t paramType
;
523 static void Write(Message
* aMsg
, const paramType
& aParam
) { }
525 Read(const Message
* aMsg
, void** aIter
, paramType
* aResult
)
527 *aResult
= paramType();
533 struct ParamTraits
<mozilla::null_t
>
535 typedef mozilla::null_t paramType
;
536 static void Write(Message
* aMsg
, const paramType
& aParam
) { }
538 Read(const Message
* aMsg
, void** aIter
, paramType
* aResult
)
540 *aResult
= paramType();
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
)
559 if (ReadParam(aMsg
, aIter
, &p
.offset
) &&
560 ReadParam(aMsg
, aIter
, &p
.length
)) {
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"
587 # error Unsupported platform
591 #endif /* DOM_PLUGINS_PLUGINMESSAGEUTILS_H */