Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim: sw=2 ts=2 et : |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "PluginScriptableObjectUtils.h" |
michael@0 | 8 | |
michael@0 | 9 | namespace { |
michael@0 | 10 | |
michael@0 | 11 | template<class InstanceType> |
michael@0 | 12 | class VariantTraits; |
michael@0 | 13 | |
michael@0 | 14 | template<> |
michael@0 | 15 | class VariantTraits<mozilla::plugins::PluginInstanceParent> |
michael@0 | 16 | { |
michael@0 | 17 | public: |
michael@0 | 18 | typedef mozilla::plugins::PluginScriptableObjectParent ScriptableObjectType; |
michael@0 | 19 | }; |
michael@0 | 20 | |
michael@0 | 21 | template<> |
michael@0 | 22 | class VariantTraits<mozilla::plugins::PluginInstanceChild> |
michael@0 | 23 | { |
michael@0 | 24 | public: |
michael@0 | 25 | typedef mozilla::plugins::PluginScriptableObjectChild ScriptableObjectType; |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | } /* anonymous namespace */ |
michael@0 | 29 | |
michael@0 | 30 | inline bool |
michael@0 | 31 | mozilla::plugins::ConvertToVariant(const Variant& aRemoteVariant, |
michael@0 | 32 | NPVariant& aVariant, |
michael@0 | 33 | PluginInstanceParent* aInstance) |
michael@0 | 34 | { |
michael@0 | 35 | switch (aRemoteVariant.type()) { |
michael@0 | 36 | case Variant::Tvoid_t: { |
michael@0 | 37 | VOID_TO_NPVARIANT(aVariant); |
michael@0 | 38 | break; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | case Variant::Tnull_t: { |
michael@0 | 42 | NULL_TO_NPVARIANT(aVariant); |
michael@0 | 43 | break; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | case Variant::Tbool: { |
michael@0 | 47 | BOOLEAN_TO_NPVARIANT(aRemoteVariant.get_bool(), aVariant); |
michael@0 | 48 | break; |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | case Variant::Tint: { |
michael@0 | 52 | INT32_TO_NPVARIANT(aRemoteVariant.get_int(), aVariant); |
michael@0 | 53 | break; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | case Variant::Tdouble: { |
michael@0 | 57 | DOUBLE_TO_NPVARIANT(aRemoteVariant.get_double(), aVariant); |
michael@0 | 58 | break; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | case Variant::TnsCString: { |
michael@0 | 62 | const nsCString& string = aRemoteVariant.get_nsCString(); |
michael@0 | 63 | NPUTF8* buffer = reinterpret_cast<NPUTF8*>(strdup(string.get())); |
michael@0 | 64 | if (!buffer) { |
michael@0 | 65 | NS_ERROR("Out of memory!"); |
michael@0 | 66 | return false; |
michael@0 | 67 | } |
michael@0 | 68 | STRINGN_TO_NPVARIANT(buffer, string.Length(), aVariant); |
michael@0 | 69 | break; |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | case Variant::TPPluginScriptableObjectParent: { |
michael@0 | 73 | NS_ASSERTION(aInstance, "Must have an instance!"); |
michael@0 | 74 | NPObject* object = NPObjectFromVariant(aRemoteVariant); |
michael@0 | 75 | if (!object) { |
michael@0 | 76 | NS_ERROR("Er, this shouldn't fail!"); |
michael@0 | 77 | return false; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | const NPNetscapeFuncs* npn = GetNetscapeFuncs(aInstance); |
michael@0 | 81 | if (!npn) { |
michael@0 | 82 | NS_ERROR("Null netscape funcs!"); |
michael@0 | 83 | return false; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | npn->retainobject(object); |
michael@0 | 87 | OBJECT_TO_NPVARIANT(object, aVariant); |
michael@0 | 88 | break; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | case Variant::TPPluginScriptableObjectChild: { |
michael@0 | 92 | NS_ASSERTION(!aInstance, "No instance should be given!"); |
michael@0 | 93 | NS_ASSERTION(PluginModuleChild::current(), |
michael@0 | 94 | "Should be running on child only!"); |
michael@0 | 95 | |
michael@0 | 96 | NPObject* object = NPObjectFromVariant(aRemoteVariant); |
michael@0 | 97 | NS_ASSERTION(object, "Null object?!"); |
michael@0 | 98 | |
michael@0 | 99 | PluginModuleChild::sBrowserFuncs.retainobject(object); |
michael@0 | 100 | OBJECT_TO_NPVARIANT(object, aVariant); |
michael@0 | 101 | break; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | default: |
michael@0 | 105 | NS_NOTREACHED("Shouldn't get here!"); |
michael@0 | 106 | return false; |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | return true; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | template <class InstanceType> |
michael@0 | 113 | bool |
michael@0 | 114 | mozilla::plugins::ConvertToRemoteVariant(const NPVariant& aVariant, |
michael@0 | 115 | Variant& aRemoteVariant, |
michael@0 | 116 | InstanceType* aInstance, |
michael@0 | 117 | bool aProtectActors) |
michael@0 | 118 | { |
michael@0 | 119 | if (NPVARIANT_IS_VOID(aVariant)) { |
michael@0 | 120 | aRemoteVariant = mozilla::void_t(); |
michael@0 | 121 | } |
michael@0 | 122 | else if (NPVARIANT_IS_NULL(aVariant)) { |
michael@0 | 123 | aRemoteVariant = mozilla::null_t(); |
michael@0 | 124 | } |
michael@0 | 125 | else if (NPVARIANT_IS_BOOLEAN(aVariant)) { |
michael@0 | 126 | aRemoteVariant = NPVARIANT_TO_BOOLEAN(aVariant); |
michael@0 | 127 | } |
michael@0 | 128 | else if (NPVARIANT_IS_INT32(aVariant)) { |
michael@0 | 129 | aRemoteVariant = NPVARIANT_TO_INT32(aVariant); |
michael@0 | 130 | } |
michael@0 | 131 | else if (NPVARIANT_IS_DOUBLE(aVariant)) { |
michael@0 | 132 | aRemoteVariant = NPVARIANT_TO_DOUBLE(aVariant); |
michael@0 | 133 | } |
michael@0 | 134 | else if (NPVARIANT_IS_STRING(aVariant)) { |
michael@0 | 135 | NPString str = NPVARIANT_TO_STRING(aVariant); |
michael@0 | 136 | nsCString string(str.UTF8Characters, str.UTF8Length); |
michael@0 | 137 | aRemoteVariant = string; |
michael@0 | 138 | } |
michael@0 | 139 | else if (NPVARIANT_IS_OBJECT(aVariant)) { |
michael@0 | 140 | NPObject* object = NPVARIANT_TO_OBJECT(aVariant); |
michael@0 | 141 | |
michael@0 | 142 | typename VariantTraits<InstanceType>::ScriptableObjectType* actor = |
michael@0 | 143 | aInstance->GetActorForNPObject(object); |
michael@0 | 144 | |
michael@0 | 145 | if (!actor) { |
michael@0 | 146 | NS_ERROR("Null actor!"); |
michael@0 | 147 | return false; |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | if (aProtectActors) { |
michael@0 | 151 | actor->Protect(); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | aRemoteVariant = actor; |
michael@0 | 155 | } |
michael@0 | 156 | else { |
michael@0 | 157 | NS_NOTREACHED("Shouldn't get here!"); |
michael@0 | 158 | return false; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | return true; |
michael@0 | 162 | } |