File: ResourceFetcher.h

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 (282 lines) | stat: -rw-r--r-- 10,041 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
/*
    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
    Copyright (C) 2001 Dirk Mueller <mueller@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.
*/

#ifndef ResourceFetcher_h
#define ResourceFetcher_h

#include "core/CoreExport.h"
#include "core/fetch/CachePolicy.h"
#include "core/fetch/FetchContext.h"
#include "core/fetch/FetchInitiatorInfo.h"
#include "core/fetch/FetchRequest.h"
#include "core/fetch/Resource.h"
#include "core/fetch/ResourceLoaderOptions.h"
#include "core/fetch/SubstituteData.h"
#include "platform/Timer.h"
#include "platform/network/ResourceError.h"
#include "platform/network/ResourceLoadPriority.h"
#include "wtf/HashMap.h"
#include "wtf/HashSet.h"
#include "wtf/ListHashSet.h"
#include "wtf/text/StringHash.h"
#include <memory>

namespace blink {

class ArchiveResource;
class MHTMLArchive;
class KURL;
class ResourceTimingInfo;

// The ResourceFetcher provides a per-context interface to the MemoryCache and
// enforces a bunch of security checks and rules for resource revalidation. Its
// lifetime is roughly per-DocumentLoader, in that it is generally created in
// the DocumentLoader constructor and loses its ability to generate network
// requests when the DocumentLoader is destroyed. Documents also hold a pointer
// to ResourceFetcher for their lifetime (and will create one if they are
// initialized without a LocalFrame), so a Document can keep a ResourceFetcher
// alive past detach if scripts still reference the Document.
class CORE_EXPORT ResourceFetcher
    : public GarbageCollectedFinalized<ResourceFetcher> {
  WTF_MAKE_NONCOPYABLE(ResourceFetcher);
  USING_PRE_FINALIZER(ResourceFetcher, clearPreloads);

 public:
  static ResourceFetcher* create(FetchContext* context) {
    return new ResourceFetcher(context);
  }
  virtual ~ResourceFetcher();
  DECLARE_VIRTUAL_TRACE();

  Resource* requestResource(FetchRequest&,
                            const ResourceFactory&,
                            const SubstituteData& = SubstituteData());

  Resource* cachedResource(const KURL&) const;

  using DocumentResourceMap = HeapHashMap<String, WeakMember<Resource>>;
  const DocumentResourceMap& allResources() const {
    return m_documentResources;
  }

  // Actually starts loading a Resource if it wasn't started during
  // requestResource().
  bool startLoad(Resource*);

  void setAutoLoadImages(bool);
  void setImagesEnabled(bool);

  FetchContext& context() const {
    return m_context ? *m_context.get() : FetchContext::nullInstance();
  }
  void clearContext();

  int requestCount() const;
  bool hasPendingRequest() const;

  enum ClearPreloadsPolicy { ClearAllPreloads, ClearSpeculativeMarkupPreloads };

  void enableIsPreloadedForTest();
  bool isPreloadedForTest(const KURL&) const;

  int countPreloads() const { return m_preloads ? m_preloads->size() : 0; }
  void clearPreloads(ClearPreloadsPolicy = ClearAllPreloads);
  void preloadStarted(Resource*);
  void logPreloadStats(ClearPreloadsPolicy);
  void warnUnusedPreloads();

  MHTMLArchive* archive() const { return m_archive.get(); }
  ArchiveResource* createArchive(Resource*);

  void setDefersLoading(bool);
  void stopFetching();
  bool isFetching() const;

  bool shouldDeferImageLoad(const KURL&) const;

  void recordResourceTimingOnRedirect(Resource*, const ResourceResponse&, bool);

  enum LoaderFinishType { DidFinishLoading, DidFinishFirstPartInMultipart };
  void handleLoaderFinish(Resource*, double finishTime, LoaderFinishType);
  void handleLoaderError(Resource*, const ResourceError&);
  bool isControlledByServiceWorker() const;

  enum ResourceLoadStartType {
    ResourceLoadingFromNetwork,
    ResourceLoadingFromCache
  };
  static const ResourceLoaderOptions& defaultResourceOptions();

  String getCacheIdentifier() const;

  static void determineRequestContext(ResourceRequest&,
                                      Resource::Type,
                                      bool isMainFrame);
  void determineRequestContext(ResourceRequest&, Resource::Type);

  void updateAllImageResourcePriorities();

  void reloadLoFiImages();

  // Calling this method before main document resource is fetched is invalid.
  ResourceTimingInfo* getNavigationTimingInfo();

  // This is only exposed for testing purposes.
  HeapListHashSet<Member<Resource>>* preloads() { return m_preloads.get(); }

  // Workaround for https://crbug.com/666214.
  // TODO(hiroshige): Remove this hack.
  void emulateLoadStartedForInspector(Resource*,
                                      const KURL&,
                                      WebURLRequest::RequestContext,
                                      const AtomicString& initiatorName);

 private:
  friend class ResourceCacheValidationSuppressor;

  explicit ResourceFetcher(FetchContext*);

  void initializeRevalidation(ResourceRequest&, Resource*);
  Resource* createResourceForLoading(FetchRequest&,
                                     const String& charset,
                                     const ResourceFactory&);
  void storePerformanceTimingInitiatorInformation(Resource*);
  ResourceLoadPriority computeLoadPriority(Resource::Type,
                                           const FetchRequest&,
                                           ResourcePriority::VisibilityStatus);

  Resource* resourceForStaticData(const FetchRequest&,
                                  const ResourceFactory&,
                                  const SubstituteData&);
  Resource* resourceForBlockedRequest(const FetchRequest&,
                                      const ResourceFactory&,
                                      ResourceRequestBlockedReason);

  // RevalidationPolicy enum values are used in UMAs https://crbug.com/579496.
  enum RevalidationPolicy { Use, Revalidate, Reload, Load };
  RevalidationPolicy determineRevalidationPolicy(Resource::Type,
                                                 const FetchRequest&,
                                                 Resource* existingResource,
                                                 bool isStaticData) const;

  void moveCachedNonBlockingResourceToBlocking(Resource*, const FetchRequest&);
  void moveResourceLoaderToNonBlocking(ResourceLoader*);
  void removeResourceLoader(ResourceLoader*);
  void handleLoadCompletion(Resource*);

  void initializeResourceRequest(ResourceRequest&,
                                 Resource::Type,
                                 FetchRequest::DeferOption);
  void requestLoadStarted(unsigned long identifier,
                          Resource*,
                          const FetchRequest&,
                          ResourceLoadStartType,
                          bool isStaticData = false);

  bool resourceNeedsLoad(Resource*, const FetchRequest&, RevalidationPolicy);

  void resourceTimingReportTimerFired(TimerBase*);

  void reloadImagesIfNotDeferred();

  void updateMemoryCacheStats(Resource*,
                              RevalidationPolicy,
                              const FetchRequest&,
                              const ResourceFactory&,
                              bool isStaticData) const;

  Member<FetchContext> m_context;

  HashSet<String> m_validatedURLs;
  mutable DocumentResourceMap m_documentResources;

  Member<HeapListHashSet<Member<Resource>>> m_preloads;
  Member<MHTMLArchive> m_archive;

  TaskRunnerTimer<ResourceFetcher> m_resourceTimingReportTimer;

  using ResourceTimingInfoMap =
      HeapHashMap<Member<Resource>, std::unique_ptr<ResourceTimingInfo>>;
  ResourceTimingInfoMap m_resourceTimingInfoMap;

  std::unique_ptr<ResourceTimingInfo> m_navigationTimingInfo;

  Vector<std::unique_ptr<ResourceTimingInfo>> m_scheduledResourceTimingReports;

  HeapHashSet<Member<ResourceLoader>> m_loaders;
  HeapHashSet<Member<ResourceLoader>> m_nonBlockingLoaders;

  // Used in hit rate histograms.
  class DeadResourceStatsRecorder {
    DISALLOW_NEW();

   public:
    DeadResourceStatsRecorder();
    ~DeadResourceStatsRecorder();

    void update(RevalidationPolicy);

   private:
    int m_useCount;
    int m_revalidateCount;
    int m_loadCount;
  };
  DeadResourceStatsRecorder m_deadStatsRecorder;

  std::unique_ptr<HashSet<String>> m_preloadedURLsForTest;

  // 28 bits left
  bool m_autoLoadImages : 1;
  bool m_imagesEnabled : 1;
  bool m_allowStaleResources : 1;
  bool m_imageFetched : 1;
};

class ResourceCacheValidationSuppressor {
  WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor);
  STACK_ALLOCATED();

 public:
  explicit ResourceCacheValidationSuppressor(ResourceFetcher* loader)
      : m_loader(loader), m_previousState(false) {
    if (m_loader) {
      m_previousState = m_loader->m_allowStaleResources;
      m_loader->m_allowStaleResources = true;
    }
  }
  ~ResourceCacheValidationSuppressor() {
    if (m_loader)
      m_loader->m_allowStaleResources = m_previousState;
  }

 private:
  Member<ResourceFetcher> m_loader;
  bool m_previousState;
};

}  // namespace blink

#endif  // ResourceFetcher_h