File: cache_manager_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (237 lines) | stat: -rw-r--r-- 9,235 bytes parent folder | download | duplicates (6)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ash/file_system_provider/content_cache/cache_manager_impl.h"

#include "base/base64.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "base/types/expected.h"
#include "chrome/browser/ash/file_system_provider/content_cache/cache_manager.h"
#include "chrome/browser/ash/file_system_provider/content_cache/content_cache_impl.h"
#include "chrome/browser/ash/file_system_provider/content_cache/context_database.h"
#include "chrome/browser/ash/file_system_provider/provided_file_system_info.h"

namespace ash::file_system_provider {

namespace {

base::File::Error CreateProviderDirectory(const base::FilePath& path) {
  base::File::Error error = base::File::FILE_ERROR_FAILED;
  if (!base::CreateDirectoryAndGetError(path, &error)) {
    return error;
  }

  return base::File::FILE_OK;
}

OptionalContextDatabase InitializeContextDatabase(
    const base::FilePath& db_path) {
  std::unique_ptr<ContextDatabase> context_db =
      std::make_unique<ContextDatabase>(db_path);
  if (context_db->Initialize()) {
    return context_db;
  }
  return std::nullopt;
}

}  // namespace

CacheManagerImpl::CacheManagerImpl(const base::FilePath& profile_path)
    : root_content_cache_directory_(
          profile_path.Append(kFspContentCacheDirName)) {}

CacheManagerImpl::~CacheManagerImpl() = default;

std::unique_ptr<CacheManager> CacheManagerImpl::Create(
    const base::FilePath& profile_path) {
  return std::make_unique<CacheManagerImpl>(profile_path);
}

void CacheManagerImpl::InitializeForProvider(
    const ProvidedFileSystemInfo& file_system_info,
    FileErrorOrContentCacheCallback callback) {
  const base::FilePath cache_directory_path =
      GetCacheDirectoryPath(file_system_info);
  if (cache_directory_path.empty()) {
    std::move(callback).Run(
        base::unexpected(base::File::FILE_ERROR_INVALID_URL));
    return;
  }

  blocking_task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE, base::BindOnce(&CreateProviderDirectory, cache_directory_path),
      base::BindOnce(&CacheManagerImpl::OnProviderDirectoryCreationComplete,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                     cache_directory_path));
}

void CacheManagerImpl::UninitializeForProvider(
    const ProvidedFileSystemInfo& file_system_info) {
  const base::FilePath cache_directory_path =
      GetCacheDirectoryPath(file_system_info);
  if (cache_directory_path.empty()) {
    return;
  }
  const base::FilePath base64_encoded_provider_folder_name =
      cache_directory_path.BaseName();
  if (!initialized_providers_.contains(base64_encoded_provider_folder_name)) {
    OnUninitializeForProvider(base64_encoded_provider_folder_name,
                              base::File::FILE_ERROR_NOT_FOUND);
    return;
  }

  // Remove the provider from the set.
  initialized_providers_.erase(base64_encoded_provider_folder_name);

  // Attempt to delete the cache directory to ensure dead files don't remain
  // on the user's disk as the logic changes in this experimental design phase.
  blocking_task_runner_->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(
          [](const base::FilePath& cache_directory_path) {
            return base::DeletePathRecursively(cache_directory_path)
                       ? base::File::FILE_OK
                       : base::File::FILE_ERROR_FAILED;
          },
          std::move(cache_directory_path)),
      base::BindOnce(&CacheManagerImpl::OnUninitializeForProvider,
                     weak_ptr_factory_.GetWeakPtr(),
                     base64_encoded_provider_folder_name));
}

bool CacheManagerImpl::IsProviderInitialized(
    const ProvidedFileSystemInfo& file_system_info) {
  const base::FilePath cache_directory_path =
      GetCacheDirectoryPath(file_system_info);
  if (cache_directory_path.empty()) {
    return false;
  }
  const base::FilePath base64_encoded_provider_folder_name =
      cache_directory_path.BaseName();
  return initialized_providers_.contains(base64_encoded_provider_folder_name);
}

void CacheManagerImpl::AddObserver(Observer* observer) {
  DCHECK(observer);
  observers_.AddObserver(observer);
}

void CacheManagerImpl::RemoveObserver(Observer* observer) {
  DCHECK(observer);
  observers_.RemoveObserver(observer);
}

void CacheManagerImpl::OnProviderDirectoryCreationComplete(
    FileErrorOrContentCacheCallback callback,
    base::FilePath cache_directory_path,
    base::File::Error result) {
  if (result != base::File::FILE_OK) {
    OnProviderInitializationComplete(cache_directory_path.BaseName(),
                                     std::move(callback),
                                     base::unexpected(result));
    return;
  }

  // Initialize the database task runner, the `ContextDatabase` will be created
  // on this task runner and bound to the task runner on return.
  scoped_refptr<base::SequencedTaskRunner> db_task_runner =
      base::ThreadPool::CreateSequencedTaskRunner(
          {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
           base::TaskShutdownBehavior::BLOCK_SHUTDOWN});
  db_task_runner->PostTaskAndReplyWithResult(
      FROM_HERE,
      base::BindOnce(&InitializeContextDatabase,
                     cache_directory_path.Append("context.db")),
      base::BindOnce(&CacheManagerImpl::OnProviderContextDatabaseSetup,
                     weak_ptr_factory_.GetWeakPtr(), cache_directory_path,
                     std::move(callback), db_task_runner));
}

void CacheManagerImpl::OnProviderContextDatabaseSetup(
    const base::FilePath& cache_directory_path,
    FileErrorOrContentCacheCallback callback,
    scoped_refptr<base::SequencedTaskRunner> db_task_runner,
    OptionalContextDatabase optional_context_db) {
  const base::FilePath base64_encoded_provider_folder_name =
      cache_directory_path.BaseName();

  if (!optional_context_db.has_value()) {
    OnProviderInitializationComplete(
        base64_encoded_provider_folder_name, std::move(callback),
        base::unexpected(base::File::FILE_ERROR_FAILED));
    return;
  }

  // Bind the `ContextDatabase` to the database task runner to ensure
  // sequenced access via the `ContentCache` instance.
  BoundContextDatabase context_db(db_task_runner,
                                  std::move(optional_context_db.value()));
  std::unique_ptr<ContentCache> content_cache =
      ContentCacheImpl::Create(cache_directory_path, std::move(context_db));
  content_cache->LoadFromDisk(
      base::BindOnce(&CacheManagerImpl::OnProviderFilesLoadedFromDisk,
                     weak_ptr_factory_.GetWeakPtr(), std::move(content_cache),
                     base64_encoded_provider_folder_name, std::move(callback)));
}

void CacheManagerImpl::OnProviderFilesLoadedFromDisk(
    std::unique_ptr<ContentCache> content_cache,
    const base::FilePath& base64_encoded_provider_folder_name,
    FileErrorOrContentCacheCallback callback) {
  initialized_providers_.emplace(base64_encoded_provider_folder_name);
  OnProviderInitializationComplete(base64_encoded_provider_folder_name,
                                   std::move(callback),
                                   std::move(content_cache));
}
void CacheManagerImpl::OnUninitializeForProvider(
    const base::FilePath& base64_encoded_provider_folder_name,
    base::File::Error result) {
  LOG_IF(ERROR, result != base::File::FILE_OK)
      << "Failed to uninitialize provider";
  // Notify all observers.
  for (auto& observer : observers_) {
    observer.OnProviderUninitialized(base64_encoded_provider_folder_name,
                                     result);
  }
}

const base::FilePath CacheManagerImpl::GetCacheDirectoryPath(
    const ProvidedFileSystemInfo& file_system_info) {
  const base::FilePath& provider_folder_name =
      file_system_info.mount_path().BaseName();
  if (provider_folder_name.empty()) {
    return base::FilePath();
  }
  // The provider folder name takes the form
  // {provider-id}:{file-system-id}:{user-hash} with the {file-system-id} being
  // escaped but ultimately provided by the extension, so let's convert it to
  // base64 before creating a directory.
  const base::FilePath base64_encoded_provider_folder_name(
      base::Base64Encode(provider_folder_name.value()));

  const base::FilePath cache_directory_path(
      root_content_cache_directory_.Append(
          base64_encoded_provider_folder_name));

  return cache_directory_path;
}

void CacheManagerImpl::OnProviderInitializationComplete(
    const base::FilePath& base64_encoded_provider_folder_name,
    FileErrorOrContentCacheCallback callback,
    FileErrorOrContentCache error_or_content_cache) {
  base::File::Error result =
      error_or_content_cache.error_or(base::File::FILE_OK);
  std::move(callback).Run(std::move(error_or_content_cache));

  for (Observer& observer : observers_) {
    observer.OnProviderInitializationComplete(
        base64_encoded_provider_folder_name, result);
  }
}

}  // namespace ash::file_system_provider