summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/loader/fetch/source_keyed_cached_metadata_handler.h
blob: 004d39a6bbba770ca57f559abf4a3076891b4334 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SOURCE_KEYED_CACHED_METADATA_HANDLER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SOURCE_KEYED_CACHED_METADATA_HANDLER_H_

#include <stdint.h>
#include "third_party/blink/renderer/platform/loader/fetch/cached_metadata.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/url_loader/cached_metadata_handler.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"

namespace blink {

// An implementation of CachedMetadataHandler which can hold multiple
// CachedMetadata entries. These entries are keyed by a cryptograph hash of the
// source code which produced them.
//
// This is used to store cached metadata for multiple inline scripts on a single
// HTML document's resource.
class PLATFORM_EXPORT SourceKeyedCachedMetadataHandler final
    : public CachedMetadataHandler {
 public:
  SourceKeyedCachedMetadataHandler(
      WTF::TextEncoding encoding,
      std::unique_ptr<CachedMetadataSender> send_callback)
      : sender_(std::move(send_callback)), encoding_(encoding) {}

  // Produce a metadata handler for a single cached metadata associated with
  // the given source code.
  SingleCachedMetadataHandler* HandlerForSource(const String& source);

  void ClearCachedMetadata(
      blink::mojom::CodeCacheHost*,
      CachedMetadataHandler::ClearCacheType cache_type) override;
  String Encoding() const override;
  bool IsServedFromCacheStorage() const override {
    return sender_->IsServedFromCacheStorage();
  }
  void OnMemoryDump(WebProcessMemoryDump* pmd,
                    const String& dump_prefix) const override;
  size_t GetCodeCacheSize() const override {
    // No need to implement this as inline scripts are not kept in
    // blink::MemoryCache
    return 0;
  }

  void SetSerializedCachedMetadata(mojo_base::BigBuffer data);

 private:
  // Keys are SHA-256, which are 256/8 = 32 bytes.
  static constexpr size_t kKeySize = 32;
  typedef Vector<uint8_t, kKeySize> Key;

  class SingleKeyHandler;
  class KeyHash;
  class KeyHashTraits : public WTF::GenericHashTraits<Key> {
   public:
    // Note: This class relies on hashes never being zero or 1 followed by all
    // zeros. Practically, our hash space is large enough that the risk of such
    // a collision is infinitesimal.

    typedef Key EmptyValueType;
    static const bool kEmptyValueIsZero = true;
    static EmptyValueType EmptyValue() {
      // Rely on integer value initialization to zero out the key array.
      return Key{};
    }

    static void ConstructDeletedValue(Key& slot, bool) {
      slot = {1};  // Remaining entries are value initialized to 0.
    }
    static bool IsDeletedValue(const Key& value) { return value == Key{1}; }
  };

  void SendToPlatform(blink::mojom::CodeCacheHost*);

  // TODO(leszeks): Maybe just store the SingleKeyHandlers directly in here?
  WTF::HashMap<Key, scoped_refptr<CachedMetadata>, KeyHash, KeyHashTraits>
      cached_metadata_map_;
  std::unique_ptr<CachedMetadataSender> sender_;

  const WTF::TextEncoding encoding_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_SOURCE_KEYED_CACHED_METADATA_HANDLER_H_