Tue, 06 Jan 2015 21:39:09 +0100
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: 2; 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 | #include "nsJSUtils.h" |
michael@0 | 6 | #include "nsString.h" |
michael@0 | 7 | #include "nsIServiceManager.h" |
michael@0 | 8 | #include "nsIScriptSecurityManager.h" |
michael@0 | 9 | #include "nsIScriptContext.h" |
michael@0 | 10 | #include "nsIScriptGlobalObject.h" |
michael@0 | 11 | #include "nsIXPConnect.h" |
michael@0 | 12 | #include "nsIMutableArray.h" |
michael@0 | 13 | #include "nsVariant.h" |
michael@0 | 14 | #include "nsIDOMBeforeUnloadEvent.h" |
michael@0 | 15 | #include "nsGkAtoms.h" |
michael@0 | 16 | #include "xpcpublic.h" |
michael@0 | 17 | #include "nsJSEnvironment.h" |
michael@0 | 18 | #include "nsDOMJSUtils.h" |
michael@0 | 19 | #include "WorkerPrivate.h" |
michael@0 | 20 | #include "mozilla/ContentEvents.h" |
michael@0 | 21 | #include "mozilla/CycleCollectedJSRuntime.h" |
michael@0 | 22 | #include "mozilla/HoldDropJSObjects.h" |
michael@0 | 23 | #include "mozilla/JSEventHandler.h" |
michael@0 | 24 | #include "mozilla/Likely.h" |
michael@0 | 25 | #include "mozilla/dom/ErrorEvent.h" |
michael@0 | 26 | #include "mozilla/dom/UnionTypes.h" |
michael@0 | 27 | |
michael@0 | 28 | namespace mozilla { |
michael@0 | 29 | |
michael@0 | 30 | using namespace dom; |
michael@0 | 31 | |
michael@0 | 32 | JSEventHandler::JSEventHandler(nsISupports* aTarget, |
michael@0 | 33 | nsIAtom* aType, |
michael@0 | 34 | const TypedEventHandler& aTypedHandler) |
michael@0 | 35 | : mEventName(aType) |
michael@0 | 36 | , mTypedHandler(aTypedHandler) |
michael@0 | 37 | { |
michael@0 | 38 | nsCOMPtr<nsISupports> base = do_QueryInterface(aTarget); |
michael@0 | 39 | mTarget = base.get(); |
michael@0 | 40 | // Note, we call HoldJSObjects to get CanSkip called before CC. |
michael@0 | 41 | HoldJSObjects(this); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | JSEventHandler::~JSEventHandler() |
michael@0 | 45 | { |
michael@0 | 46 | NS_ASSERTION(!mTarget, "Should have called Disconnect()!"); |
michael@0 | 47 | DropJSObjects(this); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | NS_IMPL_CYCLE_COLLECTION_CLASS(JSEventHandler) |
michael@0 | 51 | |
michael@0 | 52 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(JSEventHandler) |
michael@0 | 53 | tmp->mTypedHandler.ForgetHandler(); |
michael@0 | 54 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
michael@0 | 55 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(JSEventHandler) |
michael@0 | 56 | if (MOZ_UNLIKELY(cb.WantDebugInfo()) && tmp->mEventName) { |
michael@0 | 57 | nsAutoCString name; |
michael@0 | 58 | name.AppendLiteral("JSEventHandler handlerName="); |
michael@0 | 59 | name.Append( |
michael@0 | 60 | NS_ConvertUTF16toUTF8(nsDependentAtomString(tmp->mEventName)).get()); |
michael@0 | 61 | cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name.get()); |
michael@0 | 62 | } else { |
michael@0 | 63 | NS_IMPL_CYCLE_COLLECTION_DESCRIBE(JSEventHandler, tmp->mRefCnt.get()) |
michael@0 | 64 | } |
michael@0 | 65 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mTypedHandler.Ptr()) |
michael@0 | 66 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS |
michael@0 | 67 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
michael@0 | 68 | |
michael@0 | 69 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(JSEventHandler) |
michael@0 | 70 | if (tmp->IsBlackForCC()) { |
michael@0 | 71 | return true; |
michael@0 | 72 | } |
michael@0 | 73 | // If we have a target, it is the one which has tmp as onfoo handler. |
michael@0 | 74 | if (tmp->mTarget) { |
michael@0 | 75 | nsXPCOMCycleCollectionParticipant* cp = nullptr; |
michael@0 | 76 | CallQueryInterface(tmp->mTarget, &cp); |
michael@0 | 77 | nsISupports* canonical = nullptr; |
michael@0 | 78 | tmp->mTarget->QueryInterface(NS_GET_IID(nsCycleCollectionISupports), |
michael@0 | 79 | reinterpret_cast<void**>(&canonical)); |
michael@0 | 80 | // Usually CanSkip ends up unmarking the event listeners of mTarget, |
michael@0 | 81 | // so tmp may become black. |
michael@0 | 82 | if (cp && canonical && cp->CanSkip(canonical, true)) { |
michael@0 | 83 | return tmp->IsBlackForCC(); |
michael@0 | 84 | } |
michael@0 | 85 | } |
michael@0 | 86 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END |
michael@0 | 87 | |
michael@0 | 88 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(JSEventHandler) |
michael@0 | 89 | return tmp->IsBlackForCC(); |
michael@0 | 90 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END |
michael@0 | 91 | |
michael@0 | 92 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(JSEventHandler) |
michael@0 | 93 | return tmp->IsBlackForCC(); |
michael@0 | 94 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END |
michael@0 | 95 | |
michael@0 | 96 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSEventHandler) |
michael@0 | 97 | NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener) |
michael@0 | 98 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 99 | NS_INTERFACE_MAP_ENTRY(JSEventHandler) |
michael@0 | 100 | NS_INTERFACE_MAP_END |
michael@0 | 101 | |
michael@0 | 102 | NS_IMPL_CYCLE_COLLECTING_ADDREF(JSEventHandler) |
michael@0 | 103 | NS_IMPL_CYCLE_COLLECTING_RELEASE(JSEventHandler) |
michael@0 | 104 | |
michael@0 | 105 | bool |
michael@0 | 106 | JSEventHandler::IsBlackForCC() |
michael@0 | 107 | { |
michael@0 | 108 | // We can claim to be black if all the things we reference are |
michael@0 | 109 | // effectively black already. |
michael@0 | 110 | return !mTypedHandler.HasEventHandler() || |
michael@0 | 111 | !mTypedHandler.Ptr()->HasGrayCallable(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | nsresult |
michael@0 | 115 | JSEventHandler::HandleEvent(nsIDOMEvent* aEvent) |
michael@0 | 116 | { |
michael@0 | 117 | nsCOMPtr<EventTarget> target = do_QueryInterface(mTarget); |
michael@0 | 118 | if (!target || !mTypedHandler.HasEventHandler() || |
michael@0 | 119 | !GetTypedEventHandler().Ptr()->CallbackPreserveColor()) { |
michael@0 | 120 | return NS_ERROR_FAILURE; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | Event* event = aEvent->InternalDOMEvent(); |
michael@0 | 124 | bool isMainThread = event->IsMainThreadEvent(); |
michael@0 | 125 | bool isChromeHandler = |
michael@0 | 126 | isMainThread ? |
michael@0 | 127 | nsContentUtils::GetObjectPrincipal( |
michael@0 | 128 | GetTypedEventHandler().Ptr()->CallbackPreserveColor()) == |
michael@0 | 129 | nsContentUtils::GetSystemPrincipal() : |
michael@0 | 130 | mozilla::dom::workers::IsCurrentThreadRunningChromeWorker(); |
michael@0 | 131 | |
michael@0 | 132 | if (mTypedHandler.Type() == TypedEventHandler::eOnError) { |
michael@0 | 133 | MOZ_ASSERT_IF(mEventName, mEventName == nsGkAtoms::onerror); |
michael@0 | 134 | |
michael@0 | 135 | nsString errorMsg, file; |
michael@0 | 136 | EventOrString msgOrEvent; |
michael@0 | 137 | Optional<nsAString> fileName; |
michael@0 | 138 | Optional<uint32_t> lineNumber; |
michael@0 | 139 | Optional<uint32_t> columnNumber; |
michael@0 | 140 | Optional<JS::Handle<JS::Value>> error; |
michael@0 | 141 | |
michael@0 | 142 | NS_ENSURE_TRUE(aEvent, NS_ERROR_UNEXPECTED); |
michael@0 | 143 | ErrorEvent* scriptEvent = aEvent->InternalDOMEvent()->AsErrorEvent(); |
michael@0 | 144 | if (scriptEvent) { |
michael@0 | 145 | scriptEvent->GetMessage(errorMsg); |
michael@0 | 146 | msgOrEvent.SetAsString().SetData(errorMsg.Data(), errorMsg.Length()); |
michael@0 | 147 | |
michael@0 | 148 | scriptEvent->GetFilename(file); |
michael@0 | 149 | fileName = &file; |
michael@0 | 150 | |
michael@0 | 151 | lineNumber.Construct(); |
michael@0 | 152 | lineNumber.Value() = scriptEvent->Lineno(); |
michael@0 | 153 | |
michael@0 | 154 | columnNumber.Construct(); |
michael@0 | 155 | columnNumber.Value() = scriptEvent->Colno(); |
michael@0 | 156 | |
michael@0 | 157 | ThreadsafeAutoJSContext cx; |
michael@0 | 158 | error.Construct(cx); |
michael@0 | 159 | scriptEvent->GetError(cx, &error.Value()); |
michael@0 | 160 | } else { |
michael@0 | 161 | msgOrEvent.SetAsEvent() = aEvent->InternalDOMEvent(); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | nsRefPtr<OnErrorEventHandlerNonNull> handler = |
michael@0 | 165 | mTypedHandler.OnErrorEventHandler(); |
michael@0 | 166 | ErrorResult rv; |
michael@0 | 167 | bool handled = handler->Call(mTarget, msgOrEvent, fileName, lineNumber, |
michael@0 | 168 | columnNumber, error, rv); |
michael@0 | 169 | if (rv.Failed()) { |
michael@0 | 170 | return rv.ErrorCode(); |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | if (handled) { |
michael@0 | 174 | event->PreventDefaultInternal(isChromeHandler); |
michael@0 | 175 | } |
michael@0 | 176 | return NS_OK; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | if (mTypedHandler.Type() == TypedEventHandler::eOnBeforeUnload) { |
michael@0 | 180 | MOZ_ASSERT(mEventName == nsGkAtoms::onbeforeunload); |
michael@0 | 181 | |
michael@0 | 182 | nsRefPtr<OnBeforeUnloadEventHandlerNonNull> handler = |
michael@0 | 183 | mTypedHandler.OnBeforeUnloadEventHandler(); |
michael@0 | 184 | ErrorResult rv; |
michael@0 | 185 | nsString retval; |
michael@0 | 186 | handler->Call(mTarget, *(aEvent->InternalDOMEvent()), retval, rv); |
michael@0 | 187 | if (rv.Failed()) { |
michael@0 | 188 | return rv.ErrorCode(); |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | nsCOMPtr<nsIDOMBeforeUnloadEvent> beforeUnload = do_QueryInterface(aEvent); |
michael@0 | 192 | NS_ENSURE_STATE(beforeUnload); |
michael@0 | 193 | |
michael@0 | 194 | if (!DOMStringIsNull(retval)) { |
michael@0 | 195 | event->PreventDefaultInternal(isChromeHandler); |
michael@0 | 196 | |
michael@0 | 197 | nsAutoString text; |
michael@0 | 198 | beforeUnload->GetReturnValue(text); |
michael@0 | 199 | |
michael@0 | 200 | // Set the text in the beforeUnload event as long as it wasn't |
michael@0 | 201 | // already set (through event.returnValue, which takes |
michael@0 | 202 | // precedence over a value returned from a JS function in IE) |
michael@0 | 203 | if (text.IsEmpty()) { |
michael@0 | 204 | beforeUnload->SetReturnValue(retval); |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | return NS_OK; |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | MOZ_ASSERT(mTypedHandler.Type() == TypedEventHandler::eNormal); |
michael@0 | 212 | ErrorResult rv; |
michael@0 | 213 | nsRefPtr<EventHandlerNonNull> handler = mTypedHandler.NormalEventHandler(); |
michael@0 | 214 | JS::Rooted<JS::Value> retval(CycleCollectedJSRuntime::Get()->Runtime()); |
michael@0 | 215 | handler->Call(mTarget, *(aEvent->InternalDOMEvent()), &retval, rv); |
michael@0 | 216 | if (rv.Failed()) { |
michael@0 | 217 | return rv.ErrorCode(); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | // If the handler returned false and its sense is not reversed, |
michael@0 | 221 | // or the handler returned true and its sense is reversed from |
michael@0 | 222 | // the usual (false means cancel), then prevent default. |
michael@0 | 223 | if (retval.isBoolean() && |
michael@0 | 224 | retval.toBoolean() == (mEventName == nsGkAtoms::onerror || |
michael@0 | 225 | mEventName == nsGkAtoms::onmouseover)) { |
michael@0 | 226 | event->PreventDefaultInternal(isChromeHandler); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | return NS_OK; |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | } // namespace mozilla |
michael@0 | 233 | |
michael@0 | 234 | using namespace mozilla; |
michael@0 | 235 | |
michael@0 | 236 | /* |
michael@0 | 237 | * Factory functions |
michael@0 | 238 | */ |
michael@0 | 239 | |
michael@0 | 240 | nsresult |
michael@0 | 241 | NS_NewJSEventHandler(nsISupports* aTarget, |
michael@0 | 242 | nsIAtom* aEventType, |
michael@0 | 243 | const TypedEventHandler& aTypedHandler, |
michael@0 | 244 | JSEventHandler** aReturn) |
michael@0 | 245 | { |
michael@0 | 246 | NS_ENSURE_ARG(aEventType || !NS_IsMainThread()); |
michael@0 | 247 | JSEventHandler* it = |
michael@0 | 248 | new JSEventHandler(aTarget, aEventType, aTypedHandler); |
michael@0 | 249 | NS_ADDREF(*aReturn = it); |
michael@0 | 250 | |
michael@0 | 251 | return NS_OK; |
michael@0 | 252 | } |