remove_operation.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright (c) 2012 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/chromeos/drive/file_system/remove_operation.h"
6
7#include "base/sequenced_task_runner.h"
8#include "chrome/browser/chromeos/drive/drive.pb.h"
9#include "chrome/browser/chromeos/drive/file_cache.h"
10#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
11#include "chrome/browser/chromeos/drive/file_system_util.h"
12#include "chrome/browser/chromeos/drive/resource_metadata.h"
13#include "content/public/browser/browser_thread.h"
14
15using content::BrowserThread;
16
17namespace drive {
18namespace file_system {
19
20namespace {
21
22// Removes cache file and moves the metadata entry to the trash.
23FileError UpdateLocalState(internal::ResourceMetadata* metadata,
24                           internal::FileCache* cache,
25                           const base::FilePath& path,
26                           bool is_recursive,
27                           std::string* local_id,
28                           base::FilePath* changed_directory_path) {
29  FileError error = metadata->GetIdByPath(path, local_id);
30  if (error != FILE_ERROR_OK)
31    return error;
32
33  ResourceEntry entry;
34  error = metadata->GetResourceEntryById(*local_id, &entry);
35  if (error != FILE_ERROR_OK)
36    return error;
37
38  if (entry.file_info().is_directory() && !is_recursive) {
39    // Check emptiness of the directory.
40    ResourceEntryVector entries;
41    error = metadata->ReadDirectoryByPath(path, &entries);
42    if (error != FILE_ERROR_OK)
43      return error;
44    if (!entries.empty())
45      return FILE_ERROR_NOT_EMPTY;
46  }
47
48  error = cache->Remove(*local_id);
49  if (error != FILE_ERROR_OK)
50    return error;
51
52  *changed_directory_path = path.DirName();
53
54  // Move to the trash.
55  entry.set_parent_local_id(util::kDriveTrashDirLocalId);
56  return metadata->RefreshEntry(entry);
57}
58
59}  // namespace
60
61RemoveOperation::RemoveOperation(
62    base::SequencedTaskRunner* blocking_task_runner,
63    OperationObserver* observer,
64    internal::ResourceMetadata* metadata,
65    internal::FileCache* cache)
66    : blocking_task_runner_(blocking_task_runner),
67      observer_(observer),
68      metadata_(metadata),
69      cache_(cache),
70      weak_ptr_factory_(this) {
71  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
72}
73
74RemoveOperation::~RemoveOperation() {
75  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76}
77
78void RemoveOperation::Remove(const base::FilePath& path,
79                             bool is_recursive,
80                             const FileOperationCallback& callback) {
81  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
82  DCHECK(!callback.is_null());
83
84  std::string* local_id = new std::string;
85  base::FilePath* changed_directory_path = new base::FilePath;
86  base::PostTaskAndReplyWithResult(
87      blocking_task_runner_.get(),
88      FROM_HERE,
89      base::Bind(&UpdateLocalState,
90                 metadata_,
91                 cache_,
92                 path,
93                 is_recursive,
94                 local_id,
95                 changed_directory_path),
96      base::Bind(&RemoveOperation::RemoveAfterUpdateLocalState,
97                 weak_ptr_factory_.GetWeakPtr(),
98                 callback,
99                 base::Owned(local_id),
100                 base::Owned(changed_directory_path)));
101}
102
103void RemoveOperation::RemoveAfterUpdateLocalState(
104    const FileOperationCallback& callback,
105    const std::string* local_id,
106    const base::FilePath* changed_directory_path,
107    FileError error) {
108  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109  DCHECK(!callback.is_null());
110
111  if (error == FILE_ERROR_OK) {
112    observer_->OnDirectoryChangedByOperation(*changed_directory_path);
113    observer_->OnEntryUpdatedByOperation(*local_id);
114  }
115
116  callback.Run(error);
117}
118
119}  // namespace file_system
120}  // namespace drive
121