dom/events/JSEventHandler.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef mozilla_JSEventHandler_h_
michael@0 7 #define mozilla_JSEventHandler_h_
michael@0 8
michael@0 9 #include "mozilla/Attributes.h"
michael@0 10 #include "mozilla/MemoryReporting.h"
michael@0 11 #include "mozilla/dom/EventHandlerBinding.h"
michael@0 12 #include "nsCOMPtr.h"
michael@0 13 #include "nsCycleCollectionParticipant.h"
michael@0 14 #include "nsIAtom.h"
michael@0 15 #include "nsIDOMKeyEvent.h"
michael@0 16 #include "nsIDOMEventListener.h"
michael@0 17 #include "nsIScriptContext.h"
michael@0 18
michael@0 19 namespace mozilla {
michael@0 20
michael@0 21 class TypedEventHandler
michael@0 22 {
michael@0 23 public:
michael@0 24 enum HandlerType
michael@0 25 {
michael@0 26 eUnset = 0,
michael@0 27 eNormal = 0x1,
michael@0 28 eOnError = 0x2,
michael@0 29 eOnBeforeUnload = 0x3,
michael@0 30 eTypeBits = 0x3
michael@0 31 };
michael@0 32
michael@0 33 TypedEventHandler()
michael@0 34 : mBits(0)
michael@0 35 {
michael@0 36 }
michael@0 37
michael@0 38 TypedEventHandler(dom::EventHandlerNonNull* aHandler)
michael@0 39 {
michael@0 40 Assign(aHandler, eNormal);
michael@0 41 }
michael@0 42
michael@0 43 TypedEventHandler(dom::OnErrorEventHandlerNonNull* aHandler)
michael@0 44 {
michael@0 45 Assign(aHandler, eOnError);
michael@0 46 }
michael@0 47
michael@0 48 TypedEventHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
michael@0 49 {
michael@0 50 Assign(aHandler, eOnBeforeUnload);
michael@0 51 }
michael@0 52
michael@0 53 TypedEventHandler(const TypedEventHandler& aOther)
michael@0 54 {
michael@0 55 if (aOther.HasEventHandler()) {
michael@0 56 // Have to make sure we take our own ref
michael@0 57 Assign(aOther.Ptr(), aOther.Type());
michael@0 58 } else {
michael@0 59 mBits = 0;
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63 ~TypedEventHandler()
michael@0 64 {
michael@0 65 ReleaseHandler();
michael@0 66 }
michael@0 67
michael@0 68 HandlerType Type() const
michael@0 69 {
michael@0 70 return HandlerType(mBits & eTypeBits);
michael@0 71 }
michael@0 72
michael@0 73 bool HasEventHandler() const
michael@0 74 {
michael@0 75 return !!Ptr();
michael@0 76 }
michael@0 77
michael@0 78 void SetHandler(const TypedEventHandler& aHandler)
michael@0 79 {
michael@0 80 if (aHandler.HasEventHandler()) {
michael@0 81 ReleaseHandler();
michael@0 82 Assign(aHandler.Ptr(), aHandler.Type());
michael@0 83 } else {
michael@0 84 ForgetHandler();
michael@0 85 }
michael@0 86 }
michael@0 87
michael@0 88 dom::EventHandlerNonNull* NormalEventHandler() const
michael@0 89 {
michael@0 90 MOZ_ASSERT(Type() == eNormal && Ptr());
michael@0 91 return reinterpret_cast<dom::EventHandlerNonNull*>(Ptr());
michael@0 92 }
michael@0 93
michael@0 94 void SetHandler(dom::EventHandlerNonNull* aHandler)
michael@0 95 {
michael@0 96 ReleaseHandler();
michael@0 97 Assign(aHandler, eNormal);
michael@0 98 }
michael@0 99
michael@0 100 dom::OnBeforeUnloadEventHandlerNonNull* OnBeforeUnloadEventHandler() const
michael@0 101 {
michael@0 102 MOZ_ASSERT(Type() == eOnBeforeUnload);
michael@0 103 return reinterpret_cast<dom::OnBeforeUnloadEventHandlerNonNull*>(Ptr());
michael@0 104 }
michael@0 105
michael@0 106 void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
michael@0 107 {
michael@0 108 ReleaseHandler();
michael@0 109 Assign(aHandler, eOnBeforeUnload);
michael@0 110 }
michael@0 111
michael@0 112 dom::OnErrorEventHandlerNonNull* OnErrorEventHandler() const
michael@0 113 {
michael@0 114 MOZ_ASSERT(Type() == eOnError);
michael@0 115 return reinterpret_cast<dom::OnErrorEventHandlerNonNull*>(Ptr());
michael@0 116 }
michael@0 117
michael@0 118 void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
michael@0 119 {
michael@0 120 ReleaseHandler();
michael@0 121 Assign(aHandler, eOnError);
michael@0 122 }
michael@0 123
michael@0 124 dom::CallbackFunction* Ptr() const
michael@0 125 {
michael@0 126 // Have to cast eTypeBits so we don't have to worry about
michael@0 127 // promotion issues after the bitflip.
michael@0 128 return reinterpret_cast<dom::CallbackFunction*>(mBits &
michael@0 129 ~uintptr_t(eTypeBits));
michael@0 130 }
michael@0 131
michael@0 132 void ForgetHandler()
michael@0 133 {
michael@0 134 ReleaseHandler();
michael@0 135 mBits = 0;
michael@0 136 }
michael@0 137
michael@0 138 bool operator==(const TypedEventHandler& aOther) const
michael@0 139 {
michael@0 140 return
michael@0 141 Ptr() && aOther.Ptr() &&
michael@0 142 Ptr()->CallbackPreserveColor() == aOther.Ptr()->CallbackPreserveColor();
michael@0 143 }
michael@0 144
michael@0 145 private:
michael@0 146 void operator=(const TypedEventHandler&) MOZ_DELETE;
michael@0 147
michael@0 148 void ReleaseHandler()
michael@0 149 {
michael@0 150 nsISupports* ptr = Ptr();
michael@0 151 NS_IF_RELEASE(ptr);
michael@0 152 }
michael@0 153
michael@0 154 void Assign(nsISupports* aHandler, HandlerType aType)
michael@0 155 {
michael@0 156 MOZ_ASSERT(aHandler, "Must have handler");
michael@0 157 NS_ADDREF(aHandler);
michael@0 158 mBits = uintptr_t(aHandler) | uintptr_t(aType);
michael@0 159 }
michael@0 160
michael@0 161 uintptr_t mBits;
michael@0 162 };
michael@0 163
michael@0 164 /**
michael@0 165 * Implemented by script event listeners. Used to retrieve the script object
michael@0 166 * corresponding to the event target and the handler itself.
michael@0 167 *
michael@0 168 * Note, mTarget is a raw pointer and the owner of the JSEventHandler object
michael@0 169 * is expected to call Disconnect()!
michael@0 170 */
michael@0 171
michael@0 172 #define NS_JSEVENTHANDLER_IID \
michael@0 173 { 0x4f486881, 0x1956, 0x4079, \
michael@0 174 { 0x8c, 0xa0, 0xf3, 0xbd, 0x60, 0x5c, 0xc2, 0x79 } }
michael@0 175
michael@0 176 class JSEventHandler : public nsIDOMEventListener
michael@0 177 {
michael@0 178 public:
michael@0 179 NS_DECLARE_STATIC_IID_ACCESSOR(NS_JSEVENTHANDLER_IID)
michael@0 180
michael@0 181 JSEventHandler(nsISupports* aTarget, nsIAtom* aType,
michael@0 182 const TypedEventHandler& aTypedHandler);
michael@0 183
michael@0 184 virtual ~JSEventHandler();
michael@0 185
michael@0 186 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 187
michael@0 188 // nsIDOMEventListener interface
michael@0 189 NS_DECL_NSIDOMEVENTLISTENER
michael@0 190
michael@0 191 nsISupports* GetEventTarget() const
michael@0 192 {
michael@0 193 return mTarget;
michael@0 194 }
michael@0 195
michael@0 196 void Disconnect()
michael@0 197 {
michael@0 198 mTarget = nullptr;
michael@0 199 }
michael@0 200
michael@0 201 const TypedEventHandler& GetTypedEventHandler() const
michael@0 202 {
michael@0 203 return mTypedHandler;
michael@0 204 }
michael@0 205
michael@0 206 void ForgetHandler()
michael@0 207 {
michael@0 208 mTypedHandler.ForgetHandler();
michael@0 209 }
michael@0 210
michael@0 211 nsIAtom* EventName() const
michael@0 212 {
michael@0 213 return mEventName;
michael@0 214 }
michael@0 215
michael@0 216 // Set a handler for this event listener. The handler must already
michael@0 217 // be bound to the right target.
michael@0 218 void SetHandler(const TypedEventHandler& aTypedHandler)
michael@0 219 {
michael@0 220 mTypedHandler.SetHandler(aTypedHandler);
michael@0 221 }
michael@0 222 void SetHandler(dom::EventHandlerNonNull* aHandler)
michael@0 223 {
michael@0 224 mTypedHandler.SetHandler(aHandler);
michael@0 225 }
michael@0 226 void SetHandler(dom::OnBeforeUnloadEventHandlerNonNull* aHandler)
michael@0 227 {
michael@0 228 mTypedHandler.SetHandler(aHandler);
michael@0 229 }
michael@0 230 void SetHandler(dom::OnErrorEventHandlerNonNull* aHandler)
michael@0 231 {
michael@0 232 mTypedHandler.SetHandler(aHandler);
michael@0 233 }
michael@0 234
michael@0 235 size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
michael@0 236 {
michael@0 237 return 0;
michael@0 238
michael@0 239 // Measurement of the following members may be added later if DMD finds it
michael@0 240 // is worthwhile:
michael@0 241 // - mTarget
michael@0 242 //
michael@0 243 // The following members are not measured:
michael@0 244 // - mTypedHandler: may be shared with others
michael@0 245 // - mEventName: shared with others
michael@0 246 }
michael@0 247
michael@0 248 size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf)
michael@0 249 {
michael@0 250 return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
michael@0 251 }
michael@0 252
michael@0 253 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_CLASS(JSEventHandler)
michael@0 254
michael@0 255 bool IsBlackForCC();
michael@0 256
michael@0 257 protected:
michael@0 258 nsISupports* mTarget;
michael@0 259 nsCOMPtr<nsIAtom> mEventName;
michael@0 260 TypedEventHandler mTypedHandler;
michael@0 261 };
michael@0 262
michael@0 263 NS_DEFINE_STATIC_IID_ACCESSOR(JSEventHandler, NS_JSEVENTHANDLER_IID)
michael@0 264
michael@0 265 } // namespace mozilla
michael@0 266
michael@0 267 /**
michael@0 268 * Factory function. aHandler must already be bound to aTarget.
michael@0 269 * aContext is allowed to be null if aHandler is already set up.
michael@0 270 */
michael@0 271 nsresult NS_NewJSEventHandler(nsISupports* aTarget,
michael@0 272 nsIAtom* aType,
michael@0 273 const mozilla::TypedEventHandler& aTypedHandler,
michael@0 274 mozilla::JSEventHandler** aReturn);
michael@0 275
michael@0 276 #endif // mozilla_JSEventHandler_h_
michael@0 277

mercurial