1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=2 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 : * The Mozilla Foundation
20 : * Portions created by the Initial Developer are Copyright (C) 2009
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Ben Turner <bent.mozilla@gmail.com>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "PluginScriptableObjectUtils.h"
41 :
42 : namespace {
43 :
44 : template<class InstanceType>
45 : class VariantTraits;
46 :
47 : template<>
48 : class VariantTraits<mozilla::plugins::PluginInstanceParent>
49 : {
50 : public:
51 : typedef mozilla::plugins::PluginScriptableObjectParent ScriptableObjectType;
52 : };
53 :
54 : template<>
55 : class VariantTraits<mozilla::plugins::PluginInstanceChild>
56 : {
57 : public:
58 : typedef mozilla::plugins::PluginScriptableObjectChild ScriptableObjectType;
59 : };
60 :
61 : } /* anonymous namespace */
62 :
63 : inline bool
64 0 : mozilla::plugins::ConvertToVariant(const Variant& aRemoteVariant,
65 : NPVariant& aVariant,
66 : PluginInstanceParent* aInstance)
67 : {
68 0 : switch (aRemoteVariant.type()) {
69 : case Variant::Tvoid_t: {
70 0 : VOID_TO_NPVARIANT(aVariant);
71 0 : break;
72 : }
73 :
74 : case Variant::Tnull_t: {
75 0 : NULL_TO_NPVARIANT(aVariant);
76 0 : break;
77 : }
78 :
79 : case Variant::Tbool: {
80 0 : BOOLEAN_TO_NPVARIANT(aRemoteVariant.get_bool(), aVariant);
81 0 : break;
82 : }
83 :
84 : case Variant::Tint: {
85 0 : INT32_TO_NPVARIANT(aRemoteVariant.get_int(), aVariant);
86 0 : break;
87 : }
88 :
89 : case Variant::Tdouble: {
90 0 : DOUBLE_TO_NPVARIANT(aRemoteVariant.get_double(), aVariant);
91 0 : break;
92 : }
93 :
94 : case Variant::TnsCString: {
95 0 : const nsCString& string = aRemoteVariant.get_nsCString();
96 0 : NPUTF8* buffer = reinterpret_cast<NPUTF8*>(strdup(string.get()));
97 0 : if (!buffer) {
98 0 : NS_ERROR("Out of memory!");
99 0 : return false;
100 : }
101 0 : STRINGN_TO_NPVARIANT(buffer, string.Length(), aVariant);
102 0 : break;
103 : }
104 :
105 : case Variant::TPPluginScriptableObjectParent: {
106 0 : NS_ASSERTION(aInstance, "Must have an instance!");
107 0 : NPObject* object = NPObjectFromVariant(aRemoteVariant);
108 0 : if (!object) {
109 0 : NS_ERROR("Er, this shouldn't fail!");
110 0 : return false;
111 : }
112 :
113 0 : const NPNetscapeFuncs* npn = GetNetscapeFuncs(aInstance);
114 0 : if (!npn) {
115 0 : NS_ERROR("Null netscape funcs!");
116 0 : return false;
117 : }
118 :
119 0 : npn->retainobject(object);
120 0 : OBJECT_TO_NPVARIANT(object, aVariant);
121 0 : break;
122 : }
123 :
124 : case Variant::TPPluginScriptableObjectChild: {
125 0 : NS_ASSERTION(!aInstance, "No instance should be given!");
126 0 : NS_ASSERTION(PluginModuleChild::current(),
127 : "Should be running on child only!");
128 :
129 0 : NPObject* object = NPObjectFromVariant(aRemoteVariant);
130 0 : NS_ASSERTION(object, "Null object?!");
131 :
132 0 : PluginModuleChild::sBrowserFuncs.retainobject(object);
133 0 : OBJECT_TO_NPVARIANT(object, aVariant);
134 0 : break;
135 : }
136 :
137 : default:
138 0 : NS_NOTREACHED("Shouldn't get here!");
139 0 : return false;
140 : }
141 :
142 0 : return true;
143 : }
144 :
145 : template <class InstanceType>
146 : bool
147 : mozilla::plugins::ConvertToRemoteVariant(const NPVariant& aVariant,
148 : Variant& aRemoteVariant,
149 : InstanceType* aInstance,
150 : bool aProtectActors)
151 : {
152 0 : if (NPVARIANT_IS_VOID(aVariant)) {
153 0 : aRemoteVariant = mozilla::void_t();
154 : }
155 0 : else if (NPVARIANT_IS_NULL(aVariant)) {
156 0 : aRemoteVariant = mozilla::null_t();
157 : }
158 0 : else if (NPVARIANT_IS_BOOLEAN(aVariant)) {
159 0 : aRemoteVariant = NPVARIANT_TO_BOOLEAN(aVariant);
160 : }
161 0 : else if (NPVARIANT_IS_INT32(aVariant)) {
162 0 : aRemoteVariant = NPVARIANT_TO_INT32(aVariant);
163 : }
164 0 : else if (NPVARIANT_IS_DOUBLE(aVariant)) {
165 0 : aRemoteVariant = NPVARIANT_TO_DOUBLE(aVariant);
166 : }
167 0 : else if (NPVARIANT_IS_STRING(aVariant)) {
168 0 : NPString str = NPVARIANT_TO_STRING(aVariant);
169 0 : nsCString string(str.UTF8Characters, str.UTF8Length);
170 0 : aRemoteVariant = string;
171 : }
172 0 : else if (NPVARIANT_IS_OBJECT(aVariant)) {
173 0 : NPObject* object = NPVARIANT_TO_OBJECT(aVariant);
174 :
175 : typename VariantTraits<InstanceType>::ScriptableObjectType* actor =
176 0 : aInstance->GetActorForNPObject(object);
177 :
178 0 : if (!actor) {
179 0 : NS_ERROR("Null actor!");
180 0 : return false;
181 : }
182 :
183 0 : if (aProtectActors) {
184 0 : actor->Protect();
185 : }
186 :
187 0 : aRemoteVariant = actor;
188 : }
189 : else {
190 0 : NS_NOTREACHED("Shouldn't get here!");
191 0 : return false;
192 : }
193 :
194 0 : return true;
195 : }
|