1 /* -*- Mode: C++; tab-width: 8; 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 * The Mozilla Foundation
20 * Portions created by the Initial Developer are Copyright (C) 2009
21 * the Initial Developer. All Rights Reserved.
24 * Ben Turner <bent.mozilla@gmail.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "PluginScriptableObjectUtils.h"
44 template<class InstanceType
>
48 class VariantTraits
<mozilla::plugins::PluginInstanceParent
>
51 typedef mozilla::plugins::PluginScriptableObjectParent ScriptableObjectType
;
55 class VariantTraits
<mozilla::plugins::PluginInstanceChild
>
58 typedef mozilla::plugins::PluginScriptableObjectChild ScriptableObjectType
;
61 } /* anonymous namespace */
64 mozilla::plugins::ConvertToVariant(const Variant
& aRemoteVariant
,
66 PluginInstanceParent
* aInstance
)
68 switch (aRemoteVariant
.type()) {
69 case Variant::Tvoid_t
: {
70 VOID_TO_NPVARIANT(aVariant
);
74 case Variant::Tnull_t
: {
75 NULL_TO_NPVARIANT(aVariant
);
79 case Variant::Tbool
: {
80 BOOLEAN_TO_NPVARIANT(aRemoteVariant
.get_bool(), aVariant
);
85 INT32_TO_NPVARIANT(aRemoteVariant
.get_int(), aVariant
);
89 case Variant::Tdouble
: {
90 DOUBLE_TO_NPVARIANT(aRemoteVariant
.get_double(), aVariant
);
94 case Variant::TnsCString
: {
95 const nsCString
& string
= aRemoteVariant
.get_nsCString();
96 NPUTF8
* buffer
= reinterpret_cast<NPUTF8
*>(strdup(string
.get()));
98 NS_ERROR("Out of memory!");
101 STRINGN_TO_NPVARIANT(buffer
, string
.Length(), aVariant
);
105 case Variant::TPPluginScriptableObjectParent
: {
106 NS_ASSERTION(aInstance
, "Must have an instance!");
107 NPObject
* object
= NPObjectFromVariant(aRemoteVariant
);
109 NS_ERROR("Er, this shouldn't fail!");
113 const NPNetscapeFuncs
* npn
= GetNetscapeFuncs(aInstance
);
115 NS_ERROR("Null netscape funcs!");
119 npn
->retainobject(object
);
120 OBJECT_TO_NPVARIANT(object
, aVariant
);
124 case Variant::TPPluginScriptableObjectChild
: {
125 NS_ASSERTION(!aInstance
, "No instance should be given!");
126 NS_ASSERTION(PluginModuleChild::current(),
127 "Should be running on child only!");
129 NPObject
* object
= NPObjectFromVariant(aRemoteVariant
);
130 NS_ASSERTION(object
, "Null object?!");
132 PluginModuleChild::sBrowserFuncs
.retainobject(object
);
133 OBJECT_TO_NPVARIANT(object
, aVariant
);
138 NS_NOTREACHED("Shouldn't get here!");
145 template <class InstanceType
>
147 mozilla::plugins::ConvertToRemoteVariant(const NPVariant
& aVariant
,
148 Variant
& aRemoteVariant
,
149 InstanceType
* aInstance
,
152 if (NPVARIANT_IS_VOID(aVariant
)) {
153 aRemoteVariant
= mozilla::void_t();
155 else if (NPVARIANT_IS_NULL(aVariant
)) {
156 aRemoteVariant
= mozilla::null_t();
158 else if (NPVARIANT_IS_BOOLEAN(aVariant
)) {
159 aRemoteVariant
= NPVARIANT_TO_BOOLEAN(aVariant
);
161 else if (NPVARIANT_IS_INT32(aVariant
)) {
162 aRemoteVariant
= NPVARIANT_TO_INT32(aVariant
);
164 else if (NPVARIANT_IS_DOUBLE(aVariant
)) {
165 aRemoteVariant
= NPVARIANT_TO_DOUBLE(aVariant
);
167 else if (NPVARIANT_IS_STRING(aVariant
)) {
168 NPString str
= NPVARIANT_TO_STRING(aVariant
);
169 nsCString
string(str
.UTF8Characters
, str
.UTF8Length
);
170 aRemoteVariant
= string
;
172 else if (NPVARIANT_IS_OBJECT(aVariant
)) {
173 NPObject
* object
= NPVARIANT_TO_OBJECT(aVariant
);
175 typename VariantTraits
<InstanceType
>::ScriptableObjectType
* actor
=
176 aInstance
->GetActorForNPObject(object
);
179 NS_ERROR("Null actor!");
183 if (aProtectActors
) {
187 aRemoteVariant
= actor
;
190 NS_NOTREACHED("Shouldn't get here!");