File: service_worker_script_cache_map.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,316 bytes parent folder | download | duplicates (7)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_

#include <stddef.h>
#include <stdint.h>

#include <map>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/services/storage/public/mojom/service_worker_storage_control.mojom.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"

class GURL;

namespace content {

class ServiceWorkerContextCore;
class ServiceWorkerVersion;

// Class that maintains the mapping between urls and a resource id
// for a particular version's implicit script resources.
class CONTENT_EXPORT ServiceWorkerScriptCacheMap {
 public:
  ServiceWorkerScriptCacheMap(const ServiceWorkerScriptCacheMap&) = delete;
  ServiceWorkerScriptCacheMap& operator=(const ServiceWorkerScriptCacheMap&) =
      delete;

  int64_t LookupResourceId(const GURL& url);
  std::optional<std::string> LookupSha256Checksum(const GURL& url);

  // Used during the initial run of a new version to build the map
  // of resources ids.
  void NotifyStartedCaching(const GURL& url, int64_t resource_id);
  void NotifyFinishedCaching(const GURL& url,
                             int64_t size_bytes,
                             const std::string& sha256_checksum,
                             net::Error net_error,
                             const std::string& status_message);

  // Used to retrieve the results of the initial run of a new version.
  std::vector<storage::mojom::ServiceWorkerResourceRecordPtr> GetResources()
      const;

  // Used when loading an existing version.
  void SetResources(
      const std::vector<storage::mojom::ServiceWorkerResourceRecordPtr>&
          resources);

  // Updates sha256_checksum to the resource. sha256_checksum can be updated
  // after the update check process.
  void UpdateSha256Checksum(const GURL& url,
                            const std::string& sha256_checksum);

  // Writes the metadata of the existing script.
  void WriteMetadata(const GURL& url,
                     base::span<const uint8_t> data,
                     net::CompletionOnceCallback callback);
  // Clears the metadata of the existing script.
  void ClearMetadata(const GURL& url, net::CompletionOnceCallback callback);

  size_t size() const { return resource_map_.size(); }

  // net::Error code from trying to load the main script resource.
  int main_script_net_error() const { return main_script_net_error_; }

  const std::string& main_script_status_message() const {
    return main_script_status_message_;
  }

 private:
  typedef std::map<GURL, storage::mojom::ServiceWorkerResourceRecordPtr>
      ResourceMap;

  // The version objects owns its script cache and provides a rawptr to it.
  friend class ServiceWorkerVersion;
  friend class ServiceWorkerVersionBrowserTest;
  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerReadFromCacheJobTest, ResourceNotFound);
  FRIEND_TEST_ALL_PREFIXES(ServiceWorkerBrowserTest,
                           DispatchFetchEventToBrokenWorker);

  ServiceWorkerScriptCacheMap(
      ServiceWorkerVersion* owner,
      base::WeakPtr<ServiceWorkerContextCore> context);
  ~ServiceWorkerScriptCacheMap();

  void OnWriterDisconnected(uint64_t callback_id);
  void OnMetadataWritten(
      mojo::Remote<storage::mojom::ServiceWorkerResourceMetadataWriter>,
      uint64_t callback_id,
      int result);

  void RunCallback(uint64_t callback_id, int result);

  raw_ptr<ServiceWorkerVersion> owner_;
  base::WeakPtr<ServiceWorkerContextCore> context_;
  ResourceMap resource_map_;
  int main_script_net_error_ = net::OK;
  std::string main_script_status_message_;
  uint64_t next_callback_id_ = 0;
  base::flat_map</*callback_id=*/uint64_t, net::CompletionOnceCallback>
      callbacks_;

  base::WeakPtrFactory<ServiceWorkerScriptCacheMap> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_SCRIPT_CACHE_MAP_H_