Merge mozilla-central and tracemonkey. (a=blockers)
[mozilla-central.git] / dom / plugins / PluginScriptableObjectUtils-inl.h
blob91d55daa49dc5577a1e863e3610f8e95e2c7e014
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
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
14 * License.
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.
23 * Contributor(s):
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"
42 namespace {
44 template<class InstanceType>
45 class VariantTraits;
47 template<>
48 class VariantTraits<mozilla::plugins::PluginInstanceParent>
50 public:
51 typedef mozilla::plugins::PluginScriptableObjectParent ScriptableObjectType;
54 template<>
55 class VariantTraits<mozilla::plugins::PluginInstanceChild>
57 public:
58 typedef mozilla::plugins::PluginScriptableObjectChild ScriptableObjectType;
61 } /* anonymous namespace */
63 inline bool
64 mozilla::plugins::ConvertToVariant(const Variant& aRemoteVariant,
65 NPVariant& aVariant,
66 PluginInstanceParent* aInstance)
68 switch (aRemoteVariant.type()) {
69 case Variant::Tvoid_t: {
70 VOID_TO_NPVARIANT(aVariant);
71 break;
74 case Variant::Tnull_t: {
75 NULL_TO_NPVARIANT(aVariant);
76 break;
79 case Variant::Tbool: {
80 BOOLEAN_TO_NPVARIANT(aRemoteVariant.get_bool(), aVariant);
81 break;
84 case Variant::Tint: {
85 INT32_TO_NPVARIANT(aRemoteVariant.get_int(), aVariant);
86 break;
89 case Variant::Tdouble: {
90 DOUBLE_TO_NPVARIANT(aRemoteVariant.get_double(), aVariant);
91 break;
94 case Variant::TnsCString: {
95 const nsCString& string = aRemoteVariant.get_nsCString();
96 NPUTF8* buffer = reinterpret_cast<NPUTF8*>(strdup(string.get()));
97 if (!buffer) {
98 NS_ERROR("Out of memory!");
99 return false;
101 STRINGN_TO_NPVARIANT(buffer, string.Length(), aVariant);
102 break;
105 case Variant::TPPluginScriptableObjectParent: {
106 NS_ASSERTION(aInstance, "Must have an instance!");
107 NPObject* object = NPObjectFromVariant(aRemoteVariant);
108 if (!object) {
109 NS_ERROR("Er, this shouldn't fail!");
110 return false;
113 const NPNetscapeFuncs* npn = GetNetscapeFuncs(aInstance);
114 if (!npn) {
115 NS_ERROR("Null netscape funcs!");
116 return false;
119 npn->retainobject(object);
120 OBJECT_TO_NPVARIANT(object, aVariant);
121 break;
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);
134 break;
137 default:
138 NS_NOTREACHED("Shouldn't get here!");
139 return false;
142 return true;
145 template <class InstanceType>
146 bool
147 mozilla::plugins::ConvertToRemoteVariant(const NPVariant& aVariant,
148 Variant& aRemoteVariant,
149 InstanceType* aInstance,
150 bool aProtectActors)
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);
178 if (!actor) {
179 NS_ERROR("Null actor!");
180 return false;
183 if (aProtectActors) {
184 actor->Protect();
187 aRemoteVariant = actor;
189 else {
190 NS_NOTREACHED("Shouldn't get here!");
191 return false;
194 return true;