File: ResourceFetcher.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (1514 lines) | stat: -rw-r--r-- 58,867 bytes parent folder | download
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
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
/*
    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
    Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
    Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
    Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
    rights reserved.
    Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

    This class provides all functionality needed for loading images, style
    sheets and html pages from the web. It has a memory cache for these objects.
*/

#include "core/fetch/ResourceFetcher.h"

#include "core/fetch/FetchContext.h"
#include "core/fetch/FetchInitiatorTypeNames.h"
#include "core/fetch/MemoryCache.h"
#include "core/fetch/ResourceLoader.h"
#include "core/fetch/ResourceLoadingLog.h"
#include "core/fetch/UniqueIdentifier.h"
#include "platform/Histogram.h"
#include "platform/RuntimeEnabledFeatures.h"
#include "platform/instrumentation/tracing/TraceEvent.h"
#include "platform/instrumentation/tracing/TracedValue.h"
#include "platform/mhtml/ArchiveResource.h"
#include "platform/mhtml/MHTMLArchive.h"
#include "platform/network/NetworkInstrumentation.h"
#include "platform/network/NetworkUtils.h"
#include "platform/network/ResourceTimingInfo.h"
#include "platform/weborigin/KnownPorts.h"
#include "platform/weborigin/SecurityOrigin.h"
#include "platform/weborigin/SecurityPolicy.h"
#include "public/platform/Platform.h"
#include "public/platform/WebCachePolicy.h"
#include "public/platform/WebURL.h"
#include "public/platform/WebURLRequest.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
#include <memory>

using blink::WebURLRequest;

namespace blink {

namespace {

// Events for UMA. Do not reorder or delete. Add new events at the end, but
// before SriResourceIntegrityMismatchEventCount.
enum SriResourceIntegrityMismatchEvent {
  CheckingForIntegrityMismatch = 0,
  RefetchDueToIntegrityMismatch = 1,
  SriResourceIntegrityMismatchEventCount
};

#define DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, name)                        \
  case Resource::name: {                                                      \
    DEFINE_THREAD_SAFE_STATIC_LOCAL(                                          \
        EnumerationHistogram, resourceHistogram,                              \
        new EnumerationHistogram(                                             \
            "Blink.MemoryCache.RevalidationPolicy." prefix #name, Load + 1)); \
    resourceHistogram.count(policy);                                          \
    break;                                                                    \
  }

#define DEFINE_RESOURCE_HISTOGRAM(prefix)                    \
  switch (factory.type()) {                                  \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, CSSStyleSheet)  \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Font)           \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Image)          \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, ImportResource) \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, LinkPrefetch)   \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, MainResource)   \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Manifest)       \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Media)          \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Mock)           \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Raw)            \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, Script)         \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, SVGDocument)    \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, TextTrack)      \
    DEFINE_SINGLE_RESOURCE_HISTOGRAM(prefix, XSLStyleSheet)  \
  }

void addRedirectsToTimingInfo(Resource* resource, ResourceTimingInfo* info) {
  // Store redirect responses that were packed inside the final response.
  const auto& responses = resource->response().redirectResponses();
  for (size_t i = 0; i < responses.size(); ++i) {
    const KURL& newURL = i + 1 < responses.size()
                             ? KURL(responses[i + 1].url())
                             : resource->resourceRequest().url();
    bool crossOrigin =
        !SecurityOrigin::areSameSchemeHostPort(responses[i].url(), newURL);
    info->addRedirect(responses[i], crossOrigin);
  }
}

void RecordSriResourceIntegrityMismatchEvent(
    SriResourceIntegrityMismatchEvent event) {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      EnumerationHistogram, integrityHistogram,
      new EnumerationHistogram("sri.resource_integrity_mismatch_event",
                               SriResourceIntegrityMismatchEventCount));
  integrityHistogram.count(event);
}

ResourceLoadPriority typeToPriority(Resource::Type type) {
  switch (type) {
    case Resource::MainResource:
    case Resource::CSSStyleSheet:
    case Resource::Font:
      // Also parser-blocking scripts (set explicitly in loadPriority)
      return ResourceLoadPriorityVeryHigh;
    case Resource::XSLStyleSheet:
      DCHECK(RuntimeEnabledFeatures::xsltEnabled());
    case Resource::Raw:
    case Resource::ImportResource:
    case Resource::Script:
      // Also visible resources/images (set explicitly in loadPriority)
      return ResourceLoadPriorityHigh;
    case Resource::Manifest:
    case Resource::Mock:
      // Also late-body scripts discovered by the preload scanner (set
      // explicitly in loadPriority)
      return ResourceLoadPriorityMedium;
    case Resource::Image:
    case Resource::TextTrack:
    case Resource::Media:
    case Resource::SVGDocument:
      // Also async scripts (set explicitly in loadPriority)
      return ResourceLoadPriorityLow;
    case Resource::LinkPrefetch:
      return ResourceLoadPriorityVeryLow;
  }

  NOTREACHED();
  return ResourceLoadPriorityUnresolved;
}

}  // namespace

ResourceLoadPriority ResourceFetcher::computeLoadPriority(
    Resource::Type type,
    const FetchRequest& request,
    ResourcePriority::VisibilityStatus visibility) {
  ResourceLoadPriority priority = typeToPriority(type);

  // Visible resources (images in practice) get a boost to High priority.
  if (visibility == ResourcePriority::Visible)
    priority = ResourceLoadPriorityHigh;

  // Resources before the first image are considered "early" in the document and
  // resources after the first image are "late" in the document.  Important to
  // note that this is based on when the preload scanner discovers a resource
  // for the most part so the main parser may not have reached the image element
  // yet.
  if (type == Resource::Image)
    m_imageFetched = true;

  if (FetchRequest::IdleLoad == request.defer()) {
    priority = ResourceLoadPriorityVeryLow;
  } else if (type == Resource::Script) {
    // Special handling for scripts.
    // Default/Parser-Blocking/Preload early in document: High (set in
    // typeToPriority)
    // Async/Defer: Low Priority (applies to both preload and parser-inserted)
    // Preload late in document: Medium
    if (FetchRequest::LazyLoad == request.defer())
      priority = ResourceLoadPriorityLow;
    else if (request.forPreload() && m_imageFetched)
      priority = ResourceLoadPriorityMedium;
  } else if (FetchRequest::LazyLoad == request.defer()) {
    priority = ResourceLoadPriorityVeryLow;
  }

  // A manually set priority acts as a floor. This is used to ensure that
  // synchronous requests are always given the highest possible priority, as
  // well as to ensure that there isn't priority churn if images move in and out
  // of the viewport, or is displayed more than once, both in and out of the
  // viewport.
  return std::max(context().modifyPriorityForExperiments(priority),
                  request.resourceRequest().priority());
}

static void populateTimingInfo(ResourceTimingInfo* info,
                                   Resource* resource) {
  KURL initialURL = resource->response().redirectResponses().isEmpty()
                        ? resource->resourceRequest().url()
                        : resource->response().redirectResponses()[0].url();
  info->setInitialURL(initialURL);
  info->setFinalResponse(resource->response());
}

static WebURLRequest::RequestContext requestContextFromType(
    bool isMainFrame,
    Resource::Type type) {
  switch (type) {
    case Resource::MainResource:
      if (!isMainFrame)
        return WebURLRequest::RequestContextIframe;
      // FIXME: Change this to a context frame type (once we introduce them):
      // http://fetch.spec.whatwg.org/#concept-request-context-frame-type
      return WebURLRequest::RequestContextHyperlink;
    case Resource::XSLStyleSheet:
      DCHECK(RuntimeEnabledFeatures::xsltEnabled());
    case Resource::CSSStyleSheet:
      return WebURLRequest::RequestContextStyle;
    case Resource::Script:
      return WebURLRequest::RequestContextScript;
    case Resource::Font:
      return WebURLRequest::RequestContextFont;
    case Resource::Image:
      return WebURLRequest::RequestContextImage;
    case Resource::Raw:
      return WebURLRequest::RequestContextSubresource;
    case Resource::ImportResource:
      return WebURLRequest::RequestContextImport;
    case Resource::LinkPrefetch:
      return WebURLRequest::RequestContextPrefetch;
    case Resource::TextTrack:
      return WebURLRequest::RequestContextTrack;
    case Resource::SVGDocument:
      return WebURLRequest::RequestContextImage;
    case Resource::Media:  // TODO: Split this.
      return WebURLRequest::RequestContextVideo;
    case Resource::Manifest:
      return WebURLRequest::RequestContextManifest;
    case Resource::Mock:
      return WebURLRequest::RequestContextSubresource;
  }
  NOTREACHED();
  return WebURLRequest::RequestContextSubresource;
}

ResourceFetcher::ResourceFetcher(FetchContext* newContext)
    : m_context(newContext),
      m_archive(context().isMainFrame() ? nullptr : context().archive()),
      // loadingTaskRunner() is null in tests that use the null fetch context.
      m_resourceTimingReportTimer(
          context().loadingTaskRunner()
              ? context().loadingTaskRunner()
              : Platform::current()->currentThread()->getWebTaskRunner(),
          this,
          &ResourceFetcher::resourceTimingReportTimerFired),
      m_autoLoadImages(true),
      m_imagesEnabled(true),
      m_allowStaleResources(false),
      m_imageFetched(false) {}

ResourceFetcher::~ResourceFetcher() {}

Resource* ResourceFetcher::cachedResource(const KURL& resourceURL) const {
  KURL url = MemoryCache::removeFragmentIdentifierIfNeeded(resourceURL);
  const WeakMember<Resource>& resource = m_documentResources.get(url);
  return resource.get();
}

bool ResourceFetcher::isControlledByServiceWorker() const {
  return context().isControlledByServiceWorker();
}

bool ResourceFetcher::resourceNeedsLoad(Resource* resource,
                                        const FetchRequest& request,
                                        RevalidationPolicy policy) {
  // Defer a font load until it is actually needed unless this is a preload.
  if (resource->getType() == Resource::Font && !request.forPreload())
    return false;
  if (resource->isImage() && shouldDeferImageLoad(resource->url()))
    return false;
  return policy != Use || resource->stillNeedsLoad();
}

// Limit the number of URLs in m_validatedURLs to avoid memory bloat.
// http://crbug.com/52411
static const int kMaxValidatedURLsSize = 10000;

void ResourceFetcher::requestLoadStarted(unsigned long identifier,
                                         Resource* resource,
                                         const FetchRequest& request,
                                         ResourceLoadStartType type,
                                         bool isStaticData) {
  if (type == ResourceLoadingFromCache &&
      resource->getStatus() == Resource::Cached &&
      !m_validatedURLs.contains(resource->url())) {
    context().dispatchDidLoadResourceFromMemoryCache(
        identifier, resource, request.resourceRequest().frameType(),
        request.resourceRequest().requestContext());
  }

  if (isStaticData)
    return;

  if (type == ResourceLoadingFromCache && !resource->stillNeedsLoad() &&
      !m_validatedURLs.contains(request.resourceRequest().url())) {
    // Resources loaded from memory cache should be reported the first time
    // they're used.
    std::unique_ptr<ResourceTimingInfo> info = ResourceTimingInfo::create(
        request.options().initiatorInfo.name, monotonicallyIncreasingTime(),
        resource->getType() == Resource::MainResource);
    populateTimingInfo(info.get(), resource);
    info->clearLoadTimings();
    info->setLoadFinishTime(info->initialTime());
    m_scheduledResourceTimingReports.push_back(std::move(info));
    if (!m_resourceTimingReportTimer.isActive())
      m_resourceTimingReportTimer.startOneShot(0, BLINK_FROM_HERE);
  }

  if (m_validatedURLs.size() >= kMaxValidatedURLsSize) {
    m_validatedURLs.clear();
  }
  m_validatedURLs.add(request.resourceRequest().url());
}

static std::unique_ptr<TracedValue> urlForTraceEvent(const KURL& url) {
  std::unique_ptr<TracedValue> value = TracedValue::create();
  value->setString("url", url.getString());
  return value;
}

Resource* ResourceFetcher::resourceForStaticData(
    const FetchRequest& request,
    const ResourceFactory& factory,
    const SubstituteData& substituteData) {
  const KURL& url = request.resourceRequest().url();
  DCHECK(url.protocolIsData() || substituteData.isValid() || m_archive);

  // TODO(japhet): We only send main resource data: urls through WebURLLoader
  // for the benefit of a service worker test
  // (RenderViewImplTest.ServiceWorkerNetworkProviderSetup), which is at a layer
  // where it isn't easy to mock out a network load. It uses data: urls to
  // emulate the behavior it wants to test, which would otherwise be reserved
  // for network loads.
  if (!m_archive && !substituteData.isValid() &&
      (factory.type() == Resource::MainResource ||
       factory.type() == Resource::Raw))
    return nullptr;

  const String cacheIdentifier = getCacheIdentifier();
  if (Resource* oldResource =
          memoryCache()->resourceForURL(url, cacheIdentifier)) {
    // There's no reason to re-parse if we saved the data from the previous
    // parse.
    if (request.options().dataBufferingPolicy != DoNotBufferData)
      return oldResource;
    memoryCache()->remove(oldResource);
  }

  AtomicString mimetype;
  AtomicString charset;
  RefPtr<SharedBuffer> data;
  if (substituteData.isValid()) {
    mimetype = substituteData.mimeType();
    charset = substituteData.textEncoding();
    data = substituteData.content();
  } else if (url.protocolIsData()) {
    data = PassRefPtr<SharedBuffer>(
        NetworkUtils::parseDataURL(url, mimetype, charset));
    if (!data)
      return nullptr;
  } else {
    ArchiveResource* archiveResource =
        m_archive->subresourceForURL(request.url());
    // Fall back to the network if the archive doesn't contain the resource.
    if (!archiveResource)
      return nullptr;
    mimetype = archiveResource->mimeType();
    charset = archiveResource->textEncoding();
    data = archiveResource->data();
  }

  ResourceResponse response(url, mimetype, data->size(), charset, String());
  response.setHTTPStatusCode(200);
  response.setHTTPStatusText("OK");

  Resource* resource = factory.create(request.resourceRequest(),
                                      request.options(), request.charset());
  resource->setNeedsSynchronousCacheHit(substituteData.forceSynchronousLoad());
  // FIXME: We should provide a body stream here.
  resource->responseReceived(response, nullptr);
  resource->setDataBufferingPolicy(BufferData);
  if (data->size())
    resource->setResourceBuffer(data);
  resource->setIdentifier(createUniqueIdentifier());
  resource->setCacheIdentifier(cacheIdentifier);
  resource->finish();

  if (!substituteData.isValid())
    memoryCache()->add(resource);

  return resource;
}

Resource* ResourceFetcher::resourceForBlockedRequest(
    const FetchRequest& request,
    const ResourceFactory& factory,
    ResourceRequestBlockedReason blockedReason) {
  Resource* resource = factory.create(request.resourceRequest(),
                                      request.options(), request.charset());
  resource->error(ResourceError::cancelledDueToAccessCheckError(request.url(),
                                                                blockedReason));
  return resource;
}

void ResourceFetcher::moveCachedNonBlockingResourceToBlocking(
    Resource* resource,
    const FetchRequest& request) {
  // TODO(yoav): Test that non-blocking resources (video/audio/track) continue
  // to not-block even after being preloaded and discovered.
  if (resource && resource->loader() &&
      resource->isLoadEventBlockingResourceType() &&
      m_nonBlockingLoaders.contains(resource->loader()) &&
      resource->isLinkPreload() && !request.forPreload()) {
    m_nonBlockingLoaders.remove(resource->loader());
    m_loaders.add(resource->loader());
  }
}

void ResourceFetcher::updateMemoryCacheStats(Resource* resource,
                                             RevalidationPolicy policy,
                                             const FetchRequest& request,
                                             const ResourceFactory& factory,
                                             bool isStaticData) const {
  if (isStaticData)
    return;

  if (request.forPreload()) {
    DEFINE_RESOURCE_HISTOGRAM("Preload.");
  } else {
    DEFINE_RESOURCE_HISTOGRAM("");
  }

  // Aims to count Resource only referenced from MemoryCache (i.e. what would be
  // dead if MemoryCache holds weak references to Resource). Currently we check
  // references to Resource from ResourceClient and |m_preloads| only, because
  // they are major sources of references.
  if (resource && !resource->isAlive() &&
      (!m_preloads || !m_preloads->contains(resource))) {
    DEFINE_RESOURCE_HISTOGRAM("Dead.");
  }
}

Resource* ResourceFetcher::requestResource(
    FetchRequest& request,
    const ResourceFactory& factory,
    const SubstituteData& substituteData) {
  ResourceRequest& resourceRequest = request.mutableResourceRequest();

  unsigned long identifier = createUniqueIdentifier();
  network_instrumentation::ScopedResourceLoadTracker scopedResourceLoadTracker(
      identifier, resourceRequest);
  SCOPED_BLINK_UMA_HISTOGRAM_TIMER("Blink.Fetch.RequestResourceTime");
  DCHECK(request.options().synchronousPolicy == RequestAsynchronously ||
         factory.type() == Resource::Raw ||
         factory.type() == Resource::XSLStyleSheet);

  context().populateResourceRequest(
      factory.type(), request.clientHintsPreferences(),
      request.getResourceWidth(), resourceRequest);

  // TODO(dproy): Remove this. http://crbug.com/659666
  TRACE_EVENT1("blink", "ResourceFetcher::requestResource", "url",
               urlForTraceEvent(request.url()));

  if (!request.url().isValid())
    return nullptr;

  resourceRequest.setPriority(computeLoadPriority(
      factory.type(), request, ResourcePriority::NotVisible));
  initializeResourceRequest(resourceRequest, factory.type(), request.defer());
  network_instrumentation::resourcePrioritySet(identifier,
                                               resourceRequest.priority());

  ResourceRequestBlockedReason blockedReason = context().canRequest(
      factory.type(), resourceRequest,
      MemoryCache::removeFragmentIdentifierIfNeeded(request.url()),
      request.options(), request.forPreload(), request.getOriginRestriction());
  if (blockedReason != ResourceRequestBlockedReason::None) {
    DCHECK(!substituteData.forceSynchronousLoad());
    return resourceForBlockedRequest(request, factory, blockedReason);
  }

  context().willStartLoadingResource(
      identifier, resourceRequest, factory.type(),
      request.options().initiatorInfo.name, request.forPreload());
  if (!request.url().isValid())
    return nullptr;

  bool isDataUrl = resourceRequest.url().protocolIsData();
  bool isStaticData = isDataUrl || substituteData.isValid() || m_archive;
  Resource* resource(nullptr);
  if (isStaticData) {
    resource = resourceForStaticData(request, factory, substituteData);
    // Abort the request if the archive doesn't contain the resource, except in
    // the case of data URLs which might have resources such as fonts that need
    // to be decoded only on demand.  These data URLs are allowed to be
    // processed using the normal ResourceFetcher machinery.
    if (!resource && !isDataUrl && m_archive)
      return nullptr;
  }
  if (!resource) {
    resource =
        memoryCache()->resourceForURL(request.url(), getCacheIdentifier());
  }

  // See if we can use an existing resource from the cache. If so, we need to
  // move it to be load blocking.
  moveCachedNonBlockingResourceToBlocking(resource, request);

  const RevalidationPolicy policy = determineRevalidationPolicy(
      factory.type(), request, resource, isStaticData);
  TRACE_EVENT_INSTANT1("blink", "ResourceFetcher::determineRevalidationPolicy",
                       TRACE_EVENT_SCOPE_THREAD, "revalidationPolicy", policy);

  updateMemoryCacheStats(resource, policy, request, factory, isStaticData);

  resourceRequest.setAllowStoredCredentials(
      request.options().allowCredentials == AllowStoredCredentials);

  switch (policy) {
    case Reload:
      memoryCache()->remove(resource);
    // Fall through
    case Load:
      resource = createResourceForLoading(request, request.charset(), factory);
      break;
    case Revalidate:
      initializeRevalidation(resourceRequest, resource);
      break;
    case Use:
      if (resource->isLinkPreload() && !request.isLinkPreload())
        resource->setLinkPreload(false);
      break;
  }
  if (!resource)
    return nullptr;
  if (resource->getType() != factory.type()) {
    DCHECK(request.forPreload());
    return nullptr;
  }

  if (!resource->isAlive())
    m_deadStatsRecorder.update(policy);

  if (policy != Use)
    resource->setIdentifier(identifier);

  if (!request.forPreload() || policy != Use) {
    // When issuing another request for a resource that is already in-flight
    // make sure to not demote the priority of the in-flight request. If the new
    // request isn't at the same priority as the in-flight request, only allow
    // promotions. This can happen when a visible image's priority is increased
    // and then another reference to the image is parsed (which would be at a
    // lower priority).
    if (resourceRequest.priority() > resource->resourceRequest().priority())
      resource->didChangePriority(resourceRequest.priority(), 0);
  }

  // If only the fragment identifiers differ, it is the same resource.
  DCHECK(equalIgnoringFragmentIdentifier(resource->url(), request.url()));
  requestLoadStarted(
      identifier, resource, request,
      policy == Use ? ResourceLoadingFromCache : ResourceLoadingFromNetwork,
      isStaticData);
  m_documentResources.set(
      MemoryCache::removeFragmentIdentifierIfNeeded(request.url()), resource);

  // Returns with an existing resource if the resource does not need to start
  // loading immediately. If revalidation policy was determined as |Revalidate|,
  // the resource was already initialized for the revalidation here, but won't
  // start loading.
  if (!resourceNeedsLoad(resource, request, policy))
    return resource;

  if (!startLoad(resource))
    return nullptr;
  scopedResourceLoadTracker.resourceLoadContinuesBeyondScope();

  DCHECK(!resource->errorOccurred() ||
         request.options().synchronousPolicy == RequestSynchronously);
  return resource;
}

void ResourceFetcher::resourceTimingReportTimerFired(TimerBase* timer) {
  DCHECK_EQ(timer, &m_resourceTimingReportTimer);
  Vector<std::unique_ptr<ResourceTimingInfo>> timingReports;
  timingReports.swap(m_scheduledResourceTimingReports);
  for (const auto& timingInfo : timingReports)
    context().addResourceTiming(*timingInfo);
}

void ResourceFetcher::determineRequestContext(ResourceRequest& request,
                                              Resource::Type type,
                                              bool isMainFrame) {
  WebURLRequest::RequestContext requestContext =
      requestContextFromType(isMainFrame, type);
  request.setRequestContext(requestContext);
}

void ResourceFetcher::determineRequestContext(ResourceRequest& request,
                                              Resource::Type type) {
  determineRequestContext(request, type, context().isMainFrame());
}

void ResourceFetcher::initializeResourceRequest(
    ResourceRequest& request,
    Resource::Type type,
    FetchRequest::DeferOption defer) {
  if (request.getCachePolicy() == WebCachePolicy::UseProtocolCachePolicy) {
    request.setCachePolicy(
        context().resourceRequestCachePolicy(request, type, defer));
  }
  if (request.requestContext() == WebURLRequest::RequestContextUnspecified)
    determineRequestContext(request, type);
  if (type == Resource::LinkPrefetch)
    request.setHTTPHeaderField(HTTPNames::Purpose, "prefetch");

  context().addAdditionalRequestHeaders(
      request,
      (type == Resource::MainResource) ? FetchMainResource : FetchSubresource);
}

void ResourceFetcher::initializeRevalidation(
    ResourceRequest& revalidatingRequest,
    Resource* resource) {
  DCHECK(resource);
  DCHECK(memoryCache()->contains(resource));
  DCHECK(resource->isLoaded());
  DCHECK(resource->canUseCacheValidator());
  DCHECK(!resource->isCacheValidator());
  DCHECK(!context().isControlledByServiceWorker());

  const AtomicString& lastModified =
      resource->response().httpHeaderField(HTTPNames::Last_Modified);
  const AtomicString& eTag =
      resource->response().httpHeaderField(HTTPNames::ETag);
  if (!lastModified.isEmpty() || !eTag.isEmpty()) {
    DCHECK_NE(context().getCachePolicy(), CachePolicyReload);
    if (context().getCachePolicy() == CachePolicyRevalidate) {
      revalidatingRequest.setHTTPHeaderField(HTTPNames::Cache_Control,
                                             "max-age=0");
    }
  }
  if (!lastModified.isEmpty()) {
    revalidatingRequest.setHTTPHeaderField(HTTPNames::If_Modified_Since,
                                           lastModified);
  }
  if (!eTag.isEmpty())
    revalidatingRequest.setHTTPHeaderField(HTTPNames::If_None_Match, eTag);

  double stalenessLifetime = resource->stalenessLifetime();
  if (std::isfinite(stalenessLifetime) && stalenessLifetime > 0) {
    revalidatingRequest.setHTTPHeaderField(
        HTTPNames::Resource_Freshness,
        AtomicString(String::format(
            "max-age=%.0lf,stale-while-revalidate=%.0lf,age=%.0lf",
            resource->freshnessLifetime(), stalenessLifetime,
            resource->currentAge())));
  }

  resource->setRevalidatingRequest(revalidatingRequest);
}

Resource* ResourceFetcher::createResourceForLoading(
    FetchRequest& request,
    const String& charset,
    const ResourceFactory& factory) {
  const String cacheIdentifier = getCacheIdentifier();
  DCHECK(!memoryCache()->resourceForURL(request.resourceRequest().url(),
                                        cacheIdentifier));

  RESOURCE_LOADING_DVLOG(1) << "Loading Resource for "
                            << request.resourceRequest().url().elidedString();

  Resource* resource =
      factory.create(request.resourceRequest(), request.options(), charset);
  resource->setLinkPreload(request.isLinkPreload());
  if (request.forPreload()) {
    resource->setPreloadDiscoveryTime(request.preloadDiscoveryTime());
  }
  resource->setCacheIdentifier(cacheIdentifier);

  // - Don't add main resource to cache to prevent reuse.
  // - Don't add the resource if its body will not be stored.
  if (factory.type() != Resource::MainResource &&
      request.options().dataBufferingPolicy != DoNotBufferData) {
    memoryCache()->add(resource);
  }
  return resource;
}

void ResourceFetcher::storePerformanceTimingInitiatorInformation(
    Resource* resource) {
  const AtomicString& fetchInitiator = resource->options().initiatorInfo.name;
  if (fetchInitiator == FetchInitiatorTypeNames::internal)
    return;

  bool isMainResource = resource->getType() == Resource::MainResource;

  // The request can already be fetched in a previous navigation. Thus
  // startTime must be set accordingly.
  double startTime = resource->resourceRequest().navigationStartTime()
                         ? resource->resourceRequest().navigationStartTime()
                         : monotonicallyIncreasingTime();

  // This buffer is created and populated for providing transferSize
  // and redirect timing opt-in information.
  if (isMainResource) {
    DCHECK(!m_navigationTimingInfo);
    m_navigationTimingInfo =
        ResourceTimingInfo::create(fetchInitiator, startTime, isMainResource);
  }

  std::unique_ptr<ResourceTimingInfo> info =
      ResourceTimingInfo::create(fetchInitiator, startTime, isMainResource);

  if (resource->isCacheValidator()) {
    const AtomicString& timingAllowOrigin =
        resource->response().httpHeaderField(HTTPNames::Timing_Allow_Origin);
    if (!timingAllowOrigin.isEmpty())
      info->setOriginalTimingAllowOrigin(timingAllowOrigin);
  }

  if (!isMainResource ||
      context().updateTimingInfoForIFrameNavigation(info.get())) {
    m_resourceTimingInfoMap.add(resource, std::move(info));
  }
}

void ResourceFetcher::recordResourceTimingOnRedirect(
    Resource* resource,
    const ResourceResponse& redirectResponse,
    bool crossOrigin) {
  ResourceTimingInfoMap::iterator it = m_resourceTimingInfoMap.find(resource);
  if (it != m_resourceTimingInfoMap.end()) {
    it->value->addRedirect(redirectResponse, crossOrigin);
  }

  if (resource->getType() == Resource::MainResource) {
    DCHECK(m_navigationTimingInfo);
    m_navigationTimingInfo->addRedirect(redirectResponse, crossOrigin);
  }
}

ResourceFetcher::RevalidationPolicy
ResourceFetcher::determineRevalidationPolicy(Resource::Type type,
                                             const FetchRequest& fetchRequest,
                                             Resource* existingResource,
                                             bool isStaticData) const {
  const ResourceRequest& request = fetchRequest.resourceRequest();

  if (!existingResource)
    return Load;

  // Checks if the resource has an explicit policy about integrity metadata.
  //
  // This is necessary because ScriptResource and CSSStyleSheetResource objects
  // do not keep the raw data around after the source is accessed once, so if
  // the resource is accessed from the MemoryCache for a second time, there is
  // no way to redo an integrity check.
  //
  // Thus, Blink implements a scheme where it caches the integrity information
  // for those resources after the first time it is checked, and if there is
  // another request for that resource, with the same integrity metadata, Blink
  // skips the integrity calculation. However, if the integrity metadata is a
  // mismatch, the MemoryCache must be skipped here, and a new request for the
  // resource must be made to get the raw data. This is expected to be an
  // uncommon case, however, as it implies two same-origin requests to the same
  // resource, but with different integrity metadata.
  RecordSriResourceIntegrityMismatchEvent(CheckingForIntegrityMismatch);
  if (existingResource->mustRefetchDueToIntegrityMetadata(fetchRequest)) {
    RecordSriResourceIntegrityMismatchEvent(RefetchDueToIntegrityMismatch);
    return Reload;
  }

  // Service Worker's CORS fallback message must not be cached.
  if (existingResource->response().wasFallbackRequiredByServiceWorker())
    return Reload;

  // We already have a preload going for this URL.
  if (fetchRequest.forPreload() && existingResource->isPreloaded())
    return Use;

  // If the same URL has been loaded as a different type, we need to reload.
  if (existingResource->getType() != type) {
    // FIXME: If existingResource is a Preload and the new type is LinkPrefetch
    // We really should discard the new prefetch since the preload has more
    // specific type information! crbug.com/379893
    // fast/dom/HTMLLinkElement/link-and-subresource-test hits this case.
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to type mismatch.";
    return Reload;
  }

  // Do not load from cache if images are not enabled. There are two general
  // cases:
  //
  // 1. Images are disabled. Don't ever load images, even if the image is cached
  // or it is a data: url. In this case, we "Reload" the image, then defer it
  // with resourceNeedsLoad() so that it never actually goes to the network.
  //
  // 2. Images are enabled, but not loaded automatically. In this case, we will
  // Use cached resources or data: urls, but will similarly fall back to a
  // deferred network load if we don't have the data available without a network
  // request. We check allowImage() here, which is affected by m_imagesEnabled
  // but not m_autoLoadImages, in order to allow for this differing behavior.
  //
  // TODO(japhet): Can we get rid of one of these settings?
  if (existingResource->isImage() &&
      !context().allowImage(m_imagesEnabled, existingResource->url()))
    return Reload;

  // Never use cache entries for downloadToFile / useStreamOnResponse requests.
  // The data will be delivered through other paths.
  if (request.downloadToFile() || request.useStreamOnResponse())
    return Reload;

  // Never reuse opaque responses from a service worker for requests that are
  // not no-cors. https://crbug.com/625575
  if (existingResource->response().wasFetchedViaServiceWorker() &&
      existingResource->response().serviceWorkerResponseType() ==
          WebServiceWorkerResponseTypeOpaque &&
      request.fetchRequestMode() != WebURLRequest::FetchRequestModeNoCORS)
    return Reload;

  // If resource was populated from a SubstituteData load or data: url, use it.
  if (isStaticData)
    return Use;

  if (!existingResource->canReuse(request))
    return Reload;

  // Certain requests (e.g., XHRs) might have manually set headers that require
  // revalidation. In theory, this should be a Revalidate case. In practice, the
  // MemoryCache revalidation path assumes a whole bunch of things about how
  // revalidation works that manual headers violate, so punt to Reload instead.
  //
  // Similarly, a request with manually added revalidation headers can lead to a
  // 304 response for a request that wasn't flagged as a revalidation attempt.
  // Normally, successful revalidation will maintain the original response's
  // status code, but for a manual revalidation the response code remains 304.
  // In this case, the Resource likely has insufficient context to provide a
  // useful cache hit or revalidation. See http://crbug.com/643659
  if (request.isConditional() ||
      existingResource->response().httpStatusCode() == 304)
    return Reload;

  // Don't reload resources while pasting.
  if (m_allowStaleResources)
    return Use;

  if (!fetchRequest.options().canReuseRequest(existingResource->options()))
    return Reload;

  // Always use preloads.
  if (existingResource->isPreloaded())
    return Use;

  // CachePolicyHistoryBuffer uses the cache no matter what.
  CachePolicy cachePolicy = context().getCachePolicy();
  if (cachePolicy == CachePolicyHistoryBuffer)
    return Use;

  // Don't reuse resources with Cache-control: no-store.
  if (existingResource->hasCacheControlNoStoreHeader()) {
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to Cache-control: no-store.";
    return Reload;
  }

  // If credentials were sent with the previous request and won't be with this
  // one, or vice versa, re-fetch the resource.
  //
  // This helps with the case where the server sends back
  // "Access-Control-Allow-Origin: *" all the time, but some of the client's
  // requests are made without CORS and some with.
  if (existingResource->resourceRequest().allowStoredCredentials() !=
      request.allowStoredCredentials()) {
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to difference in credentials "
                                 "settings.";
    return Reload;
  }

  // During the initial load, avoid loading the same resource multiple times for
  // a single document, even if the cache policies would tell us to. We also
  // group loads of the same resource together. Raw resources are exempted, as
  // XHRs fall into this category and may have user-set Cache-Control: headers
  // or other factors that require separate requests.
  if (type != Resource::Raw) {
    if (!context().isLoadComplete() &&
        m_validatedURLs.contains(existingResource->url()))
      return Use;
    if (existingResource->isLoading())
      return Use;
  }

  if (request.getCachePolicy() == WebCachePolicy::BypassingCache)
    return Reload;

  // CachePolicyReload always reloads
  if (cachePolicy == CachePolicyReload) {
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to CachePolicyReload.";
    return Reload;
  }

  // We'll try to reload the resource if it failed last time.
  if (existingResource->errorOccurred()) {
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::"
                                 "determineRevalidationPolicye reloading due "
                                 "to resource being in the error state";
    return Reload;
  }

  // List of available images logic allows images to be re-used without cache
  // validation. We restrict this only to images from memory cache which are the
  // same as the version in the current document.
  if (type == Resource::Image &&
      existingResource == cachedResource(request.url()))
    return Use;

  // Defer to the browser process cache for Vary header handling.
  if (existingResource->hasVaryHeader())
    return Reload;

  // If any of the redirects in the chain to loading the resource were not
  // cacheable, we cannot reuse our cached resource.
  if (!existingResource->canReuseRedirectChain()) {
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to an uncacheable redirect";
    return Reload;
  }

  // Check if the cache headers requires us to revalidate (cache expiration for
  // example).
  if (cachePolicy == CachePolicyRevalidate ||
      existingResource->mustRevalidateDueToCacheHeaders() ||
      request.cacheControlContainsNoCache()) {
    // See if the resource has usable ETag or Last-modified headers. If the page
    // is controlled by the ServiceWorker, we choose the Reload policy because
    // the revalidation headers should not be exposed to the
    // ServiceWorker.(crbug.com/429570)
    if (existingResource->canUseCacheValidator() &&
        !context().isControlledByServiceWorker()) {
      // If the resource is already a cache validator but not started yet, the
      // |Use| policy should be applied to subsequent requests.
      if (existingResource->isCacheValidator()) {
        DCHECK(existingResource->stillNeedsLoad());
        return Use;
      }
      return Revalidate;
    }

    // No, must reload.
    RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
                                 "reloading due to missing cache validators.";
    return Reload;
  }

  return Use;
}

void ResourceFetcher::setAutoLoadImages(bool enable) {
  if (enable == m_autoLoadImages)
    return;

  m_autoLoadImages = enable;

  if (!m_autoLoadImages)
    return;

  reloadImagesIfNotDeferred();
}

void ResourceFetcher::setImagesEnabled(bool enable) {
  if (enable == m_imagesEnabled)
    return;

  m_imagesEnabled = enable;

  if (!m_imagesEnabled)
    return;

  reloadImagesIfNotDeferred();
}

bool ResourceFetcher::shouldDeferImageLoad(const KURL& url) const {
  return !context().allowImage(m_imagesEnabled, url) || !m_autoLoadImages;
}

void ResourceFetcher::reloadImagesIfNotDeferred() {
  for (Resource* resource : m_documentResources.values()) {
    if (resource->getType() == Resource::Image && resource->stillNeedsLoad() &&
        !shouldDeferImageLoad(resource->url()))
      startLoad(resource);
  }
}

void ResourceFetcher::clearContext() {
  clearPreloads(ResourceFetcher::ClearAllPreloads);
  m_context.clear();
}

int ResourceFetcher::requestCount() const {
  return m_loaders.size();
}

bool ResourceFetcher::hasPendingRequest() const {
  return m_loaders.size() > 0 || m_nonBlockingLoaders.size() > 0;
}

void ResourceFetcher::preloadStarted(Resource* resource) {
  if (m_preloads && m_preloads->contains(resource))
    return;
  resource->increasePreloadCount();

  if (!m_preloads)
    m_preloads = new HeapListHashSet<Member<Resource>>;
  m_preloads->add(resource);

  if (m_preloadedURLsForTest)
    m_preloadedURLsForTest->add(resource->url().getString());
}

void ResourceFetcher::enableIsPreloadedForTest() {
  if (m_preloadedURLsForTest)
    return;
  m_preloadedURLsForTest = WTF::wrapUnique(new HashSet<String>);

  if (m_preloads) {
    for (const auto& resource : *m_preloads)
      m_preloadedURLsForTest->add(resource->url().getString());
  }
}

bool ResourceFetcher::isPreloadedForTest(const KURL& url) const {
  DCHECK(m_preloadedURLsForTest);
  return m_preloadedURLsForTest->contains(url.getString());
}

void ResourceFetcher::clearPreloads(ClearPreloadsPolicy policy) {
  if (!m_preloads)
    return;

  logPreloadStats(policy);

  for (const auto& resource : *m_preloads) {
    if (policy == ClearAllPreloads || !resource->isLinkPreload()) {
      resource->decreasePreloadCount();
      if (resource->getPreloadResult() == Resource::PreloadNotReferenced)
        memoryCache()->remove(resource.get());
      m_preloads->remove(resource);
    }
  }
  if (!m_preloads->size())
    m_preloads.clear();
}

void ResourceFetcher::warnUnusedPreloads() {
  if (!m_preloads)
    return;
  for (const auto& resource : *m_preloads) {
    if (resource && resource->isLinkPreload() &&
        resource->getPreloadResult() == Resource::PreloadNotReferenced) {
      context().addConsoleMessage(
          "The resource " + resource->url().getString() +
              " was preloaded using link preload but not used within a few "
              "seconds from the window's load event. Please make sure it "
              "wasn't preloaded for nothing.",
          FetchContext::LogWarningMessage);
    }
  }
}

ArchiveResource* ResourceFetcher::createArchive(Resource* resource) {
  // Only the top-frame can load MHTML.
  if (!context().isMainFrame())
    return nullptr;
  m_archive = MHTMLArchive::create(resource->url(), resource->resourceBuffer());
  return m_archive ? m_archive->mainResource() : nullptr;
}

ResourceTimingInfo* ResourceFetcher::getNavigationTimingInfo() {
  return m_navigationTimingInfo.get();
}

void ResourceFetcher::handleLoadCompletion(Resource* resource) {
  context().didLoadResource(resource);

  resource->reloadIfLoFiOrPlaceholderImage(this, Resource::kReloadIfNeeded);
}

void ResourceFetcher::handleLoaderFinish(Resource* resource,
                                         double finishTime,
                                         LoaderFinishType type) {
  DCHECK(resource);

  ResourceLoader* loader = resource->loader();
  if (type == DidFinishFirstPartInMultipart) {
    // When loading a multipart resource, make the loader non-block when
    // finishing loading the first part.
    moveResourceLoaderToNonBlocking(loader);
  } else {
    removeResourceLoader(loader);
    DCHECK(!m_nonBlockingLoaders.contains(loader));
  }
  DCHECK(!m_loaders.contains(loader));

  const int64_t encodedDataLength = resource->response().encodedDataLength();

  if (resource->getType() == Resource::MainResource) {
    DCHECK(m_navigationTimingInfo);
    // Store redirect responses that were packed inside the final response.
    addRedirectsToTimingInfo(resource, m_navigationTimingInfo.get());
    if (resource->response().isHTTP()) {
      populateTimingInfo(m_navigationTimingInfo.get(), resource);
      m_navigationTimingInfo->addFinalTransferSize(
          encodedDataLength == -1 ? 0 : encodedDataLength);
    }
  }
  if (std::unique_ptr<ResourceTimingInfo> info =
          m_resourceTimingInfoMap.take(resource)) {
    // Store redirect responses that were packed inside the final response.
    addRedirectsToTimingInfo(resource, info.get());

    if (resource->response().isHTTP() &&
        resource->response().httpStatusCode() < 400) {
      populateTimingInfo(info.get(), resource);
      info->setLoadFinishTime(finishTime);
      // encodedDataLength == -1 means "not available".
      // TODO(ricea): Find cases where it is not available but the
      // PerformanceResourceTiming spec requires it to be available and fix
      // them.
      info->addFinalTransferSize(encodedDataLength == -1 ? 0
                                                         : encodedDataLength);

      if (resource->options().requestInitiatorContext == DocumentContext)
        context().addResourceTiming(*info);
      resource->reportResourceTimingToClients(*info);
    }
  }

  context().dispatchDidFinishLoading(resource->identifier(), finishTime,
                                     encodedDataLength);

  if (type == DidFinishLoading)
    resource->finish(finishTime);

  handleLoadCompletion(resource);
}

void ResourceFetcher::handleLoaderError(Resource* resource,
                                        const ResourceError& error) {
  DCHECK(resource);

  removeResourceLoader(resource->loader());

  m_resourceTimingInfoMap.take(resource);

  bool isInternalRequest = resource->options().initiatorInfo.name ==
                           FetchInitiatorTypeNames::internal;

  context().dispatchDidFail(resource->identifier(), error,
                            resource->response().encodedDataLength(),
                            isInternalRequest);

  resource->error(error);

  handleLoadCompletion(resource);
}

void ResourceFetcher::moveResourceLoaderToNonBlocking(ResourceLoader* loader) {
  DCHECK(loader);
  // TODO(yoav): Convert CHECK to DCHECK if no crash reports come in.
  CHECK(m_loaders.contains(loader));
  m_nonBlockingLoaders.add(loader);
  m_loaders.remove(loader);
}

bool ResourceFetcher::startLoad(Resource* resource) {
  DCHECK(resource);
  DCHECK(resource->stillNeedsLoad());
  if (!context().shouldLoadNewResource(resource->getType())) {
    memoryCache()->remove(resource);
    return false;
  }

  ResourceRequest request(resource->resourceRequest());
  context().dispatchWillSendRequest(resource->identifier(), request,
                                    ResourceResponse(),
                                    resource->options().initiatorInfo);

  // TODO(shaochuan): Saving modified ResourceRequest back to |resource|, remove
  // once dispatchWillSendRequest() takes const ResourceRequest.
  // crbug.com/632580
  resource->setResourceRequest(request);

  // Resource requests from suborigins should not be intercepted by the service
  // worker of the physical origin. This has the effect that, for now,
  // suborigins do not work with service workers. See
  // https://w3c.github.io/webappsec-suborigins/.
  SecurityOrigin* sourceOrigin = context().getSecurityOrigin();
  if (sourceOrigin && sourceOrigin->hasSuborigin())
    request.setSkipServiceWorker(WebURLRequest::SkipServiceWorker::All);

  ResourceLoader* loader = ResourceLoader::create(this, resource);
  if (resource->shouldBlockLoadEvent())
    m_loaders.add(loader);
  else
    m_nonBlockingLoaders.add(loader);

  storePerformanceTimingInitiatorInformation(resource);
  resource->setFetcherSecurityOrigin(sourceOrigin);

  loader->activateCacheAwareLoadingIfNeeded(request);
  loader->start(request);
  return true;
}

void ResourceFetcher::removeResourceLoader(ResourceLoader* loader) {
  DCHECK(loader);
  if (m_loaders.contains(loader))
    m_loaders.remove(loader);
  else if (m_nonBlockingLoaders.contains(loader))
    m_nonBlockingLoaders.remove(loader);
  else
    NOTREACHED();
}

void ResourceFetcher::stopFetching() {
  HeapVector<Member<ResourceLoader>> loadersToCancel;
  for (const auto& loader : m_nonBlockingLoaders)
    loadersToCancel.push_back(loader);
  for (const auto& loader : m_loaders)
    loadersToCancel.push_back(loader);

  for (const auto& loader : loadersToCancel) {
    if (m_loaders.contains(loader) || m_nonBlockingLoaders.contains(loader))
      loader->cancel();
  }
}

bool ResourceFetcher::isFetching() const {
  return !m_loaders.isEmpty();
}

void ResourceFetcher::setDefersLoading(bool defers) {
  for (const auto& loader : m_nonBlockingLoaders)
    loader->setDefersLoading(defers);
  for (const auto& loader : m_loaders)
    loader->setDefersLoading(defers);
}

void ResourceFetcher::updateAllImageResourcePriorities() {
  TRACE_EVENT0(
      "blink",
      "ResourceLoadPriorityOptimizer::updateAllImageResourcePriorities");
  for (const auto& documentResource : m_documentResources) {
    Resource* resource = documentResource.value.get();
    if (!resource || !resource->isImage() || !resource->isLoading())
      continue;

    ResourcePriority resourcePriority = resource->priorityFromObservers();
    ResourceLoadPriority resourceLoadPriority = computeLoadPriority(
        Resource::Image,
        FetchRequest(resource->resourceRequest(), FetchInitiatorInfo()),
        resourcePriority.visibility);
    if (resourceLoadPriority == resource->resourceRequest().priority())
      continue;

    resource->didChangePriority(resourceLoadPriority,
                                resourcePriority.intraPriorityValue);
    network_instrumentation::resourcePrioritySet(resource->identifier(),
                                                 resourceLoadPriority);
    context().dispatchDidChangeResourcePriority(
        resource->identifier(), resourceLoadPriority,
        resourcePriority.intraPriorityValue);
  }
}

void ResourceFetcher::reloadLoFiImages() {
  for (const auto& documentResource : m_documentResources) {
    Resource* resource = documentResource.value.get();
    if (resource)
      resource->reloadIfLoFiOrPlaceholderImage(this, Resource::kReloadAlways);
  }
}

void ResourceFetcher::logPreloadStats(ClearPreloadsPolicy policy) {
  if (!m_preloads)
    return;
  unsigned scripts = 0;
  unsigned scriptMisses = 0;
  unsigned stylesheets = 0;
  unsigned stylesheetMisses = 0;
  unsigned images = 0;
  unsigned imageMisses = 0;
  unsigned fonts = 0;
  unsigned fontMisses = 0;
  unsigned medias = 0;
  unsigned mediaMisses = 0;
  unsigned textTracks = 0;
  unsigned textTrackMisses = 0;
  unsigned imports = 0;
  unsigned importMisses = 0;
  unsigned raws = 0;
  unsigned rawMisses = 0;
  for (const auto& resource : *m_preloads) {
    // Do not double count link rel preloads. These do not get cleared if the
    // ClearPreloadsPolicy is only clearing speculative markup preloads.
    if (resource->isLinkPreload() && policy == ClearSpeculativeMarkupPreloads) {
      continue;
    }
    int missCount =
        resource->getPreloadResult() == Resource::PreloadNotReferenced ? 1 : 0;
    switch (resource->getType()) {
      case Resource::Image:
        images++;
        imageMisses += missCount;
        break;
      case Resource::Script:
        scripts++;
        scriptMisses += missCount;
        break;
      case Resource::CSSStyleSheet:
        stylesheets++;
        stylesheetMisses += missCount;
        break;
      case Resource::Font:
        fonts++;
        fontMisses += missCount;
        break;
      case Resource::Media:
        medias++;
        mediaMisses += missCount;
        break;
      case Resource::TextTrack:
        textTracks++;
        textTrackMisses += missCount;
        break;
      case Resource::ImportResource:
        imports++;
        importMisses += missCount;
        break;
      case Resource::Raw:
        raws++;
        rawMisses += missCount;
        break;
      case Resource::Mock:
        // Do not count Resource::Mock because this type is only for testing.
        break;
      default:
        NOTREACHED();
    }
  }
  DEFINE_STATIC_LOCAL(CustomCountHistogram, imagePreloads,
                      ("PreloadScanner.Counts2.Image", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, imagePreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Image", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, scriptPreloads,
                      ("PreloadScanner.Counts2.Script", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, scriptPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Script", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, stylesheetPreloads,
                      ("PreloadScanner.Counts2.CSSStyleSheet", 0, 100, 25));
  DEFINE_STATIC_LOCAL(
      CustomCountHistogram, stylesheetPreloadMisses,
      ("PreloadScanner.Counts2.Miss.CSSStyleSheet", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, fontPreloads,
                      ("PreloadScanner.Counts2.Font", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, fontPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Font", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, mediaPreloads,
                      ("PreloadScanner.Counts2.Media", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, mediaPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Media", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, textTrackPreloads,
                      ("PreloadScanner.Counts2.TextTrack", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, textTrackPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.TextTrack", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, importPreloads,
                      ("PreloadScanner.Counts2.Import", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, importPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Import", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, rawPreloads,
                      ("PreloadScanner.Counts2.Raw", 0, 100, 25));
  DEFINE_STATIC_LOCAL(CustomCountHistogram, rawPreloadMisses,
                      ("PreloadScanner.Counts2.Miss.Raw", 0, 100, 25));
  if (images)
    imagePreloads.count(images);
  if (imageMisses)
    imagePreloadMisses.count(imageMisses);
  if (scripts)
    scriptPreloads.count(scripts);
  if (scriptMisses)
    scriptPreloadMisses.count(scriptMisses);
  if (stylesheets)
    stylesheetPreloads.count(stylesheets);
  if (stylesheetMisses)
    stylesheetPreloadMisses.count(stylesheetMisses);
  if (fonts)
    fontPreloads.count(fonts);
  if (fontMisses)
    fontPreloadMisses.count(fontMisses);
  if (medias)
    mediaPreloads.count(medias);
  if (mediaMisses)
    mediaPreloadMisses.count(mediaMisses);
  if (textTracks)
    textTrackPreloads.count(textTracks);
  if (textTrackMisses)
    textTrackPreloadMisses.count(textTrackMisses);
  if (imports)
    importPreloads.count(imports);
  if (importMisses)
    importPreloadMisses.count(importMisses);
  if (raws)
    rawPreloads.count(raws);
  if (rawMisses)
    rawPreloadMisses.count(rawMisses);
}

const ResourceLoaderOptions& ResourceFetcher::defaultResourceOptions() {
  DEFINE_STATIC_LOCAL(
      ResourceLoaderOptions, options,
      (BufferData, AllowStoredCredentials, ClientRequestedCredentials,
       CheckContentSecurityPolicy, DocumentContext));
  return options;
}

String ResourceFetcher::getCacheIdentifier() const {
  if (context().isControlledByServiceWorker())
    return String::number(context().serviceWorkerID());
  return MemoryCache::defaultCacheIdentifier();
}

void ResourceFetcher::emulateLoadStartedForInspector(
    Resource* resource,
    const KURL& url,
    WebURLRequest::RequestContext requestContext,
    const AtomicString& initiatorName) {
  if (cachedResource(url))
    return;
  ResourceRequest resourceRequest(url);
  resourceRequest.setRequestContext(requestContext);
  FetchRequest request(resourceRequest, initiatorName, resource->options());
  context().canRequest(resource->getType(), resource->lastResourceRequest(),
                       resource->lastResourceRequest().url(), request.options(),
                       false, request.getOriginRestriction());
  requestLoadStarted(resource->identifier(), resource, request,
                     ResourceLoadingFromCache);
}

ResourceFetcher::DeadResourceStatsRecorder::DeadResourceStatsRecorder()
    : m_useCount(0), m_revalidateCount(0), m_loadCount(0) {}

ResourceFetcher::DeadResourceStatsRecorder::~DeadResourceStatsRecorder() {
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, hitCountHistogram,
      new CustomCountHistogram("WebCore.ResourceFetcher.HitCount", 0, 1000,
                               50));
  hitCountHistogram.count(m_useCount);
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, revalidateCountHistogram,
      new CustomCountHistogram("WebCore.ResourceFetcher.RevalidateCount", 0,
                               1000, 50));
  revalidateCountHistogram.count(m_revalidateCount);
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, loadCountHistogram,
      new CustomCountHistogram("WebCore.ResourceFetcher.LoadCount", 0, 1000,
                               50));
  loadCountHistogram.count(m_loadCount);
}

void ResourceFetcher::DeadResourceStatsRecorder::update(
    RevalidationPolicy policy) {
  switch (policy) {
    case Reload:
    case Load:
      ++m_loadCount;
      return;
    case Revalidate:
      ++m_revalidateCount;
      return;
    case Use:
      ++m_useCount;
      return;
  }
}

DEFINE_TRACE(ResourceFetcher) {
  visitor->trace(m_context);
  visitor->trace(m_archive);
  visitor->trace(m_loaders);
  visitor->trace(m_nonBlockingLoaders);
  visitor->trace(m_documentResources);
  visitor->trace(m_preloads);
  visitor->trace(m_resourceTimingInfoMap);
}

}  // namespace blink