dom/plugins/ipc/PluginIdentifierChild.h

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * vim: sw=2 ts=2 et :
     3  * This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this
     5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #ifndef dom_plugins_PluginIdentifierChild_h
     8 #define dom_plugins_PluginIdentifierChild_h
    10 #include "mozilla/plugins/PPluginIdentifierChild.h"
    11 #include "npapi.h"
    12 #include "npruntime.h"
    14 #include "nsString.h"
    16 namespace mozilla {
    17 namespace plugins {
    19 class PluginModuleChild;
    21 /**
    22  * Plugin identifiers may be "temporary", see the comment on the
    23  * PPluginIdentifier constructor for details. This means that any IPDL method
    24  * which receives a PPluginIdentifierChild* parameter must use StackIdentifier
    25  * to track it.
    26  */
    27 class PluginIdentifierChild : public PPluginIdentifierChild
    28 {
    29   friend class PluginModuleChild;
    30 public:
    31   bool IsString()
    32   {
    33     return mIsString;
    34   }
    36   NPIdentifier ToNPIdentifier()
    37   {
    38     if (mCanonicalIdentifier) {
    39       return mCanonicalIdentifier;
    40     }
    42     NS_ASSERTION(mHashed, "Handing out an unhashed identifier?");
    43     return this;
    44   }
    46   void MakePermanent();
    48   class MOZ_STACK_CLASS StackIdentifier
    49   {
    50   public:
    51     StackIdentifier(PPluginIdentifierChild* actor)
    52       : mIdentifier(static_cast<PluginIdentifierChild*>(actor))
    53     {
    54       if (mIdentifier)
    55         mIdentifier->StartTemporary();
    56     }
    58     ~StackIdentifier() {
    59       if (mIdentifier)
    60         mIdentifier->FinishTemporary();
    61     }
    63     PluginIdentifierChild* operator->() { return mIdentifier; }
    65   private:
    66     PluginIdentifierChild* mIdentifier;
    67   };
    69 protected:
    70   PluginIdentifierChild(bool aIsString)
    71     : mCanonicalIdentifier(nullptr)
    72     , mHashed(false)
    73     , mTemporaryRefs(0)
    74     , mIsString(aIsString)
    75   {
    76     MOZ_COUNT_CTOR(PluginIdentifierChild);
    77   }
    79   virtual ~PluginIdentifierChild()
    80   {
    81     MOZ_COUNT_DTOR(PluginIdentifierChild);
    82   }
    84   // The following functions are implemented by the subclasses for their
    85   // identifier maps.
    86   virtual PluginIdentifierChild* GetCanonical() = 0;
    87   virtual void Hash() = 0;
    88   virtual void Unhash() = 0;
    90 private:
    91   void StartTemporary();
    92   void FinishTemporary();
    94   // There's a possibility that we already have an actor that wraps the same
    95   // string or int because we do all this identifier construction
    96   // asynchronously. In this case we need to hand out the canonical version
    97   // created by the child side.
    98   //
    99   // In order to deal with temporary identifiers which appear on the stack,
   100   // identifiers use the following state invariants:
   101   //
   102   // * mCanonicalIdentifier is non-nullptr: this is a duplicate identifier, no
   103   //   further information is necessary.
   104   // * mHashed is false: this identifier is a newborn, non-permanent identifier
   105   // * mHashed is true, mTemporaryRefs is 0: this identifier is permanent
   106   // * mHashed is true, mTemporaryRefs is non-0: this identifier is temporary;
   107   //   if NPN_GetFooIdentifier is called for it, we need to retain it. If
   108   //   all stack references are lost, unhash it because it will soon be 
   109   //   deleted.
   111   PluginIdentifierChild* mCanonicalIdentifier;
   112   bool mHashed;
   113   unsigned int mTemporaryRefs;
   114   bool mIsString;
   115 };
   117 class PluginIdentifierChildString : public PluginIdentifierChild
   118 {
   119   friend class PluginModuleChild;
   120 public:
   121   NPUTF8* ToString()
   122   {
   123     return ToNewCString(mString);
   124   }
   126 protected:
   127   PluginIdentifierChildString(const nsCString& aString)
   128     : PluginIdentifierChild(true),
   129       mString(aString)
   130   { }
   132   virtual PluginIdentifierChild* GetCanonical();
   133   virtual void Hash();
   134   virtual void Unhash();
   136   nsCString mString;
   137 };
   139 class PluginIdentifierChildInt : public PluginIdentifierChild
   140 {
   141   friend class PluginModuleChild;
   142 public:
   143   int32_t ToInt()
   144   {
   145     return mInt;
   146   }
   148 protected:
   149   PluginIdentifierChildInt(int32_t aInt)
   150     : PluginIdentifierChild(false),
   151       mInt(aInt)
   152   { }
   154   virtual PluginIdentifierChild* GetCanonical();
   155   virtual void Hash();
   156   virtual void Unhash();
   158   int32_t mInt;
   159 };
   161 } // namespace plugins
   162 } // namespace mozilla
   164 #endif // dom_plugins_PluginIdentifierChild_h

mercurial