Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / sync_file_system / local / root_delete_helper.cc
blob1c5070970bdfc03700fc11c89d112d73e66fd225
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 "webkit/browser/fileapi/file_system_context.h"
14 #include "webkit/browser/fileapi/file_system_url.h"
15 #include "webkit/browser/fileapi/sandbox_file_system_backend_delegate.h"
16 #include "webkit/common/fileapi/file_system_util.h"
18 namespace sync_file_system {
20 namespace {
22 // This runs on FileSystemContext's default_file_task_runner.
23 void ResetFileChangeTracker(
24 fileapi::FileSystemContext* file_system_context,
25 const fileapi::FileSystemURL& url) {
26 DCHECK(file_system_context->default_file_task_runner()->
27 RunsTasksOnCurrentThread());
28 SyncFileSystemBackend* backend =
29 SyncFileSystemBackend::GetBackend(file_system_context);
30 DCHECK(backend);
31 DCHECK(backend->change_tracker());
32 backend->change_tracker()->ResetForFileSystem(url.origin(), url.type());
35 } // namespace
37 RootDeleteHelper::RootDeleteHelper(
38 fileapi::FileSystemContext* file_system_context,
39 LocalFileSyncStatus* sync_status,
40 const fileapi::FileSystemURL& url,
41 const FileStatusCallback& callback)
42 : file_system_context_(file_system_context),
43 url_(url),
44 callback_(callback),
45 sync_status_(sync_status),
46 weak_factory_(this) {
47 DCHECK(file_system_context_);
48 DCHECK(url_.is_valid());
49 DCHECK(!callback_.is_null());
50 DCHECK(sync_status_);
51 // This is expected to run on the filesystem root.
52 DCHECK(fileapi::VirtualPath::IsRootPath(url.path()));
55 RootDeleteHelper::~RootDeleteHelper() {
58 void RootDeleteHelper::Run() {
59 util::Log(logging::LOG_INFO, FROM_HERE,
60 "Deleting the entire local filesystem for remote root deletion: "
61 "%s", url_.DebugString().c_str());
63 file_system_context_->DeleteFileSystem(
64 url_.origin(), url_.type(),
65 base::Bind(&RootDeleteHelper::DidDeleteFileSystem,
66 weak_factory_.GetWeakPtr()));
69 void RootDeleteHelper::DidDeleteFileSystem(base::PlatformFileError error) {
70 // Ignore errors, no idea how to deal with it.
72 DCHECK(!sync_status_->IsWritable(url_));
73 DCHECK(!sync_status_->IsWriting(url_));
75 // All writes to the entire file system must be now blocked, so we have
76 // to be able to safely reset the local changes and sync statuses for it.
77 // TODO(kinuko): This should be probably automatically handled in
78 // DeleteFileSystem via QuotaUtil::DeleteOriginDataOnFileThread.
79 file_system_context_->default_file_task_runner()->PostTaskAndReply(
80 FROM_HERE,
81 base::Bind(&ResetFileChangeTracker, file_system_context_, url_),
82 base::Bind(&RootDeleteHelper::DidResetFileChangeTracker,
83 weak_factory_.GetWeakPtr()));
86 void RootDeleteHelper::DidResetFileChangeTracker() {
87 DCHECK(!sync_status_->IsWritable(url_));
88 DCHECK(!sync_status_->IsWriting(url_));
90 // Reopening the filesystem.
91 file_system_context_->sandbox_delegate()->OpenFileSystem(
92 url_.origin(), url_.type(),
93 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
94 base::Bind(&RootDeleteHelper::DidOpenFileSystem,
95 weak_factory_.GetWeakPtr()), GURL());
98 void RootDeleteHelper::DidOpenFileSystem(const GURL& /* root */,
99 const std::string& /* name */,
100 base::PlatformFileError error) {
101 FileStatusCallback callback = callback_;
102 callback.Run(error);
105 } // namespace sync_file_system