File: sync_file_system_backend.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 (298 lines) | stat: -rw-r--r-- 10,664 bytes parent folder | download | duplicates (5)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2013 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/sync_file_system/local/sync_file_system_backend.h"

#include <string>
#include <utility>

#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/types/pass_key.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
#include "chrome/browser/sync_file_system/local/syncable_file_system_operation.h"
#include "chrome/browser/sync_file_system/sync_file_system_service.h"
#include "chrome/browser/sync_file_system/sync_file_system_service_factory.h"
#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/file_system/file_stream_reader.h"
#include "storage/browser/file_system/file_stream_writer.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation.h"
#include "storage/common/file_system/file_system_util.h"

using content::BrowserThread;

namespace sync_file_system {

namespace {

bool CalledOnUIThread() {
  // Ensure that these methods are called on the UI thread, except for unittests
  // where a UI thread might not have been created.
  return BrowserThread::CurrentlyOn(BrowserThread::UI) ||
         !BrowserThread::IsThreadInitialized(BrowserThread::UI);
}

}  // namespace

SyncFileSystemBackend::SyncFileSystemBackend(Profile* profile)
    : profile_(profile) {
  DCHECK(CalledOnUIThread());
  // Register the service name here to enable to crack an URL on SyncFileSystem
  // even if SyncFileSystemService has not started yet.
  RegisterSyncableFileSystem();
}

SyncFileSystemBackend::~SyncFileSystemBackend() {
  if (change_tracker_) {
    GetDelegate()->file_task_runner()->DeleteSoon(
        FROM_HERE, change_tracker_.release());
  }
}

// static
std::unique_ptr<SyncFileSystemBackend>
SyncFileSystemBackend::CreateForTesting() {
  DCHECK(CalledOnUIThread());
  auto backend = std::make_unique<SyncFileSystemBackend>(nullptr);
  backend->skip_initialize_syncfs_service_for_testing_ = true;
  return backend;
}

bool SyncFileSystemBackend::CanHandleType(storage::FileSystemType type) const {
  return type == storage::kFileSystemTypeSyncable ||
         type == storage::kFileSystemTypeSyncableForInternalSync;
}

void SyncFileSystemBackend::Initialize(storage::FileSystemContext* context) {
  DCHECK(context);
  DCHECK(!context_);
  context_ = context;

  storage::SandboxFileSystemBackendDelegate* delegate = GetDelegate();
  delegate->RegisterQuotaUpdateObserver(storage::kFileSystemTypeSyncable);
  delegate->RegisterQuotaUpdateObserver(
      storage::kFileSystemTypeSyncableForInternalSync);
}

void SyncFileSystemBackend::ResolveURL(const storage::FileSystemURL& url,
                                       storage::OpenFileSystemMode mode,
                                       ResolveURLCallback callback) {
  DCHECK(CanHandleType(url.type()));

  if (skip_initialize_syncfs_service_for_testing_) {
    GetDelegate()->OpenFileSystem(
        url.GetBucket(), url.type(), mode, std::move(callback),
        GetSyncableFileSystemRootURI(url.origin().GetURL()));
    return;
  }

  // It is safe to pass Unretained(this) since |context_| owns it.
  SyncStatusCallback initialize_callback = base::BindOnce(
      &SyncFileSystemBackend::DidInitializeSyncFileSystemService,
      base::Unretained(this), base::RetainedRef(context_.get()),
      url.origin().GetURL(), url.type(), mode, std::move(callback));
  InitializeSyncFileSystemService(url.origin().GetURL(),
                                  std::move(initialize_callback));
}

storage::AsyncFileUtil* SyncFileSystemBackend::GetAsyncFileUtil(
    storage::FileSystemType type) {
  return GetDelegate()->file_util();
}

storage::WatcherManager* SyncFileSystemBackend::GetWatcherManager(
    storage::FileSystemType type) {
  return nullptr;
}

storage::CopyOrMoveFileValidatorFactory*
SyncFileSystemBackend::GetCopyOrMoveFileValidatorFactory(
    storage::FileSystemType type,
    base::File::Error* error_code) {
  DCHECK(error_code);
  *error_code = base::File::FILE_OK;
  return nullptr;
}

std::unique_ptr<storage::FileSystemOperation>
SyncFileSystemBackend::CreateFileSystemOperation(
    storage::OperationType type,
    const storage::FileSystemURL& url,
    storage::FileSystemContext* context,
    base::File::Error* error_code) const {
  DCHECK(CanHandleType(url.type()));
  DCHECK(context);
  DCHECK(error_code);

  std::unique_ptr<storage::FileSystemOperationContext> operation_context =
      GetDelegate()->CreateFileSystemOperationContext(url, context, error_code);
  if (!operation_context)
    return nullptr;

  if (url.type() == storage::kFileSystemTypeSyncableForInternalSync) {
    return storage::FileSystemOperation::Create(type, url, context,
                                                std::move(operation_context));
  }

  return std::make_unique<SyncableFileSystemOperation>(
      type, url, context, std::move(operation_context),
      base::PassKey<SyncFileSystemBackend>());
}

bool SyncFileSystemBackend::SupportsStreaming(
    const storage::FileSystemURL& url) const {
  return false;
}

bool SyncFileSystemBackend::HasInplaceCopyImplementation(
    storage::FileSystemType type) const {
  return false;
}

std::unique_ptr<storage::FileStreamReader>
SyncFileSystemBackend::CreateFileStreamReader(
    const storage::FileSystemURL& url,
    int64_t offset,
    int64_t max_bytes_to_read,
    const base::Time& expected_modification_time,
    storage::FileSystemContext* context,
    file_access::ScopedFileAccessDelegate::
        RequestFilesAccessIOCallback /*file_access*/) const {
  DCHECK(CanHandleType(url.type()));
  return GetDelegate()->CreateFileStreamReader(
      url, offset, expected_modification_time, context);
}

std::unique_ptr<storage::FileStreamWriter>
SyncFileSystemBackend::CreateFileStreamWriter(
    const storage::FileSystemURL& url,
    int64_t offset,
    storage::FileSystemContext* context) const {
  DCHECK(CanHandleType(url.type()));
  return GetDelegate()->CreateFileStreamWriter(
      url, offset, context, storage::kFileSystemTypeSyncableForInternalSync);
}

storage::FileSystemQuotaUtil* SyncFileSystemBackend::GetQuotaUtil() {
  return GetDelegate();
}

const storage::UpdateObserverList* SyncFileSystemBackend::GetUpdateObservers(
    storage::FileSystemType type) const {
  return GetDelegate()->GetUpdateObservers(type);
}

const storage::ChangeObserverList* SyncFileSystemBackend::GetChangeObservers(
    storage::FileSystemType type) const {
  return GetDelegate()->GetChangeObservers(type);
}

const storage::AccessObserverList* SyncFileSystemBackend::GetAccessObservers(
    storage::FileSystemType type) const {
  return GetDelegate()->GetAccessObservers(type);
}

// static
SyncFileSystemBackend* SyncFileSystemBackend::GetBackend(
    const storage::FileSystemContext* file_system_context) {
  DCHECK(file_system_context);
  return static_cast<SyncFileSystemBackend*>(
      file_system_context->GetFileSystemBackend(
          storage::kFileSystemTypeSyncable));
}

void SyncFileSystemBackend::SetLocalFileChangeTracker(
    std::unique_ptr<LocalFileChangeTracker> tracker) {
  DCHECK(!change_tracker_);
  DCHECK(tracker);
  change_tracker_ = std::move(tracker);

  storage::SandboxFileSystemBackendDelegate* delegate = GetDelegate();
  delegate->AddFileUpdateObserver(storage::kFileSystemTypeSyncable,
                                  change_tracker_.get(),
                                  delegate->file_task_runner());
  delegate->AddFileChangeObserver(storage::kFileSystemTypeSyncable,
                                  change_tracker_.get(),
                                  delegate->file_task_runner());
}

void SyncFileSystemBackend::set_sync_context(
    LocalFileSyncContext* sync_context) {
  DCHECK(!sync_context_.get());
  sync_context_ = sync_context;
}

storage::SandboxFileSystemBackendDelegate* SyncFileSystemBackend::GetDelegate()
    const {
  DCHECK(context_);
  DCHECK(context_->sandbox_delegate());
  return context_->sandbox_delegate();
}

void SyncFileSystemBackend::InitializeSyncFileSystemService(
    const GURL& origin_url,
    SyncStatusCallback callback) {
  // Repost to switch from IO thread to UI thread.
  if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
    DCHECK_CURRENTLY_ON(BrowserThread::IO);
    // It is safe to pass Unretained(this) (see comments in OpenFileSystem()).
    content::GetUIThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(&SyncFileSystemBackend::InitializeSyncFileSystemService,
                       base::Unretained(this), origin_url,
                       std::move(callback)));
    return;
  }

  if (!g_browser_process->profile_manager()->IsValidProfile(profile_)) {
    // Profile was destroyed.
    std::move(callback).Run(SYNC_FILE_ERROR_FAILED);
    return;
  }

  SyncFileSystemService* service =
      SyncFileSystemServiceFactory::GetForProfile(profile_);
  DCHECK(service);
  service->InitializeForApp(context_, origin_url, std::move(callback));
}

void SyncFileSystemBackend::DidInitializeSyncFileSystemService(
    storage::FileSystemContext* context,
    const GURL& origin_url,
    storage::FileSystemType type,
    storage::OpenFileSystemMode mode,
    ResolveURLCallback callback,
    SyncStatusCode status) {
  // Repost to switch from UI thread to IO thread.
  if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
    DCHECK_CURRENTLY_ON(BrowserThread::UI);
    // It is safe to pass Unretained(this) since |context| owns it.
    content::GetIOThreadTaskRunner({})->PostTask(
        FROM_HERE,
        base::BindOnce(
            &SyncFileSystemBackend::DidInitializeSyncFileSystemService,
            base::Unretained(this), base::RetainedRef(context), origin_url,
            type, mode, std::move(callback), status));
    return;
  }

  if (status != sync_file_system::SYNC_STATUS_OK) {
    std::move(callback).Run(GURL(), std::string(),
                            SyncStatusCodeToFileError(status));
    return;
  }

  std::move(callback).Run(GetSyncableFileSystemRootURI(origin_url),
                          GetFileSystemName(origin_url, type),
                          base::File::FILE_OK);
}

}  // namespace sync_file_system