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_COPY_OPERATION_H_ 6 #define CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_ 7 8 #include "base/basictypes.h" 9 #include "base/memory/ref_counted.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "chrome/browser/chromeos/drive/file_errors.h" 13 #include "chrome/browser/drive/drive_service_interface.h" 14 #include "google_apis/drive/gdata_errorcode.h" 15 16 namespace base { 17 class FilePath; 18 class SequencedTaskRunner; 19 class Time; 20 } // namespace base 21 22 namespace google_apis { 23 class AboutResource; 24 class ResourceEntry; 25 } // namespace google_apis 26 27 namespace drive { 28 29 class JobScheduler; 30 class ResourceEntry; 31 32 namespace internal { 33 class FileCache; 34 class ResourceMetadata; 35 } // namespace internal 36 37 namespace file_system { 38 39 class CreateFileOperation; 40 class OperationObserver; 41 42 // This class encapsulates the drive Copy function. It is responsible for 43 // sending the request to the drive API, then updating the local state and 44 // metadata to reflect the new state. 45 class CopyOperation { 46 public: 47 CopyOperation(base::SequencedTaskRunner* blocking_task_runner, 48 OperationObserver* observer, 49 JobScheduler* scheduler, 50 internal::ResourceMetadata* metadata, 51 internal::FileCache* cache, 52 const ResourceIdCanonicalizer& id_canonicalizer); 53 ~CopyOperation(); 54 55 // Performs the copy operation on the file at drive path |src_file_path| 56 // with a target of |dest_file_path|. 57 // If |preserve_last_modified| is set to true, this tries to preserve 58 // last modified time stamp. This is supported only on Drive API v2. 59 // Invokes |callback| when finished with the result of the operation. 60 // |callback| must not be null. 61 void Copy(const base::FilePath& src_file_path, 62 const base::FilePath& dest_file_path, 63 bool preserve_last_modified, 64 const FileOperationCallback& callback); 65 66 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|. 67 // |local_src_file_path| must be a file from the local file system. 68 // |remote_dest_file_path| is the virtual destination path within Drive file 69 // system. 70 // 71 // |callback| must not be null. 72 void TransferFileFromLocalToRemote( 73 const base::FilePath& local_src_file_path, 74 const base::FilePath& remote_dest_file_path, 75 const FileOperationCallback& callback); 76 77 private: 78 // Params for Copy(). 79 struct CopyParams; 80 81 // Part of Copy(). Called after prepartion is done. 82 void CopyAfterPrepare(const CopyParams& params, 83 ResourceEntry* src_entry, 84 std::string* parent_resource_id, 85 FileError error); 86 87 // Part of TransferFileFromLocalToRemote(). Called after preparation is done. 88 // |gdoc_resource_id| and |parent_resource_id| is available only if the file 89 // is JSON GDoc file. 90 void TransferFileFromLocalToRemoteAfterPrepare( 91 const base::FilePath& local_src_path, 92 const base::FilePath& remote_dest_path, 93 const FileOperationCallback& callback, 94 std::string* gdoc_resource_id, 95 std::string* parent_resource_id, 96 FileError error); 97 98 // Copies resource with |resource_id| into the directory |parent_resource_id| 99 // with renaming it to |new_title|. 100 void CopyResourceOnServer(const std::string& resource_id, 101 const std::string& parent_resource_id, 102 const std::string& new_title, 103 const base::Time& last_modified, 104 const FileOperationCallback& callback); 105 106 // Part of CopyResourceOnServer. Called after server side copy is done. 107 void CopyResourceOnServerAfterServerSideCopy( 108 const FileOperationCallback& callback, 109 google_apis::GDataErrorCode status, 110 scoped_ptr<google_apis::ResourceEntry> resource_entry); 111 112 // Part of CopyResourceOnServer. Called after local state update is done. 113 void CopyResourceOnServerAfterUpdateLocalState( 114 const FileOperationCallback& callback, 115 base::FilePath* file_path, 116 FileError error); 117 118 // Creates an empty file on the server at |remote_dest_path| to ensure 119 // the location, stores a file at |local_file_path| in cache and marks it 120 // dirty, so that SyncClient will upload the data later. 121 void ScheduleTransferRegularFile(const base::FilePath& local_src_path, 122 const base::FilePath& remote_dest_path, 123 const FileOperationCallback& callback); 124 125 // Part of ScheduleTransferRegularFile(). Called after GetFileSize() is 126 // completed. 127 void ScheduleTransferRegularFileAfterGetFileSize( 128 const base::FilePath& local_src_path, 129 const base::FilePath& remote_dest_path, 130 const FileOperationCallback& callback, 131 int64 local_file_size); 132 133 // Part of ScheduleTransferRegularFile(). Called after GetAboutResource() 134 // is completed. 135 void ScheduleTransferRegularFileAfterGetAboutResource( 136 const base::FilePath& local_src_path, 137 const base::FilePath& remote_dest_path, 138 const FileOperationCallback& callback, 139 int64 local_file_size, 140 google_apis::GDataErrorCode status, 141 scoped_ptr<google_apis::AboutResource> about_resource); 142 143 // Part of ScheduleTransferRegularFile(). Called after file creation. 144 void ScheduleTransferRegularFileAfterCreate( 145 const base::FilePath& local_src_path, 146 const base::FilePath& remote_dest_path, 147 const FileOperationCallback& callback, 148 FileError error); 149 150 // Part of ScheduleTransferRegularFile(). Called after updating local state 151 // is completed. 152 void ScheduleTransferRegularFileAfterUpdateLocalState( 153 const FileOperationCallback& callback, 154 std::string* local_id, 155 FileError error); 156 157 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 158 OperationObserver* observer_; 159 JobScheduler* scheduler_; 160 internal::ResourceMetadata* metadata_; 161 internal::FileCache* cache_; 162 ResourceIdCanonicalizer id_canonicalizer_; 163 164 // Uploading a new file is internally implemented by creating a dirty file. 165 scoped_ptr<CreateFileOperation> create_file_operation_; 166 167 // Note: This should remain the last member so it'll be destroyed and 168 // invalidate the weak pointers before any other members are destroyed. 169 base::WeakPtrFactory<CopyOperation> weak_ptr_factory_; 170 DISALLOW_COPY_AND_ASSIGN(CopyOperation); 171 }; 172 173 } // namespace file_system 174 } // namespace drive 175 176 #endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_ 177