dom/plugins/ipc/PluginIdentifierChild.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/plugins/ipc/PluginIdentifierChild.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     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_PluginIdentifierChild_h
    1.11 +#define dom_plugins_PluginIdentifierChild_h
    1.12 +
    1.13 +#include "mozilla/plugins/PPluginIdentifierChild.h"
    1.14 +#include "npapi.h"
    1.15 +#include "npruntime.h"
    1.16 +
    1.17 +#include "nsString.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace plugins {
    1.21 +
    1.22 +class PluginModuleChild;
    1.23 +
    1.24 +/**
    1.25 + * Plugin identifiers may be "temporary", see the comment on the
    1.26 + * PPluginIdentifier constructor for details. This means that any IPDL method
    1.27 + * which receives a PPluginIdentifierChild* parameter must use StackIdentifier
    1.28 + * to track it.
    1.29 + */
    1.30 +class PluginIdentifierChild : public PPluginIdentifierChild
    1.31 +{
    1.32 +  friend class PluginModuleChild;
    1.33 +public:
    1.34 +  bool IsString()
    1.35 +  {
    1.36 +    return mIsString;
    1.37 +  }
    1.38 +
    1.39 +  NPIdentifier ToNPIdentifier()
    1.40 +  {
    1.41 +    if (mCanonicalIdentifier) {
    1.42 +      return mCanonicalIdentifier;
    1.43 +    }
    1.44 +
    1.45 +    NS_ASSERTION(mHashed, "Handing out an unhashed identifier?");
    1.46 +    return this;
    1.47 +  }
    1.48 +
    1.49 +  void MakePermanent();
    1.50 +
    1.51 +  class MOZ_STACK_CLASS StackIdentifier
    1.52 +  {
    1.53 +  public:
    1.54 +    StackIdentifier(PPluginIdentifierChild* actor)
    1.55 +      : mIdentifier(static_cast<PluginIdentifierChild*>(actor))
    1.56 +    {
    1.57 +      if (mIdentifier)
    1.58 +        mIdentifier->StartTemporary();
    1.59 +    }
    1.60 +
    1.61 +    ~StackIdentifier() {
    1.62 +      if (mIdentifier)
    1.63 +        mIdentifier->FinishTemporary();
    1.64 +    }
    1.65 +
    1.66 +    PluginIdentifierChild* operator->() { return mIdentifier; }
    1.67 +
    1.68 +  private:
    1.69 +    PluginIdentifierChild* mIdentifier;
    1.70 +  };
    1.71 +
    1.72 +protected:
    1.73 +  PluginIdentifierChild(bool aIsString)
    1.74 +    : mCanonicalIdentifier(nullptr)
    1.75 +    , mHashed(false)
    1.76 +    , mTemporaryRefs(0)
    1.77 +    , mIsString(aIsString)
    1.78 +  {
    1.79 +    MOZ_COUNT_CTOR(PluginIdentifierChild);
    1.80 +  }
    1.81 +
    1.82 +  virtual ~PluginIdentifierChild()
    1.83 +  {
    1.84 +    MOZ_COUNT_DTOR(PluginIdentifierChild);
    1.85 +  }
    1.86 +
    1.87 +  // The following functions are implemented by the subclasses for their
    1.88 +  // identifier maps.
    1.89 +  virtual PluginIdentifierChild* GetCanonical() = 0;
    1.90 +  virtual void Hash() = 0;
    1.91 +  virtual void Unhash() = 0;
    1.92 +
    1.93 +private:
    1.94 +  void StartTemporary();
    1.95 +  void FinishTemporary();
    1.96 +
    1.97 +  // There's a possibility that we already have an actor that wraps the same
    1.98 +  // string or int because we do all this identifier construction
    1.99 +  // asynchronously. In this case we need to hand out the canonical version
   1.100 +  // created by the child side.
   1.101 +  //
   1.102 +  // In order to deal with temporary identifiers which appear on the stack,
   1.103 +  // identifiers use the following state invariants:
   1.104 +  //
   1.105 +  // * mCanonicalIdentifier is non-nullptr: this is a duplicate identifier, no
   1.106 +  //   further information is necessary.
   1.107 +  // * mHashed is false: this identifier is a newborn, non-permanent identifier
   1.108 +  // * mHashed is true, mTemporaryRefs is 0: this identifier is permanent
   1.109 +  // * mHashed is true, mTemporaryRefs is non-0: this identifier is temporary;
   1.110 +  //   if NPN_GetFooIdentifier is called for it, we need to retain it. If
   1.111 +  //   all stack references are lost, unhash it because it will soon be 
   1.112 +  //   deleted.
   1.113 +
   1.114 +  PluginIdentifierChild* mCanonicalIdentifier;
   1.115 +  bool mHashed;
   1.116 +  unsigned int mTemporaryRefs;
   1.117 +  bool mIsString;
   1.118 +};
   1.119 +
   1.120 +class PluginIdentifierChildString : public PluginIdentifierChild
   1.121 +{
   1.122 +  friend class PluginModuleChild;
   1.123 +public:
   1.124 +  NPUTF8* ToString()
   1.125 +  {
   1.126 +    return ToNewCString(mString);
   1.127 +  }
   1.128 +
   1.129 +protected:
   1.130 +  PluginIdentifierChildString(const nsCString& aString)
   1.131 +    : PluginIdentifierChild(true),
   1.132 +      mString(aString)
   1.133 +  { }
   1.134 +
   1.135 +  virtual PluginIdentifierChild* GetCanonical();
   1.136 +  virtual void Hash();
   1.137 +  virtual void Unhash();
   1.138 +
   1.139 +  nsCString mString;
   1.140 +};
   1.141 +
   1.142 +class PluginIdentifierChildInt : public PluginIdentifierChild
   1.143 +{
   1.144 +  friend class PluginModuleChild;
   1.145 +public:
   1.146 +  int32_t ToInt()
   1.147 +  {
   1.148 +    return mInt;
   1.149 +  }
   1.150 +
   1.151 +protected:
   1.152 +  PluginIdentifierChildInt(int32_t aInt)
   1.153 +    : PluginIdentifierChild(false),
   1.154 +      mInt(aInt)
   1.155 +  { }
   1.156 +
   1.157 +  virtual PluginIdentifierChild* GetCanonical();
   1.158 +  virtual void Hash();
   1.159 +  virtual void Unhash();
   1.160 +
   1.161 +  int32_t mInt;
   1.162 +};
   1.163 +
   1.164 +} // namespace plugins
   1.165 +} // namespace mozilla
   1.166 +
   1.167 +#endif // dom_plugins_PluginIdentifierChild_h

mercurial