DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (27a812186ff4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=2 et :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef dom_plugins_PluginScriptableObjectUtils_h
#define dom_plugins_PluginScriptableObjectUtils_h

#include "PluginModuleParent.h"
#include "PluginModuleChild.h"
#include "PluginInstanceParent.h"
#include "PluginInstanceChild.h"
#include "PluginScriptableObjectParent.h"
#include "PluginScriptableObjectChild.h"

#include "npapi.h"
#include "npfunctions.h"
#include "npruntime.h"

#include "nsDebug.h"

namespace mozilla {
namespace plugins {

inline PluginInstanceParent*
GetInstance(NPObject* aObject)
{
  NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(),
               "Bad class!");

  ParentNPObject* object = reinterpret_cast<ParentNPObject*>(aObject);
  if (object->invalidated) {
    NS_WARNING("Calling method on an invalidated object!");
    return nullptr;
  }
  if (!object->parent) {
    return nullptr;
  }
  return object->parent->GetInstance();
}

inline NPObject*
NPObjectFromVariant(const Variant& aRemoteVariant)
{
  switch (aRemoteVariant.type()) {
    case Variant::TPPluginScriptableObjectParent: {
      PluginScriptableObjectParent* actor =
        const_cast<PluginScriptableObjectParent*>(
          reinterpret_cast<const PluginScriptableObjectParent*>(
            aRemoteVariant.get_PPluginScriptableObjectParent()));
      return actor->GetObject(true);
    }

    case Variant::TPPluginScriptableObjectChild: {
      PluginScriptableObjectChild* actor =
        const_cast<PluginScriptableObjectChild*>(
          reinterpret_cast<const PluginScriptableObjectChild*>(
            aRemoteVariant.get_PPluginScriptableObjectChild()));
      return actor->GetObject(true);
    }

    default:
      NS_NOTREACHED("Shouldn't get here!");
      return nullptr;
  }
}

inline NPObject*
NPObjectFromVariant(const NPVariant& aVariant)
{
  NS_ASSERTION(NPVARIANT_IS_OBJECT(aVariant), "Wrong variant type!");
  return NPVARIANT_TO_OBJECT(aVariant);
}

inline const NPNetscapeFuncs*
GetNetscapeFuncs(PluginInstanceParent* aInstance)
{
  PluginModuleParent* module = aInstance->Module();
  if (!module) {
    NS_WARNING("Null module?!");
    return nullptr;
  }
  return module->GetNetscapeFuncs();
}

inline const NPNetscapeFuncs*
GetNetscapeFuncs(NPObject* aObject)
{
  NS_ASSERTION(aObject->_class == PluginScriptableObjectParent::GetClass(),
               "Bad class!");

  PluginInstanceParent* instance = GetInstance(aObject);
  if (!instance) {
    return nullptr;
  }

  return GetNetscapeFuncs(instance);
}

inline void
ReleaseRemoteVariant(Variant& aVariant)
{
  switch (aVariant.type()) {
    case Variant::TPPluginScriptableObjectParent: {
      PluginScriptableObjectParent* actor =
        const_cast<PluginScriptableObjectParent*>(
          reinterpret_cast<const PluginScriptableObjectParent*>(
            aVariant.get_PPluginScriptableObjectParent()));
      actor->Unprotect();
      break;
    }

    case Variant::TPPluginScriptableObjectChild: {
      NS_ASSERTION(PluginModuleChild::current(),
                   "Should only be running in the child!");
      PluginScriptableObjectChild* actor =
        const_cast<PluginScriptableObjectChild*>(
          reinterpret_cast<const PluginScriptableObjectChild*>(
            aVariant.get_PPluginScriptableObjectChild()));
      actor->Unprotect();
      break;
    }

  default:
    break; // Intentional fall-through for other variant types.
  }

  aVariant = mozilla::void_t();
}

bool
ConvertToVariant(const Variant& aRemoteVariant,
                 NPVariant& aVariant,
                 PluginInstanceParent* aInstance = nullptr);

template <class InstanceType>
bool
ConvertToRemoteVariant(const NPVariant& aVariant,
                       Variant& aRemoteVariant,
                       InstanceType* aInstance,
                       bool aProtectActors = false);

class ProtectedVariant
{
public:
  ProtectedVariant(const NPVariant& aVariant,
                   PluginInstanceParent* aInstance)
  {
    mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true);
  }

  ProtectedVariant(const NPVariant& aVariant,
                   PluginInstanceChild* aInstance)
  {
    mOk = ConvertToRemoteVariant(aVariant, mVariant, aInstance, true);
  }

  ~ProtectedVariant() {
    ReleaseRemoteVariant(mVariant);
  }

  bool IsOk() {
    return mOk;
  }

  operator const Variant&() {
    return mVariant;
  }

private:
  Variant mVariant;
  bool mOk;
};

class ProtectedVariantArray
{
public:
  ProtectedVariantArray(const NPVariant* aArgs,
                        uint32_t aCount,
                        PluginInstanceParent* aInstance)
    : mUsingShadowArray(false)
  {
    for (uint32_t index = 0; index < aCount; index++) {
      Variant* remoteVariant = mArray.AppendElement();
      if (!(remoteVariant && 
            ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance,
                                   true))) {
        mOk = false;
        return;
      }
    }
    mOk = true;
  }

  ProtectedVariantArray(const NPVariant* aArgs,
                        uint32_t aCount,
                        PluginInstanceChild* aInstance)
    : mUsingShadowArray(false)
  {
    for (uint32_t index = 0; index < aCount; index++) {
      Variant* remoteVariant = mArray.AppendElement();
      if (!(remoteVariant && 
            ConvertToRemoteVariant(aArgs[index], *remoteVariant, aInstance,
                                   true))) {
        mOk = false;
        return;
      }
    }
    mOk = true;
  }

  ~ProtectedVariantArray()
  {
    InfallibleTArray<Variant>& vars = EnsureAndGetShadowArray();
    uint32_t count = vars.Length();
    for (uint32_t index = 0; index < count; index++) {
      ReleaseRemoteVariant(vars[index]);
    }
  }

  operator const InfallibleTArray<Variant>&()
  {
    return EnsureAndGetShadowArray();
  }

  bool IsOk()
  {
    return mOk;
  }

private:
  InfallibleTArray<Variant>&
  EnsureAndGetShadowArray()
  {
    if (!mUsingShadowArray) {
      mShadowArray.SwapElements(mArray);
      mUsingShadowArray = true;
    }
    return mShadowArray;
  }

  // We convert the variants fallibly, but pass them to Call*()
  // methods as an infallible array
  nsTArray<Variant> mArray;
  InfallibleTArray<Variant> mShadowArray;
  bool mOk;
  bool mUsingShadowArray;
};

template<class ActorType>
struct ProtectedActorTraits
{
  static bool Nullable();
};

template<class ActorType, class Traits=ProtectedActorTraits<ActorType> >
class ProtectedActor
{
public:
  ProtectedActor(ActorType* aActor) : mActor(aActor)
  {
    if (!Traits::Nullable()) {
      NS_ASSERTION(mActor, "This should never be null!");
    }
  }

  ~ProtectedActor()
  {
    if (Traits::Nullable() && !mActor)
      return;
    mActor->Unprotect();
  }

  ActorType* operator->()
  {
    return mActor;
  }

  operator bool()
  {
    return !!mActor;
  }

private:
  ActorType* mActor;
};

template<>
struct ProtectedActorTraits<PluginScriptableObjectParent>
{
  static bool Nullable() { return true; }
};

template<>
struct ProtectedActorTraits<PluginScriptableObjectChild>
{
  static bool Nullable() { return false; }
};

} /* namespace plugins */
} /* namespace mozilla */

#include "PluginScriptableObjectUtils-inl.h"

#endif /* dom_plugins_PluginScriptableObjectUtils_h */