1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRELOAD_HELPER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRELOAD_HELPER_H_
7 
8 #include "base/optional.h"
9 #include "third_party/blink/renderer/platform/loader/fetch/resource.h"
10 
11 namespace blink {
12 
13 class AlternateSignedExchangeResourceInfo;
14 class Document;
15 class LocalFrame;
16 class SingleModuleClient;
17 struct LinkLoadParameters;
18 struct ViewportDescription;
19 
20 // PreloadHelper is a helper class for preload, module preload, prefetch,
21 // DNS prefetch, and preconnect triggered by <link> elements and "Link" HTTP
22 // response headers.
23 class PreloadHelper final {
24   STATIC_ONLY(PreloadHelper);
25 
26  public:
27   enum CanLoadResources {
28     kOnlyLoadResources,
29     kDoNotLoadResources,
30     kLoadResourcesAndPreconnect
31   };
32 
33   // Media links cannot be preloaded until the first chunk is parsed. The rest
34   // can be preloaded at commit time.
35   enum MediaPreloadPolicy { kLoadAll, kOnlyLoadNonMedia, kOnlyLoadMedia };
36 
37   static void LoadLinksFromHeader(
38       const String& header_value,
39       const KURL& base_url,
40       LocalFrame&,
41       Document*,  // can be nullptr
42       CanLoadResources,
43       MediaPreloadPolicy,
44       const ViewportDescription*,  // can be nullptr
45       std::unique_ptr<AlternateSignedExchangeResourceInfo>,
46       const base::UnguessableToken* /* can be nullptr */);
47   static Resource* StartPreload(ResourceType, FetchParameters&, Document&);
48 
49   // Currently only used for UseCounter.
50   enum LinkCaller {
51     kLinkCalledFromHeader,
52     kLinkCalledFromMarkup,
53   };
54 
55   static void DnsPrefetchIfNeeded(const LinkLoadParameters&,
56                                   Document*,
57                                   LocalFrame*,
58                                   LinkCaller);
59   static void PreconnectIfNeeded(const LinkLoadParameters&,
60                                  Document*,
61                                  LocalFrame*,
62                                  LinkCaller);
63   static Resource* PrefetchIfNeeded(const LinkLoadParameters&, Document&);
64   static Resource* PreloadIfNeeded(const LinkLoadParameters&,
65                                    Document&,
66                                    const KURL& base_url,
67                                    LinkCaller,
68                                    const ViewportDescription*,
69                                    ParserDisposition);
70   static void ModulePreloadIfNeeded(const LinkLoadParameters&,
71                                     Document&,
72                                     const ViewportDescription*,
73                                     SingleModuleClient*);
74 
75   static base::Optional<ResourceType> GetResourceTypeFromAsAttribute(
76       const String& as);
77 };
78 
79 }  // namespace blink
80 
81 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_PRELOAD_HELPER_H_
82