dom/plugins/ipc/PluginScriptableObjectUtils.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/ipc/PluginScriptableObjectUtils.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,306 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef dom_plugins_PluginScriptableObjectUtils_h
    1.11 +#define dom_plugins_PluginScriptableObjectUtils_h
    1.12 +
    1.13 +#include "PluginModuleParent.h"
    1.14 +#include "PluginModuleChild.h"
    1.15 +#include "PluginInstanceParent.h"
    1.16 +#include "PluginInstanceChild.h"
    1.17 +#include "PluginScriptableObjectParent.h"
    1.18 +#include "PluginScriptableObjectChild.h"
    1.19 +
    1.20 +#include "npapi.h"
    1.21 +#include "npfunctions.h"
    1.22 +#include "npruntime.h"
    1.23 +
    1.24 +#include "nsDebug.h"
    1.25 +
    1.26 +namespace mozilla {
    1.27 +namespace plugins {
    1.28 +
    1.29 +inline PluginInstanceParent*
    1.30 +GetInstance(NPObject* aObject)
    1.31 +{
    1.32 +  NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(),
    1.33 +               "Bad class!");
    1.34 +
    1.35 +  ParentNPObject* object = reinterpret_cast<ParentNPObject*>(aObject);
    1.36 +  if (object->invalidated) {
    1.37 +    NS_WARNING("Calling method on an invalidated object!");
    1.38 +    return nullptr;
    1.39 +  }
    1.40 +  if (!object->parent) {
    1.41 +    return nullptr;
    1.42 +  }
    1.43 +  return object->parent->GetInstance();
    1.44 +}
    1.45 +
    1.46 +inline NPObject*
    1.47 +NPObjectFromVariant(const Variant& aRemoteVariant)
    1.48 +{
    1.49 +  switch (aRemoteVariant.type()) {
    1.50 +    case Variant::TPPluginScriptableObjectParent: {
    1.51 +      PluginScriptableObjectParent* actor =
    1.52 +        const_cast<PluginScriptableObjectParent*>(
    1.53 +          reinterpret_cast<const PluginScriptableObjectParent*>(
    1.54 +            aRemoteVariant.get_PPluginScriptableObjectParent()));
    1.55 +      return actor->GetObject(true);
    1.56 +    }
    1.57 +
    1.58 +    case Variant::TPPluginScriptableObjectChild: {
    1.59 +      PluginScriptableObjectChild* actor =
    1.60 +        const_cast<PluginScriptableObjectChild*>(
    1.61 +          reinterpret_cast<const PluginScriptableObjectChild*>(
    1.62 +            aRemoteVariant.get_PPluginScriptableObjectChild()));
    1.63 +      return actor->GetObject(true);
    1.64 +    }
    1.65 +
    1.66 +    default:
    1.67 +      NS_NOTREACHED("Shouldn't get here!");
    1.68 +      return nullptr;
    1.69 +  }
    1.70 +}
    1.71 +
    1.72 +inline NPObject*
    1.73 +NPObjectFromVariant(const NPVariant& aVariant)
    1.74 +{
    1.75 +  NS_ASSERTION(NPVARIANT_IS_OBJECT(aVariant), "Wrong variant type!");
    1.76 +  return NPVARIANT_TO_OBJECT(aVariant);
    1.77 +}
    1.78 +
    1.79 +inline const NPNetscapeFuncs*
    1.80 +GetNetscapeFuncs(PluginInstanceParent* aInstance)
    1.81 +{
    1.82 +  PluginModuleParent* module = aInstance->Module();
    1.83 +  if (!module) {
    1.84 +    NS_WARNING("Null module?!");
    1.85 +    return nullptr;
    1.86 +  }
    1.87 +  return module->GetNetscapeFuncs();
    1.88 +}
    1.89 +
    1.90 +inline const NPNetscapeFuncs*
    1.91 +GetNetscapeFuncs(NPObject* aObject)
    1.92 +{
    1.93 +  NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(),
    1.94 +               "Bad class!");
    1.95 +
    1.96 +  PluginInstanceParent* instance = GetInstance(aObject);
    1.97 +  if (!instance) {
    1.98 +    return nullptr;
    1.99 +  }
   1.100 +
   1.101 +  return GetNetscapeFuncs(instance);
   1.102 +}
   1.103 +
   1.104 +inline void
   1.105 +ReleaseRemoteVariant(Variant& aVariant)
   1.106 +{
   1.107 +  switch (aVariant.type()) {
   1.108 +    case Variant::TPPluginScriptableObjectParent: {
   1.109 +      PluginScriptableObjectParent* actor =
   1.110 +        const_cast<PluginScriptableObjectParent*>(
   1.111 +          reinterpret_cast<const PluginScriptableObjectParent*>(
   1.112 +            aVariant.get_PPluginScriptableObjectParent()));
   1.113 +      actor->Unprotect();
   1.114 +      break;
   1.115 +    }
   1.116 +
   1.117 +    case Variant::TPPluginScriptableObjectChild: {
   1.118 +      NS_ASSERTION(PluginModuleChild::current(),
   1.119 +                   "Should only be running in the child!");
   1.120 +      PluginScriptableObjectChild* actor =
   1.121 +        const_cast<PluginScriptableObjectChild*>(
   1.122 +          reinterpret_cast<const PluginScriptableObjectChild*>(
   1.123 +            aVariant.get_PPluginScriptableObjectChild()));
   1.124 +      actor->Unprotect();
   1.125 +      break;
   1.126 +    }
   1.127 +
   1.128 +  default:
   1.129 +    break; // Intentional fall-through for other variant types.
   1.130 +  }
   1.131 +
   1.132 +  aVariant = mozilla::void_t();
   1.133 +}
   1.134 +
   1.135 +bool
   1.136 +ConvertToVariant(const Variant& aRemoteVariant,
   1.137 +                 NPVariant& aVariant,
   1.138 +                 PluginInstanceParent* aInstance = nullptr);
   1.139 +
   1.140 +template <class InstanceType>
   1.141 +bool
   1.142 +ConvertToRemoteVariant(const NPVariant& aVariant,
   1.143 +                       Variant& aRemoteVariant,
   1.144 +                       InstanceType* aInstance,
   1.145 +                       bool aProtectActors = false);
   1.146 +
   1.147 +class ProtectedVariant
   1.148 +{
   1.149 +public:
   1.150 +  ProtectedVariant(const NPVariant& aVariant,
   1.151 +                   PluginInstanceParent* aInstance)
   1.152 +  {
   1.153 +    mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true);
   1.154 +  }
   1.155 +
   1.156 +  ProtectedVariant(const NPVariant& aVariant,
   1.157 +                   PluginInstanceChild* aInstance)
   1.158 +  {
   1.159 +    mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true);
   1.160 +  }
   1.161 +
   1.162 +  ~ProtectedVariant() {
   1.163 +    ReleaseRemoteVariant(mVariant);
   1.164 +  }
   1.165 +
   1.166 +  bool IsOk() {
   1.167 +    return mOk;
   1.168 +  }
   1.169 +
   1.170 +  operator const Variant&() {
   1.171 +    return mVariant;
   1.172 +  }
   1.173 +
   1.174 +private:
   1.175 +  Variant mVariant;
   1.176 +  bool mOk;
   1.177 +};
   1.178 +
   1.179 +class ProtectedVariantArray
   1.180 +{
   1.181 +public:
   1.182 +  ProtectedVariantArray(const NPVariant* aArgs,
   1.183 +                        uint32_t aCount,
   1.184 +                        PluginInstanceParent* aInstance)
   1.185 +    : mUsingShadowArray(false)
   1.186 +  {
   1.187 +    for (uint32_t index = 0; index < aCount; index++) {
   1.188 +      Variant* remoteVariant = mArray.AppendElement();
   1.189 +      if (!(remoteVariant && 
   1.190 +            ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance,
   1.191 +                                   true))) {
   1.192 +        mOk = false;
   1.193 +        return;
   1.194 +      }
   1.195 +    }
   1.196 +    mOk = true;
   1.197 +  }
   1.198 +
   1.199 +  ProtectedVariantArray(const NPVariant* aArgs,
   1.200 +                        uint32_t aCount,
   1.201 +                        PluginInstanceChild* aInstance)
   1.202 +    : mUsingShadowArray(false)
   1.203 +  {
   1.204 +    for (uint32_t index = 0; index < aCount; index++) {
   1.205 +      Variant* remoteVariant = mArray.AppendElement();
   1.206 +      if (!(remoteVariant && 
   1.207 +            ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance,
   1.208 +                                   true))) {
   1.209 +        mOk = false;
   1.210 +        return;
   1.211 +      }
   1.212 +    }
   1.213 +    mOk = true;
   1.214 +  }
   1.215 +
   1.216 +  ~ProtectedVariantArray()
   1.217 +  {
   1.218 +    InfallibleTArray<Variant>& vars = EnsureAndGetShadowArray();
   1.219 +    uint32_t count = vars.Length();
   1.220 +    for (uint32_t index = 0; index < count; index++) {
   1.221 +      ReleaseRemoteVariant(vars[index]);
   1.222 +    }
   1.223 +  }
   1.224 +
   1.225 +  operator const InfallibleTArray<Variant>&()
   1.226 +  {
   1.227 +    return EnsureAndGetShadowArray();
   1.228 +  }
   1.229 +
   1.230 +  bool IsOk()
   1.231 +  {
   1.232 +    return mOk;
   1.233 +  }
   1.234 +
   1.235 +private:
   1.236 +  InfallibleTArray<Variant>&
   1.237 +  EnsureAndGetShadowArray()
   1.238 +  {
   1.239 +    if (!mUsingShadowArray) {
   1.240 +      mShadowArray.SwapElements(mArray);
   1.241 +      mUsingShadowArray = true;
   1.242 +    }
   1.243 +    return mShadowArray;
   1.244 +  }
   1.245 +
   1.246 +  // We convert the variants fallibly, but pass them to Call*()
   1.247 +  // methods as an infallible array
   1.248 +  nsTArray<Variant> mArray;
   1.249 +  InfallibleTArray<Variant> mShadowArray;
   1.250 +  bool mOk;
   1.251 +  bool mUsingShadowArray;
   1.252 +};
   1.253 +
   1.254 +template<class ActorType>
   1.255 +struct ProtectedActorTraits
   1.256 +{
   1.257 +  static bool Nullable();
   1.258 +};
   1.259 +
   1.260 +template<class ActorType, class Traits=ProtectedActorTraits<ActorType> >
   1.261 +class ProtectedActor
   1.262 +{
   1.263 +public:
   1.264 +  ProtectedActor(ActorType* aActor) : mActor(aActor)
   1.265 +  {
   1.266 +    if (!Traits::Nullable()) {
   1.267 +      NS_ASSERTION(mActor, "This should never be null!");
   1.268 +    }
   1.269 +  }
   1.270 +
   1.271 +  ~ProtectedActor()
   1.272 +  {
   1.273 +    if (Traits::Nullable() && !mActor)
   1.274 +      return;
   1.275 +    mActor->Unprotect();
   1.276 +  }
   1.277 +
   1.278 +  ActorType* operator->()
   1.279 +  {
   1.280 +    return mActor;
   1.281 +  }
   1.282 +
   1.283 +  operator bool()
   1.284 +  {
   1.285 +    return !!mActor;
   1.286 +  }
   1.287 +
   1.288 +private:
   1.289 +  ActorType* mActor;
   1.290 +};
   1.291 +
   1.292 +template<>
   1.293 +struct ProtectedActorTraits<PluginScriptableObjectParent>
   1.294 +{
   1.295 +  static bool Nullable() { return true; }
   1.296 +};
   1.297 +
   1.298 +template<>
   1.299 +struct ProtectedActorTraits<PluginScriptableObjectChild>
   1.300 +{
   1.301 +  static bool Nullable() { return false; }
   1.302 +};
   1.303 +
   1.304 +} /* namespace plugins */
   1.305 +} /* namespace mozilla */
   1.306 +
   1.307 +#include "PluginScriptableObjectUtils-inl.h"
   1.308 +
   1.309 +#endif /* dom_plugins_PluginScriptableObjectUtils_h */

mercurial