1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/sync_file_system/local/root_delete_helper.h"
7 #include "base/sequenced_task_runner.h"
8 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
9 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
10 #include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
11 #include "chrome/browser/sync_file_system/logger.h"
12 #include "chrome/browser/sync_file_system/sync_callbacks.h"
13 #include "storage/browser/fileapi/file_system_context.h"
14 #include "storage/browser/fileapi/file_system_url.h"
15 #include "storage/browser/fileapi/sandbox_file_system_backend_delegate.h"
16 #include "storage/common/fileapi/file_system_util.h"
18 namespace sync_file_system
{
22 // This runs on FileSystemContext's default_file_task_runner.
23 void ResetFileChangeTracker(storage::FileSystemContext
* file_system_context
,
24 const storage::FileSystemURL
& url
) {
25 DCHECK(file_system_context
->default_file_task_runner()->
26 RunsTasksOnCurrentThread());
27 SyncFileSystemBackend
* backend
=
28 SyncFileSystemBackend::GetBackend(file_system_context
);
30 DCHECK(backend
->change_tracker());
31 backend
->change_tracker()->ResetForFileSystem(url
.origin(), url
.type());
36 RootDeleteHelper::RootDeleteHelper(
37 storage::FileSystemContext
* file_system_context
,
38 LocalFileSyncStatus
* sync_status
,
39 const storage::FileSystemURL
& url
,
40 const FileStatusCallback
& callback
)
41 : file_system_context_(file_system_context
),
44 sync_status_(sync_status
),
46 DCHECK(file_system_context_
.get());
47 DCHECK(url_
.is_valid());
48 DCHECK(!callback_
.is_null());
50 // This is expected to run on the filesystem root.
51 DCHECK(storage::VirtualPath::IsRootPath(url
.path()));
54 RootDeleteHelper::~RootDeleteHelper() {
57 void RootDeleteHelper::Run() {
58 util::Log(logging::LOG_VERBOSE
, FROM_HERE
,
59 "Deleting the entire local filesystem for remote root deletion: "
60 "%s", url_
.DebugString().c_str());
62 file_system_context_
->DeleteFileSystem(
63 url_
.origin(), url_
.type(),
64 base::Bind(&RootDeleteHelper::DidDeleteFileSystem
,
65 weak_factory_
.GetWeakPtr()));
68 void RootDeleteHelper::DidDeleteFileSystem(base::File::Error error
) {
69 // Ignore errors, no idea how to deal with it.
71 DCHECK(!sync_status_
->IsWritable(url_
));
72 DCHECK(!sync_status_
->IsWriting(url_
));
74 // All writes to the entire file system must be now blocked, so we have
75 // to be able to safely reset the local changes and sync statuses for it.
76 // TODO(kinuko): This should be probably automatically handled in
77 // DeleteFileSystem via QuotaUtil::DeleteOriginDataOnFileThread.
78 file_system_context_
->default_file_task_runner()->PostTaskAndReply(
80 base::Bind(&ResetFileChangeTracker
, file_system_context_
, url_
),
81 base::Bind(&RootDeleteHelper::DidResetFileChangeTracker
,
82 weak_factory_
.GetWeakPtr()));
85 void RootDeleteHelper::DidResetFileChangeTracker() {
86 DCHECK(!sync_status_
->IsWritable(url_
));
87 DCHECK(!sync_status_
->IsWriting(url_
));
89 // Reopening the filesystem.
90 file_system_context_
->sandbox_delegate()->OpenFileSystem(
93 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT
,
94 base::Bind(&RootDeleteHelper::DidOpenFileSystem
,
95 weak_factory_
.GetWeakPtr()),
99 void RootDeleteHelper::DidOpenFileSystem(const GURL
& /* root */,
100 const std::string
& /* name */,
101 base::File::Error error
) {
102 FileStatusCallback callback
= callback_
;
106 } // namespace sync_file_system