Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
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 | * |
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 | #include "imgRequest.h" |
michael@0 | 8 | #include "ImageLogging.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "imgLoader.h" |
michael@0 | 11 | #include "imgRequestProxy.h" |
michael@0 | 12 | #include "imgStatusTracker.h" |
michael@0 | 13 | #include "ImageFactory.h" |
michael@0 | 14 | #include "Image.h" |
michael@0 | 15 | #include "RasterImage.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "nsIChannel.h" |
michael@0 | 18 | #include "nsICachingChannel.h" |
michael@0 | 19 | #include "nsIThreadRetargetableRequest.h" |
michael@0 | 20 | #include "nsIInputStream.h" |
michael@0 | 21 | #include "nsIMultiPartChannel.h" |
michael@0 | 22 | #include "nsIHttpChannel.h" |
michael@0 | 23 | #include "nsIApplicationCache.h" |
michael@0 | 24 | #include "nsIApplicationCacheChannel.h" |
michael@0 | 25 | #include "nsMimeTypes.h" |
michael@0 | 26 | |
michael@0 | 27 | #include "nsIInterfaceRequestorUtils.h" |
michael@0 | 28 | #include "nsISupportsPrimitives.h" |
michael@0 | 29 | #include "nsIScriptSecurityManager.h" |
michael@0 | 30 | #include "nsContentUtils.h" |
michael@0 | 31 | |
michael@0 | 32 | #include "nsICacheEntry.h" |
michael@0 | 33 | |
michael@0 | 34 | #include "plstr.h" // PL_strcasestr(...) |
michael@0 | 35 | #include "nsNetUtil.h" |
michael@0 | 36 | #include "nsIProtocolHandler.h" |
michael@0 | 37 | #include "imgIRequest.h" |
michael@0 | 38 | |
michael@0 | 39 | using namespace mozilla; |
michael@0 | 40 | using namespace mozilla::image; |
michael@0 | 41 | |
michael@0 | 42 | #if defined(PR_LOGGING) |
michael@0 | 43 | PRLogModuleInfo * |
michael@0 | 44 | GetImgLog() |
michael@0 | 45 | { |
michael@0 | 46 | static PRLogModuleInfo *sImgLog; |
michael@0 | 47 | if (!sImgLog) |
michael@0 | 48 | sImgLog = PR_NewLogModule("imgRequest"); |
michael@0 | 49 | return sImgLog; |
michael@0 | 50 | } |
michael@0 | 51 | #endif |
michael@0 | 52 | |
michael@0 | 53 | NS_IMPL_ISUPPORTS(imgRequest, |
michael@0 | 54 | nsIStreamListener, nsIRequestObserver, |
michael@0 | 55 | nsIThreadRetargetableStreamListener, |
michael@0 | 56 | nsIChannelEventSink, |
michael@0 | 57 | nsIInterfaceRequestor, |
michael@0 | 58 | nsIAsyncVerifyRedirectCallback) |
michael@0 | 59 | |
michael@0 | 60 | imgRequest::imgRequest(imgLoader* aLoader) |
michael@0 | 61 | : mLoader(aLoader) |
michael@0 | 62 | , mStatusTracker(new imgStatusTracker(nullptr)) |
michael@0 | 63 | , mValidator(nullptr) |
michael@0 | 64 | , mInnerWindowId(0) |
michael@0 | 65 | , mCORSMode(imgIRequest::CORS_NONE) |
michael@0 | 66 | , mDecodeRequested(false) |
michael@0 | 67 | , mIsMultiPartChannel(false) |
michael@0 | 68 | , mGotData(false) |
michael@0 | 69 | , mIsInCache(false) |
michael@0 | 70 | , mResniffMimeType(false) |
michael@0 | 71 | { } |
michael@0 | 72 | |
michael@0 | 73 | imgRequest::~imgRequest() |
michael@0 | 74 | { |
michael@0 | 75 | if (mURI) { |
michael@0 | 76 | nsAutoCString spec; |
michael@0 | 77 | mURI->GetSpec(spec); |
michael@0 | 78 | LOG_FUNC_WITH_PARAM(GetImgLog(), "imgRequest::~imgRequest()", "keyuri", spec.get()); |
michael@0 | 79 | } else |
michael@0 | 80 | LOG_FUNC(GetImgLog(), "imgRequest::~imgRequest()"); |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | nsresult imgRequest::Init(nsIURI *aURI, |
michael@0 | 84 | nsIURI *aCurrentURI, |
michael@0 | 85 | nsIURI *aFirstPartyIsolationURI, |
michael@0 | 86 | nsIRequest *aRequest, |
michael@0 | 87 | nsIChannel *aChannel, |
michael@0 | 88 | imgCacheEntry *aCacheEntry, |
michael@0 | 89 | void *aLoadId, |
michael@0 | 90 | nsIPrincipal* aLoadingPrincipal, |
michael@0 | 91 | int32_t aCORSMode) |
michael@0 | 92 | { |
michael@0 | 93 | MOZ_ASSERT(NS_IsMainThread(), "Cannot use nsIURI off main thread!"); |
michael@0 | 94 | |
michael@0 | 95 | LOG_FUNC(GetImgLog(), "imgRequest::Init"); |
michael@0 | 96 | |
michael@0 | 97 | NS_ABORT_IF_FALSE(!mImage, "Multiple calls to init"); |
michael@0 | 98 | NS_ABORT_IF_FALSE(aURI, "No uri"); |
michael@0 | 99 | NS_ABORT_IF_FALSE(aCurrentURI, "No current uri"); |
michael@0 | 100 | NS_ABORT_IF_FALSE(aRequest, "No request"); |
michael@0 | 101 | NS_ABORT_IF_FALSE(aChannel, "No channel"); |
michael@0 | 102 | |
michael@0 | 103 | mProperties = do_CreateInstance("@mozilla.org/properties;1"); |
michael@0 | 104 | |
michael@0 | 105 | // Use ImageURL to ensure access to URI data off main thread. |
michael@0 | 106 | mURI = new ImageURL(aURI); |
michael@0 | 107 | mCurrentURI = aCurrentURI; |
michael@0 | 108 | mFirstPartyIsolationURI = aFirstPartyIsolationURI; |
michael@0 | 109 | mRequest = aRequest; |
michael@0 | 110 | mChannel = aChannel; |
michael@0 | 111 | mTimedChannel = do_QueryInterface(mChannel); |
michael@0 | 112 | |
michael@0 | 113 | mLoadingPrincipal = aLoadingPrincipal; |
michael@0 | 114 | mCORSMode = aCORSMode; |
michael@0 | 115 | |
michael@0 | 116 | mChannel->GetNotificationCallbacks(getter_AddRefs(mPrevChannelSink)); |
michael@0 | 117 | |
michael@0 | 118 | NS_ASSERTION(mPrevChannelSink != this, |
michael@0 | 119 | "Initializing with a channel that already calls back to us!"); |
michael@0 | 120 | |
michael@0 | 121 | mChannel->SetNotificationCallbacks(this); |
michael@0 | 122 | |
michael@0 | 123 | mCacheEntry = aCacheEntry; |
michael@0 | 124 | |
michael@0 | 125 | SetLoadId(aLoadId); |
michael@0 | 126 | |
michael@0 | 127 | return NS_OK; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | already_AddRefed<imgStatusTracker> |
michael@0 | 131 | imgRequest::GetStatusTracker() |
michael@0 | 132 | { |
michael@0 | 133 | if (mImage && mGotData) { |
michael@0 | 134 | NS_ABORT_IF_FALSE(!mStatusTracker, |
michael@0 | 135 | "Should have given mStatusTracker to mImage"); |
michael@0 | 136 | return mImage->GetStatusTracker(); |
michael@0 | 137 | } else { |
michael@0 | 138 | NS_ABORT_IF_FALSE(mStatusTracker, |
michael@0 | 139 | "Should have mStatusTracker until we create mImage"); |
michael@0 | 140 | nsRefPtr<imgStatusTracker> statusTracker = mStatusTracker; |
michael@0 | 141 | MOZ_ASSERT(statusTracker); |
michael@0 | 142 | return statusTracker.forget(); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void imgRequest::SetCacheEntry(imgCacheEntry *entry) |
michael@0 | 147 | { |
michael@0 | 148 | mCacheEntry = entry; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | bool imgRequest::HasCacheEntry() const |
michael@0 | 152 | { |
michael@0 | 153 | return mCacheEntry != nullptr; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | void imgRequest::ResetCacheEntry() |
michael@0 | 157 | { |
michael@0 | 158 | if (HasCacheEntry()) { |
michael@0 | 159 | mCacheEntry->SetDataSize(0); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | void imgRequest::AddProxy(imgRequestProxy *proxy) |
michael@0 | 164 | { |
michael@0 | 165 | NS_PRECONDITION(proxy, "null imgRequestProxy passed in"); |
michael@0 | 166 | LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::AddProxy", "proxy", proxy); |
michael@0 | 167 | |
michael@0 | 168 | // If we're empty before adding, we have to tell the loader we now have |
michael@0 | 169 | // proxies. |
michael@0 | 170 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 171 | if (statusTracker->ConsumerCount() == 0) { |
michael@0 | 172 | NS_ABORT_IF_FALSE(mURI, "Trying to SetHasProxies without key uri."); |
michael@0 | 173 | mLoader->SetHasProxies(mFirstPartyIsolationURI, mURI); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | statusTracker->AddConsumer(proxy); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | nsresult imgRequest::RemoveProxy(imgRequestProxy *proxy, nsresult aStatus) |
michael@0 | 180 | { |
michael@0 | 181 | LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::RemoveProxy", "proxy", proxy); |
michael@0 | 182 | |
michael@0 | 183 | // This will remove our animation consumers, so after removing |
michael@0 | 184 | // this proxy, we don't end up without proxies with observers, but still |
michael@0 | 185 | // have animation consumers. |
michael@0 | 186 | proxy->ClearAnimationConsumers(); |
michael@0 | 187 | |
michael@0 | 188 | // Let the status tracker do its thing before we potentially call Cancel() |
michael@0 | 189 | // below, because Cancel() may result in OnStopRequest being called back |
michael@0 | 190 | // before Cancel() returns, leaving the image in a different state then the |
michael@0 | 191 | // one it was in at this point. |
michael@0 | 192 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 193 | if (!statusTracker->RemoveConsumer(proxy, aStatus)) |
michael@0 | 194 | return NS_OK; |
michael@0 | 195 | |
michael@0 | 196 | if (statusTracker->ConsumerCount() == 0) { |
michael@0 | 197 | // If we have no observers, there's nothing holding us alive. If we haven't |
michael@0 | 198 | // been cancelled and thus removed from the cache, tell the image loader so |
michael@0 | 199 | // we can be evicted from the cache. |
michael@0 | 200 | if (mCacheEntry) { |
michael@0 | 201 | NS_ABORT_IF_FALSE(mURI, "Removing last observer without key uri."); |
michael@0 | 202 | |
michael@0 | 203 | mLoader->SetHasNoProxies(mURI, mCacheEntry); |
michael@0 | 204 | } |
michael@0 | 205 | #if defined(PR_LOGGING) |
michael@0 | 206 | else { |
michael@0 | 207 | nsAutoCString spec; |
michael@0 | 208 | mURI->GetSpec(spec); |
michael@0 | 209 | LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::RemoveProxy no cache entry", "uri", spec.get()); |
michael@0 | 210 | } |
michael@0 | 211 | #endif |
michael@0 | 212 | |
michael@0 | 213 | /* If |aStatus| is a failure code, then cancel the load if it is still in progress. |
michael@0 | 214 | Otherwise, let the load continue, keeping 'this' in the cache with no observers. |
michael@0 | 215 | This way, if a proxy is destroyed without calling cancel on it, it won't leak |
michael@0 | 216 | and won't leave a bad pointer in the observer list. |
michael@0 | 217 | */ |
michael@0 | 218 | if (statusTracker->IsLoading() && NS_FAILED(aStatus)) { |
michael@0 | 219 | LOG_MSG(GetImgLog(), "imgRequest::RemoveProxy", "load in progress. canceling"); |
michael@0 | 220 | |
michael@0 | 221 | this->Cancel(NS_BINDING_ABORTED); |
michael@0 | 222 | } |
michael@0 | 223 | |
michael@0 | 224 | /* break the cycle from the cache entry. */ |
michael@0 | 225 | mCacheEntry = nullptr; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | // If a proxy is removed for a reason other than its owner being |
michael@0 | 229 | // changed, remove the proxy from the loadgroup. |
michael@0 | 230 | if (aStatus != NS_IMAGELIB_CHANGING_OWNER) |
michael@0 | 231 | proxy->RemoveFromLoadGroup(true); |
michael@0 | 232 | |
michael@0 | 233 | return NS_OK; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | void imgRequest::CancelAndAbort(nsresult aStatus) |
michael@0 | 237 | { |
michael@0 | 238 | LOG_SCOPE(GetImgLog(), "imgRequest::CancelAndAbort"); |
michael@0 | 239 | |
michael@0 | 240 | Cancel(aStatus); |
michael@0 | 241 | |
michael@0 | 242 | // It's possible for the channel to fail to open after we've set our |
michael@0 | 243 | // notification callbacks. In that case, make sure to break the cycle between |
michael@0 | 244 | // the channel and us, because it won't. |
michael@0 | 245 | if (mChannel) { |
michael@0 | 246 | mChannel->SetNotificationCallbacks(mPrevChannelSink); |
michael@0 | 247 | mPrevChannelSink = nullptr; |
michael@0 | 248 | } |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | class imgRequestMainThreadCancel : public nsRunnable |
michael@0 | 252 | { |
michael@0 | 253 | public: |
michael@0 | 254 | imgRequestMainThreadCancel(imgRequest *aImgRequest, nsresult aStatus) |
michael@0 | 255 | : mImgRequest(aImgRequest) |
michael@0 | 256 | , mStatus(aStatus) |
michael@0 | 257 | { |
michael@0 | 258 | MOZ_ASSERT(!NS_IsMainThread(), "Create me off main thread only!"); |
michael@0 | 259 | MOZ_ASSERT(aImgRequest); |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | NS_IMETHOD Run() |
michael@0 | 263 | { |
michael@0 | 264 | MOZ_ASSERT(NS_IsMainThread(), "I should be running on the main thread!"); |
michael@0 | 265 | mImgRequest->ContinueCancel(mStatus); |
michael@0 | 266 | return NS_OK; |
michael@0 | 267 | } |
michael@0 | 268 | private: |
michael@0 | 269 | nsRefPtr<imgRequest> mImgRequest; |
michael@0 | 270 | nsresult mStatus; |
michael@0 | 271 | }; |
michael@0 | 272 | |
michael@0 | 273 | void imgRequest::Cancel(nsresult aStatus) |
michael@0 | 274 | { |
michael@0 | 275 | /* The Cancel() method here should only be called by this class. */ |
michael@0 | 276 | |
michael@0 | 277 | LOG_SCOPE(GetImgLog(), "imgRequest::Cancel"); |
michael@0 | 278 | |
michael@0 | 279 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 280 | |
michael@0 | 281 | statusTracker->MaybeUnblockOnload(); |
michael@0 | 282 | |
michael@0 | 283 | statusTracker->RecordCancel(); |
michael@0 | 284 | |
michael@0 | 285 | if (NS_IsMainThread()) { |
michael@0 | 286 | ContinueCancel(aStatus); |
michael@0 | 287 | } else { |
michael@0 | 288 | NS_DispatchToMainThread(new imgRequestMainThreadCancel(this, aStatus)); |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | void imgRequest::ContinueCancel(nsresult aStatus) |
michael@0 | 293 | { |
michael@0 | 294 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 295 | |
michael@0 | 296 | RemoveFromCache(); |
michael@0 | 297 | |
michael@0 | 298 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 299 | if (mRequest && statusTracker->IsLoading()) { |
michael@0 | 300 | mRequest->Cancel(aStatus); |
michael@0 | 301 | } |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | nsresult imgRequest::GetURI(ImageURL **aURI) |
michael@0 | 305 | { |
michael@0 | 306 | MOZ_ASSERT(aURI); |
michael@0 | 307 | |
michael@0 | 308 | LOG_FUNC(GetImgLog(), "imgRequest::GetURI"); |
michael@0 | 309 | |
michael@0 | 310 | if (mURI) { |
michael@0 | 311 | *aURI = mURI; |
michael@0 | 312 | NS_ADDREF(*aURI); |
michael@0 | 313 | return NS_OK; |
michael@0 | 314 | } |
michael@0 | 315 | |
michael@0 | 316 | return NS_ERROR_FAILURE; |
michael@0 | 317 | } |
michael@0 | 318 | |
michael@0 | 319 | nsresult imgRequest::GetSecurityInfo(nsISupports **aSecurityInfo) |
michael@0 | 320 | { |
michael@0 | 321 | LOG_FUNC(GetImgLog(), "imgRequest::GetSecurityInfo"); |
michael@0 | 322 | |
michael@0 | 323 | // Missing security info means this is not a security load |
michael@0 | 324 | // i.e. it is not an error when security info is missing |
michael@0 | 325 | NS_IF_ADDREF(*aSecurityInfo = mSecurityInfo); |
michael@0 | 326 | return NS_OK; |
michael@0 | 327 | } |
michael@0 | 328 | |
michael@0 | 329 | void imgRequest::RemoveFromCache() |
michael@0 | 330 | { |
michael@0 | 331 | LOG_SCOPE(GetImgLog(), "imgRequest::RemoveFromCache"); |
michael@0 | 332 | |
michael@0 | 333 | if (mIsInCache) { |
michael@0 | 334 | // mCacheEntry is nulled out when we have no more observers. |
michael@0 | 335 | if (mCacheEntry) |
michael@0 | 336 | mLoader->RemoveFromCache(mCacheEntry); |
michael@0 | 337 | else { |
michael@0 | 338 | mLoader->RemoveFromCache(mLoader->GetCacheKey(mFirstPartyIsolationURI, mURI, nullptr), |
michael@0 | 339 | mLoader->GetCache(mURI), |
michael@0 | 340 | mLoader->GetCacheQueue(mURI)); |
michael@0 | 341 | } |
michael@0 | 342 | } |
michael@0 | 343 | |
michael@0 | 344 | mCacheEntry = nullptr; |
michael@0 | 345 | } |
michael@0 | 346 | |
michael@0 | 347 | int32_t imgRequest::Priority() const |
michael@0 | 348 | { |
michael@0 | 349 | int32_t priority = nsISupportsPriority::PRIORITY_NORMAL; |
michael@0 | 350 | nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mRequest); |
michael@0 | 351 | if (p) |
michael@0 | 352 | p->GetPriority(&priority); |
michael@0 | 353 | return priority; |
michael@0 | 354 | } |
michael@0 | 355 | |
michael@0 | 356 | void imgRequest::AdjustPriority(imgRequestProxy *proxy, int32_t delta) |
michael@0 | 357 | { |
michael@0 | 358 | // only the first proxy is allowed to modify the priority of this image load. |
michael@0 | 359 | // |
michael@0 | 360 | // XXX(darin): this is probably not the most optimal algorithm as we may want |
michael@0 | 361 | // to increase the priority of requests that have a lot of proxies. the key |
michael@0 | 362 | // concern though is that image loads remain lower priority than other pieces |
michael@0 | 363 | // of content such as link clicks, CSS, and JS. |
michael@0 | 364 | // |
michael@0 | 365 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 366 | if (!statusTracker->FirstConsumerIs(proxy)) |
michael@0 | 367 | return; |
michael@0 | 368 | |
michael@0 | 369 | nsCOMPtr<nsISupportsPriority> p = do_QueryInterface(mChannel); |
michael@0 | 370 | if (p) |
michael@0 | 371 | p->AdjustPriority(delta); |
michael@0 | 372 | } |
michael@0 | 373 | |
michael@0 | 374 | void imgRequest::SetIsInCache(bool incache) |
michael@0 | 375 | { |
michael@0 | 376 | LOG_FUNC_WITH_PARAM(GetImgLog(), "imgRequest::SetIsCacheable", "incache", incache); |
michael@0 | 377 | mIsInCache = incache; |
michael@0 | 378 | } |
michael@0 | 379 | |
michael@0 | 380 | void imgRequest::UpdateCacheEntrySize() |
michael@0 | 381 | { |
michael@0 | 382 | if (mCacheEntry) |
michael@0 | 383 | mCacheEntry->SetDataSize(mImage->SizeOfData()); |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | void imgRequest::SetCacheValidation(imgCacheEntry* aCacheEntry, nsIRequest* aRequest) |
michael@0 | 387 | { |
michael@0 | 388 | /* get the expires info */ |
michael@0 | 389 | if (aCacheEntry) { |
michael@0 | 390 | nsCOMPtr<nsICachingChannel> cacheChannel(do_QueryInterface(aRequest)); |
michael@0 | 391 | if (cacheChannel) { |
michael@0 | 392 | nsCOMPtr<nsISupports> cacheToken; |
michael@0 | 393 | cacheChannel->GetCacheToken(getter_AddRefs(cacheToken)); |
michael@0 | 394 | if (cacheToken) { |
michael@0 | 395 | nsCOMPtr<nsICacheEntry> entryDesc(do_QueryInterface(cacheToken)); |
michael@0 | 396 | if (entryDesc) { |
michael@0 | 397 | uint32_t expiration; |
michael@0 | 398 | /* get the expiration time from the caching channel's token */ |
michael@0 | 399 | entryDesc->GetExpirationTime(&expiration); |
michael@0 | 400 | |
michael@0 | 401 | // Expiration time defaults to 0. We set the expiration time on our |
michael@0 | 402 | // entry if it hasn't been set yet. |
michael@0 | 403 | if (aCacheEntry->GetExpiryTime() == 0) |
michael@0 | 404 | aCacheEntry->SetExpiryTime(expiration); |
michael@0 | 405 | } |
michael@0 | 406 | } |
michael@0 | 407 | } |
michael@0 | 408 | |
michael@0 | 409 | // Determine whether the cache entry must be revalidated when we try to use it. |
michael@0 | 410 | // Currently, only HTTP specifies this information... |
michael@0 | 411 | nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aRequest)); |
michael@0 | 412 | if (httpChannel) { |
michael@0 | 413 | bool bMustRevalidate = false; |
michael@0 | 414 | |
michael@0 | 415 | httpChannel->IsNoStoreResponse(&bMustRevalidate); |
michael@0 | 416 | |
michael@0 | 417 | if (!bMustRevalidate) { |
michael@0 | 418 | httpChannel->IsNoCacheResponse(&bMustRevalidate); |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | if (!bMustRevalidate) { |
michael@0 | 422 | nsAutoCString cacheHeader; |
michael@0 | 423 | |
michael@0 | 424 | httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("Cache-Control"), |
michael@0 | 425 | cacheHeader); |
michael@0 | 426 | if (PL_strcasestr(cacheHeader.get(), "must-revalidate")) { |
michael@0 | 427 | bMustRevalidate = true; |
michael@0 | 428 | } |
michael@0 | 429 | } |
michael@0 | 430 | |
michael@0 | 431 | // Cache entries default to not needing to validate. We ensure that |
michael@0 | 432 | // multiple calls to this function don't override an earlier decision to |
michael@0 | 433 | // validate by making validation a one-way decision. |
michael@0 | 434 | if (bMustRevalidate) |
michael@0 | 435 | aCacheEntry->SetMustValidate(bMustRevalidate); |
michael@0 | 436 | } |
michael@0 | 437 | |
michael@0 | 438 | // We always need to validate file URIs. |
michael@0 | 439 | nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); |
michael@0 | 440 | if (channel) { |
michael@0 | 441 | nsCOMPtr<nsIURI> uri; |
michael@0 | 442 | channel->GetURI(getter_AddRefs(uri)); |
michael@0 | 443 | bool isfile = false; |
michael@0 | 444 | uri->SchemeIs("file", &isfile); |
michael@0 | 445 | if (isfile) |
michael@0 | 446 | aCacheEntry->SetMustValidate(isfile); |
michael@0 | 447 | } |
michael@0 | 448 | } |
michael@0 | 449 | } |
michael@0 | 450 | |
michael@0 | 451 | namespace { // anon |
michael@0 | 452 | |
michael@0 | 453 | already_AddRefed<nsIApplicationCache> |
michael@0 | 454 | GetApplicationCache(nsIRequest* aRequest) |
michael@0 | 455 | { |
michael@0 | 456 | nsresult rv; |
michael@0 | 457 | |
michael@0 | 458 | nsCOMPtr<nsIApplicationCacheChannel> appCacheChan = do_QueryInterface(aRequest); |
michael@0 | 459 | if (!appCacheChan) { |
michael@0 | 460 | return nullptr; |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | bool fromAppCache; |
michael@0 | 464 | rv = appCacheChan->GetLoadedFromApplicationCache(&fromAppCache); |
michael@0 | 465 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 466 | |
michael@0 | 467 | if (!fromAppCache) { |
michael@0 | 468 | return nullptr; |
michael@0 | 469 | } |
michael@0 | 470 | |
michael@0 | 471 | nsCOMPtr<nsIApplicationCache> appCache; |
michael@0 | 472 | rv = appCacheChan->GetApplicationCache(getter_AddRefs(appCache)); |
michael@0 | 473 | NS_ENSURE_SUCCESS(rv, nullptr); |
michael@0 | 474 | |
michael@0 | 475 | return appCache.forget(); |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | } // anon |
michael@0 | 479 | |
michael@0 | 480 | bool |
michael@0 | 481 | imgRequest::CacheChanged(nsIRequest* aNewRequest) |
michael@0 | 482 | { |
michael@0 | 483 | nsCOMPtr<nsIApplicationCache> newAppCache = GetApplicationCache(aNewRequest); |
michael@0 | 484 | |
michael@0 | 485 | // Application cache not involved at all or the same app cache involved |
michael@0 | 486 | // in both of the loads (original and new). |
michael@0 | 487 | if (newAppCache == mApplicationCache) |
michael@0 | 488 | return false; |
michael@0 | 489 | |
michael@0 | 490 | // In a rare case it may happen that two objects still refer |
michael@0 | 491 | // the same application cache version. |
michael@0 | 492 | if (newAppCache && mApplicationCache) { |
michael@0 | 493 | nsresult rv; |
michael@0 | 494 | |
michael@0 | 495 | nsAutoCString oldAppCacheClientId, newAppCacheClientId; |
michael@0 | 496 | rv = mApplicationCache->GetClientID(oldAppCacheClientId); |
michael@0 | 497 | NS_ENSURE_SUCCESS(rv, true); |
michael@0 | 498 | rv = newAppCache->GetClientID(newAppCacheClientId); |
michael@0 | 499 | NS_ENSURE_SUCCESS(rv, true); |
michael@0 | 500 | |
michael@0 | 501 | if (oldAppCacheClientId == newAppCacheClientId) |
michael@0 | 502 | return false; |
michael@0 | 503 | } |
michael@0 | 504 | |
michael@0 | 505 | // When we get here, app caches differ or app cache is involved |
michael@0 | 506 | // just in one of the loads what we also consider as a change |
michael@0 | 507 | // in a loading cache. |
michael@0 | 508 | return true; |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | nsresult |
michael@0 | 512 | imgRequest::LockImage() |
michael@0 | 513 | { |
michael@0 | 514 | return mImage->LockImage(); |
michael@0 | 515 | } |
michael@0 | 516 | |
michael@0 | 517 | nsresult |
michael@0 | 518 | imgRequest::UnlockImage() |
michael@0 | 519 | { |
michael@0 | 520 | return mImage->UnlockImage(); |
michael@0 | 521 | } |
michael@0 | 522 | |
michael@0 | 523 | nsresult |
michael@0 | 524 | imgRequest::RequestDecode() |
michael@0 | 525 | { |
michael@0 | 526 | // If we've initialized our image, we can request a decode. |
michael@0 | 527 | if (mImage) { |
michael@0 | 528 | return mImage->RequestDecode(); |
michael@0 | 529 | } |
michael@0 | 530 | |
michael@0 | 531 | // Otherwise, flag to do it when we get the image |
michael@0 | 532 | mDecodeRequested = true; |
michael@0 | 533 | |
michael@0 | 534 | return NS_OK; |
michael@0 | 535 | } |
michael@0 | 536 | |
michael@0 | 537 | nsresult |
michael@0 | 538 | imgRequest::StartDecoding() |
michael@0 | 539 | { |
michael@0 | 540 | // If we've initialized our image, we can request a decode. |
michael@0 | 541 | if (mImage) { |
michael@0 | 542 | return mImage->StartDecoding(); |
michael@0 | 543 | } |
michael@0 | 544 | |
michael@0 | 545 | // Otherwise, flag to do it when we get the image |
michael@0 | 546 | mDecodeRequested = true; |
michael@0 | 547 | |
michael@0 | 548 | return NS_OK; |
michael@0 | 549 | } |
michael@0 | 550 | |
michael@0 | 551 | /** nsIRequestObserver methods **/ |
michael@0 | 552 | |
michael@0 | 553 | /* void onStartRequest (in nsIRequest request, in nsISupports ctxt); */ |
michael@0 | 554 | NS_IMETHODIMP imgRequest::OnStartRequest(nsIRequest *aRequest, nsISupports *ctxt) |
michael@0 | 555 | { |
michael@0 | 556 | LOG_SCOPE(GetImgLog(), "imgRequest::OnStartRequest"); |
michael@0 | 557 | |
michael@0 | 558 | // Figure out if we're multipart |
michael@0 | 559 | nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest)); |
michael@0 | 560 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 561 | if (mpchan) { |
michael@0 | 562 | mIsMultiPartChannel = true; |
michael@0 | 563 | statusTracker->SetIsMultipart(); |
michael@0 | 564 | } |
michael@0 | 565 | |
michael@0 | 566 | // If we're not multipart, we shouldn't have an image yet |
michael@0 | 567 | NS_ABORT_IF_FALSE(mIsMultiPartChannel || !mImage, |
michael@0 | 568 | "Already have an image for non-multipart request"); |
michael@0 | 569 | |
michael@0 | 570 | // If we're multipart and about to load another image, signal so we can |
michael@0 | 571 | // detect the mime type in OnDataAvailable. |
michael@0 | 572 | if (mIsMultiPartChannel && mImage) { |
michael@0 | 573 | mResniffMimeType = true; |
michael@0 | 574 | |
michael@0 | 575 | // Tell the image to reinitialize itself. We have to do this in |
michael@0 | 576 | // OnStartRequest so that its state machine is always in a consistent |
michael@0 | 577 | // state. |
michael@0 | 578 | // Note that if our MIME type changes, mImage will be replaced with a |
michael@0 | 579 | // new object. |
michael@0 | 580 | mImage->OnNewSourceData(); |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | /* |
michael@0 | 584 | * If mRequest is null here, then we need to set it so that we'll be able to |
michael@0 | 585 | * cancel it if our Cancel() method is called. Note that this can only |
michael@0 | 586 | * happen for multipart channels. We could simply not null out mRequest for |
michael@0 | 587 | * non-last parts, if GetIsLastPart() were reliable, but it's not. See |
michael@0 | 588 | * https://bugzilla.mozilla.org/show_bug.cgi?id=339610 |
michael@0 | 589 | */ |
michael@0 | 590 | if (!mRequest) { |
michael@0 | 591 | NS_ASSERTION(mpchan, |
michael@0 | 592 | "We should have an mRequest here unless we're multipart"); |
michael@0 | 593 | nsCOMPtr<nsIChannel> chan; |
michael@0 | 594 | mpchan->GetBaseChannel(getter_AddRefs(chan)); |
michael@0 | 595 | mRequest = chan; |
michael@0 | 596 | } |
michael@0 | 597 | |
michael@0 | 598 | // Note: refreshing statusTracker in case OnNewSourceData changed it. |
michael@0 | 599 | statusTracker = GetStatusTracker(); |
michael@0 | 600 | statusTracker->OnStartRequest(); |
michael@0 | 601 | |
michael@0 | 602 | nsCOMPtr<nsIChannel> channel(do_QueryInterface(aRequest)); |
michael@0 | 603 | if (channel) |
michael@0 | 604 | channel->GetSecurityInfo(getter_AddRefs(mSecurityInfo)); |
michael@0 | 605 | |
michael@0 | 606 | /* Get our principal */ |
michael@0 | 607 | nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest)); |
michael@0 | 608 | if (chan) { |
michael@0 | 609 | nsCOMPtr<nsIScriptSecurityManager> secMan = nsContentUtils::GetSecurityManager(); |
michael@0 | 610 | if (secMan) { |
michael@0 | 611 | nsresult rv = secMan->GetChannelPrincipal(chan, |
michael@0 | 612 | getter_AddRefs(mPrincipal)); |
michael@0 | 613 | if (NS_FAILED(rv)) { |
michael@0 | 614 | return rv; |
michael@0 | 615 | } |
michael@0 | 616 | } |
michael@0 | 617 | } |
michael@0 | 618 | |
michael@0 | 619 | SetCacheValidation(mCacheEntry, aRequest); |
michael@0 | 620 | |
michael@0 | 621 | mApplicationCache = GetApplicationCache(aRequest); |
michael@0 | 622 | |
michael@0 | 623 | // Shouldn't we be dead already if this gets hit? Probably multipart/x-mixed-replace... |
michael@0 | 624 | if (statusTracker->ConsumerCount() == 0) { |
michael@0 | 625 | this->Cancel(NS_IMAGELIB_ERROR_FAILURE); |
michael@0 | 626 | } |
michael@0 | 627 | |
michael@0 | 628 | // Try to retarget OnDataAvailable to a decode thread. |
michael@0 | 629 | nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aRequest); |
michael@0 | 630 | nsCOMPtr<nsIThreadRetargetableRequest> retargetable = |
michael@0 | 631 | do_QueryInterface(aRequest); |
michael@0 | 632 | if (httpChannel && retargetable && |
michael@0 | 633 | ImageFactory::CanRetargetOnDataAvailable(mURI, mIsMultiPartChannel)) { |
michael@0 | 634 | nsAutoCString mimeType; |
michael@0 | 635 | nsresult rv = httpChannel->GetContentType(mimeType); |
michael@0 | 636 | if (NS_SUCCEEDED(rv) && !mimeType.EqualsLiteral(IMAGE_SVG_XML)) { |
michael@0 | 637 | // Image object not created until OnDataAvailable, so forward to static |
michael@0 | 638 | // DecodePool directly. |
michael@0 | 639 | nsCOMPtr<nsIEventTarget> target = RasterImage::GetEventTarget(); |
michael@0 | 640 | rv = retargetable->RetargetDeliveryTo(target); |
michael@0 | 641 | } |
michael@0 | 642 | PR_LOG(GetImgLog(), PR_LOG_WARNING, |
michael@0 | 643 | ("[this=%p] imgRequest::OnStartRequest -- " |
michael@0 | 644 | "RetargetDeliveryTo rv %d=%s\n", |
michael@0 | 645 | this, rv, NS_SUCCEEDED(rv) ? "succeeded" : "failed")); |
michael@0 | 646 | } |
michael@0 | 647 | |
michael@0 | 648 | return NS_OK; |
michael@0 | 649 | } |
michael@0 | 650 | |
michael@0 | 651 | /* void onStopRequest (in nsIRequest request, in nsISupports ctxt, in nsresult status); */ |
michael@0 | 652 | NS_IMETHODIMP imgRequest::OnStopRequest(nsIRequest *aRequest, nsISupports *ctxt, nsresult status) |
michael@0 | 653 | { |
michael@0 | 654 | LOG_FUNC(GetImgLog(), "imgRequest::OnStopRequest"); |
michael@0 | 655 | |
michael@0 | 656 | // XXXldb What if this is a non-last part of a multipart request? |
michael@0 | 657 | // xxx before we release our reference to mRequest, lets |
michael@0 | 658 | // save the last status that we saw so that the |
michael@0 | 659 | // imgRequestProxy will have access to it. |
michael@0 | 660 | if (mRequest) { |
michael@0 | 661 | mRequest = nullptr; // we no longer need the request |
michael@0 | 662 | } |
michael@0 | 663 | |
michael@0 | 664 | // stop holding a ref to the channel, since we don't need it anymore |
michael@0 | 665 | if (mChannel) { |
michael@0 | 666 | mChannel->SetNotificationCallbacks(mPrevChannelSink); |
michael@0 | 667 | mPrevChannelSink = nullptr; |
michael@0 | 668 | mChannel = nullptr; |
michael@0 | 669 | } |
michael@0 | 670 | |
michael@0 | 671 | bool lastPart = true; |
michael@0 | 672 | nsCOMPtr<nsIMultiPartChannel> mpchan(do_QueryInterface(aRequest)); |
michael@0 | 673 | if (mpchan) |
michael@0 | 674 | mpchan->GetIsLastPart(&lastPart); |
michael@0 | 675 | |
michael@0 | 676 | // Tell the image that it has all of the source data. Note that this can |
michael@0 | 677 | // trigger a failure, since the image might be waiting for more non-optional |
michael@0 | 678 | // data and this is the point where we break the news that it's not coming. |
michael@0 | 679 | if (mImage) { |
michael@0 | 680 | nsresult rv = mImage->OnImageDataComplete(aRequest, ctxt, status, lastPart); |
michael@0 | 681 | |
michael@0 | 682 | // If we got an error in the OnImageDataComplete() call, we don't want to |
michael@0 | 683 | // proceed as if nothing bad happened. However, we also want to give |
michael@0 | 684 | // precedence to failure status codes from necko, since presumably they're |
michael@0 | 685 | // more meaningful. |
michael@0 | 686 | if (NS_FAILED(rv) && NS_SUCCEEDED(status)) |
michael@0 | 687 | status = rv; |
michael@0 | 688 | } |
michael@0 | 689 | |
michael@0 | 690 | // If the request went through, update the cache entry size. Otherwise, |
michael@0 | 691 | // cancel the request, which removes us from the cache. |
michael@0 | 692 | if (mImage && NS_SUCCEEDED(status)) { |
michael@0 | 693 | // We update the cache entry size here because this is where we finish |
michael@0 | 694 | // loading compressed source data, which is part of our size calculus. |
michael@0 | 695 | UpdateCacheEntrySize(); |
michael@0 | 696 | } |
michael@0 | 697 | else { |
michael@0 | 698 | // stops animations, removes from cache |
michael@0 | 699 | this->Cancel(status); |
michael@0 | 700 | } |
michael@0 | 701 | |
michael@0 | 702 | if (!mImage) { |
michael@0 | 703 | // We have to fire imgStatusTracker::OnStopRequest ourselves because there's |
michael@0 | 704 | // no image capable of doing so. |
michael@0 | 705 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 706 | statusTracker->OnStopRequest(lastPart, status); |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | mTimedChannel = nullptr; |
michael@0 | 710 | return NS_OK; |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | struct mimetype_closure |
michael@0 | 714 | { |
michael@0 | 715 | nsACString* newType; |
michael@0 | 716 | }; |
michael@0 | 717 | |
michael@0 | 718 | /* prototype for these defined below */ |
michael@0 | 719 | static NS_METHOD sniff_mimetype_callback(nsIInputStream* in, void* closure, const char* fromRawSegment, |
michael@0 | 720 | uint32_t toOffset, uint32_t count, uint32_t *writeCount); |
michael@0 | 721 | |
michael@0 | 722 | /** nsThreadRetargetableStreamListener methods **/ |
michael@0 | 723 | NS_IMETHODIMP |
michael@0 | 724 | imgRequest::CheckListenerChain() |
michael@0 | 725 | { |
michael@0 | 726 | // TODO Might need more checking here. |
michael@0 | 727 | NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread!"); |
michael@0 | 728 | return NS_OK; |
michael@0 | 729 | } |
michael@0 | 730 | |
michael@0 | 731 | /** nsIStreamListener methods **/ |
michael@0 | 732 | |
michael@0 | 733 | /* void onDataAvailable (in nsIRequest request, in nsISupports ctxt, in nsIInputStream inStr, in unsigned long long sourceOffset, in unsigned long count); */ |
michael@0 | 734 | NS_IMETHODIMP |
michael@0 | 735 | imgRequest::OnDataAvailable(nsIRequest *aRequest, nsISupports *ctxt, |
michael@0 | 736 | nsIInputStream *inStr, uint64_t sourceOffset, |
michael@0 | 737 | uint32_t count) |
michael@0 | 738 | { |
michael@0 | 739 | LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgRequest::OnDataAvailable", "count", count); |
michael@0 | 740 | |
michael@0 | 741 | NS_ASSERTION(aRequest, "imgRequest::OnDataAvailable -- no request!"); |
michael@0 | 742 | |
michael@0 | 743 | nsresult rv; |
michael@0 | 744 | |
michael@0 | 745 | if (!mGotData || mResniffMimeType) { |
michael@0 | 746 | LOG_SCOPE(GetImgLog(), "imgRequest::OnDataAvailable |First time through... finding mimetype|"); |
michael@0 | 747 | |
michael@0 | 748 | mGotData = true; |
michael@0 | 749 | |
michael@0 | 750 | // Store and reset this for the invariant that it's always false after |
michael@0 | 751 | // calls to OnDataAvailable (see bug 907575) |
michael@0 | 752 | bool resniffMimeType = mResniffMimeType; |
michael@0 | 753 | mResniffMimeType = false; |
michael@0 | 754 | |
michael@0 | 755 | mimetype_closure closure; |
michael@0 | 756 | nsAutoCString newType; |
michael@0 | 757 | closure.newType = &newType; |
michael@0 | 758 | |
michael@0 | 759 | /* look at the first few bytes and see if we can tell what the data is from that |
michael@0 | 760 | * since servers tend to lie. :( |
michael@0 | 761 | */ |
michael@0 | 762 | uint32_t out; |
michael@0 | 763 | inStr->ReadSegments(sniff_mimetype_callback, &closure, count, &out); |
michael@0 | 764 | |
michael@0 | 765 | nsCOMPtr<nsIChannel> chan(do_QueryInterface(aRequest)); |
michael@0 | 766 | if (newType.IsEmpty()) { |
michael@0 | 767 | LOG_SCOPE(GetImgLog(), "imgRequest::OnDataAvailable |sniffing of mimetype failed|"); |
michael@0 | 768 | |
michael@0 | 769 | rv = NS_ERROR_FAILURE; |
michael@0 | 770 | if (chan) { |
michael@0 | 771 | rv = chan->GetContentType(newType); |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | if (NS_FAILED(rv)) { |
michael@0 | 775 | PR_LOG(GetImgLog(), PR_LOG_ERROR, |
michael@0 | 776 | ("[this=%p] imgRequest::OnDataAvailable -- Content type unavailable from the channel\n", |
michael@0 | 777 | this)); |
michael@0 | 778 | |
michael@0 | 779 | this->Cancel(NS_IMAGELIB_ERROR_FAILURE); |
michael@0 | 780 | |
michael@0 | 781 | return NS_BINDING_ABORTED; |
michael@0 | 782 | } |
michael@0 | 783 | |
michael@0 | 784 | LOG_MSG(GetImgLog(), "imgRequest::OnDataAvailable", "Got content type from the channel"); |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | // If we're a regular image and this is the first call to OnDataAvailable, |
michael@0 | 788 | // this will always be true. If we've resniffed our MIME type (i.e. we're a |
michael@0 | 789 | // multipart/x-mixed-replace image), we have to be able to switch our image |
michael@0 | 790 | // type and decoder. |
michael@0 | 791 | // We always reinitialize for SVGs, because they have no way of |
michael@0 | 792 | // reinitializing themselves. |
michael@0 | 793 | if (mContentType != newType || newType.EqualsLiteral(IMAGE_SVG_XML)) { |
michael@0 | 794 | mContentType = newType; |
michael@0 | 795 | |
michael@0 | 796 | // If we've resniffed our MIME type and it changed, we need to create a |
michael@0 | 797 | // new status tracker to give to the image, because we don't have one of |
michael@0 | 798 | // our own any more. |
michael@0 | 799 | if (resniffMimeType) { |
michael@0 | 800 | NS_ABORT_IF_FALSE(mIsMultiPartChannel, "Resniffing a non-multipart image"); |
michael@0 | 801 | |
michael@0 | 802 | nsRefPtr<imgStatusTracker> freshTracker = new imgStatusTracker(nullptr); |
michael@0 | 803 | nsRefPtr<imgStatusTracker> oldStatusTracker = GetStatusTracker(); |
michael@0 | 804 | freshTracker->AdoptConsumers(oldStatusTracker); |
michael@0 | 805 | mStatusTracker = freshTracker.forget(); |
michael@0 | 806 | } |
michael@0 | 807 | |
michael@0 | 808 | SetProperties(chan); |
michael@0 | 809 | |
michael@0 | 810 | LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnDataAvailable", "content type", mContentType.get()); |
michael@0 | 811 | |
michael@0 | 812 | // XXX If server lied about mimetype and it's SVG, we may need to copy |
michael@0 | 813 | // the data and dispatch back to the main thread, AND tell the channel to |
michael@0 | 814 | // dispatch there in the future. |
michael@0 | 815 | |
michael@0 | 816 | // Now we can create a new image to hold the data. If we don't have a decoder |
michael@0 | 817 | // for this mimetype we'll find out about it here. |
michael@0 | 818 | mImage = ImageFactory::CreateImage(aRequest, mStatusTracker, mContentType, |
michael@0 | 819 | mURI, mIsMultiPartChannel, |
michael@0 | 820 | static_cast<uint32_t>(mInnerWindowId)); |
michael@0 | 821 | |
michael@0 | 822 | // Release our copy of the status tracker since the image owns it now. |
michael@0 | 823 | mStatusTracker = nullptr; |
michael@0 | 824 | |
michael@0 | 825 | // Notify listeners that we have an image. |
michael@0 | 826 | // XXX(seth): The name of this notification method is pretty misleading. |
michael@0 | 827 | nsRefPtr<imgStatusTracker> statusTracker = GetStatusTracker(); |
michael@0 | 828 | statusTracker->OnDataAvailable(); |
michael@0 | 829 | |
michael@0 | 830 | if (mImage->HasError() && !mIsMultiPartChannel) { // Probably bad mimetype |
michael@0 | 831 | // We allow multipart images to fail to initialize without cancelling the |
michael@0 | 832 | // load because subsequent images might be fine; thus only single part |
michael@0 | 833 | // images end up here. |
michael@0 | 834 | this->Cancel(NS_ERROR_FAILURE); |
michael@0 | 835 | return NS_BINDING_ABORTED; |
michael@0 | 836 | } |
michael@0 | 837 | |
michael@0 | 838 | NS_ABORT_IF_FALSE(statusTracker->HasImage(), "Status tracker should have an image!"); |
michael@0 | 839 | NS_ABORT_IF_FALSE(mImage, "imgRequest should have an image!"); |
michael@0 | 840 | |
michael@0 | 841 | if (mDecodeRequested) |
michael@0 | 842 | mImage->StartDecoding(); |
michael@0 | 843 | } |
michael@0 | 844 | } |
michael@0 | 845 | |
michael@0 | 846 | // Notify the image that it has new data. |
michael@0 | 847 | rv = mImage->OnImageDataAvailable(aRequest, ctxt, inStr, sourceOffset, count); |
michael@0 | 848 | |
michael@0 | 849 | if (NS_FAILED(rv)) { |
michael@0 | 850 | PR_LOG(GetImgLog(), PR_LOG_WARNING, |
michael@0 | 851 | ("[this=%p] imgRequest::OnDataAvailable -- " |
michael@0 | 852 | "copy to RasterImage failed\n", this)); |
michael@0 | 853 | this->Cancel(NS_IMAGELIB_ERROR_FAILURE); |
michael@0 | 854 | return NS_BINDING_ABORTED; |
michael@0 | 855 | } |
michael@0 | 856 | |
michael@0 | 857 | return NS_OK; |
michael@0 | 858 | } |
michael@0 | 859 | |
michael@0 | 860 | class SetPropertiesEvent : public nsRunnable |
michael@0 | 861 | { |
michael@0 | 862 | public: |
michael@0 | 863 | SetPropertiesEvent(imgRequest* aImgRequest, nsIChannel* aChan) |
michael@0 | 864 | : mImgRequest(aImgRequest) |
michael@0 | 865 | , mChan(aChan) |
michael@0 | 866 | { |
michael@0 | 867 | MOZ_ASSERT(!NS_IsMainThread(), "Should be created off the main thread"); |
michael@0 | 868 | MOZ_ASSERT(aImgRequest, "aImgRequest cannot be null"); |
michael@0 | 869 | } |
michael@0 | 870 | NS_IMETHOD Run() |
michael@0 | 871 | { |
michael@0 | 872 | MOZ_ASSERT(NS_IsMainThread(), "Should run on the main thread only"); |
michael@0 | 873 | MOZ_ASSERT(mImgRequest, "mImgRequest cannot be null"); |
michael@0 | 874 | mImgRequest->SetProperties(mChan); |
michael@0 | 875 | return NS_OK; |
michael@0 | 876 | } |
michael@0 | 877 | private: |
michael@0 | 878 | nsRefPtr<imgRequest> mImgRequest; |
michael@0 | 879 | nsCOMPtr<nsIChannel> mChan; |
michael@0 | 880 | }; |
michael@0 | 881 | |
michael@0 | 882 | void |
michael@0 | 883 | imgRequest::SetProperties(nsIChannel* aChan) |
michael@0 | 884 | { |
michael@0 | 885 | // Force execution on main thread since some property objects are non |
michael@0 | 886 | // threadsafe. |
michael@0 | 887 | if (!NS_IsMainThread()) { |
michael@0 | 888 | NS_DispatchToMainThread(new SetPropertiesEvent(this, aChan)); |
michael@0 | 889 | return; |
michael@0 | 890 | } |
michael@0 | 891 | /* set our mimetype as a property */ |
michael@0 | 892 | nsCOMPtr<nsISupportsCString> contentType(do_CreateInstance("@mozilla.org/supports-cstring;1")); |
michael@0 | 893 | if (contentType) { |
michael@0 | 894 | contentType->SetData(mContentType); |
michael@0 | 895 | mProperties->Set("type", contentType); |
michael@0 | 896 | } |
michael@0 | 897 | |
michael@0 | 898 | /* set our content disposition as a property */ |
michael@0 | 899 | nsAutoCString disposition; |
michael@0 | 900 | if (aChan) { |
michael@0 | 901 | aChan->GetContentDispositionHeader(disposition); |
michael@0 | 902 | } |
michael@0 | 903 | if (!disposition.IsEmpty()) { |
michael@0 | 904 | nsCOMPtr<nsISupportsCString> contentDisposition(do_CreateInstance("@mozilla.org/supports-cstring;1")); |
michael@0 | 905 | if (contentDisposition) { |
michael@0 | 906 | contentDisposition->SetData(disposition); |
michael@0 | 907 | mProperties->Set("content-disposition", contentDisposition); |
michael@0 | 908 | } |
michael@0 | 909 | } |
michael@0 | 910 | } |
michael@0 | 911 | |
michael@0 | 912 | static NS_METHOD sniff_mimetype_callback(nsIInputStream* in, |
michael@0 | 913 | void* data, |
michael@0 | 914 | const char* fromRawSegment, |
michael@0 | 915 | uint32_t toOffset, |
michael@0 | 916 | uint32_t count, |
michael@0 | 917 | uint32_t *writeCount) |
michael@0 | 918 | { |
michael@0 | 919 | mimetype_closure* closure = static_cast<mimetype_closure*>(data); |
michael@0 | 920 | |
michael@0 | 921 | NS_ASSERTION(closure, "closure is null!"); |
michael@0 | 922 | |
michael@0 | 923 | if (count > 0) |
michael@0 | 924 | imgLoader::GetMimeTypeFromContent(fromRawSegment, count, *closure->newType); |
michael@0 | 925 | |
michael@0 | 926 | *writeCount = 0; |
michael@0 | 927 | return NS_ERROR_FAILURE; |
michael@0 | 928 | } |
michael@0 | 929 | |
michael@0 | 930 | |
michael@0 | 931 | /** nsIInterfaceRequestor methods **/ |
michael@0 | 932 | |
michael@0 | 933 | NS_IMETHODIMP |
michael@0 | 934 | imgRequest::GetInterface(const nsIID & aIID, void **aResult) |
michael@0 | 935 | { |
michael@0 | 936 | if (!mPrevChannelSink || aIID.Equals(NS_GET_IID(nsIChannelEventSink))) |
michael@0 | 937 | return QueryInterface(aIID, aResult); |
michael@0 | 938 | |
michael@0 | 939 | NS_ASSERTION(mPrevChannelSink != this, |
michael@0 | 940 | "Infinite recursion - don't keep track of channel sinks that are us!"); |
michael@0 | 941 | return mPrevChannelSink->GetInterface(aIID, aResult); |
michael@0 | 942 | } |
michael@0 | 943 | |
michael@0 | 944 | /** nsIChannelEventSink methods **/ |
michael@0 | 945 | NS_IMETHODIMP |
michael@0 | 946 | imgRequest::AsyncOnChannelRedirect(nsIChannel *oldChannel, |
michael@0 | 947 | nsIChannel *newChannel, uint32_t flags, |
michael@0 | 948 | nsIAsyncVerifyRedirectCallback *callback) |
michael@0 | 949 | { |
michael@0 | 950 | NS_ASSERTION(mRequest && mChannel, "Got a channel redirect after we nulled out mRequest!"); |
michael@0 | 951 | NS_ASSERTION(mChannel == oldChannel, "Got a channel redirect for an unknown channel!"); |
michael@0 | 952 | NS_ASSERTION(newChannel, "Got a redirect to a NULL channel!"); |
michael@0 | 953 | |
michael@0 | 954 | SetCacheValidation(mCacheEntry, oldChannel); |
michael@0 | 955 | |
michael@0 | 956 | // Prepare for callback |
michael@0 | 957 | mRedirectCallback = callback; |
michael@0 | 958 | mNewRedirectChannel = newChannel; |
michael@0 | 959 | |
michael@0 | 960 | nsCOMPtr<nsIChannelEventSink> sink(do_GetInterface(mPrevChannelSink)); |
michael@0 | 961 | if (sink) { |
michael@0 | 962 | nsresult rv = sink->AsyncOnChannelRedirect(oldChannel, newChannel, flags, |
michael@0 | 963 | this); |
michael@0 | 964 | if (NS_FAILED(rv)) { |
michael@0 | 965 | mRedirectCallback = nullptr; |
michael@0 | 966 | mNewRedirectChannel = nullptr; |
michael@0 | 967 | } |
michael@0 | 968 | return rv; |
michael@0 | 969 | } |
michael@0 | 970 | |
michael@0 | 971 | (void) OnRedirectVerifyCallback(NS_OK); |
michael@0 | 972 | return NS_OK; |
michael@0 | 973 | } |
michael@0 | 974 | |
michael@0 | 975 | NS_IMETHODIMP |
michael@0 | 976 | imgRequest::OnRedirectVerifyCallback(nsresult result) |
michael@0 | 977 | { |
michael@0 | 978 | NS_ASSERTION(mRedirectCallback, "mRedirectCallback not set in callback"); |
michael@0 | 979 | NS_ASSERTION(mNewRedirectChannel, "mNewRedirectChannel not set in callback"); |
michael@0 | 980 | |
michael@0 | 981 | if (NS_FAILED(result)) { |
michael@0 | 982 | mRedirectCallback->OnRedirectVerifyCallback(result); |
michael@0 | 983 | mRedirectCallback = nullptr; |
michael@0 | 984 | mNewRedirectChannel = nullptr; |
michael@0 | 985 | return NS_OK; |
michael@0 | 986 | } |
michael@0 | 987 | |
michael@0 | 988 | mChannel = mNewRedirectChannel; |
michael@0 | 989 | mTimedChannel = do_QueryInterface(mChannel); |
michael@0 | 990 | mNewRedirectChannel = nullptr; |
michael@0 | 991 | |
michael@0 | 992 | #if defined(PR_LOGGING) |
michael@0 | 993 | nsAutoCString oldspec; |
michael@0 | 994 | if (mCurrentURI) |
michael@0 | 995 | mCurrentURI->GetSpec(oldspec); |
michael@0 | 996 | LOG_MSG_WITH_PARAM(GetImgLog(), "imgRequest::OnChannelRedirect", "old", oldspec.get()); |
michael@0 | 997 | #endif |
michael@0 | 998 | |
michael@0 | 999 | // make sure we have a protocol that returns data rather than opens |
michael@0 | 1000 | // an external application, e.g. mailto: |
michael@0 | 1001 | mChannel->GetURI(getter_AddRefs(mCurrentURI)); |
michael@0 | 1002 | bool doesNotReturnData = false; |
michael@0 | 1003 | nsresult rv = |
michael@0 | 1004 | NS_URIChainHasFlags(mCurrentURI, nsIProtocolHandler::URI_DOES_NOT_RETURN_DATA, |
michael@0 | 1005 | &doesNotReturnData); |
michael@0 | 1006 | |
michael@0 | 1007 | if (NS_SUCCEEDED(rv) && doesNotReturnData) |
michael@0 | 1008 | rv = NS_ERROR_ABORT; |
michael@0 | 1009 | |
michael@0 | 1010 | if (NS_FAILED(rv)) { |
michael@0 | 1011 | mRedirectCallback->OnRedirectVerifyCallback(rv); |
michael@0 | 1012 | mRedirectCallback = nullptr; |
michael@0 | 1013 | return NS_OK; |
michael@0 | 1014 | } |
michael@0 | 1015 | |
michael@0 | 1016 | mRedirectCallback->OnRedirectVerifyCallback(NS_OK); |
michael@0 | 1017 | mRedirectCallback = nullptr; |
michael@0 | 1018 | return NS_OK; |
michael@0 | 1019 | } |