root_delete_helper.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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.
4
5#include "chrome/browser/sync_file_system/local/root_delete_helper.h"
6
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"
17
18namespace sync_file_system {
19
20namespace {
21
22// This runs on FileSystemContext's default_file_task_runner.
23void 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());
33}
34
35}  // namespace
36
37RootDeleteHelper::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()));
53}
54
55RootDeleteHelper::~RootDeleteHelper() {
56}
57
58void RootDeleteHelper::Run() {
59  util::Log(logging::LOG_VERBOSE, FROM_HERE,
60            "Deleting the entire local filesystem for remote root deletion: "
61            "%s", url_.DebugString().c_str());
62
63  file_system_context_->DeleteFileSystem(
64      url_.origin(), url_.type(),
65      base::Bind(&RootDeleteHelper::DidDeleteFileSystem,
66                 weak_factory_.GetWeakPtr()));
67}
68
69void RootDeleteHelper::DidDeleteFileSystem(base::File::Error error) {
70  // Ignore errors, no idea how to deal with it.
71
72  DCHECK(!sync_status_->IsWritable(url_));
73  DCHECK(!sync_status_->IsWriting(url_));
74
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()));
84}
85
86void RootDeleteHelper::DidResetFileChangeTracker() {
87  DCHECK(!sync_status_->IsWritable(url_));
88  DCHECK(!sync_status_->IsWriting(url_));
89
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());
96}
97
98void RootDeleteHelper::DidOpenFileSystem(const GURL& /* root */,
99                                         const std::string& /* name */,
100                                         base::File::Error error) {
101  FileStatusCallback callback = callback_;
102  callback.Run(error);
103}
104
105}  // namespace sync_file_system
106