remove_operation.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_REMOVE_OPERATION_H_ 6#define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_REMOVE_OPERATION_H_ 7 8#include "base/basictypes.h" 9#include "base/memory/scoped_ptr.h" 10#include "base/memory/weak_ptr.h" 11#include "chrome/browser/chromeos/drive/file_errors.h" 12#include "chrome/browser/chromeos/drive/file_system_interface.h" 13#include "chrome/browser/google_apis/gdata_errorcode.h" 14 15namespace base { 16class FilePath; 17} // namespace base 18 19namespace drive { 20 21class JobScheduler; 22class ResourceEntry; 23 24namespace internal { 25class FileCache; 26class ResourceMetadata; 27} // namespace internal 28 29namespace file_system { 30 31class OperationObserver; 32 33// This class encapsulates the drive Remove function. It is responsible for 34// sending the request to the drive API, then updating the local state and 35// metadata to reflect the new state. 36class RemoveOperation { 37 public: 38 RemoveOperation(OperationObserver* observer, 39 JobScheduler* scheduler, 40 internal::ResourceMetadata* metadata, 41 internal::FileCache* cache); 42 ~RemoveOperation(); 43 44 // Removes the resource at |path|. If |path| is a directory and |is_recursive| 45 // is set, it recursively removes all the descendants. If |is_recursive| is 46 // not set, it succeeds only when the directory is empty. 47 // 48 // |callback| must not be null. 49 void Remove(const base::FilePath& path, 50 bool is_recursive, 51 const FileOperationCallback& callback); 52 53 private: 54 // Part of Remove(). Called after GetResourceEntryByPath() is complete. 55 void RemoveAfterGetResourceEntry(const base::FilePath& path, 56 bool is_recursive, 57 const FileOperationCallback& callback, 58 FileError error, 59 scoped_ptr<ResourceEntry> entry); 60 61 // Part of Remove(). Called when is_recursive = false and trying to remove 62 // a directory. In this case the emptiness of directory must be checked. 63 void RemoveAfterReadDirectory(const std::string& resource_id, 64 const FileOperationCallback& callback, 65 FileError error, 66 scoped_ptr<ResourceEntryVector> entries); 67 68 // Part of Remove(). Called after server-side removal is done. Removes the 69 // entry with |resource_id| from the resource metadata and the cache. 70 void RemoveResourceLocally(const FileOperationCallback& callback, 71 const std::string& resource_id, 72 google_apis::GDataErrorCode status); 73 74 // Part of Remove(). Sends notification for directory changes, and runs 75 // |callback| with |error|. 76 void NotifyDirectoryChanged(const FileOperationCallback& callback, 77 FileError error, 78 const base::FilePath& directory_path); 79 80 OperationObserver* observer_; 81 JobScheduler* scheduler_; 82 internal::ResourceMetadata* metadata_; 83 internal::FileCache* cache_; 84 85 // Note: This should remain the last member so it'll be destroyed and 86 // invalidate the weak pointers before any other members are destroyed. 87 base::WeakPtrFactory<RemoveOperation> weak_ptr_factory_; 88 DISALLOW_COPY_AND_ASSIGN(RemoveOperation); 89}; 90 91} // namespace file_system 92} // namespace drive 93 94#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_REMOVE_OPERATION_H_ 95