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

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_PluginIdentifierChild_h
michael@0 8 #define dom_plugins_PluginIdentifierChild_h
michael@0 9
michael@0 10 #include "mozilla/plugins/PPluginIdentifierChild.h"
michael@0 11 #include "npapi.h"
michael@0 12 #include "npruntime.h"
michael@0 13
michael@0 14 #include "nsString.h"
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace plugins {
michael@0 18
michael@0 19 class PluginModuleChild;
michael@0 20
michael@0 21 /**
michael@0 22 * Plugin identifiers may be "temporary", see the comment on the
michael@0 23 * PPluginIdentifier constructor for details. This means that any IPDL method
michael@0 24 * which receives a PPluginIdentifierChild* parameter must use StackIdentifier
michael@0 25 * to track it.
michael@0 26 */
michael@0 27 class PluginIdentifierChild : public PPluginIdentifierChild
michael@0 28 {
michael@0 29 friend class PluginModuleChild;
michael@0 30 public:
michael@0 31 bool IsString()
michael@0 32 {
michael@0 33 return mIsString;
michael@0 34 }
michael@0 35
michael@0 36 NPIdentifier ToNPIdentifier()
michael@0 37 {
michael@0 38 if (mCanonicalIdentifier) {
michael@0 39 return mCanonicalIdentifier;
michael@0 40 }
michael@0 41
michael@0 42 NS_ASSERTION(mHashed, "Handing out an unhashed identifier?");
michael@0 43 return this;
michael@0 44 }
michael@0 45
michael@0 46 void MakePermanent();
michael@0 47
michael@0 48 class MOZ_STACK_CLASS StackIdentifier
michael@0 49 {
michael@0 50 public:
michael@0 51 StackIdentifier(PPluginIdentifierChild* actor)
michael@0 52 : mIdentifier(static_cast<PluginIdentifierChild*>(actor))
michael@0 53 {
michael@0 54 if (mIdentifier)
michael@0 55 mIdentifier->StartTemporary();
michael@0 56 }
michael@0 57
michael@0 58 ~StackIdentifier() {
michael@0 59 if (mIdentifier)
michael@0 60 mIdentifier->FinishTemporary();
michael@0 61 }
michael@0 62
michael@0 63 PluginIdentifierChild* operator->() { return mIdentifier; }
michael@0 64
michael@0 65 private:
michael@0 66 PluginIdentifierChild* mIdentifier;
michael@0 67 };
michael@0 68
michael@0 69 protected:
michael@0 70 PluginIdentifierChild(bool aIsString)
michael@0 71 : mCanonicalIdentifier(nullptr)
michael@0 72 , mHashed(false)
michael@0 73 , mTemporaryRefs(0)
michael@0 74 , mIsString(aIsString)
michael@0 75 {
michael@0 76 MOZ_COUNT_CTOR(PluginIdentifierChild);
michael@0 77 }
michael@0 78
michael@0 79 virtual ~PluginIdentifierChild()
michael@0 80 {
michael@0 81 MOZ_COUNT_DTOR(PluginIdentifierChild);
michael@0 82 }
michael@0 83
michael@0 84 // The following functions are implemented by the subclasses for their
michael@0 85 // identifier maps.
michael@0 86 virtual PluginIdentifierChild* GetCanonical() = 0;
michael@0 87 virtual void Hash() = 0;
michael@0 88 virtual void Unhash() = 0;
michael@0 89
michael@0 90 private:
michael@0 91 void StartTemporary();
michael@0 92 void FinishTemporary();
michael@0 93
michael@0 94 // There's a possibility that we already have an actor that wraps the same
michael@0 95 // string or int because we do all this identifier construction
michael@0 96 // asynchronously. In this case we need to hand out the canonical version
michael@0 97 // created by the child side.
michael@0 98 //
michael@0 99 // In order to deal with temporary identifiers which appear on the stack,
michael@0 100 // identifiers use the following state invariants:
michael@0 101 //
michael@0 102 // * mCanonicalIdentifier is non-nullptr: this is a duplicate identifier, no
michael@0 103 // further information is necessary.
michael@0 104 // * mHashed is false: this identifier is a newborn, non-permanent identifier
michael@0 105 // * mHashed is true, mTemporaryRefs is 0: this identifier is permanent
michael@0 106 // * mHashed is true, mTemporaryRefs is non-0: this identifier is temporary;
michael@0 107 // if NPN_GetFooIdentifier is called for it, we need to retain it. If
michael@0 108 // all stack references are lost, unhash it because it will soon be
michael@0 109 // deleted.
michael@0 110
michael@0 111 PluginIdentifierChild* mCanonicalIdentifier;
michael@0 112 bool mHashed;
michael@0 113 unsigned int mTemporaryRefs;
michael@0 114 bool mIsString;
michael@0 115 };
michael@0 116
michael@0 117 class PluginIdentifierChildString : public PluginIdentifierChild
michael@0 118 {
michael@0 119 friend class PluginModuleChild;
michael@0 120 public:
michael@0 121 NPUTF8* ToString()
michael@0 122 {
michael@0 123 return ToNewCString(mString);
michael@0 124 }
michael@0 125
michael@0 126 protected:
michael@0 127 PluginIdentifierChildString(const nsCString& aString)
michael@0 128 : PluginIdentifierChild(true),
michael@0 129 mString(aString)
michael@0 130 { }
michael@0 131
michael@0 132 virtual PluginIdentifierChild* GetCanonical();
michael@0 133 virtual void Hash();
michael@0 134 virtual void Unhash();
michael@0 135
michael@0 136 nsCString mString;
michael@0 137 };
michael@0 138
michael@0 139 class PluginIdentifierChildInt : public PluginIdentifierChild
michael@0 140 {
michael@0 141 friend class PluginModuleChild;
michael@0 142 public:
michael@0 143 int32_t ToInt()
michael@0 144 {
michael@0 145 return mInt;
michael@0 146 }
michael@0 147
michael@0 148 protected:
michael@0 149 PluginIdentifierChildInt(int32_t aInt)
michael@0 150 : PluginIdentifierChild(false),
michael@0 151 mInt(aInt)
michael@0 152 { }
michael@0 153
michael@0 154 virtual PluginIdentifierChild* GetCanonical();
michael@0 155 virtual void Hash();
michael@0 156 virtual void Unhash();
michael@0 157
michael@0 158 int32_t mInt;
michael@0 159 };
michael@0 160
michael@0 161 } // namespace plugins
michael@0 162 } // namespace mozilla
michael@0 163
michael@0 164 #endif // dom_plugins_PluginIdentifierChild_h

mercurial