1 // Copyright 2017 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_PLATFORM_LOADER_FETCH_PRELOAD_KEY_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_PRELOAD_KEY_H_
7 
8 #include "third_party/blink/renderer/platform/loader/fetch/resource.h"
9 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
10 #include "third_party/blink/renderer/platform/weborigin/kurl_hash.h"
11 #include "third_party/blink/renderer/platform/wtf/hash_traits.h"
12 
13 namespace blink {
14 
15 // PreloadKey is a key type of the preloads map in a fetch group (a.k.a.
16 // blink::ResourceFetcher).
17 struct PreloadKey final {
18  public:
19   struct Hash {
20     STATIC_ONLY(Hash);
21 
22    public:
GetHashfinal::Hash23     static unsigned GetHash(const PreloadKey& key) {
24       return KURLHash::GetHash(key.url);
25     }
Equalfinal::Hash26     static bool Equal(const PreloadKey& x, const PreloadKey& y) {
27       return x == y;
28     }
29     static constexpr bool safe_to_compare_to_empty_or_deleted = false;
30   };
31 
32   PreloadKey() = default;
PreloadKeyfinal33   PreloadKey(const KURL& url, ResourceType type)
34       : url(RemoveFragmentFromUrl(url)), type(type) {}
35 
36   bool operator==(const PreloadKey& x) const {
37     return url == x.url && type == x.type;
38   }
39 
RemoveFragmentFromUrlfinal40   static KURL RemoveFragmentFromUrl(const KURL& src) {
41     if (!src.HasFragmentIdentifier())
42       return src;
43     KURL url = src;
44     url.RemoveFragmentIdentifier();
45     return url;
46   }
47 
48   KURL url;
49   ResourceType type = ResourceType::kImage;
50 };
51 
52 }  // namespace blink
53 
54 namespace WTF {
55 
56 template <>
57 struct DefaultHash<blink::PreloadKey> {
58   using Hash = blink::PreloadKey::Hash;
59 };
60 
61 template <>
62 struct HashTraits<blink::PreloadKey>
63     : public SimpleClassHashTraits<blink::PreloadKey> {
64   static const bool kEmptyValueIsZero = false;
65 
66   static bool IsDeletedValue(const blink::PreloadKey& value) {
67     return HashTraits<blink::KURL>::IsDeletedValue(value.url);
68   }
69 
70   static void ConstructDeletedValue(blink::PreloadKey& slot, bool zero_value) {
71     HashTraits<blink::KURL>::ConstructDeletedValue(slot.url, zero_value);
72   }
73 };
74 
75 }  // namespace WTF
76 
77 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_PRELOAD_KEY_H_
78