Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
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 | #include "mozilla/DebugOnly.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nscore.h" |
michael@0 | 9 | #include "nsRequestObserverProxy.h" |
michael@0 | 10 | #include "nsIRequest.h" |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "prlog.h" |
michael@0 | 13 | |
michael@0 | 14 | using namespace mozilla; |
michael@0 | 15 | |
michael@0 | 16 | #if defined(PR_LOGGING) |
michael@0 | 17 | static PRLogModuleInfo *gRequestObserverProxyLog; |
michael@0 | 18 | #endif |
michael@0 | 19 | |
michael@0 | 20 | #undef LOG |
michael@0 | 21 | #define LOG(args) PR_LOG(gRequestObserverProxyLog, PR_LOG_DEBUG, args) |
michael@0 | 22 | |
michael@0 | 23 | //----------------------------------------------------------------------------- |
michael@0 | 24 | // nsARequestObserverEvent internal class... |
michael@0 | 25 | //----------------------------------------------------------------------------- |
michael@0 | 26 | |
michael@0 | 27 | nsARequestObserverEvent::nsARequestObserverEvent(nsIRequest *request) |
michael@0 | 28 | : mRequest(request) |
michael@0 | 29 | { |
michael@0 | 30 | NS_PRECONDITION(mRequest, "null pointer"); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | //----------------------------------------------------------------------------- |
michael@0 | 34 | // nsOnStartRequestEvent internal class... |
michael@0 | 35 | //----------------------------------------------------------------------------- |
michael@0 | 36 | |
michael@0 | 37 | class nsOnStartRequestEvent : public nsARequestObserverEvent |
michael@0 | 38 | { |
michael@0 | 39 | nsRefPtr<nsRequestObserverProxy> mProxy; |
michael@0 | 40 | public: |
michael@0 | 41 | nsOnStartRequestEvent(nsRequestObserverProxy *proxy, |
michael@0 | 42 | nsIRequest *request) |
michael@0 | 43 | : nsARequestObserverEvent(request) |
michael@0 | 44 | , mProxy(proxy) |
michael@0 | 45 | { |
michael@0 | 46 | NS_PRECONDITION(mProxy, "null pointer"); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | virtual ~nsOnStartRequestEvent() {} |
michael@0 | 50 | |
michael@0 | 51 | NS_IMETHOD Run() |
michael@0 | 52 | { |
michael@0 | 53 | LOG(("nsOnStartRequestEvent::HandleEvent [req=%x]\n", mRequest.get())); |
michael@0 | 54 | |
michael@0 | 55 | if (!mProxy->mObserver) { |
michael@0 | 56 | NS_NOTREACHED("already handled onStopRequest event (observer is null)"); |
michael@0 | 57 | return NS_OK; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | LOG(("handle startevent=%p\n", this)); |
michael@0 | 61 | nsresult rv = mProxy->mObserver->OnStartRequest(mRequest, mProxy->mContext); |
michael@0 | 62 | if (NS_FAILED(rv)) { |
michael@0 | 63 | LOG(("OnStartRequest failed [rv=%x] canceling request!\n", rv)); |
michael@0 | 64 | rv = mRequest->Cancel(rv); |
michael@0 | 65 | NS_ASSERTION(NS_SUCCEEDED(rv), "Cancel failed for request!"); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | return NS_OK; |
michael@0 | 69 | } |
michael@0 | 70 | }; |
michael@0 | 71 | |
michael@0 | 72 | //----------------------------------------------------------------------------- |
michael@0 | 73 | // nsOnStopRequestEvent internal class... |
michael@0 | 74 | //----------------------------------------------------------------------------- |
michael@0 | 75 | |
michael@0 | 76 | class nsOnStopRequestEvent : public nsARequestObserverEvent |
michael@0 | 77 | { |
michael@0 | 78 | nsRefPtr<nsRequestObserverProxy> mProxy; |
michael@0 | 79 | public: |
michael@0 | 80 | nsOnStopRequestEvent(nsRequestObserverProxy *proxy, |
michael@0 | 81 | nsIRequest *request) |
michael@0 | 82 | : nsARequestObserverEvent(request) |
michael@0 | 83 | , mProxy(proxy) |
michael@0 | 84 | { |
michael@0 | 85 | NS_PRECONDITION(mProxy, "null pointer"); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | virtual ~nsOnStopRequestEvent() {} |
michael@0 | 89 | |
michael@0 | 90 | NS_IMETHOD Run() |
michael@0 | 91 | { |
michael@0 | 92 | LOG(("nsOnStopRequestEvent::HandleEvent [req=%x]\n", mRequest.get())); |
michael@0 | 93 | |
michael@0 | 94 | nsMainThreadPtrHandle<nsIRequestObserver> observer = mProxy->mObserver; |
michael@0 | 95 | if (!observer) { |
michael@0 | 96 | NS_NOTREACHED("already handled onStopRequest event (observer is null)"); |
michael@0 | 97 | return NS_OK; |
michael@0 | 98 | } |
michael@0 | 99 | // Do not allow any more events to be handled after OnStopRequest |
michael@0 | 100 | mProxy->mObserver = 0; |
michael@0 | 101 | |
michael@0 | 102 | nsresult status = NS_OK; |
michael@0 | 103 | DebugOnly<nsresult> rv = mRequest->GetStatus(&status); |
michael@0 | 104 | NS_ASSERTION(NS_SUCCEEDED(rv), "GetStatus failed for request!"); |
michael@0 | 105 | |
michael@0 | 106 | LOG(("handle stopevent=%p\n", this)); |
michael@0 | 107 | (void) observer->OnStopRequest(mRequest, mProxy->mContext, status); |
michael@0 | 108 | |
michael@0 | 109 | return NS_OK; |
michael@0 | 110 | } |
michael@0 | 111 | }; |
michael@0 | 112 | |
michael@0 | 113 | //----------------------------------------------------------------------------- |
michael@0 | 114 | // nsRequestObserverProxy::nsISupports implementation... |
michael@0 | 115 | //----------------------------------------------------------------------------- |
michael@0 | 116 | |
michael@0 | 117 | NS_IMPL_ISUPPORTS(nsRequestObserverProxy, |
michael@0 | 118 | nsIRequestObserver, |
michael@0 | 119 | nsIRequestObserverProxy) |
michael@0 | 120 | |
michael@0 | 121 | //----------------------------------------------------------------------------- |
michael@0 | 122 | // nsRequestObserverProxy::nsIRequestObserver implementation... |
michael@0 | 123 | //----------------------------------------------------------------------------- |
michael@0 | 124 | |
michael@0 | 125 | NS_IMETHODIMP |
michael@0 | 126 | nsRequestObserverProxy::OnStartRequest(nsIRequest *request, |
michael@0 | 127 | nsISupports *context) |
michael@0 | 128 | { |
michael@0 | 129 | MOZ_ASSERT(!context || context == mContext); |
michael@0 | 130 | LOG(("nsRequestObserverProxy::OnStartRequest [this=%p req=%x]\n", this, request)); |
michael@0 | 131 | |
michael@0 | 132 | nsOnStartRequestEvent *ev = |
michael@0 | 133 | new nsOnStartRequestEvent(this, request); |
michael@0 | 134 | if (!ev) |
michael@0 | 135 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 136 | |
michael@0 | 137 | LOG(("post startevent=%p\n", ev)); |
michael@0 | 138 | nsresult rv = FireEvent(ev); |
michael@0 | 139 | if (NS_FAILED(rv)) |
michael@0 | 140 | delete ev; |
michael@0 | 141 | return rv; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | NS_IMETHODIMP |
michael@0 | 145 | nsRequestObserverProxy::OnStopRequest(nsIRequest *request, |
michael@0 | 146 | nsISupports *context, |
michael@0 | 147 | nsresult status) |
michael@0 | 148 | { |
michael@0 | 149 | MOZ_ASSERT(!context || context == mContext); |
michael@0 | 150 | LOG(("nsRequestObserverProxy: OnStopRequest [this=%p req=%x status=%x]\n", |
michael@0 | 151 | this, request, status)); |
michael@0 | 152 | |
michael@0 | 153 | // The status argument is ignored because, by the time the OnStopRequestEvent |
michael@0 | 154 | // is actually processed, the status of the request may have changed :-( |
michael@0 | 155 | // To make sure that an accurate status code is always used, GetStatus() is |
michael@0 | 156 | // called when the OnStopRequestEvent is actually processed (see above). |
michael@0 | 157 | |
michael@0 | 158 | nsOnStopRequestEvent *ev = |
michael@0 | 159 | new nsOnStopRequestEvent(this, request); |
michael@0 | 160 | if (!ev) |
michael@0 | 161 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 162 | |
michael@0 | 163 | LOG(("post stopevent=%p\n", ev)); |
michael@0 | 164 | nsresult rv = FireEvent(ev); |
michael@0 | 165 | if (NS_FAILED(rv)) |
michael@0 | 166 | delete ev; |
michael@0 | 167 | return rv; |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | //----------------------------------------------------------------------------- |
michael@0 | 171 | // nsRequestObserverProxy::nsIRequestObserverProxy implementation... |
michael@0 | 172 | //----------------------------------------------------------------------------- |
michael@0 | 173 | |
michael@0 | 174 | NS_IMETHODIMP |
michael@0 | 175 | nsRequestObserverProxy::Init(nsIRequestObserver *observer, nsISupports *context) |
michael@0 | 176 | { |
michael@0 | 177 | NS_ENSURE_ARG_POINTER(observer); |
michael@0 | 178 | |
michael@0 | 179 | #if defined(PR_LOGGING) |
michael@0 | 180 | if (!gRequestObserverProxyLog) |
michael@0 | 181 | gRequestObserverProxyLog = PR_NewLogModule("nsRequestObserverProxy"); |
michael@0 | 182 | #endif |
michael@0 | 183 | |
michael@0 | 184 | mObserver = new nsMainThreadPtrHolder<nsIRequestObserver>(observer); |
michael@0 | 185 | mContext = new nsMainThreadPtrHolder<nsISupports>(context); |
michael@0 | 186 | |
michael@0 | 187 | return NS_OK; |
michael@0 | 188 | } |
michael@0 | 189 | |
michael@0 | 190 | //----------------------------------------------------------------------------- |
michael@0 | 191 | // nsRequestObserverProxy implementation... |
michael@0 | 192 | //----------------------------------------------------------------------------- |
michael@0 | 193 | |
michael@0 | 194 | nsresult |
michael@0 | 195 | nsRequestObserverProxy::FireEvent(nsARequestObserverEvent *event) |
michael@0 | 196 | { |
michael@0 | 197 | nsCOMPtr<nsIEventTarget> mainThread(do_GetMainThread()); |
michael@0 | 198 | return mainThread->Dispatch(event, NS_DISPATCH_NORMAL); |
michael@0 | 199 | } |