Bug 1492664 - update funsize scripts to use TASKCLUSTER_ROOT_URL; r=sfraser
[gecko.git] / image / imgRequestProxy.cpp
blob05118106a05c590088dc0c0ac2024ce4c9642df7
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "imgRequestProxy.h"
9 #include "ImageLogging.h"
10 #include "imgLoader.h"
11 #include "Image.h"
12 #include "ImageOps.h"
13 #include "nsError.h"
14 #include "nsCRTGlue.h"
15 #include "imgINotificationObserver.h"
16 #include "mozilla/dom/TabGroup.h" // for TabGroup
17 #include "mozilla/dom/DocGroup.h" // for DocGroup
18 #include "mozilla/Move.h"
19 #include "mozilla/Telemetry.h" // for Telemetry
21 using namespace mozilla;
22 using namespace mozilla::image;
24 // The split of imgRequestProxy and imgRequestProxyStatic means that
25 // certain overridden functions need to be usable in the destructor.
26 // Since virtual functions can't be used in that way, this class
27 // provides a behavioural trait for each class to use instead.
28 class ProxyBehaviour {
29 public:
30 virtual ~ProxyBehaviour() = default;
32 virtual already_AddRefed<mozilla::image::Image> GetImage() const = 0;
33 virtual bool HasImage() const = 0;
34 virtual already_AddRefed<ProgressTracker> GetProgressTracker() const = 0;
35 virtual imgRequest* GetOwner() const = 0;
36 virtual void SetOwner(imgRequest* aOwner) = 0;
39 class RequestBehaviour : public ProxyBehaviour {
40 public:
41 RequestBehaviour() : mOwner(nullptr), mOwnerHasImage(false) {}
43 already_AddRefed<mozilla::image::Image> GetImage() const override;
44 bool HasImage() const override;
45 already_AddRefed<ProgressTracker> GetProgressTracker() const override;
47 imgRequest* GetOwner() const override { return mOwner; }
49 void SetOwner(imgRequest* aOwner) override {
50 mOwner = aOwner;
52 if (mOwner) {
53 RefPtr<ProgressTracker> ownerProgressTracker = GetProgressTracker();
54 mOwnerHasImage = ownerProgressTracker && ownerProgressTracker->HasImage();
55 } else {
56 mOwnerHasImage = false;
60 private:
61 // We maintain the following invariant:
62 // The proxy is registered at most with a single imgRequest as an observer,
63 // and whenever it is, mOwner points to that object. This helps ensure that
64 // imgRequestProxy::~imgRequestProxy unregisters the proxy as an observer
65 // from whatever request it was registered with (if any). This, in turn,
66 // means that imgRequest::mObservers will not have any stale pointers in it.
67 RefPtr<imgRequest> mOwner;
69 bool mOwnerHasImage;
72 already_AddRefed<mozilla::image::Image> RequestBehaviour::GetImage() const {
73 if (!mOwnerHasImage) {
74 return nullptr;
76 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
77 return progressTracker->GetImage();
80 already_AddRefed<ProgressTracker> RequestBehaviour::GetProgressTracker() const {
81 // NOTE: It's possible that our mOwner has an Image that it didn't notify
82 // us about, if we were Canceled before its Image was constructed.
83 // (Canceling removes us as an observer, so mOwner has no way to notify us).
84 // That's why this method uses mOwner->GetProgressTracker() instead of just
85 // mOwner->mProgressTracker -- we might have a null mImage and yet have an
86 // mOwner with a non-null mImage (and a null mProgressTracker pointer).
87 return mOwner->GetProgressTracker();
90 NS_IMPL_ADDREF(imgRequestProxy)
91 NS_IMPL_RELEASE(imgRequestProxy)
93 NS_INTERFACE_MAP_BEGIN(imgRequestProxy)
94 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, imgIRequest)
95 NS_INTERFACE_MAP_ENTRY(imgIRequest)
96 NS_INTERFACE_MAP_ENTRY(nsIRequest)
97 NS_INTERFACE_MAP_ENTRY(nsISupportsPriority)
98 NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITimedChannel, TimedChannel() != nullptr)
99 NS_INTERFACE_MAP_END
101 imgRequestProxy::imgRequestProxy()
102 : mBehaviour(new RequestBehaviour),
103 mURI(nullptr),
104 mListener(nullptr),
105 mLoadFlags(nsIRequest::LOAD_NORMAL),
106 mLockCount(0),
107 mAnimationConsumers(0),
108 mCanceled(false),
109 mIsInLoadGroup(false),
110 mForceDispatchLoadGroup(false),
111 mListenerIsStrongRef(false),
112 mDecodeRequested(false),
113 mPendingNotify(false),
114 mValidating(false),
115 mHadListener(false),
116 mHadDispatch(false) {
117 /* member initializers and constructor code */
118 LOG_FUNC(gImgLog, "imgRequestProxy::imgRequestProxy");
121 imgRequestProxy::~imgRequestProxy() {
122 /* destructor code */
123 MOZ_ASSERT(!mListener, "Someone forgot to properly cancel this request!");
125 // If we had a listener, that means we would have issued notifications. With
126 // bug 1359833, we added support for main thread scheduler groups. Each
127 // imgRequestProxy may have its own associated listener, document and/or
128 // scheduler group. Typically most imgRequestProxy belong to the same
129 // document, or have no listener, which means we will want to execute all main
130 // thread code in that shared scheduler group. Less frequently, there may be
131 // multiple imgRequests and they have separate documents, which means that
132 // when we issue state notifications, some or all need to be dispatched to the
133 // appropriate scheduler group for each request. This should be rare, so we
134 // want to monitor the frequency of dispatching in the wild.
135 if (mHadListener) {
136 mozilla::Telemetry::Accumulate(mozilla::Telemetry::IMAGE_REQUEST_DISPATCHED,
137 mHadDispatch);
140 MOZ_RELEASE_ASSERT(!mLockCount, "Someone forgot to unlock on time?");
142 ClearAnimationConsumers();
144 // Explicitly set mListener to null to ensure that the RemoveProxy
145 // call below can't send |this| to an arbitrary listener while |this|
146 // is being destroyed. This is all belt-and-suspenders in view of the
147 // above assert.
148 NullOutListener();
150 /* Call RemoveProxy with a successful status. This will keep the
151 channel, if still downloading data, from being canceled if 'this' is
152 the last observer. This allows the image to continue to download and
153 be cached even if no one is using it currently.
155 mCanceled = true;
156 RemoveFromOwner(NS_OK);
158 RemoveFromLoadGroup();
159 LOG_FUNC(gImgLog, "imgRequestProxy::~imgRequestProxy");
162 nsresult imgRequestProxy::Init(imgRequest* aOwner, nsILoadGroup* aLoadGroup,
163 nsIDocument* aLoadingDocument, nsIURI* aURI,
164 imgINotificationObserver* aObserver) {
165 MOZ_ASSERT(!GetOwner() && !mListener,
166 "imgRequestProxy is already initialized");
168 LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequestProxy::Init", "request", aOwner);
170 MOZ_ASSERT(mAnimationConsumers == 0, "Cannot have animation before Init");
172 mBehaviour->SetOwner(aOwner);
173 mListener = aObserver;
174 // Make sure to addref mListener before the AddToOwner call below, since
175 // that call might well want to release it if the imgRequest has
176 // already seen OnStopRequest.
177 if (mListener) {
178 mHadListener = true;
179 mListenerIsStrongRef = true;
180 NS_ADDREF(mListener);
182 mLoadGroup = aLoadGroup;
183 mURI = aURI;
185 // Note: AddToOwner won't send all the On* notifications immediately
186 AddToOwner(aLoadingDocument);
188 return NS_OK;
191 nsresult imgRequestProxy::ChangeOwner(imgRequest* aNewOwner) {
192 MOZ_ASSERT(GetOwner(), "Cannot ChangeOwner on a proxy without an owner!");
194 if (mCanceled) {
195 // Ensure that this proxy has received all notifications to date
196 // before we clean it up when removing it from the old owner below.
197 SyncNotifyListener();
200 // If we're holding locks, unlock the old image.
201 // Note that UnlockImage decrements mLockCount each time it's called.
202 uint32_t oldLockCount = mLockCount;
203 while (mLockCount) {
204 UnlockImage();
207 // If we're holding animation requests, undo them.
208 uint32_t oldAnimationConsumers = mAnimationConsumers;
209 ClearAnimationConsumers();
211 GetOwner()->RemoveProxy(this, NS_OK);
213 mBehaviour->SetOwner(aNewOwner);
214 MOZ_ASSERT(!GetValidator(), "New owner cannot be validating!");
216 // If we were locked, apply the locks here
217 for (uint32_t i = 0; i < oldLockCount; i++) {
218 LockImage();
221 // If we had animation requests, restore them here. Note that we
222 // do this *after* RemoveProxy, which clears out animation consumers
223 // (see bug 601723).
224 for (uint32_t i = 0; i < oldAnimationConsumers; i++) {
225 IncrementAnimationConsumers();
228 AddToOwner(nullptr);
229 return NS_OK;
232 void imgRequestProxy::MarkValidating() {
233 MOZ_ASSERT(GetValidator());
234 mValidating = true;
237 void imgRequestProxy::ClearValidating() {
238 MOZ_ASSERT(mValidating);
239 MOZ_ASSERT(!GetValidator());
240 mValidating = false;
242 // If we'd previously requested a synchronous decode, request a decode on the
243 // new image.
244 if (mDecodeRequested) {
245 mDecodeRequested = false;
246 StartDecoding(imgIContainer::FLAG_NONE);
250 bool imgRequestProxy::IsOnEventTarget() const {
251 // Ensure we are in some main thread context because the scheduler group
252 // methods are only safe to call on the main thread.
253 MOZ_ASSERT(NS_IsMainThread());
255 if (mTabGroup) {
256 MOZ_ASSERT(mEventTarget);
257 return mTabGroup->IsSafeToRun();
260 if (mListener) {
261 // If we have no scheduler group but we do have a listener, then we know
262 // that the listener requires unlabelled dispatch.
263 MOZ_ASSERT(mEventTarget);
264 return mozilla::SchedulerGroup::IsSafeToRunUnlabeled();
267 // No listener means it is always safe, as there is nothing to do.
268 return true;
271 already_AddRefed<nsIEventTarget> imgRequestProxy::GetEventTarget() const {
272 nsCOMPtr<nsIEventTarget> target(mEventTarget);
273 return target.forget();
276 nsresult imgRequestProxy::DispatchWithTargetIfAvailable(
277 already_AddRefed<nsIRunnable> aEvent) {
278 LOG_FUNC(gImgLog, "imgRequestProxy::DispatchWithTargetIfAvailable");
280 // This method should only be used when it is *expected* that we are
281 // dispatching an event (e.g. we want to handle an event asynchronously)
282 // rather we need to (e.g. we are in the wrong scheduler group context).
283 // As such, we do not set mHadDispatch for telemetry purposes.
284 if (mEventTarget) {
285 mEventTarget->Dispatch(std::move(aEvent), NS_DISPATCH_NORMAL);
286 return NS_OK;
289 return NS_DispatchToMainThread(std::move(aEvent));
292 void imgRequestProxy::DispatchWithTarget(already_AddRefed<nsIRunnable> aEvent) {
293 LOG_FUNC(gImgLog, "imgRequestProxy::DispatchWithTarget");
295 MOZ_ASSERT(mListener || mTabGroup);
296 MOZ_ASSERT(mEventTarget);
298 mHadDispatch = true;
299 mEventTarget->Dispatch(std::move(aEvent), NS_DISPATCH_NORMAL);
302 void imgRequestProxy::AddToOwner(nsIDocument* aLoadingDocument) {
303 // An imgRequestProxy can be initialized with neither a listener nor a
304 // document. The caller could follow up later by cloning the canonical
305 // imgRequestProxy with the actual listener. This is possible because
306 // imgLoader::LoadImage does not require a valid listener to be provided.
308 // Without a listener, we don't need to set our scheduler group, because
309 // we have nothing to signal. However if we were told what document this
310 // is for, it is likely that future listeners will belong to the same
311 // scheduler group.
313 // With a listener, we always need to update our scheduler group. A null
314 // scheduler group is valid with or without a document, but that means
315 // we will use the most generic event target possible on dispatch.
316 if (aLoadingDocument) {
317 RefPtr<mozilla::dom::DocGroup> docGroup = aLoadingDocument->GetDocGroup();
318 if (docGroup) {
319 mTabGroup = docGroup->GetTabGroup();
320 MOZ_ASSERT(mTabGroup);
322 mEventTarget = docGroup->EventTargetFor(mozilla::TaskCategory::Other);
323 MOZ_ASSERT(mEventTarget);
327 if (mListener && !mEventTarget) {
328 mEventTarget = do_GetMainThread();
331 imgRequest* owner = GetOwner();
332 if (!owner) {
333 return;
336 owner->AddProxy(this);
339 void imgRequestProxy::RemoveFromOwner(nsresult aStatus) {
340 imgRequest* owner = GetOwner();
341 if (owner) {
342 if (mValidating) {
343 imgCacheValidator* validator = owner->GetValidator();
344 MOZ_ASSERT(validator);
345 validator->RemoveProxy(this);
346 mValidating = false;
349 owner->RemoveProxy(this, aStatus);
353 void imgRequestProxy::AddToLoadGroup() {
354 NS_ASSERTION(!mIsInLoadGroup, "Whaa, we're already in the loadgroup!");
355 MOZ_ASSERT(!mForceDispatchLoadGroup);
357 /* While in theory there could be a dispatch outstanding to remove this
358 request from the load group, in practice we only add to the load group
359 (when previously not in a load group) at initialization. */
360 if (!mIsInLoadGroup && mLoadGroup) {
361 LOG_FUNC(gImgLog, "imgRequestProxy::AddToLoadGroup");
362 mLoadGroup->AddRequest(this, nullptr);
363 mIsInLoadGroup = true;
367 void imgRequestProxy::RemoveFromLoadGroup() {
368 if (!mIsInLoadGroup || !mLoadGroup) {
369 return;
372 /* Sometimes we may not be able to remove ourselves from the load group in
373 the current context. This is because our listeners are not re-entrant (e.g.
374 we are in the middle of CancelAndForgetObserver or SyncClone). */
375 if (mForceDispatchLoadGroup) {
376 LOG_FUNC(gImgLog, "imgRequestProxy::RemoveFromLoadGroup -- dispatch");
378 /* We take away the load group from the request temporarily; this prevents
379 additional dispatches via RemoveFromLoadGroup occurring, as well as
380 MoveToBackgroundInLoadGroup from removing and readding. This is safe
381 because we know that once we get here, blocking the load group at all is
382 unnecessary. */
383 mIsInLoadGroup = false;
384 nsCOMPtr<nsILoadGroup> loadGroup = std::move(mLoadGroup);
385 RefPtr<imgRequestProxy> self(this);
386 DispatchWithTargetIfAvailable(NS_NewRunnableFunction(
387 "imgRequestProxy::RemoveFromLoadGroup", [self, loadGroup]() -> void {
388 loadGroup->RemoveRequest(self, nullptr, NS_OK);
389 }));
390 return;
393 LOG_FUNC(gImgLog, "imgRequestProxy::RemoveFromLoadGroup");
395 /* calling RemoveFromLoadGroup may cause the document to finish
396 loading, which could result in our death. We need to make sure
397 that we stay alive long enough to fight another battle... at
398 least until we exit this function. */
399 nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
400 mLoadGroup->RemoveRequest(this, nullptr, NS_OK);
401 mLoadGroup = nullptr;
402 mIsInLoadGroup = false;
405 void imgRequestProxy::MoveToBackgroundInLoadGroup() {
406 /* Even if we are still in the load group, we may have taken away the load
407 group reference itself because we are in the process of leaving the group.
408 In that case, there is no need to background the request. */
409 if (!mLoadGroup) {
410 return;
413 /* There is no need to dispatch if we only need to add ourselves to the load
414 group without removal. It is the removal which causes the problematic
415 callbacks (see RemoveFromLoadGroup). */
416 if (mIsInLoadGroup && mForceDispatchLoadGroup) {
417 LOG_FUNC(gImgLog,
418 "imgRequestProxy::MoveToBackgroundInLoadGroup -- dispatch");
420 RefPtr<imgRequestProxy> self(this);
421 DispatchWithTargetIfAvailable(NS_NewRunnableFunction(
422 "imgRequestProxy::MoveToBackgroundInLoadGroup",
423 [self]() -> void { self->MoveToBackgroundInLoadGroup(); }));
424 return;
427 LOG_FUNC(gImgLog, "imgRequestProxy::MoveToBackgroundInLoadGroup");
428 nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
429 if (mIsInLoadGroup) {
430 mLoadGroup->RemoveRequest(this, nullptr, NS_OK);
433 mLoadFlags |= nsIRequest::LOAD_BACKGROUND;
434 mLoadGroup->AddRequest(this, nullptr);
437 /** nsIRequest / imgIRequest methods **/
439 NS_IMETHODIMP
440 imgRequestProxy::GetName(nsACString& aName) {
441 aName.Truncate();
443 if (mURI) {
444 mURI->GetSpec(aName);
447 return NS_OK;
450 NS_IMETHODIMP
451 imgRequestProxy::IsPending(bool* _retval) { return NS_ERROR_NOT_IMPLEMENTED; }
453 NS_IMETHODIMP
454 imgRequestProxy::GetStatus(nsresult* aStatus) {
455 return NS_ERROR_NOT_IMPLEMENTED;
458 NS_IMETHODIMP
459 imgRequestProxy::Cancel(nsresult status) {
460 if (mCanceled) {
461 return NS_ERROR_FAILURE;
464 LOG_SCOPE(gImgLog, "imgRequestProxy::Cancel");
466 mCanceled = true;
468 nsCOMPtr<nsIRunnable> ev = new imgCancelRunnable(this, status);
469 return DispatchWithTargetIfAvailable(ev.forget());
472 void imgRequestProxy::DoCancel(nsresult status) {
473 RemoveFromOwner(status);
474 RemoveFromLoadGroup();
475 NullOutListener();
478 NS_IMETHODIMP
479 imgRequestProxy::CancelAndForgetObserver(nsresult aStatus) {
480 // If mCanceled is true but mListener is non-null, that means
481 // someone called Cancel() on us but the imgCancelRunnable is still
482 // pending. We still need to null out mListener before returning
483 // from this function in this case. That means we want to do the
484 // RemoveProxy call right now, because we need to deliver the
485 // onStopRequest.
486 if (mCanceled && !mListener) {
487 return NS_ERROR_FAILURE;
490 LOG_SCOPE(gImgLog, "imgRequestProxy::CancelAndForgetObserver");
492 mCanceled = true;
493 mForceDispatchLoadGroup = true;
494 RemoveFromOwner(aStatus);
495 RemoveFromLoadGroup();
496 mForceDispatchLoadGroup = false;
498 NullOutListener();
500 return NS_OK;
503 NS_IMETHODIMP
504 imgRequestProxy::StartDecoding(uint32_t aFlags) {
505 // Flag this, so we know to request after validation if pending.
506 if (IsValidating()) {
507 mDecodeRequested = true;
508 return NS_OK;
511 RefPtr<Image> image = GetImage();
512 if (image) {
513 return image->StartDecoding(aFlags);
516 if (GetOwner()) {
517 GetOwner()->StartDecoding();
520 return NS_OK;
523 bool imgRequestProxy::StartDecodingWithResult(uint32_t aFlags) {
524 // Flag this, so we know to request after validation if pending.
525 if (IsValidating()) {
526 mDecodeRequested = true;
527 return false;
530 RefPtr<Image> image = GetImage();
531 if (image) {
532 return image->StartDecodingWithResult(aFlags);
535 if (GetOwner()) {
536 GetOwner()->StartDecoding();
539 return false;
542 NS_IMETHODIMP
543 imgRequestProxy::LockImage() {
544 mLockCount++;
545 RefPtr<Image> image = GetImage();
546 if (image) {
547 return image->LockImage();
549 return NS_OK;
552 NS_IMETHODIMP
553 imgRequestProxy::UnlockImage() {
554 MOZ_ASSERT(mLockCount > 0, "calling unlock but no locks!");
556 mLockCount--;
557 RefPtr<Image> image = GetImage();
558 if (image) {
559 return image->UnlockImage();
561 return NS_OK;
564 NS_IMETHODIMP
565 imgRequestProxy::RequestDiscard() {
566 RefPtr<Image> image = GetImage();
567 if (image) {
568 return image->RequestDiscard();
570 return NS_OK;
573 NS_IMETHODIMP
574 imgRequestProxy::IncrementAnimationConsumers() {
575 mAnimationConsumers++;
576 RefPtr<Image> image = GetImage();
577 if (image) {
578 image->IncrementAnimationConsumers();
580 return NS_OK;
583 NS_IMETHODIMP
584 imgRequestProxy::DecrementAnimationConsumers() {
585 // We may get here if some responsible code called Increment,
586 // then called us, but we have meanwhile called ClearAnimationConsumers
587 // because we needed to get rid of them earlier (see
588 // imgRequest::RemoveProxy), and hence have nothing left to
589 // decrement. (In such a case we got rid of the animation consumers
590 // early, but not the observer.)
591 if (mAnimationConsumers > 0) {
592 mAnimationConsumers--;
593 RefPtr<Image> image = GetImage();
594 if (image) {
595 image->DecrementAnimationConsumers();
598 return NS_OK;
601 void imgRequestProxy::ClearAnimationConsumers() {
602 while (mAnimationConsumers > 0) {
603 DecrementAnimationConsumers();
607 NS_IMETHODIMP
608 imgRequestProxy::Suspend() { return NS_ERROR_NOT_IMPLEMENTED; }
610 NS_IMETHODIMP
611 imgRequestProxy::Resume() { return NS_ERROR_NOT_IMPLEMENTED; }
613 NS_IMETHODIMP
614 imgRequestProxy::GetLoadGroup(nsILoadGroup** loadGroup) {
615 NS_IF_ADDREF(*loadGroup = mLoadGroup.get());
616 return NS_OK;
618 NS_IMETHODIMP
619 imgRequestProxy::SetLoadGroup(nsILoadGroup* loadGroup) {
620 if (loadGroup != mLoadGroup) {
621 MOZ_ASSERT_UNREACHABLE("Switching load groups is unsupported!");
622 return NS_ERROR_NOT_IMPLEMENTED;
624 return NS_OK;
627 NS_IMETHODIMP
628 imgRequestProxy::GetLoadFlags(nsLoadFlags* flags) {
629 *flags = mLoadFlags;
630 return NS_OK;
632 NS_IMETHODIMP
633 imgRequestProxy::SetLoadFlags(nsLoadFlags flags) {
634 mLoadFlags = flags;
635 return NS_OK;
638 /** imgIRequest methods **/
640 NS_IMETHODIMP
641 imgRequestProxy::GetImage(imgIContainer** aImage) {
642 NS_ENSURE_TRUE(aImage, NS_ERROR_NULL_POINTER);
643 // It's possible that our owner has an image but hasn't notified us of it -
644 // that'll happen if we get Canceled before the owner instantiates its image
645 // (because Canceling unregisters us as a listener on mOwner). If we're
646 // in that situation, just grab the image off of mOwner.
647 RefPtr<Image> image = GetImage();
648 nsCOMPtr<imgIContainer> imageToReturn;
649 if (image) {
650 imageToReturn = image;
652 if (!imageToReturn && GetOwner()) {
653 imageToReturn = GetOwner()->GetImage();
655 if (!imageToReturn) {
656 return NS_ERROR_FAILURE;
659 imageToReturn.swap(*aImage);
661 return NS_OK;
664 NS_IMETHODIMP
665 imgRequestProxy::GetImageStatus(uint32_t* aStatus) {
666 if (IsValidating()) {
667 // We are currently validating the image, and so our status could revert if
668 // we discard the cache. We should also be deferring notifications, such
669 // that the caller will be notified when validation completes. Rather than
670 // risk misleading the caller, return nothing.
671 *aStatus = imgIRequest::STATUS_NONE;
672 } else {
673 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
674 *aStatus = progressTracker->GetImageStatus();
677 return NS_OK;
680 NS_IMETHODIMP
681 imgRequestProxy::GetImageErrorCode(nsresult* aStatus) {
682 if (!GetOwner()) {
683 return NS_ERROR_FAILURE;
686 *aStatus = GetOwner()->GetImageErrorCode();
688 return NS_OK;
691 NS_IMETHODIMP
692 imgRequestProxy::GetURI(nsIURI** aURI) {
693 MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread to convert URI");
694 nsCOMPtr<nsIURI> uri = mURI;
695 uri.forget(aURI);
696 return NS_OK;
699 nsresult imgRequestProxy::GetFinalURI(nsIURI** aURI) {
700 if (!GetOwner()) {
701 return NS_ERROR_FAILURE;
704 return GetOwner()->GetFinalURI(aURI);
707 NS_IMETHODIMP
708 imgRequestProxy::GetNotificationObserver(imgINotificationObserver** aObserver) {
709 *aObserver = mListener;
710 NS_IF_ADDREF(*aObserver);
711 return NS_OK;
714 NS_IMETHODIMP
715 imgRequestProxy::GetMimeType(char** aMimeType) {
716 if (!GetOwner()) {
717 return NS_ERROR_FAILURE;
720 const char* type = GetOwner()->GetMimeType();
721 if (!type) {
722 return NS_ERROR_FAILURE;
725 *aMimeType = NS_xstrdup(type);
727 return NS_OK;
730 imgRequestProxy* imgRequestProxy::NewClonedProxy() {
731 return new imgRequestProxy();
734 NS_IMETHODIMP
735 imgRequestProxy::Clone(imgINotificationObserver* aObserver,
736 imgIRequest** aClone) {
737 nsresult result;
738 imgRequestProxy* proxy;
739 result = PerformClone(aObserver, nullptr, /* aSyncNotify */ true, &proxy);
740 *aClone = proxy;
741 return result;
744 nsresult imgRequestProxy::SyncClone(imgINotificationObserver* aObserver,
745 nsIDocument* aLoadingDocument,
746 imgRequestProxy** aClone) {
747 return PerformClone(aObserver, aLoadingDocument,
748 /* aSyncNotify */ true, aClone);
751 nsresult imgRequestProxy::Clone(imgINotificationObserver* aObserver,
752 nsIDocument* aLoadingDocument,
753 imgRequestProxy** aClone) {
754 return PerformClone(aObserver, aLoadingDocument,
755 /* aSyncNotify */ false, aClone);
758 nsresult imgRequestProxy::PerformClone(imgINotificationObserver* aObserver,
759 nsIDocument* aLoadingDocument,
760 bool aSyncNotify,
761 imgRequestProxy** aClone) {
762 MOZ_ASSERT(aClone, "Null out param");
764 LOG_SCOPE(gImgLog, "imgRequestProxy::Clone");
766 *aClone = nullptr;
767 RefPtr<imgRequestProxy> clone = NewClonedProxy();
769 nsCOMPtr<nsILoadGroup> loadGroup;
770 if (aLoadingDocument) {
771 loadGroup = aLoadingDocument->GetDocumentLoadGroup();
774 // It is important to call |SetLoadFlags()| before calling |Init()| because
775 // |Init()| adds the request to the loadgroup.
776 // When a request is added to a loadgroup, its load flags are merged
777 // with the load flags of the loadgroup.
778 // XXXldb That's not true anymore. Stuff from imgLoader adds the
779 // request to the loadgroup.
780 clone->SetLoadFlags(mLoadFlags);
781 nsresult rv = clone->Init(mBehaviour->GetOwner(), loadGroup, aLoadingDocument,
782 mURI, aObserver);
783 if (NS_FAILED(rv)) {
784 return rv;
787 // Assign to *aClone before calling Notify so that if the caller expects to
788 // only be notified for requests it's already holding pointers to it won't be
789 // surprised.
790 NS_ADDREF(*aClone = clone);
792 imgCacheValidator* validator = GetValidator();
793 if (validator) {
794 // Note that if we have a validator, we don't want to issue notifications at
795 // here because we want to defer until that completes. AddProxy will add us
796 // to the load group; we cannot avoid that in this case, because we don't
797 // know when the validation will complete, and if it will cause us to
798 // discard our cached state anyways. We are probably already blocked by the
799 // original LoadImage(WithChannel) request in any event.
800 clone->MarkValidating();
801 validator->AddProxy(clone);
802 } else {
803 // We only want to add the request to the load group of the owning document
804 // if it is still in progress. Some callers cannot handle a supurious load
805 // group removal (e.g. print preview) so we must be careful. On the other
806 // hand, if after cloning, the original request proxy is cancelled /
807 // destroyed, we need to ensure that any clones still block the load group
808 // if it is incomplete.
809 bool addToLoadGroup = mIsInLoadGroup;
810 if (!addToLoadGroup) {
811 RefPtr<ProgressTracker> tracker = clone->GetProgressTracker();
812 addToLoadGroup =
813 tracker && !(tracker->GetProgress() & FLAG_LOAD_COMPLETE);
816 if (addToLoadGroup) {
817 clone->AddToLoadGroup();
820 if (aSyncNotify) {
821 // This is wrong!!! We need to notify asynchronously, but there's code
822 // that assumes that we don't. This will be fixed in bug 580466. Note that
823 // if we have a validator, we won't issue notifications anyways because
824 // they are deferred, so there is no point in requesting.
825 clone->mForceDispatchLoadGroup = true;
826 clone->SyncNotifyListener();
827 clone->mForceDispatchLoadGroup = false;
828 } else {
829 // Without a validator, we can request asynchronous notifications
830 // immediately. If there was a validator, this would override the deferral
831 // and that would be incorrect.
832 clone->NotifyListener();
836 return NS_OK;
839 NS_IMETHODIMP
840 imgRequestProxy::GetImagePrincipal(nsIPrincipal** aPrincipal) {
841 if (!GetOwner()) {
842 return NS_ERROR_FAILURE;
845 nsCOMPtr<nsIPrincipal> principal = GetOwner()->GetPrincipal();
846 principal.forget(aPrincipal);
847 return NS_OK;
850 NS_IMETHODIMP
851 imgRequestProxy::GetMultipart(bool* aMultipart) {
852 if (!GetOwner()) {
853 return NS_ERROR_FAILURE;
856 *aMultipart = GetOwner()->GetMultipart();
858 return NS_OK;
861 NS_IMETHODIMP
862 imgRequestProxy::GetCORSMode(int32_t* aCorsMode) {
863 if (!GetOwner()) {
864 return NS_ERROR_FAILURE;
867 *aCorsMode = GetOwner()->GetCORSMode();
869 return NS_OK;
872 NS_IMETHODIMP
873 imgRequestProxy::BoostPriority(uint32_t aCategory) {
874 NS_ENSURE_STATE(GetOwner() && !mCanceled);
875 GetOwner()->BoostPriority(aCategory);
876 return NS_OK;
879 /** nsISupportsPriority methods **/
881 NS_IMETHODIMP
882 imgRequestProxy::GetPriority(int32_t* priority) {
883 NS_ENSURE_STATE(GetOwner());
884 *priority = GetOwner()->Priority();
885 return NS_OK;
888 NS_IMETHODIMP
889 imgRequestProxy::SetPriority(int32_t priority) {
890 NS_ENSURE_STATE(GetOwner() && !mCanceled);
891 GetOwner()->AdjustPriority(this, priority - GetOwner()->Priority());
892 return NS_OK;
895 NS_IMETHODIMP
896 imgRequestProxy::AdjustPriority(int32_t priority) {
897 // We don't require |!mCanceled| here. This may be called even if we're
898 // cancelled, because it's invoked as part of the process of removing an image
899 // from the load group.
900 NS_ENSURE_STATE(GetOwner());
901 GetOwner()->AdjustPriority(this, priority);
902 return NS_OK;
905 static const char* NotificationTypeToString(int32_t aType) {
906 switch (aType) {
907 case imgINotificationObserver::SIZE_AVAILABLE:
908 return "SIZE_AVAILABLE";
909 case imgINotificationObserver::FRAME_UPDATE:
910 return "FRAME_UPDATE";
911 case imgINotificationObserver::FRAME_COMPLETE:
912 return "FRAME_COMPLETE";
913 case imgINotificationObserver::LOAD_COMPLETE:
914 return "LOAD_COMPLETE";
915 case imgINotificationObserver::DECODE_COMPLETE:
916 return "DECODE_COMPLETE";
917 case imgINotificationObserver::DISCARD:
918 return "DISCARD";
919 case imgINotificationObserver::UNLOCKED_DRAW:
920 return "UNLOCKED_DRAW";
921 case imgINotificationObserver::IS_ANIMATED:
922 return "IS_ANIMATED";
923 case imgINotificationObserver::HAS_TRANSPARENCY:
924 return "HAS_TRANSPARENCY";
925 default:
926 MOZ_ASSERT_UNREACHABLE("Notification list should be exhaustive");
927 return "(unknown notification)";
931 void imgRequestProxy::Notify(int32_t aType,
932 const mozilla::gfx::IntRect* aRect) {
933 MOZ_ASSERT(aType != imgINotificationObserver::LOAD_COMPLETE,
934 "Should call OnLoadComplete");
936 LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::Notify", "type",
937 NotificationTypeToString(aType));
939 if (!mListener || mCanceled) {
940 return;
943 if (!IsOnEventTarget()) {
944 RefPtr<imgRequestProxy> self(this);
945 if (aRect) {
946 const mozilla::gfx::IntRect rect = *aRect;
947 DispatchWithTarget(NS_NewRunnableFunction(
948 "imgRequestProxy::Notify",
949 [self, rect, aType]() -> void { self->Notify(aType, &rect); }));
950 } else {
951 DispatchWithTarget(NS_NewRunnableFunction(
952 "imgRequestProxy::Notify",
953 [self, aType]() -> void { self->Notify(aType, nullptr); }));
955 return;
958 // Make sure the listener stays alive while we notify.
959 nsCOMPtr<imgINotificationObserver> listener(mListener);
961 listener->Notify(this, aType, aRect);
964 void imgRequestProxy::OnLoadComplete(bool aLastPart) {
965 LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::OnLoadComplete", "uri", mURI);
967 // There's all sorts of stuff here that could kill us (the OnStopRequest call
968 // on the listener, the removal from the loadgroup, the release of the
969 // listener, etc). Don't let them do it.
970 RefPtr<imgRequestProxy> self(this);
972 if (!IsOnEventTarget()) {
973 DispatchWithTarget(NS_NewRunnableFunction(
974 "imgRequestProxy::OnLoadComplete",
975 [self, aLastPart]() -> void { self->OnLoadComplete(aLastPart); }));
976 return;
979 if (mListener && !mCanceled) {
980 // Hold a ref to the listener while we call it, just in case.
981 nsCOMPtr<imgINotificationObserver> listener(mListener);
982 listener->Notify(this, imgINotificationObserver::LOAD_COMPLETE, nullptr);
985 // If we're expecting more data from a multipart channel, re-add ourself
986 // to the loadgroup so that the document doesn't lose track of the load.
987 // If the request is already a background request and there's more data
988 // coming, we can just leave the request in the loadgroup as-is.
989 if (aLastPart || (mLoadFlags & nsIRequest::LOAD_BACKGROUND) == 0) {
990 if (aLastPart) {
991 RemoveFromLoadGroup();
992 } else {
993 // More data is coming, so change the request to be a background request
994 // and put it back in the loadgroup.
995 MoveToBackgroundInLoadGroup();
999 if (mListenerIsStrongRef && aLastPart) {
1000 MOZ_ASSERT(mListener, "How did that happen?");
1001 // Drop our strong ref to the listener now that we're done with
1002 // everything. Note that this can cancel us and other fun things
1003 // like that. Don't add anything in this method after this point.
1004 imgINotificationObserver* obs = mListener;
1005 mListenerIsStrongRef = false;
1006 NS_RELEASE(obs);
1010 void imgRequestProxy::NullOutListener() {
1011 // If we have animation consumers, then they don't matter anymore
1012 if (mListener) {
1013 ClearAnimationConsumers();
1016 if (mListenerIsStrongRef) {
1017 // Releasing could do weird reentery stuff, so just play it super-safe
1018 nsCOMPtr<imgINotificationObserver> obs;
1019 obs.swap(mListener);
1020 mListenerIsStrongRef = false;
1021 } else {
1022 mListener = nullptr;
1025 // Note that we don't free the event target. We actually need that to ensure
1026 // we get removed from the ProgressTracker properly. No harm in keeping it
1027 // however.
1028 mTabGroup = nullptr;
1031 NS_IMETHODIMP
1032 imgRequestProxy::GetStaticRequest(imgIRequest** aReturn) {
1033 imgRequestProxy* proxy;
1034 nsresult result = GetStaticRequest(nullptr, &proxy);
1035 *aReturn = proxy;
1036 return result;
1039 nsresult imgRequestProxy::GetStaticRequest(nsIDocument* aLoadingDocument,
1040 imgRequestProxy** aReturn) {
1041 *aReturn = nullptr;
1042 RefPtr<Image> image = GetImage();
1044 bool animated;
1045 if (!image || (NS_SUCCEEDED(image->GetAnimated(&animated)) && !animated)) {
1046 // Early exit - we're not animated, so we don't have to do anything.
1047 NS_ADDREF(*aReturn = this);
1048 return NS_OK;
1051 // Check for errors in the image. Callers code rely on GetStaticRequest
1052 // failing in this case, though with FrozenImage there's no technical reason
1053 // for it anymore.
1054 if (image->HasError()) {
1055 return NS_ERROR_FAILURE;
1058 // We are animated. We need to create a frozen version of this image.
1059 RefPtr<Image> frozenImage = ImageOps::Freeze(image);
1061 // Create a static imgRequestProxy with our new extracted frame.
1062 nsCOMPtr<nsIPrincipal> currentPrincipal;
1063 GetImagePrincipal(getter_AddRefs(currentPrincipal));
1064 RefPtr<imgRequestProxy> req =
1065 new imgRequestProxyStatic(frozenImage, currentPrincipal);
1066 req->Init(nullptr, nullptr, aLoadingDocument, mURI, nullptr);
1068 NS_ADDREF(*aReturn = req);
1070 return NS_OK;
1073 void imgRequestProxy::NotifyListener() {
1074 // It would be nice to notify the observer directly in the status tracker
1075 // instead of through the proxy, but there are several places we do extra
1076 // processing when we receive notifications (like OnStopRequest()), and we
1077 // need to check mCanceled everywhere too.
1079 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
1080 if (GetOwner()) {
1081 // Send the notifications to our listener asynchronously.
1082 progressTracker->Notify(this);
1083 } else {
1084 // We don't have an imgRequest, so we can only notify the clone of our
1085 // current state, but we still have to do that asynchronously.
1086 MOZ_ASSERT(HasImage(), "if we have no imgRequest, we should have an Image");
1087 progressTracker->NotifyCurrentState(this);
1091 void imgRequestProxy::SyncNotifyListener() {
1092 // It would be nice to notify the observer directly in the status tracker
1093 // instead of through the proxy, but there are several places we do extra
1094 // processing when we receive notifications (like OnStopRequest()), and we
1095 // need to check mCanceled everywhere too.
1097 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
1098 progressTracker->SyncNotify(this);
1101 void imgRequestProxy::SetHasImage() {
1102 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
1103 MOZ_ASSERT(progressTracker);
1104 RefPtr<Image> image = progressTracker->GetImage();
1105 MOZ_ASSERT(image);
1107 // Force any private status related to the owner to reflect
1108 // the presence of an image;
1109 mBehaviour->SetOwner(mBehaviour->GetOwner());
1111 // Apply any locks we have
1112 for (uint32_t i = 0; i < mLockCount; ++i) {
1113 image->LockImage();
1116 // Apply any animation consumers we have
1117 for (uint32_t i = 0; i < mAnimationConsumers; i++) {
1118 image->IncrementAnimationConsumers();
1122 already_AddRefed<ProgressTracker> imgRequestProxy::GetProgressTracker() const {
1123 return mBehaviour->GetProgressTracker();
1126 already_AddRefed<mozilla::image::Image> imgRequestProxy::GetImage() const {
1127 return mBehaviour->GetImage();
1130 bool RequestBehaviour::HasImage() const {
1131 if (!mOwnerHasImage) {
1132 return false;
1134 RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
1135 return progressTracker ? progressTracker->HasImage() : false;
1138 bool imgRequestProxy::HasImage() const { return mBehaviour->HasImage(); }
1140 imgRequest* imgRequestProxy::GetOwner() const { return mBehaviour->GetOwner(); }
1142 imgCacheValidator* imgRequestProxy::GetValidator() const {
1143 imgRequest* owner = GetOwner();
1144 if (!owner) {
1145 return nullptr;
1147 return owner->GetValidator();
1150 ////////////////// imgRequestProxyStatic methods
1152 class StaticBehaviour : public ProxyBehaviour {
1153 public:
1154 explicit StaticBehaviour(mozilla::image::Image* aImage) : mImage(aImage) {}
1156 already_AddRefed<mozilla::image::Image> GetImage() const override {
1157 RefPtr<mozilla::image::Image> image = mImage;
1158 return image.forget();
1161 bool HasImage() const override { return mImage; }
1163 already_AddRefed<ProgressTracker> GetProgressTracker() const override {
1164 return mImage->GetProgressTracker();
1167 imgRequest* GetOwner() const override { return nullptr; }
1169 void SetOwner(imgRequest* aOwner) override {
1170 MOZ_ASSERT(!aOwner,
1171 "We shouldn't be giving static requests a non-null owner.");
1174 private:
1175 // Our image. We have to hold a strong reference here, because that's normally
1176 // the job of the underlying request.
1177 RefPtr<mozilla::image::Image> mImage;
1180 imgRequestProxyStatic::imgRequestProxyStatic(mozilla::image::Image* aImage,
1181 nsIPrincipal* aPrincipal)
1182 : mPrincipal(aPrincipal) {
1183 mBehaviour = mozilla::MakeUnique<StaticBehaviour>(aImage);
1186 NS_IMETHODIMP
1187 imgRequestProxyStatic::GetImagePrincipal(nsIPrincipal** aPrincipal) {
1188 if (!mPrincipal) {
1189 return NS_ERROR_FAILURE;
1192 NS_ADDREF(*aPrincipal = mPrincipal);
1194 return NS_OK;
1197 imgRequestProxy* imgRequestProxyStatic::NewClonedProxy() {
1198 nsCOMPtr<nsIPrincipal> currentPrincipal;
1199 GetImagePrincipal(getter_AddRefs(currentPrincipal));
1200 RefPtr<mozilla::image::Image> image = GetImage();
1201 return new imgRequestProxyStatic(image, currentPrincipal);