copy_operation.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/scoped_ptr.h" 10#include "base/memory/weak_ptr.h" 11#include "base/values.h" 12#include "chrome/browser/chromeos/drive/drive_resource_metadata.h" 13#include "chrome/browser/google_apis/drive_uploader.h" 14#include "chrome/browser/google_apis/gdata_errorcode.h" 15 16class FilePath; 17class GURL; 18 19namespace google_apis { 20class DriveServiceInterface; 21class DriveUploaderInterface; 22} 23 24namespace drive { 25 26class DriveCache; 27class DriveEntryProto; 28class DriveFileSystemInterface; 29 30using google_apis::DocumentEntry; 31using google_apis::GDataErrorCode; 32 33namespace file_system { 34 35class OperationObserver; 36 37// This class encapsulates the drive Copy function. It is resposible for 38// sending the request to the drive API, then updating the local state and 39// metadata to reflect the new state. 40class CopyOperation { 41 public: 42 CopyOperation(google_apis::DriveServiceInterface* drive_service, 43 DriveFileSystemInterface* drive_file_system, 44 DriveResourceMetadata* metadata, 45 google_apis::DriveUploaderInterface* uploader, 46 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, 47 OperationObserver* observer); 48 virtual ~CopyOperation(); 49 50 // Performs the copy operation on the file at drive path |src_file_path| 51 // with a target of |dest_file_path|. Invokes |callback| when finished with 52 // the result of the operation. 53 void Copy(const FilePath& src_file_path, 54 const FilePath& dest_file_path, 55 const FileOperationCallback& callback); 56 57 // Initiates transfer of |remote_src_file_path| to |local_dest_file_path|. 58 // |remote_src_file_path| is the virtual source path on the Drive file system. 59 // |local_dest_file_path| is the destination path on the local file system. 60 // 61 // Must be called from *UI* thread. |callback| is run on the calling thread. 62 // |callback| must not be null. 63 void TransferFileFromRemoteToLocal(const FilePath& remote_src_file_path, 64 const FilePath& local_dest_file_path, 65 const FileOperationCallback& callback); 66 67 // Initiates transfer of |local_src_file_path| to |remote_dest_file_path|. 68 // |local_src_file_path| must be a file from the local file system. 69 // |remote_dest_file_path| is the virtual destination path within Drive file 70 // system. 71 // 72 // Must be called from *UI* thread. |callback| is run on the calling thread. 73 // |callback| must not be null. 74 void TransferFileFromLocalToRemote(const FilePath& local_src_file_path, 75 const FilePath& remote_dest_file_path, 76 const FileOperationCallback& callback); 77 78 // Initiates transfer of |local_file_path| to |remote_dest_file_path|. 79 // |local_file_path| must be a regular file (i.e. not a hosted document) from 80 // the local file system, |remote_dest_file_path| is the virtual destination 81 // path within Drive file system. 82 // 83 // Must be called from *UI* thread. |callback| is run on the calling thread. 84 void TransferRegularFile(const FilePath& local_file_path, 85 const FilePath& remote_dest_file_path, 86 const FileOperationCallback& callback); 87 88 private: 89 // Struct used for StartFileUpload(). 90 struct StartFileUploadParams; 91 92 // Invoked upon completion of GetFileByPath initiated by 93 // TransferFileFromRemoteToLocal. If GetFileByPath reports no error, calls 94 // CopyLocalFileOnBlockingPool to copy |local_file_path| to 95 // |local_dest_file_path|. 96 // 97 // Can be called from UI thread. |callback| is run on the calling thread. 98 // |callback| must not be null. 99 void OnGetFileCompleteForTransferFile(const FilePath& local_dest_file_path, 100 const FileOperationCallback& callback, 101 DriveFileError error, 102 const FilePath& local_file_path, 103 const std::string& unused_mime_type, 104 DriveFileType file_type); 105 106 // Copies a document with |resource_id| to the directory at |dir_path| 107 // and names the copied document as |new_name|. 108 // 109 // Can be called from UI thread. |callback| is run on the calling thread. 110 // |callback| must not be null. 111 void CopyDocumentToDirectory(const FilePath& dir_path, 112 const std::string& resource_id, 113 const FilePath::StringType& new_name, 114 const FileOperationCallback& callback); 115 116 // Callback for handling document copy attempt. 117 // |callback| must not be null. 118 void OnCopyDocumentCompleted(const FilePath& dir_path, 119 const FileOperationCallback& callback, 120 GDataErrorCode status, 121 scoped_ptr<base::Value> data); 122 123 // Moves a file or directory at |file_path| in the root directory to 124 // another directory at |dir_path|. This function does nothing if 125 // |dir_path| points to the root directory. 126 // 127 // Can be called from UI thread. |callback| is run on the calling thread. 128 // |callback| must not be null. 129 void MoveEntryFromRootDirectory(const FilePath& directory_path, 130 const FileOperationCallback& callback, 131 DriveFileError error, 132 const FilePath& file_path); 133 134 // Part of MoveEntryFromRootDirectory(). Called after 135 // GetEntryInfoPairByPaths() is complete. |callback| must not be null. 136 void MoveEntryFromRootDirectoryAfterGetEntryInfoPair( 137 const FileOperationCallback& callback, 138 scoped_ptr<EntryInfoPairResult> result); 139 140 // Moves entry specified by |file_path| to the directory specified by 141 // |dir_path| and calls |callback| asynchronously. 142 // |callback| must not be null. 143 void MoveEntryToDirectory(const FilePath& file_path, 144 const FilePath& directory_path, 145 const FileMoveCallback& callback, 146 GDataErrorCode status, 147 const GURL& /* document_url */); 148 149 // Callback when an entry is moved to another directory on the client side. 150 // Notifies the directory change and runs |callback|. 151 // |callback| must not be null. 152 void NotifyAndRunFileOperationCallback( 153 const FileOperationCallback& callback, 154 DriveFileError error, 155 const FilePath& moved_file_path); 156 157 // Part of Copy(). Called after GetEntryInfoPairByPaths() is 158 // complete. |callback| must not be null. 159 void CopyAfterGetEntryInfoPair(const FilePath& dest_file_path, 160 const FileOperationCallback& callback, 161 scoped_ptr<EntryInfoPairResult> result); 162 163 // Invoked upon completion of GetFileByPath initiated by Copy. If 164 // GetFileByPath reports no error, calls TransferRegularFile to transfer 165 // |local_file_path| to |remote_dest_file_path|. 166 // 167 // Can be called from UI thread. |callback| is run on the calling thread. 168 void OnGetFileCompleteForCopy(const FilePath& remote_dest_file_path, 169 const FileOperationCallback& callback, 170 DriveFileError error, 171 const FilePath& local_file_path, 172 const std::string& unused_mime_type, 173 DriveFileType file_type); 174 175 // Kicks off file upload once it receives |file_size| and |content_type|. 176 void StartFileUpload(const StartFileUploadParams& params, 177 int64* file_size, 178 std::string* content_type, 179 DriveFileError error); 180 181 // Part of StartFileUpload(). Called after GetEntryInfoByPath() 182 // is complete. 183 void StartFileUploadAfterGetEntryInfo( 184 const StartFileUploadParams& params, 185 int64 file_size, 186 const std::string& content_type, 187 DriveFileError error, 188 scoped_ptr<DriveEntryProto> entry_proto); 189 190 // Helper function that completes bookkeeping tasks related to 191 // completed file transfer. 192 void OnTransferCompleted(const FileOperationCallback& callback, 193 google_apis::DriveUploadError error, 194 const FilePath& drive_path, 195 const FilePath& file_path, 196 scoped_ptr<DocumentEntry> document_entry); 197 198 // Part of TransferFileFromLocalToRemote(). Called after 199 // GetEntryInfoByPath() is complete. 200 void TransferFileFromLocalToRemoteAfterGetEntryInfo( 201 const FilePath& local_src_file_path, 202 const FilePath& remote_dest_file_path, 203 const FileOperationCallback& callback, 204 DriveFileError error, 205 scoped_ptr<DriveEntryProto> entry_proto); 206 207 // Initiates transfer of |local_file_path| with |resource_id| to 208 // |remote_dest_file_path|. |local_file_path| must be a file from the local 209 // file system, |remote_dest_file_path| is the virtual destination path within 210 // Drive file system. If |resource_id| is a non-empty string, the transfer is 211 // handled by CopyDocumentToDirectory. Otherwise, the transfer is handled by 212 // TransferRegularFile. 213 // 214 // Must be called from *UI* thread. |callback| is run on the calling thread. 215 // |callback| must not be null. 216 void TransferFileForResourceId( 217 const FilePath& local_file_path, 218 const FilePath& remote_dest_file_path, 219 const FileOperationCallback& callback, 220 std::string* resource_id); 221 222 google_apis::DriveServiceInterface* drive_service_; 223 DriveFileSystemInterface* drive_file_system_; 224 DriveResourceMetadata* metadata_; 225 google_apis::DriveUploaderInterface* uploader_; 226 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; 227 OperationObserver* observer_; 228 229 // WeakPtrFactory bound to the UI thread. 230 // Note: This should remain the last member so it'll be destroyed and 231 // invalidate the weak pointers before any other members are destroyed. 232 base::WeakPtrFactory<CopyOperation> weak_ptr_factory_; 233 234 DISALLOW_COPY_AND_ASSIGN(CopyOperation); 235}; 236 237} // namespace file_system 238} // namespace drive 239 240#endif // CHROME_BROWSER_CHROMEOS_DRIVE_FILE_SYSTEM_COPY_OPERATION_H_ 241