| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/drive/file_system/move_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/move_operation.h" |
| 6 | 6 |
| 7 #include "chrome/browser/chromeos/drive/drive.pb.h" | 7 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 8 #include "chrome/browser/chromeos/drive/drive_cache.h" | 8 #include "chrome/browser/chromeos/drive/drive_cache.h" |
| 9 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 9 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" | 10 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 | 78 |
| 79 Rename(src_file_path, dest_file_path.BaseName().value(), | 79 Rename(src_file_path, dest_file_path.BaseName().value(), |
| 80 final_file_path_update_callback); | 80 final_file_path_update_callback); |
| 81 return; | 81 return; |
| 82 } | 82 } |
| 83 | 83 |
| 84 // Otherwise, the move operation involves three steps: | 84 // Otherwise, the move operation involves three steps: |
| 85 // 1. Renames the file at |src_file_path| to basename(|dest_file_path|) | 85 // 1. Renames the file at |src_file_path| to basename(|dest_file_path|) |
| 86 // within the same directory. The rename operation is a no-op if | 86 // within the same directory. The rename operation is a no-op if |
| 87 // basename(|src_file_path|) equals to basename(|dest_file_path|). | 87 // basename(|src_file_path|) equals to basename(|dest_file_path|). |
| 88 // 2. Removes the file from its parent directory (the file is not deleted, but | 88 // 2. Removes the file from its parent directory (the file is not deleted), |
| 89 // just becomes orphaned). | 89 // which effectively moves the file to the root directory. |
| 90 // 3. Adds the file to the parent directory of |dest_file_path|. | 90 // 3. Adds the file to the parent directory of |dest_file_path|, which |
| 91 // | 91 // effectively moves the file from the root directory to the parent |
| 92 // TODO(kinaba): After the step 2, the file gets into the state with no parent | 92 // directory of |dest_file_path|. |
| 93 // node. Our current implementation regards the state as belonging to the root | |
| 94 // directory, so below the file is dealt as such. In fact, this is not the | |
| 95 // case on the server side. No-parent and in-root is a different concept. We | |
| 96 // need to make our implementation consistent to the server: crbug.com/171207. | |
| 97 const FileMoveCallback add_file_to_directory_callback = | 93 const FileMoveCallback add_file_to_directory_callback = |
| 98 base::Bind(&MoveOperation::AddEntryToDirectory, | 94 base::Bind(&MoveOperation::MoveEntryFromRootDirectory, |
| 99 weak_ptr_factory_.GetWeakPtr(), | 95 weak_ptr_factory_.GetWeakPtr(), |
| 100 dest_parent_path, | 96 dest_parent_path, |
| 101 callback); | 97 callback); |
| 102 | 98 |
| 103 const FileMoveCallback remove_file_from_directory_callback = | 99 const FileMoveCallback remove_file_from_directory_callback = |
| 104 base::Bind(&MoveOperation::RemoveEntryFromDirectory, | 100 base::Bind(&MoveOperation::RemoveEntryFromNonRootDirectory, |
| 105 weak_ptr_factory_.GetWeakPtr(), | 101 weak_ptr_factory_.GetWeakPtr(), |
| 106 add_file_to_directory_callback); | 102 add_file_to_directory_callback); |
| 107 | 103 |
| 108 Rename(src_file_path, dest_file_path.BaseName().value(), | 104 Rename(src_file_path, dest_file_path.BaseName().value(), |
| 109 remove_file_from_directory_callback); | 105 remove_file_from_directory_callback); |
| 110 } | 106 } |
| 111 | 107 |
| 112 void MoveOperation::OnFilePathUpdated(const FileOperationCallback& callback, | 108 void MoveOperation::OnFilePathUpdated(const FileOperationCallback& callback, |
| 113 DriveFileError error, | 109 DriveFileError error, |
| 114 const FilePath& /* file_path */) { | 110 const FilePath& /* file_path */) { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 197 } |
| 202 | 198 |
| 203 metadata_->RenameEntry( | 199 metadata_->RenameEntry( |
| 204 file_path, | 200 file_path, |
| 205 new_name, | 201 new_name, |
| 206 base::Bind(&MoveOperation::NotifyAndRunFileMoveCallback, | 202 base::Bind(&MoveOperation::NotifyAndRunFileMoveCallback, |
| 207 weak_ptr_factory_.GetWeakPtr(), | 203 weak_ptr_factory_.GetWeakPtr(), |
| 208 callback)); | 204 callback)); |
| 209 } | 205 } |
| 210 | 206 |
| 211 void MoveOperation::RemoveEntryFromDirectory( | 207 void MoveOperation::RemoveEntryFromNonRootDirectory( |
| 212 const FileMoveCallback& callback, | 208 const FileMoveCallback& callback, |
| 213 DriveFileError error, | 209 DriveFileError error, |
| 214 const FilePath& file_path) { | 210 const FilePath& file_path) { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 216 DCHECK(!callback.is_null()); | 212 DCHECK(!callback.is_null()); |
| 217 | 213 |
| 214 const FilePath dir_path = file_path.DirName(); |
| 215 // Return if there is an error or |dir_path| is the root directory. |
| 216 if (error != DRIVE_FILE_OK || dir_path == FilePath(kDriveRootDirectory)) { |
| 217 callback.Run(error, file_path); |
| 218 return; |
| 219 } |
| 220 |
| 218 metadata_->GetEntryInfoPairByPaths( | 221 metadata_->GetEntryInfoPairByPaths( |
| 219 file_path, | 222 file_path, |
| 220 file_path.DirName(), | 223 dir_path, |
| 221 base::Bind( | 224 base::Bind( |
| 222 &MoveOperation::RemoveEntryFromDirectoryAfterEntryInfoPair, | 225 &MoveOperation::RemoveEntryFromNonRootDirectoryAfterEntryInfoPair, |
| 223 weak_ptr_factory_.GetWeakPtr(), | 226 weak_ptr_factory_.GetWeakPtr(), |
| 224 callback)); | 227 callback)); |
| 225 } | 228 } |
| 226 | 229 |
| 227 void MoveOperation::RemoveEntryFromDirectoryAfterEntryInfoPair( | 230 void MoveOperation::RemoveEntryFromNonRootDirectoryAfterEntryInfoPair( |
| 228 const FileMoveCallback& callback, | 231 const FileMoveCallback& callback, |
| 229 scoped_ptr<EntryInfoPairResult> result) { | 232 scoped_ptr<EntryInfoPairResult> result) { |
| 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 231 DCHECK(!callback.is_null()); | 234 DCHECK(!callback.is_null()); |
| 232 DCHECK(result.get()); | 235 DCHECK(result.get()); |
| 233 | 236 |
| 234 const FilePath& file_path = result->first.path; | 237 const FilePath& file_path = result->first.path; |
| 235 if (result->first.error != DRIVE_FILE_OK) { | 238 if (result->first.error != DRIVE_FILE_OK) { |
| 236 callback.Run(result->first.error, file_path); | 239 callback.Run(result->first.error, file_path); |
| 237 return; | 240 return; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 256 weak_ptr_factory_.GetWeakPtr(), | 259 weak_ptr_factory_.GetWeakPtr(), |
| 257 file_path, | 260 file_path, |
| 258 FilePath(kDriveRootDirectory), | 261 FilePath(kDriveRootDirectory), |
| 259 base::Bind(&MoveOperation::NotifyAndRunFileMoveCallback, | 262 base::Bind(&MoveOperation::NotifyAndRunFileMoveCallback, |
| 260 weak_ptr_factory_.GetWeakPtr(), | 263 weak_ptr_factory_.GetWeakPtr(), |
| 261 callback))); | 264 callback))); |
| 262 } | 265 } |
| 263 | 266 |
| 264 // TODO(zork): Share with CopyOperation. | 267 // TODO(zork): Share with CopyOperation. |
| 265 // See: crbug.com/150050 | 268 // See: crbug.com/150050 |
| 266 void MoveOperation::AddEntryToDirectory(const FilePath& directory_path, | 269 void MoveOperation::MoveEntryFromRootDirectory( |
| 267 const FileOperationCallback& callback, | 270 const FilePath& directory_path, |
| 268 DriveFileError error, | 271 const FileOperationCallback& callback, |
| 269 const FilePath& file_path) { | 272 DriveFileError error, |
| 273 const FilePath& file_path) { |
| 270 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 274 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 271 DCHECK(!callback.is_null()); | 275 DCHECK(!callback.is_null()); |
| 276 DCHECK_EQ(kDriveRootDirectory, file_path.DirName().value()); |
| 277 |
| 278 // Return if there is an error or |dir_path| is the root directory. |
| 279 if (error != DRIVE_FILE_OK || |
| 280 directory_path == FilePath(kDriveRootDirectory)) { |
| 281 callback.Run(error); |
| 282 return; |
| 283 } |
| 272 | 284 |
| 273 metadata_->GetEntryInfoPairByPaths( | 285 metadata_->GetEntryInfoPairByPaths( |
| 274 file_path, | 286 file_path, |
| 275 directory_path, | 287 directory_path, |
| 276 base::Bind( | 288 base::Bind( |
| 277 &MoveOperation::AddEntryToDirectoryAfterGetEntryInfoPair, | 289 &MoveOperation::MoveEntryFromRootDirectoryAfterGetEntryInfoPair, |
| 278 weak_ptr_factory_.GetWeakPtr(), | 290 weak_ptr_factory_.GetWeakPtr(), |
| 279 callback)); | 291 callback)); |
| 280 } | 292 } |
| 281 | 293 |
| 282 // TODO(zork): Share with CopyOperation. | 294 // TODO(zork): Share with CopyOperation. |
| 283 // See: crbug.com/150050 | 295 // See: crbug.com/150050 |
| 284 void MoveOperation::AddEntryToDirectoryAfterGetEntryInfoPair( | 296 void MoveOperation::MoveEntryFromRootDirectoryAfterGetEntryInfoPair( |
| 285 const FileOperationCallback& callback, | 297 const FileOperationCallback& callback, |
| 286 scoped_ptr<EntryInfoPairResult> result) { | 298 scoped_ptr<EntryInfoPairResult> result) { |
| 287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 299 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 288 DCHECK(!callback.is_null()); | 300 DCHECK(!callback.is_null()); |
| 289 DCHECK(result.get()); | 301 DCHECK(result.get()); |
| 290 | 302 |
| 291 if (result->first.error != DRIVE_FILE_OK) { | 303 if (result->first.error != DRIVE_FILE_OK) { |
| 292 callback.Run(result->first.error); | 304 callback.Run(result->first.error); |
| 293 return; | 305 return; |
| 294 } else if (result->second.error != DRIVE_FILE_OK) { | 306 } else if (result->second.error != DRIVE_FILE_OK) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 DCHECK(!callback.is_null()); | 372 DCHECK(!callback.is_null()); |
| 361 | 373 |
| 362 if (error == DRIVE_FILE_OK) | 374 if (error == DRIVE_FILE_OK) |
| 363 observer_->OnDirectoryChangedByOperation(moved_file_path.DirName()); | 375 observer_->OnDirectoryChangedByOperation(moved_file_path.DirName()); |
| 364 | 376 |
| 365 callback.Run(error, moved_file_path); | 377 callback.Run(error, moved_file_path); |
| 366 } | 378 } |
| 367 | 379 |
| 368 } // namespace file_system | 380 } // namespace file_system |
| 369 } // namespace drive | 381 } // namespace drive |
| OLD | NEW |