1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "imgRequestProxy.h"
#include <utility>
#include "Image.h"
#include "ImageLogging.h"
#include "ImageOps.h"
#include "ImageTypes.h"
#include "imgINotificationObserver.h"
#include "imgLoader.h"
#include "mozilla/dom/Document.h"
#include "mozilla/Telemetry.h" // for Telemetry
#include "mozilla/dom/DocGroup.h" // for DocGroup
#include "nsCRTGlue.h"
#include "nsError.h"
using namespace mozilla;
using namespace mozilla::image;
using mozilla::dom::Document;
// The split of imgRequestProxy and imgRequestProxyStatic means that
// certain overridden functions need to be usable in the destructor.
// Since virtual functions can't be used in that way, this class
// provides a behavioural trait for each class to use instead.
class ProxyBehaviour {
public:
virtual ~ProxyBehaviour() = default;
virtual already_AddRefed<mozilla::image::Image> GetImage() const = 0;
virtual bool HasImage() const = 0;
virtual already_AddRefed<ProgressTracker> GetProgressTracker() const = 0;
virtual imgRequest* GetOwner() const = 0;
virtual void SetOwner(imgRequest* aOwner) = 0;
};
class RequestBehaviour : public ProxyBehaviour {
public:
RequestBehaviour() : mOwner(nullptr), mOwnerHasImage(false) {}
already_AddRefed<mozilla::image::Image> GetImage() const override;
bool HasImage() const override;
already_AddRefed<ProgressTracker> GetProgressTracker() const override;
imgRequest* GetOwner() const override { return mOwner; }
void SetOwner(imgRequest* aOwner) override {
mOwner = aOwner;
if (mOwner) {
RefPtr<ProgressTracker> ownerProgressTracker = GetProgressTracker();
mOwnerHasImage = ownerProgressTracker && ownerProgressTracker->HasImage();
} else {
mOwnerHasImage = false;
}
}
private:
// We maintain the following invariant:
// The proxy is registered at most with a single imgRequest as an observer,
// and whenever it is, mOwner points to that object. This helps ensure that
// imgRequestProxy::~imgRequestProxy unregisters the proxy as an observer
// from whatever request it was registered with (if any). This, in turn,
// means that imgRequest::mObservers will not have any stale pointers in it.
RefPtr<imgRequest> mOwner;
bool mOwnerHasImage;
};
already_AddRefed<mozilla::image::Image> RequestBehaviour::GetImage() const {
if (!mOwnerHasImage) {
return nullptr;
}
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
return progressTracker->GetImage();
}
already_AddRefed<ProgressTracker> RequestBehaviour::GetProgressTracker() const {
// NOTE: It's possible that our mOwner has an Image that it didn't notify
// us about, if we were Canceled before its Image was constructed.
// (Canceling removes us as an observer, so mOwner has no way to notify us).
// That's why this method uses mOwner->GetProgressTracker() instead of just
// mOwner->mProgressTracker -- we might have a null mImage and yet have an
// mOwner with a non-null mImage (and a null mProgressTracker pointer).
return mOwner->GetProgressTracker();
}
NS_IMPL_ADDREF(imgRequestProxy)
NS_IMPL_RELEASE(imgRequestProxy)
NS_INTERFACE_MAP_BEGIN(imgRequestProxy)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, PreloaderBase)
NS_INTERFACE_MAP_ENTRY(imgIRequest)
NS_INTERFACE_MAP_ENTRY(nsIRequest)
NS_INTERFACE_MAP_ENTRY(nsISupportsPriority)
NS_INTERFACE_MAP_ENTRY_CONCRETE(imgRequestProxy)
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsITimedChannel, TimedChannel() != nullptr)
NS_INTERFACE_MAP_END
imgRequestProxy::imgRequestProxy()
: mBehaviour(new RequestBehaviour),
mURI(nullptr),
mListener(nullptr),
mLoadFlags(nsIRequest::LOAD_NORMAL),
mLockCount(0),
mAnimationConsumers(0),
mCanceled(false),
mIsInLoadGroup(false),
mForceDispatchLoadGroup(false),
mListenerIsStrongRef(false),
mDecodeRequested(false),
mPendingNotify(false),
mValidating(false),
mHadListener(false),
mHadDispatch(false) {
/* member initializers and constructor code */
LOG_FUNC(gImgLog, "imgRequestProxy::imgRequestProxy");
}
imgRequestProxy::~imgRequestProxy() {
/* destructor code */
MOZ_ASSERT(!mListener, "Someone forgot to properly cancel this request!");
// If we had a listener, that means we would have issued notifications. With
// bug 1359833, we added support for main thread scheduler groups. Each
// imgRequestProxy may have its own associated listener, document and/or
// scheduler group. Typically most imgRequestProxy belong to the same
// document, or have no listener, which means we will want to execute all main
// thread code in that shared scheduler group. Less frequently, there may be
// multiple imgRequests and they have separate documents, which means that
// when we issue state notifications, some or all need to be dispatched to the
// appropriate scheduler group for each request. This should be rare, so we
// want to monitor the frequency of dispatching in the wild.
if (mHadListener) {
mozilla::Telemetry::Accumulate(mozilla::Telemetry::IMAGE_REQUEST_DISPATCHED,
mHadDispatch);
}
MOZ_RELEASE_ASSERT(!mLockCount, "Someone forgot to unlock on time?");
ClearAnimationConsumers();
// Explicitly set mListener to null to ensure that the RemoveProxy
// call below can't send |this| to an arbitrary listener while |this|
// is being destroyed. This is all belt-and-suspenders in view of the
// above assert.
NullOutListener();
/* Call RemoveProxy with a successful status. This will keep the
channel, if still downloading data, from being canceled if 'this' is
the last observer. This allows the image to continue to download and
be cached even if no one is using it currently.
*/
mCanceled = true;
RemoveFromOwner(NS_OK);
RemoveFromLoadGroup();
LOG_FUNC(gImgLog, "imgRequestProxy::~imgRequestProxy");
}
nsresult imgRequestProxy::Init(imgRequest* aOwner, nsILoadGroup* aLoadGroup,
Document* aLoadingDocument, nsIURI* aURI,
imgINotificationObserver* aObserver) {
MOZ_ASSERT(!GetOwner() && !mListener,
"imgRequestProxy is already initialized");
LOG_SCOPE_WITH_PARAM(gImgLog, "imgRequestProxy::Init", "request", aOwner);
MOZ_ASSERT(mAnimationConsumers == 0, "Cannot have animation before Init");
mBehaviour->SetOwner(aOwner);
mListener = aObserver;
// Make sure to addref mListener before the AddToOwner call below, since
// that call might well want to release it if the imgRequest has
// already seen OnStopRequest.
if (mListener) {
mHadListener = true;
mListenerIsStrongRef = true;
NS_ADDREF(mListener);
}
mLoadGroup = aLoadGroup;
mURI = aURI;
// Note: AddToOwner won't send all the On* notifications immediately
AddToOwner(aLoadingDocument);
return NS_OK;
}
nsresult imgRequestProxy::ChangeOwner(imgRequest* aNewOwner) {
MOZ_ASSERT(GetOwner(), "Cannot ChangeOwner on a proxy without an owner!");
if (mCanceled) {
// Ensure that this proxy has received all notifications to date
// before we clean it up when removing it from the old owner below.
SyncNotifyListener();
}
// If we're holding locks, unlock the old image.
// Note that UnlockImage decrements mLockCount each time it's called.
uint32_t oldLockCount = mLockCount;
while (mLockCount) {
UnlockImage();
}
// If we're holding animation requests, undo them.
uint32_t oldAnimationConsumers = mAnimationConsumers;
ClearAnimationConsumers();
GetOwner()->RemoveProxy(this, NS_OK);
mBehaviour->SetOwner(aNewOwner);
MOZ_ASSERT(!GetValidator(), "New owner cannot be validating!");
// If we were locked, apply the locks here
for (uint32_t i = 0; i < oldLockCount; i++) {
LockImage();
}
// If we had animation requests, restore them here. Note that we
// do this *after* RemoveProxy, which clears out animation consumers
// (see bug 601723).
for (uint32_t i = 0; i < oldAnimationConsumers; i++) {
IncrementAnimationConsumers();
}
AddToOwner(nullptr);
return NS_OK;
}
void imgRequestProxy::MarkValidating() {
MOZ_ASSERT(GetValidator());
mValidating = true;
}
void imgRequestProxy::ClearValidating() {
MOZ_ASSERT(mValidating);
MOZ_ASSERT(!GetValidator());
mValidating = false;
// If we'd previously requested a synchronous decode, request a decode on the
// new image.
if (mDecodeRequested) {
mDecodeRequested = false;
StartDecoding(imgIContainer::FLAG_NONE);
}
}
already_AddRefed<nsIEventTarget> imgRequestProxy::GetEventTarget() const {
nsCOMPtr<nsIEventTarget> target(mEventTarget);
return target.forget();
}
nsresult imgRequestProxy::DispatchWithTargetIfAvailable(
already_AddRefed<nsIRunnable> aEvent) {
LOG_FUNC(gImgLog, "imgRequestProxy::DispatchWithTargetIfAvailable");
// This method should only be used when it is *expected* that we are
// dispatching an event (e.g. we want to handle an event asynchronously)
// rather we need to (e.g. we are in the wrong scheduler group context).
// As such, we do not set mHadDispatch for telemetry purposes.
if (mEventTarget) {
mEventTarget->Dispatch(CreateMediumHighRunnable(std::move(aEvent)),
NS_DISPATCH_NORMAL);
return NS_OK;
}
return NS_DispatchToMainThread(CreateMediumHighRunnable(std::move(aEvent)));
}
void imgRequestProxy::AddToOwner(Document* aLoadingDocument) {
// An imgRequestProxy can be initialized with neither a listener nor a
// document. The caller could follow up later by cloning the canonical
// imgRequestProxy with the actual listener. This is possible because
// imgLoader::LoadImage does not require a valid listener to be provided.
//
// Without a listener, we don't need to set our scheduler group, because
// we have nothing to signal. However if we were told what document this
// is for, it is likely that future listeners will belong to the same
// scheduler group.
//
// With a listener, we always need to update our scheduler group. A null
// scheduler group is valid with or without a document, but that means
// we will use the most generic event target possible on dispatch.
if (aLoadingDocument) {
RefPtr<mozilla::dom::DocGroup> docGroup = aLoadingDocument->GetDocGroup();
if (docGroup) {
mEventTarget = docGroup->EventTargetFor(mozilla::TaskCategory::Other);
MOZ_ASSERT(mEventTarget);
}
}
if (mListener && !mEventTarget) {
mEventTarget = do_GetMainThread();
}
imgRequest* owner = GetOwner();
if (!owner) {
return;
}
owner->AddProxy(this);
}
void imgRequestProxy::RemoveFromOwner(nsresult aStatus) {
imgRequest* owner = GetOwner();
if (owner) {
if (mValidating) {
imgCacheValidator* validator = owner->GetValidator();
MOZ_ASSERT(validator);
validator->RemoveProxy(this);
mValidating = false;
}
owner->RemoveProxy(this, aStatus);
}
}
void imgRequestProxy::AddToLoadGroup() {
NS_ASSERTION(!mIsInLoadGroup, "Whaa, we're already in the loadgroup!");
MOZ_ASSERT(!mForceDispatchLoadGroup);
/* While in theory there could be a dispatch outstanding to remove this
request from the load group, in practice we only add to the load group
(when previously not in a load group) at initialization. */
if (!mIsInLoadGroup && mLoadGroup) {
LOG_FUNC(gImgLog, "imgRequestProxy::AddToLoadGroup");
mLoadGroup->AddRequest(this, nullptr);
mIsInLoadGroup = true;
}
}
void imgRequestProxy::RemoveFromLoadGroup() {
if (!mIsInLoadGroup || !mLoadGroup) {
return;
}
/* Sometimes we may not be able to remove ourselves from the load group in
the current context. This is because our listeners are not re-entrant (e.g.
we are in the middle of CancelAndForgetObserver or SyncClone). */
if (mForceDispatchLoadGroup) {
LOG_FUNC(gImgLog, "imgRequestProxy::RemoveFromLoadGroup -- dispatch");
/* We take away the load group from the request temporarily; this prevents
additional dispatches via RemoveFromLoadGroup occurring, as well as
MoveToBackgroundInLoadGroup from removing and readding. This is safe
because we know that once we get here, blocking the load group at all is
unnecessary. */
mIsInLoadGroup = false;
nsCOMPtr<nsILoadGroup> loadGroup = std::move(mLoadGroup);
RefPtr<imgRequestProxy> self(this);
DispatchWithTargetIfAvailable(NS_NewRunnableFunction(
"imgRequestProxy::RemoveFromLoadGroup", [self, loadGroup]() -> void {
loadGroup->RemoveRequest(self, nullptr, NS_OK);
}));
return;
}
LOG_FUNC(gImgLog, "imgRequestProxy::RemoveFromLoadGroup");
/* calling RemoveFromLoadGroup may cause the document to finish
loading, which could result in our death. We need to make sure
that we stay alive long enough to fight another battle... at
least until we exit this function. */
nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
mLoadGroup->RemoveRequest(this, nullptr, NS_OK);
mLoadGroup = nullptr;
mIsInLoadGroup = false;
}
void imgRequestProxy::MoveToBackgroundInLoadGroup() {
/* Even if we are still in the load group, we may have taken away the load
group reference itself because we are in the process of leaving the group.
In that case, there is no need to background the request. */
if (!mLoadGroup) {
return;
}
/* There is no need to dispatch if we only need to add ourselves to the load
group without removal. It is the removal which causes the problematic
callbacks (see RemoveFromLoadGroup). */
if (mIsInLoadGroup && mForceDispatchLoadGroup) {
LOG_FUNC(gImgLog,
"imgRequestProxy::MoveToBackgroundInLoadGroup -- dispatch");
RefPtr<imgRequestProxy> self(this);
DispatchWithTargetIfAvailable(NS_NewRunnableFunction(
"imgRequestProxy::MoveToBackgroundInLoadGroup",
[self]() -> void { self->MoveToBackgroundInLoadGroup(); }));
return;
}
LOG_FUNC(gImgLog, "imgRequestProxy::MoveToBackgroundInLoadGroup");
nsCOMPtr<imgIRequest> kungFuDeathGrip(this);
if (mIsInLoadGroup) {
mLoadGroup->RemoveRequest(this, nullptr, NS_OK);
}
mLoadFlags |= nsIRequest::LOAD_BACKGROUND;
mLoadGroup->AddRequest(this, nullptr);
}
/** nsIRequest / imgIRequest methods **/
NS_IMETHODIMP
imgRequestProxy::GetName(nsACString& aName) {
aName.Truncate();
if (mURI) {
mURI->GetSpec(aName);
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::IsPending(bool* _retval) { return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
imgRequestProxy::GetStatus(nsresult* aStatus) {
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_IMETHODIMP
imgRequestProxy::Cancel(nsresult status) {
if (mCanceled) {
return NS_ERROR_FAILURE;
}
LOG_SCOPE(gImgLog, "imgRequestProxy::Cancel");
mCanceled = true;
nsCOMPtr<nsIRunnable> ev = new imgCancelRunnable(this, status);
return DispatchWithTargetIfAvailable(ev.forget());
}
void imgRequestProxy::DoCancel(nsresult status) {
RemoveFromOwner(status);
RemoveFromLoadGroup();
NullOutListener();
}
NS_IMETHODIMP
imgRequestProxy::CancelAndForgetObserver(nsresult aStatus) {
// If mCanceled is true but mListener is non-null, that means
// someone called Cancel() on us but the imgCancelRunnable is still
// pending. We still need to null out mListener before returning
// from this function in this case. That means we want to do the
// RemoveProxy call right now, because we need to deliver the
// onStopRequest.
if (mCanceled && !mListener) {
return NS_ERROR_FAILURE;
}
LOG_SCOPE(gImgLog, "imgRequestProxy::CancelAndForgetObserver");
mCanceled = true;
mForceDispatchLoadGroup = true;
RemoveFromOwner(aStatus);
RemoveFromLoadGroup();
mForceDispatchLoadGroup = false;
NullOutListener();
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::StartDecoding(uint32_t aFlags) {
// Flag this, so we know to request after validation if pending.
if (IsValidating()) {
mDecodeRequested = true;
return NS_OK;
}
RefPtr<Image> image = GetImage();
if (image) {
return image->StartDecoding(aFlags);
}
if (GetOwner()) {
GetOwner()->StartDecoding();
}
return NS_OK;
}
bool imgRequestProxy::StartDecodingWithResult(uint32_t aFlags) {
// Flag this, so we know to request after validation if pending.
if (IsValidating()) {
mDecodeRequested = true;
return false;
}
RefPtr<Image> image = GetImage();
if (image) {
return image->StartDecodingWithResult(aFlags);
}
if (GetOwner()) {
GetOwner()->StartDecoding();
}
return false;
}
imgIContainer::DecodeResult imgRequestProxy::RequestDecodeWithResult(
uint32_t aFlags) {
if (IsValidating()) {
mDecodeRequested = true;
return imgIContainer::DECODE_REQUESTED;
}
RefPtr<Image> image = GetImage();
if (image) {
return image->RequestDecodeWithResult(aFlags);
}
if (GetOwner()) {
GetOwner()->StartDecoding();
}
return imgIContainer::DECODE_REQUESTED;
}
NS_IMETHODIMP
imgRequestProxy::LockImage() {
mLockCount++;
RefPtr<Image> image =
GetOwner() && GetOwner()->ImageAvailable() ? GetImage() : nullptr;
if (image) {
return image->LockImage();
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::UnlockImage() {
MOZ_ASSERT(mLockCount > 0, "calling unlock but no locks!");
mLockCount--;
RefPtr<Image> image =
GetOwner() && GetOwner()->ImageAvailable() ? GetImage() : nullptr;
if (image) {
return image->UnlockImage();
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::RequestDiscard() {
RefPtr<Image> image = GetImage();
if (image) {
return image->RequestDiscard();
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::IncrementAnimationConsumers() {
mAnimationConsumers++;
RefPtr<Image> image =
GetOwner() && GetOwner()->ImageAvailable() ? GetImage() : nullptr;
if (image) {
image->IncrementAnimationConsumers();
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::DecrementAnimationConsumers() {
// We may get here if some responsible code called Increment,
// then called us, but we have meanwhile called ClearAnimationConsumers
// because we needed to get rid of them earlier (see
// imgRequest::RemoveProxy), and hence have nothing left to
// decrement. (In such a case we got rid of the animation consumers
// early, but not the observer.)
if (mAnimationConsumers > 0) {
mAnimationConsumers--;
RefPtr<Image> image =
GetOwner() && GetOwner()->ImageAvailable() ? GetImage() : nullptr;
if (image) {
image->DecrementAnimationConsumers();
}
}
return NS_OK;
}
void imgRequestProxy::ClearAnimationConsumers() {
while (mAnimationConsumers > 0) {
DecrementAnimationConsumers();
}
}
NS_IMETHODIMP
imgRequestProxy::Suspend() { return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
imgRequestProxy::Resume() { return NS_ERROR_NOT_IMPLEMENTED; }
NS_IMETHODIMP
imgRequestProxy::GetLoadGroup(nsILoadGroup** loadGroup) {
NS_IF_ADDREF(*loadGroup = mLoadGroup.get());
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::SetLoadGroup(nsILoadGroup* loadGroup) {
if (loadGroup != mLoadGroup) {
MOZ_ASSERT_UNREACHABLE("Switching load groups is unsupported!");
return NS_ERROR_NOT_IMPLEMENTED;
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetLoadFlags(nsLoadFlags* flags) {
*flags = mLoadFlags;
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::SetLoadFlags(nsLoadFlags flags) {
mLoadFlags = flags;
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetTRRMode(nsIRequest::TRRMode* aTRRMode) {
return GetTRRModeImpl(aTRRMode);
}
NS_IMETHODIMP
imgRequestProxy::SetTRRMode(nsIRequest::TRRMode aTRRMode) {
return SetTRRModeImpl(aTRRMode);
}
/** imgIRequest methods **/
NS_IMETHODIMP
imgRequestProxy::GetImage(imgIContainer** aImage) {
NS_ENSURE_TRUE(aImage, NS_ERROR_NULL_POINTER);
// It's possible that our owner has an image but hasn't notified us of it -
// that'll happen if we get Canceled before the owner instantiates its image
// (because Canceling unregisters us as a listener on mOwner). If we're
// in that situation, just grab the image off of mOwner.
RefPtr<Image> image = GetImage();
nsCOMPtr<imgIContainer> imageToReturn;
if (image) {
imageToReturn = image;
}
if (!imageToReturn && GetOwner()) {
imageToReturn = GetOwner()->GetImage();
}
if (!imageToReturn) {
return NS_ERROR_FAILURE;
}
imageToReturn.swap(*aImage);
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetProducerId(uint32_t* aId) {
NS_ENSURE_TRUE(aId, NS_ERROR_NULL_POINTER);
nsCOMPtr<imgIContainer> image;
nsresult rv = GetImage(getter_AddRefs(image));
if (NS_SUCCEEDED(rv)) {
*aId = image->GetProducerId();
} else {
*aId = layers::kContainerProducerID_Invalid;
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetImageStatus(uint32_t* aStatus) {
if (IsValidating()) {
// We are currently validating the image, and so our status could revert if
// we discard the cache. We should also be deferring notifications, such
// that the caller will be notified when validation completes. Rather than
// risk misleading the caller, return nothing.
*aStatus = imgIRequest::STATUS_NONE;
} else {
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
*aStatus = progressTracker->GetImageStatus();
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetImageErrorCode(nsresult* aStatus) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
*aStatus = GetOwner()->GetImageErrorCode();
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetURI(nsIURI** aURI) {
MOZ_ASSERT(NS_IsMainThread(), "Must be on main thread to convert URI");
nsCOMPtr<nsIURI> uri = mURI;
uri.forget(aURI);
return NS_OK;
}
nsresult imgRequestProxy::GetFinalURI(nsIURI** aURI) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
return GetOwner()->GetFinalURI(aURI);
}
NS_IMETHODIMP
imgRequestProxy::GetNotificationObserver(imgINotificationObserver** aObserver) {
*aObserver = mListener;
NS_IF_ADDREF(*aObserver);
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetMimeType(char** aMimeType) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
const char* type = GetOwner()->GetMimeType();
if (!type) {
return NS_ERROR_FAILURE;
}
*aMimeType = NS_xstrdup(type);
return NS_OK;
}
imgRequestProxy* imgRequestProxy::NewClonedProxy() {
return new imgRequestProxy();
}
NS_IMETHODIMP
imgRequestProxy::Clone(imgINotificationObserver* aObserver,
imgIRequest** aClone) {
nsresult result;
imgRequestProxy* proxy;
result = PerformClone(aObserver, nullptr, /* aSyncNotify */ true, &proxy);
*aClone = proxy;
return result;
}
nsresult imgRequestProxy::SyncClone(imgINotificationObserver* aObserver,
Document* aLoadingDocument,
imgRequestProxy** aClone) {
return PerformClone(aObserver, aLoadingDocument,
/* aSyncNotify */ true, aClone);
}
nsresult imgRequestProxy::Clone(imgINotificationObserver* aObserver,
Document* aLoadingDocument,
imgRequestProxy** aClone) {
return PerformClone(aObserver, aLoadingDocument,
/* aSyncNotify */ false, aClone);
}
nsresult imgRequestProxy::PerformClone(imgINotificationObserver* aObserver,
Document* aLoadingDocument,
bool aSyncNotify,
imgRequestProxy** aClone) {
MOZ_ASSERT(aClone, "Null out param");
LOG_SCOPE(gImgLog, "imgRequestProxy::Clone");
*aClone = nullptr;
RefPtr<imgRequestProxy> clone = NewClonedProxy();
nsCOMPtr<nsILoadGroup> loadGroup;
if (aLoadingDocument) {
loadGroup = aLoadingDocument->GetDocumentLoadGroup();
}
// It is important to call |SetLoadFlags()| before calling |Init()| because
// |Init()| adds the request to the loadgroup.
// When a request is added to a loadgroup, its load flags are merged
// with the load flags of the loadgroup.
// XXXldb That's not true anymore. Stuff from imgLoader adds the
// request to the loadgroup.
clone->SetLoadFlags(mLoadFlags);
nsresult rv = clone->Init(mBehaviour->GetOwner(), loadGroup, aLoadingDocument,
mURI, aObserver);
if (NS_FAILED(rv)) {
return rv;
}
// Assign to *aClone before calling Notify so that if the caller expects to
// only be notified for requests it's already holding pointers to it won't be
// surprised.
NS_ADDREF(*aClone = clone);
imgCacheValidator* validator = GetValidator();
if (validator) {
// Note that if we have a validator, we don't want to issue notifications at
// here because we want to defer until that completes. AddProxy will add us
// to the load group; we cannot avoid that in this case, because we don't
// know when the validation will complete, and if it will cause us to
// discard our cached state anyways. We are probably already blocked by the
// original LoadImage(WithChannel) request in any event.
clone->MarkValidating();
validator->AddProxy(clone);
} else {
// We only want to add the request to the load group of the owning document
// if it is still in progress. Some callers cannot handle a supurious load
// group removal (e.g. print preview) so we must be careful. On the other
// hand, if after cloning, the original request proxy is cancelled /
// destroyed, we need to ensure that any clones still block the load group
// if it is incomplete.
bool addToLoadGroup = mIsInLoadGroup;
if (!addToLoadGroup) {
RefPtr<ProgressTracker> tracker = clone->GetProgressTracker();
addToLoadGroup =
tracker && !(tracker->GetProgress() & FLAG_LOAD_COMPLETE);
}
if (addToLoadGroup) {
clone->AddToLoadGroup();
}
if (aSyncNotify) {
// This is wrong!!! We need to notify asynchronously, but there's code
// that assumes that we don't. This will be fixed in bug 580466. Note that
// if we have a validator, we won't issue notifications anyways because
// they are deferred, so there is no point in requesting.
clone->mForceDispatchLoadGroup = true;
clone->SyncNotifyListener();
clone->mForceDispatchLoadGroup = false;
} else {
// Without a validator, we can request asynchronous notifications
// immediately. If there was a validator, this would override the deferral
// and that would be incorrect.
clone->NotifyListener();
}
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetImagePrincipal(nsIPrincipal** aPrincipal) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIPrincipal> principal = GetOwner()->GetPrincipal();
principal.forget(aPrincipal);
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetHadCrossOriginRedirects(bool* aHadCrossOriginRedirects) {
*aHadCrossOriginRedirects = false;
nsCOMPtr<nsITimedChannel> timedChannel = TimedChannel();
if (timedChannel) {
bool allRedirectsSameOrigin = false;
*aHadCrossOriginRedirects =
NS_SUCCEEDED(
timedChannel->GetAllRedirectsSameOrigin(&allRedirectsSameOrigin)) &&
!allRedirectsSameOrigin;
}
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetMultipart(bool* aMultipart) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
*aMultipart = GetOwner()->GetMultipart();
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::GetCORSMode(int32_t* aCorsMode) {
if (!GetOwner()) {
return NS_ERROR_FAILURE;
}
*aCorsMode = GetOwner()->GetCORSMode();
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::BoostPriority(uint32_t aCategory) {
NS_ENSURE_STATE(GetOwner() && !mCanceled);
GetOwner()->BoostPriority(aCategory);
return NS_OK;
}
/** nsISupportsPriority methods **/
NS_IMETHODIMP
imgRequestProxy::GetPriority(int32_t* priority) {
NS_ENSURE_STATE(GetOwner());
*priority = GetOwner()->Priority();
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::SetPriority(int32_t priority) {
NS_ENSURE_STATE(GetOwner() && !mCanceled);
GetOwner()->AdjustPriority(this, priority - GetOwner()->Priority());
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxy::AdjustPriority(int32_t priority) {
// We don't require |!mCanceled| here. This may be called even if we're
// cancelled, because it's invoked as part of the process of removing an image
// from the load group.
NS_ENSURE_STATE(GetOwner());
GetOwner()->AdjustPriority(this, priority);
return NS_OK;
}
static const char* NotificationTypeToString(int32_t aType) {
switch (aType) {
case imgINotificationObserver::SIZE_AVAILABLE:
return "SIZE_AVAILABLE";
case imgINotificationObserver::FRAME_UPDATE:
return "FRAME_UPDATE";
case imgINotificationObserver::FRAME_COMPLETE:
return "FRAME_COMPLETE";
case imgINotificationObserver::LOAD_COMPLETE:
return "LOAD_COMPLETE";
case imgINotificationObserver::DECODE_COMPLETE:
return "DECODE_COMPLETE";
case imgINotificationObserver::DISCARD:
return "DISCARD";
case imgINotificationObserver::UNLOCKED_DRAW:
return "UNLOCKED_DRAW";
case imgINotificationObserver::IS_ANIMATED:
return "IS_ANIMATED";
case imgINotificationObserver::HAS_TRANSPARENCY:
return "HAS_TRANSPARENCY";
default:
MOZ_ASSERT_UNREACHABLE("Notification list should be exhaustive");
return "(unknown notification)";
}
}
void imgRequestProxy::Notify(int32_t aType,
const mozilla::gfx::IntRect* aRect) {
MOZ_ASSERT(aType != imgINotificationObserver::LOAD_COMPLETE,
"Should call OnLoadComplete");
LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::Notify", "type",
NotificationTypeToString(aType));
if (!mListener || mCanceled) {
return;
}
// Make sure the listener stays alive while we notify.
nsCOMPtr<imgINotificationObserver> listener(mListener);
listener->Notify(this, aType, aRect);
}
void imgRequestProxy::OnLoadComplete(bool aLastPart) {
LOG_FUNC_WITH_PARAM(gImgLog, "imgRequestProxy::OnLoadComplete", "uri", mURI);
// There's all sorts of stuff here that could kill us (the OnStopRequest call
// on the listener, the removal from the loadgroup, the release of the
// listener, etc). Don't let them do it.
RefPtr<imgRequestProxy> self(this);
if (mListener && !mCanceled) {
// Hold a ref to the listener while we call it, just in case.
nsCOMPtr<imgINotificationObserver> listener(mListener);
listener->Notify(this, imgINotificationObserver::LOAD_COMPLETE, nullptr);
}
// If we're expecting more data from a multipart channel, re-add ourself
// to the loadgroup so that the document doesn't lose track of the load.
// If the request is already a background request and there's more data
// coming, we can just leave the request in the loadgroup as-is.
if (aLastPart || (mLoadFlags & nsIRequest::LOAD_BACKGROUND) == 0) {
if (aLastPart) {
RemoveFromLoadGroup();
nsresult errorCode = NS_OK;
// if the load is cross origin without CORS, or the CORS access is
// rejected, always fire load event to avoid leaking site information for
// <link rel=preload>.
// XXXedgar, currently we don't do the same thing for <img>.
imgRequest* request = GetOwner();
if (!request || !(request->IsDeniedCrossSiteCORSRequest() ||
request->IsCrossSiteNoCORSRequest())) {
uint32_t status = imgIRequest::STATUS_NONE;
GetImageStatus(&status);
if (status & imgIRequest::STATUS_ERROR) {
errorCode = NS_ERROR_FAILURE;
}
}
NotifyStop(errorCode);
} else {
// More data is coming, so change the request to be a background request
// and put it back in the loadgroup.
MoveToBackgroundInLoadGroup();
}
}
if (mListenerIsStrongRef && aLastPart) {
MOZ_ASSERT(mListener, "How did that happen?");
// Drop our strong ref to the listener now that we're done with
// everything. Note that this can cancel us and other fun things
// like that. Don't add anything in this method after this point.
imgINotificationObserver* obs = mListener;
mListenerIsStrongRef = false;
NS_RELEASE(obs);
}
}
void imgRequestProxy::NullOutListener() {
// If we have animation consumers, then they don't matter anymore
if (mListener) {
ClearAnimationConsumers();
}
if (mListenerIsStrongRef) {
// Releasing could do weird reentery stuff, so just play it super-safe
nsCOMPtr<imgINotificationObserver> obs;
obs.swap(mListener);
mListenerIsStrongRef = false;
} else {
mListener = nullptr;
}
}
NS_IMETHODIMP
imgRequestProxy::GetStaticRequest(imgIRequest** aReturn) {
RefPtr<imgRequestProxy> proxy =
GetStaticRequest(static_cast<Document*>(nullptr));
if (proxy != this) {
RefPtr<Image> image = GetImage();
if (image && image->HasError()) {
// image/test/unit/test_async_notification_404.js needs this, but ideally
// this special case can be removed from the scripted codepath.
return NS_ERROR_FAILURE;
}
}
proxy.forget(aReturn);
return NS_OK;
}
already_AddRefed<imgRequestProxy> imgRequestProxy::GetStaticRequest(
Document* aLoadingDocument) {
MOZ_DIAGNOSTIC_ASSERT(!aLoadingDocument ||
aLoadingDocument->IsStaticDocument());
RefPtr<Image> image = GetImage();
bool animated;
if (!image || (NS_SUCCEEDED(image->GetAnimated(&animated)) && !animated)) {
// Early exit - we're not animated, so we don't have to do anything.
return do_AddRef(this);
}
// We are animated. We need to create a frozen version of this image.
RefPtr<Image> frozenImage = ImageOps::Freeze(image);
// Create a static imgRequestProxy with our new extracted frame.
nsCOMPtr<nsIPrincipal> currentPrincipal;
GetImagePrincipal(getter_AddRefs(currentPrincipal));
bool hadCrossOriginRedirects = true;
GetHadCrossOriginRedirects(&hadCrossOriginRedirects);
RefPtr<imgRequestProxy> req = new imgRequestProxyStatic(
frozenImage, currentPrincipal, hadCrossOriginRedirects);
req->Init(nullptr, nullptr, aLoadingDocument, mURI, nullptr);
return req.forget();
}
void imgRequestProxy::NotifyListener() {
// It would be nice to notify the observer directly in the status tracker
// instead of through the proxy, but there are several places we do extra
// processing when we receive notifications (like OnStopRequest()), and we
// need to check mCanceled everywhere too.
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
if (GetOwner()) {
// Send the notifications to our listener asynchronously.
progressTracker->Notify(this);
} else {
// We don't have an imgRequest, so we can only notify the clone of our
// current state, but we still have to do that asynchronously.
MOZ_ASSERT(HasImage(), "if we have no imgRequest, we should have an Image");
progressTracker->NotifyCurrentState(this);
}
}
void imgRequestProxy::SyncNotifyListener() {
// It would be nice to notify the observer directly in the status tracker
// instead of through the proxy, but there are several places we do extra
// processing when we receive notifications (like OnStopRequest()), and we
// need to check mCanceled everywhere too.
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
progressTracker->SyncNotify(this);
}
void imgRequestProxy::SetHasImage() {
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
MOZ_ASSERT(progressTracker);
RefPtr<Image> image = progressTracker->GetImage();
MOZ_ASSERT(image);
// Force any private status related to the owner to reflect
// the presence of an image;
mBehaviour->SetOwner(mBehaviour->GetOwner());
// Apply any locks we have
for (uint32_t i = 0; i < mLockCount; ++i) {
image->LockImage();
}
// Apply any animation consumers we have
for (uint32_t i = 0; i < mAnimationConsumers; i++) {
image->IncrementAnimationConsumers();
}
}
already_AddRefed<ProgressTracker> imgRequestProxy::GetProgressTracker() const {
return mBehaviour->GetProgressTracker();
}
already_AddRefed<mozilla::image::Image> imgRequestProxy::GetImage() const {
return mBehaviour->GetImage();
}
bool RequestBehaviour::HasImage() const {
if (!mOwnerHasImage) {
return false;
}
RefPtr<ProgressTracker> progressTracker = GetProgressTracker();
return progressTracker ? progressTracker->HasImage() : false;
}
bool imgRequestProxy::HasImage() const { return mBehaviour->HasImage(); }
imgRequest* imgRequestProxy::GetOwner() const { return mBehaviour->GetOwner(); }
imgCacheValidator* imgRequestProxy::GetValidator() const {
imgRequest* owner = GetOwner();
if (!owner) {
return nullptr;
}
return owner->GetValidator();
}
nsITimedChannel* imgRequestProxy::TimedChannel() {
if (!GetOwner()) {
return nullptr;
}
return GetOwner()->GetTimedChannel();
}
////////////////// imgRequestProxyStatic methods
class StaticBehaviour : public ProxyBehaviour {
public:
explicit StaticBehaviour(mozilla::image::Image* aImage) : mImage(aImage) {}
already_AddRefed<mozilla::image::Image> GetImage() const override {
RefPtr<mozilla::image::Image> image = mImage;
return image.forget();
}
bool HasImage() const override { return mImage; }
already_AddRefed<ProgressTracker> GetProgressTracker() const override {
return mImage->GetProgressTracker();
}
imgRequest* GetOwner() const override { return nullptr; }
void SetOwner(imgRequest* aOwner) override {
MOZ_ASSERT(!aOwner,
"We shouldn't be giving static requests a non-null owner.");
}
private:
// Our image. We have to hold a strong reference here, because that's normally
// the job of the underlying request.
RefPtr<mozilla::image::Image> mImage;
};
imgRequestProxyStatic::imgRequestProxyStatic(mozilla::image::Image* aImage,
nsIPrincipal* aPrincipal,
bool aHadCrossOriginRedirects)
: mPrincipal(aPrincipal),
mHadCrossOriginRedirects(aHadCrossOriginRedirects) {
mBehaviour = mozilla::MakeUnique<StaticBehaviour>(aImage);
}
NS_IMETHODIMP
imgRequestProxyStatic::GetImagePrincipal(nsIPrincipal** aPrincipal) {
if (!mPrincipal) {
return NS_ERROR_FAILURE;
}
NS_ADDREF(*aPrincipal = mPrincipal);
return NS_OK;
}
NS_IMETHODIMP
imgRequestProxyStatic::GetHadCrossOriginRedirects(
bool* aHadCrossOriginRedirects) {
*aHadCrossOriginRedirects = mHadCrossOriginRedirects;
return NS_OK;
}
imgRequestProxy* imgRequestProxyStatic::NewClonedProxy() {
nsCOMPtr<nsIPrincipal> currentPrincipal;
GetImagePrincipal(getter_AddRefs(currentPrincipal));
bool hadCrossOriginRedirects = true;
GetHadCrossOriginRedirects(&hadCrossOriginRedirects);
RefPtr<mozilla::image::Image> image = GetImage();
return new imgRequestProxyStatic(image, currentPrincipal,
hadCrossOriginRedirects);
}
|