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 | #ifndef dom_plugins_PluginScriptableObjectUtils_h |
michael@0 | 8 | #define dom_plugins_PluginScriptableObjectUtils_h |
michael@0 | 9 | |
michael@0 | 10 | #include "PluginModuleParent.h" |
michael@0 | 11 | #include "PluginModuleChild.h" |
michael@0 | 12 | #include "PluginInstanceParent.h" |
michael@0 | 13 | #include "PluginInstanceChild.h" |
michael@0 | 14 | #include "PluginScriptableObjectParent.h" |
michael@0 | 15 | #include "PluginScriptableObjectChild.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "npapi.h" |
michael@0 | 18 | #include "npfunctions.h" |
michael@0 | 19 | #include "npruntime.h" |
michael@0 | 20 | |
michael@0 | 21 | #include "nsDebug.h" |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | namespace plugins { |
michael@0 | 25 | |
michael@0 | 26 | inline PluginInstanceParent* |
michael@0 | 27 | GetInstance(NPObject* aObject) |
michael@0 | 28 | { |
michael@0 | 29 | NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(), |
michael@0 | 30 | "Bad class!"); |
michael@0 | 31 | |
michael@0 | 32 | ParentNPObject* object = reinterpret_cast<ParentNPObject*>(aObject); |
michael@0 | 33 | if (object->invalidated) { |
michael@0 | 34 | NS_WARNING("Calling method on an invalidated object!"); |
michael@0 | 35 | return nullptr; |
michael@0 | 36 | } |
michael@0 | 37 | if (!object->parent) { |
michael@0 | 38 | return nullptr; |
michael@0 | 39 | } |
michael@0 | 40 | return object->parent->GetInstance(); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | inline NPObject* |
michael@0 | 44 | NPObjectFromVariant(const Variant& aRemoteVariant) |
michael@0 | 45 | { |
michael@0 | 46 | switch (aRemoteVariant.type()) { |
michael@0 | 47 | case Variant::TPPluginScriptableObjectParent: { |
michael@0 | 48 | PluginScriptableObjectParent* actor = |
michael@0 | 49 | const_cast<PluginScriptableObjectParent*>( |
michael@0 | 50 | reinterpret_cast<const PluginScriptableObjectParent*>( |
michael@0 | 51 | aRemoteVariant.get_PPluginScriptableObjectParent())); |
michael@0 | 52 | return actor->GetObject(true); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | case Variant::TPPluginScriptableObjectChild: { |
michael@0 | 56 | PluginScriptableObjectChild* actor = |
michael@0 | 57 | const_cast<PluginScriptableObjectChild*>( |
michael@0 | 58 | reinterpret_cast<const PluginScriptableObjectChild*>( |
michael@0 | 59 | aRemoteVariant.get_PPluginScriptableObjectChild())); |
michael@0 | 60 | return actor->GetObject(true); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | default: |
michael@0 | 64 | NS_NOTREACHED("Shouldn't get here!"); |
michael@0 | 65 | return nullptr; |
michael@0 | 66 | } |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | inline NPObject* |
michael@0 | 70 | NPObjectFromVariant(const NPVariant& aVariant) |
michael@0 | 71 | { |
michael@0 | 72 | NS_ASSERTION(NPVARIANT_IS_OBJECT(aVariant), "Wrong variant type!"); |
michael@0 | 73 | return NPVARIANT_TO_OBJECT(aVariant); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | inline const NPNetscapeFuncs* |
michael@0 | 77 | GetNetscapeFuncs(PluginInstanceParent* aInstance) |
michael@0 | 78 | { |
michael@0 | 79 | PluginModuleParent* module = aInstance->Module(); |
michael@0 | 80 | if (!module) { |
michael@0 | 81 | NS_WARNING("Null module?!"); |
michael@0 | 82 | return nullptr; |
michael@0 | 83 | } |
michael@0 | 84 | return module->GetNetscapeFuncs(); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | inline const NPNetscapeFuncs* |
michael@0 | 88 | GetNetscapeFuncs(NPObject* aObject) |
michael@0 | 89 | { |
michael@0 | 90 | NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(), |
michael@0 | 91 | "Bad class!"); |
michael@0 | 92 | |
michael@0 | 93 | PluginInstanceParent* instance = GetInstance(aObject); |
michael@0 | 94 | if (!instance) { |
michael@0 | 95 | return nullptr; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | return GetNetscapeFuncs(instance); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | inline void |
michael@0 | 102 | ReleaseRemoteVariant(Variant& aVariant) |
michael@0 | 103 | { |
michael@0 | 104 | switch (aVariant.type()) { |
michael@0 | 105 | case Variant::TPPluginScriptableObjectParent: { |
michael@0 | 106 | PluginScriptableObjectParent* actor = |
michael@0 | 107 | const_cast<PluginScriptableObjectParent*>( |
michael@0 | 108 | reinterpret_cast<const PluginScriptableObjectParent*>( |
michael@0 | 109 | aVariant.get_PPluginScriptableObjectParent())); |
michael@0 | 110 | actor->Unprotect(); |
michael@0 | 111 | break; |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | case Variant::TPPluginScriptableObjectChild: { |
michael@0 | 115 | NS_ASSERTION(PluginModuleChild::current(), |
michael@0 | 116 | "Should only be running in the child!"); |
michael@0 | 117 | PluginScriptableObjectChild* actor = |
michael@0 | 118 | const_cast<PluginScriptableObjectChild*>( |
michael@0 | 119 | reinterpret_cast<const PluginScriptableObjectChild*>( |
michael@0 | 120 | aVariant.get_PPluginScriptableObjectChild())); |
michael@0 | 121 | actor->Unprotect(); |
michael@0 | 122 | break; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | default: |
michael@0 | 126 | break; // Intentional fall-through for other variant types. |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | aVariant = mozilla::void_t(); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | bool |
michael@0 | 133 | ConvertToVariant(const Variant& aRemoteVariant, |
michael@0 | 134 | NPVariant& aVariant, |
michael@0 | 135 | PluginInstanceParent* aInstance = nullptr); |
michael@0 | 136 | |
michael@0 | 137 | template <class InstanceType> |
michael@0 | 138 | bool |
michael@0 | 139 | ConvertToRemoteVariant(const NPVariant& aVariant, |
michael@0 | 140 | Variant& aRemoteVariant, |
michael@0 | 141 | InstanceType* aInstance, |
michael@0 | 142 | bool aProtectActors = false); |
michael@0 | 143 | |
michael@0 | 144 | class ProtectedVariant |
michael@0 | 145 | { |
michael@0 | 146 | public: |
michael@0 | 147 | ProtectedVariant(const NPVariant& aVariant, |
michael@0 | 148 | PluginInstanceParent* aInstance) |
michael@0 | 149 | { |
michael@0 | 150 | mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | ProtectedVariant(const NPVariant& aVariant, |
michael@0 | 154 | PluginInstanceChild* aInstance) |
michael@0 | 155 | { |
michael@0 | 156 | mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true); |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | ~ProtectedVariant() { |
michael@0 | 160 | ReleaseRemoteVariant(mVariant); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | bool IsOk() { |
michael@0 | 164 | return mOk; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | operator const Variant&() { |
michael@0 | 168 | return mVariant; |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | private: |
michael@0 | 172 | Variant mVariant; |
michael@0 | 173 | bool mOk; |
michael@0 | 174 | }; |
michael@0 | 175 | |
michael@0 | 176 | class ProtectedVariantArray |
michael@0 | 177 | { |
michael@0 | 178 | public: |
michael@0 | 179 | ProtectedVariantArray(const NPVariant* aArgs, |
michael@0 | 180 | uint32_t aCount, |
michael@0 | 181 | PluginInstanceParent* aInstance) |
michael@0 | 182 | : mUsingShadowArray(false) |
michael@0 | 183 | { |
michael@0 | 184 | for (uint32_t index = 0; index < aCount; index++) { |
michael@0 | 185 | Variant* remoteVariant = mArray.AppendElement(); |
michael@0 | 186 | if (!(remoteVariant && |
michael@0 | 187 | ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance, |
michael@0 | 188 | true))) { |
michael@0 | 189 | mOk = false; |
michael@0 | 190 | return; |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | mOk = true; |
michael@0 | 194 | } |
michael@0 | 195 | |
michael@0 | 196 | ProtectedVariantArray(const NPVariant* aArgs, |
michael@0 | 197 | uint32_t aCount, |
michael@0 | 198 | PluginInstanceChild* aInstance) |
michael@0 | 199 | : mUsingShadowArray(false) |
michael@0 | 200 | { |
michael@0 | 201 | for (uint32_t index = 0; index < aCount; index++) { |
michael@0 | 202 | Variant* remoteVariant = mArray.AppendElement(); |
michael@0 | 203 | if (!(remoteVariant && |
michael@0 | 204 | ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance, |
michael@0 | 205 | true))) { |
michael@0 | 206 | mOk = false; |
michael@0 | 207 | return; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | mOk = true; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | ~ProtectedVariantArray() |
michael@0 | 214 | { |
michael@0 | 215 | InfallibleTArray<Variant>& vars = EnsureAndGetShadowArray(); |
michael@0 | 216 | uint32_t count = vars.Length(); |
michael@0 | 217 | for (uint32_t index = 0; index < count; index++) { |
michael@0 | 218 | ReleaseRemoteVariant(vars[index]); |
michael@0 | 219 | } |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | operator const InfallibleTArray<Variant>&() |
michael@0 | 223 | { |
michael@0 | 224 | return EnsureAndGetShadowArray(); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | bool IsOk() |
michael@0 | 228 | { |
michael@0 | 229 | return mOk; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | private: |
michael@0 | 233 | InfallibleTArray<Variant>& |
michael@0 | 234 | EnsureAndGetShadowArray() |
michael@0 | 235 | { |
michael@0 | 236 | if (!mUsingShadowArray) { |
michael@0 | 237 | mShadowArray.SwapElements(mArray); |
michael@0 | 238 | mUsingShadowArray = true; |
michael@0 | 239 | } |
michael@0 | 240 | return mShadowArray; |
michael@0 | 241 | } |
michael@0 | 242 | |
michael@0 | 243 | // We convert the variants fallibly, but pass them to Call*() |
michael@0 | 244 | // methods as an infallible array |
michael@0 | 245 | nsTArray<Variant> mArray; |
michael@0 | 246 | InfallibleTArray<Variant> mShadowArray; |
michael@0 | 247 | bool mOk; |
michael@0 | 248 | bool mUsingShadowArray; |
michael@0 | 249 | }; |
michael@0 | 250 | |
michael@0 | 251 | template<class ActorType> |
michael@0 | 252 | struct ProtectedActorTraits |
michael@0 | 253 | { |
michael@0 | 254 | static bool Nullable(); |
michael@0 | 255 | }; |
michael@0 | 256 | |
michael@0 | 257 | template<class ActorType, class Traits=ProtectedActorTraits<ActorType> > |
michael@0 | 258 | class ProtectedActor |
michael@0 | 259 | { |
michael@0 | 260 | public: |
michael@0 | 261 | ProtectedActor(ActorType* aActor) : mActor(aActor) |
michael@0 | 262 | { |
michael@0 | 263 | if (!Traits::Nullable()) { |
michael@0 | 264 | NS_ASSERTION(mActor, "This should never be null!"); |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | ~ProtectedActor() |
michael@0 | 269 | { |
michael@0 | 270 | if (Traits::Nullable() && !mActor) |
michael@0 | 271 | return; |
michael@0 | 272 | mActor->Unprotect(); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | ActorType* operator->() |
michael@0 | 276 | { |
michael@0 | 277 | return mActor; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | operator bool() |
michael@0 | 281 | { |
michael@0 | 282 | return !!mActor; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | private: |
michael@0 | 286 | ActorType* mActor; |
michael@0 | 287 | }; |
michael@0 | 288 | |
michael@0 | 289 | template<> |
michael@0 | 290 | struct ProtectedActorTraits<PluginScriptableObjectParent> |
michael@0 | 291 | { |
michael@0 | 292 | static bool Nullable() { return true; } |
michael@0 | 293 | }; |
michael@0 | 294 | |
michael@0 | 295 | template<> |
michael@0 | 296 | struct ProtectedActorTraits<PluginScriptableObjectChild> |
michael@0 | 297 | { |
michael@0 | 298 | static bool Nullable() { return false; } |
michael@0 | 299 | }; |
michael@0 | 300 | |
michael@0 | 301 | } /* namespace plugins */ |
michael@0 | 302 | } /* namespace mozilla */ |
michael@0 | 303 | |
michael@0 | 304 | #include "PluginScriptableObjectUtils-inl.h" |
michael@0 | 305 | |
michael@0 | 306 | #endif /* dom_plugins_PluginScriptableObjectUtils_h */ |