File: shared_dictionary_disk_cache.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (210 lines) | stat: -rw-r--r-- 7,810 bytes parent folder | download | duplicates (4)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "services/network/shared_dictionary/shared_dictionary_disk_cache.h"

#include <limits>

#include "base/functional/callback_helpers.h"
#include "base/trace_event/trace_event.h"
#include "base/trace_event/trace_id_helper.h"

namespace network {
namespace {

void RunTaskAndCallback(
    base::OnceCallback<int(net::CompletionOnceCallback)> task,
    net::CompletionOnceCallback callback) {
  auto split_callback = base::SplitOnceCallback(std::move(callback));
  int result = std::move(task).Run(std::move(split_callback.first));
  if (result != net::ERR_IO_PENDING) {
    std::move(split_callback.second).Run(std::move(result));
  }
}

void RunTaksAndEntryResultCallback(
    base::OnceCallback<disk_cache::EntryResult(disk_cache::EntryResultCallback)>
        task,
    disk_cache::EntryResultCallback callback) {
  auto split_callback = base::SplitOnceCallback(std::move(callback));
  disk_cache::EntryResult result =
      std::move(task).Run(std::move(split_callback.first));
  if (result.net_error() != net::ERR_IO_PENDING) {
    std::move(split_callback.second).Run(std::move(result));
  }
}

}  // namespace

SharedDictionaryDiskCache::SharedDictionaryDiskCache() = default;

void SharedDictionaryDiskCache::Initialize(
    const base::FilePath& cache_directory_path,
#if BUILDFLAG(IS_ANDROID)
    disk_cache::ApplicationStatusListenerGetter app_status_listener_getter,
#endif  // BUILDFLAG(IS_ANDROID)
    scoped_refptr<disk_cache::BackendFileOperationsFactory>
        file_operations_factory) {
  DCHECK_EQ(State::kBeforeInitialize, state_);
  state_ = State::kInitializing;
  disk_cache::BackendResult result = CreateCacheBackend(
      cache_directory_path,
#if BUILDFLAG(IS_ANDROID)
      app_status_listener_getter,
#endif  // BUILDFLAG(IS_ANDROID)
      std::move(file_operations_factory),
      base::BindOnce(&SharedDictionaryDiskCache::DidCreateBackend,
                     GetWeakPtr()));
  if (result.net_error != net::ERR_IO_PENDING) {
    DidCreateBackend(std::move(result));
  }
}

SharedDictionaryDiskCache::~SharedDictionaryDiskCache() = default;

disk_cache::BackendResult SharedDictionaryDiskCache::CreateCacheBackend(
    const base::FilePath& cache_directory_path,
#if BUILDFLAG(IS_ANDROID)
    disk_cache::ApplicationStatusListenerGetter app_status_listener_getter,
#endif  // BUILDFLAG(IS_ANDROID)
    scoped_refptr<disk_cache::BackendFileOperationsFactory>
        file_operations_factory,
    disk_cache::BackendResultCallback callback) {
  CHECK(!cache_directory_path.empty());

  // We use APP_CACHE to avoid the auto-eviction.
  // Also we use std::numeric_limits<int64_t>::max() for `max_bytes`, because
  // the cache size is controlled by the SharedDictionaryManagerOnDisk.
  return disk_cache::CreateCacheBackend(
      net::APP_CACHE, net::CACHE_BACKEND_SIMPLE, file_operations_factory.get(),
      cache_directory_path, /*max_bytes=*/std::numeric_limits<int64_t>::max(),
      disk_cache::ResetHandling::kResetOnError,
      /*net_log=*/nullptr, /*cache_encryption_delegate=*/nullptr,
      std::move(callback)
#if BUILDFLAG(IS_ANDROID)
          ,
      std::move(app_status_listener_getter)
#endif  // BUILDFLAG(IS_ANDROID));
  );
}

disk_cache::EntryResult SharedDictionaryDiskCache::OpenOrCreateEntry(
    const std::string& key,
    bool create,
    disk_cache::EntryResultCallback callback) {
  switch (state_) {
    case State::kBeforeInitialize:
      NOTREACHED();
    case State::kInitializing:
      // It is safe to use Unretained() below because
      // `pending_disk_cache_tasks_` is owned by `this` and the passed task
      // `SharedDictionaryDiskCache::OpenOrCreateEntry()` will be called only
      // when `this` is available.
      pending_disk_cache_tasks_.push_back(base::BindOnce(
          &RunTaksAndEntryResultCallback,
          base::BindOnce(&SharedDictionaryDiskCache::OpenOrCreateEntry,
                         base::Unretained(this), key, create),
          std::move(callback)));
      return disk_cache::EntryResult::MakeError(net::ERR_IO_PENDING);
    case State::kInitialized:
      DCHECK(backend_);
      return create
                 ? backend_->CreateEntry(key, net::LOW, std::move(callback))
                 : backend_->OpenEntry(key, net::HIGHEST, std::move(callback));
    case State::kFailed:
      return disk_cache::EntryResult::MakeError(net::ERR_FAILED);
  }
}

int SharedDictionaryDiskCache::DoomEntry(const std::string& key,
                                         net::CompletionOnceCallback callback) {
  switch (state_) {
    case State::kBeforeInitialize:
      NOTREACHED();
    case State::kInitializing:
      // It is safe to use Unretained() below because
      // `pending_disk_cache_tasks_` is owned by `this` and the passed task
      // `SharedDictionaryDiskCache::DoomEntry()` will be called only when
      // `this` is available.
      pending_disk_cache_tasks_.push_back(
          base::BindOnce(&RunTaskAndCallback,
                         base::BindOnce(&SharedDictionaryDiskCache::DoomEntry,
                                        base::Unretained(this), key),
                         std::move(callback)));
      return net::ERR_IO_PENDING;
    case State::kInitialized:
      DCHECK(backend_);
      return backend_->DoomEntry(key, net::LOW, std::move(callback));
    case State::kFailed:
      return net::ERR_FAILED;
  }
}

int SharedDictionaryDiskCache::ClearAll(net::CompletionOnceCallback callback) {
  switch (state_) {
    case State::kBeforeInitialize:
      NOTREACHED();
    case State::kInitializing:
      // It is safe to use Unretained() below because
      // `pending_disk_cache_tasks_` is owned by `this` and the passed task
      // `SharedDictionaryDiskCache::ClearAll()` will be called only when `this`
      // is available.
      pending_disk_cache_tasks_.push_back(
          base::BindOnce(&RunTaskAndCallback,
                         base::BindOnce(&SharedDictionaryDiskCache::ClearAll,
                                        base::Unretained(this)),
                         std::move(callback)));
      return net::ERR_IO_PENDING;
    case State::kInitialized:
      DCHECK(backend_);
      return backend_->DoomAllEntries(std::move(callback));
    case State::kFailed:
      return net::ERR_FAILED;
  }
}

void SharedDictionaryDiskCache::CreateIterator(
    base::OnceCallback<void(std::unique_ptr<disk_cache::Backend::Iterator>)>
        callback) {
  switch (state_) {
    case State::kBeforeInitialize:
      NOTREACHED();
    case State::kInitializing:
      // It is safe to use Unretained() below because
      // `pending_disk_cache_tasks_` is owned by `this` and the passed task
      // `SharedDictionaryDiskCache::CreateIterator()` will be called only when
      // `this` is available.
      pending_disk_cache_tasks_.push_back(
          base::BindOnce(&SharedDictionaryDiskCache::CreateIterator,
                         base::Unretained(this), std::move(callback)));
      return;
    case State::kInitialized:
      DCHECK(backend_);
      std::move(callback).Run(backend_->CreateIterator());
      return;
    case State::kFailed:
      std::move(callback).Run(nullptr);
      return;
  }
}

void SharedDictionaryDiskCache::DidCreateBackend(
    disk_cache::BackendResult result) {
  if (result.net_error != net::OK) {
    state_ = State::kFailed;
  } else {
    state_ = State::kInitialized;
    backend_ = std::move(result.backend);
  }
  auto tasks = std::move(pending_disk_cache_tasks_);
  base::WeakPtr<SharedDictionaryDiskCache> weak_ptr = GetWeakPtr();
  for (auto& task : tasks) {
    std::move(task).Run();
    if (!weak_ptr) {
      return;
    }
  }
}

}  // namespace network