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/copy_operation.h" | 5 #include "chrome/browser/chromeos/drive/file_system/copy_operation.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/task_runner_util.h" | 10 #include "base/task_runner_util.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 120 |
121 return FILE_ERROR_OK; | 121 return FILE_ERROR_OK; |
122 } | 122 } |
123 | 123 |
124 // Gets the file size of the |local_path|, and the ResourceEntry for the parent | 124 // Gets the file size of the |local_path|, and the ResourceEntry for the parent |
125 // of |remote_path| to prepare the necessary information for transfer. | 125 // of |remote_path| to prepare the necessary information for transfer. |
126 FileError PrepareTransferFileFromLocalToRemote( | 126 FileError PrepareTransferFileFromLocalToRemote( |
127 internal::ResourceMetadata* metadata, | 127 internal::ResourceMetadata* metadata, |
128 const base::FilePath& local_src_path, | 128 const base::FilePath& local_src_path, |
129 const base::FilePath& remote_dest_path, | 129 const base::FilePath& remote_dest_path, |
130 std::string* gdoc_resource_id) { | 130 std::string* gdoc_resource_id, |
| 131 std::string* parent_resource_id) { |
131 ResourceEntry parent_entry; | 132 ResourceEntry parent_entry; |
132 FileError error = metadata->GetResourceEntryByPath( | 133 FileError error = metadata->GetResourceEntryByPath( |
133 remote_dest_path.DirName(), &parent_entry); | 134 remote_dest_path.DirName(), &parent_entry); |
134 if (error != FILE_ERROR_OK) | 135 if (error != FILE_ERROR_OK) |
135 return error; | 136 return error; |
136 | 137 |
137 // The destination's parent must be a directory. | 138 // The destination's parent must be a directory. |
138 if (!parent_entry.file_info().is_directory()) | 139 if (!parent_entry.file_info().is_directory()) |
139 return FILE_ERROR_NOT_A_DIRECTORY; | 140 return FILE_ERROR_NOT_A_DIRECTORY; |
140 | 141 |
141 // Try to parse GDoc File and extract the resource id, if necessary. | 142 // Try to parse GDoc File and extract the resource id, if necessary. |
142 // Failing isn't problem. It'd be handled as a regular file, then. | 143 // Failing isn't problem. It'd be handled as a regular file, then. |
143 if (util::HasGDocFileExtension(local_src_path)) | 144 if (util::HasGDocFileExtension(local_src_path)) { |
144 *gdoc_resource_id = util::ReadResourceIdFromGDocFile(local_src_path); | 145 *gdoc_resource_id = util::ReadResourceIdFromGDocFile(local_src_path); |
| 146 *parent_resource_id = parent_entry.resource_id(); |
| 147 } |
145 | 148 |
146 return FILE_ERROR_OK; | 149 return FILE_ERROR_OK; |
147 } | 150 } |
148 | 151 |
149 } // namespace | 152 } // namespace |
150 | 153 |
151 CopyOperation::CopyOperation(base::SequencedTaskRunner* blocking_task_runner, | 154 CopyOperation::CopyOperation(base::SequencedTaskRunner* blocking_task_runner, |
152 OperationObserver* observer, | 155 OperationObserver* observer, |
153 JobScheduler* scheduler, | 156 JobScheduler* scheduler, |
154 internal::ResourceMetadata* metadata, | 157 internal::ResourceMetadata* metadata, |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 280 } |
278 | 281 |
279 void CopyOperation::TransferFileFromLocalToRemote( | 282 void CopyOperation::TransferFileFromLocalToRemote( |
280 const base::FilePath& local_src_path, | 283 const base::FilePath& local_src_path, |
281 const base::FilePath& remote_dest_path, | 284 const base::FilePath& remote_dest_path, |
282 const FileOperationCallback& callback) { | 285 const FileOperationCallback& callback) { |
283 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
284 DCHECK(!callback.is_null()); | 287 DCHECK(!callback.is_null()); |
285 | 288 |
286 std::string* gdoc_resource_id = new std::string; | 289 std::string* gdoc_resource_id = new std::string; |
| 290 std::string* parent_resource_id = new std::string; |
287 base::PostTaskAndReplyWithResult( | 291 base::PostTaskAndReplyWithResult( |
288 blocking_task_runner_.get(), | 292 blocking_task_runner_.get(), |
289 FROM_HERE, | 293 FROM_HERE, |
290 base::Bind( | 294 base::Bind( |
291 &PrepareTransferFileFromLocalToRemote, | 295 &PrepareTransferFileFromLocalToRemote, |
292 metadata_, local_src_path, remote_dest_path, gdoc_resource_id), | 296 metadata_, local_src_path, remote_dest_path, |
| 297 gdoc_resource_id, parent_resource_id), |
293 base::Bind( | 298 base::Bind( |
294 &CopyOperation::TransferFileFromLocalToRemoteAfterPrepare, | 299 &CopyOperation::TransferFileFromLocalToRemoteAfterPrepare, |
295 weak_ptr_factory_.GetWeakPtr(), | 300 weak_ptr_factory_.GetWeakPtr(), |
296 local_src_path, remote_dest_path, callback, | 301 local_src_path, remote_dest_path, callback, |
297 base::Owned(gdoc_resource_id))); | 302 base::Owned(gdoc_resource_id), base::Owned(parent_resource_id))); |
298 } | 303 } |
299 | 304 |
300 void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare( | 305 void CopyOperation::TransferFileFromLocalToRemoteAfterPrepare( |
301 const base::FilePath& local_src_path, | 306 const base::FilePath& local_src_path, |
302 const base::FilePath& remote_dest_path, | 307 const base::FilePath& remote_dest_path, |
303 const FileOperationCallback& callback, | 308 const FileOperationCallback& callback, |
304 std::string* gdoc_resource_id, | 309 std::string* gdoc_resource_id, |
| 310 std::string* parent_resource_id, |
305 FileError error) { | 311 FileError error) { |
306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
307 DCHECK(!callback.is_null()); | 313 DCHECK(!callback.is_null()); |
308 | 314 |
309 if (error != FILE_ERROR_OK) { | 315 if (error != FILE_ERROR_OK) { |
310 callback.Run(error); | 316 callback.Run(error); |
311 return; | 317 return; |
312 } | 318 } |
313 | 319 |
314 // For regular files, schedule the transfer. | 320 // For regular files, schedule the transfer. |
315 if (gdoc_resource_id->empty()) { | 321 if (gdoc_resource_id->empty()) { |
316 ScheduleTransferRegularFile(local_src_path, remote_dest_path, callback); | 322 ScheduleTransferRegularFile(local_src_path, remote_dest_path, callback); |
317 return; | 323 return; |
318 } | 324 } |
319 | 325 |
320 // This is uploading a JSON file representing a hosted document. | 326 // This is uploading a JSON file representing a hosted document. |
321 // Copy the document on the Drive server. | 327 // Copy the document on the Drive server. |
322 | 328 |
323 // GDoc file may contain a resource ID in the old format. | 329 // GDoc file may contain a resource ID in the old format. |
324 const std::string canonicalized_resource_id = | 330 const std::string canonicalized_resource_id = |
325 drive_service_->CanonicalizeResourceId(*gdoc_resource_id); | 331 drive_service_->CanonicalizeResourceId(*gdoc_resource_id); |
326 | 332 |
327 // TODO(hidehiko): Use CopyResource for Drive API v2. | 333 // If Drive API v2 is enabled, we can copy resources on server side. |
| 334 if (util::IsDriveV2ApiEnabled()) { |
| 335 CopyResourceOnServer( |
| 336 *gdoc_resource_id, *parent_resource_id, |
| 337 // Drop the document extension, which should not be in the title. |
| 338 // TODO(yoshiki): Remove this code with crbug.com/223304. |
| 339 remote_dest_path.BaseName().RemoveExtension().AsUTF8Unsafe(), |
| 340 callback); |
| 341 return; |
| 342 } |
328 | 343 |
329 CopyHostedDocument( | 344 CopyHostedDocument( |
330 canonicalized_resource_id, | 345 canonicalized_resource_id, |
331 // Drop the document extension, which should not be | 346 // Drop the document extension, which should not be |
332 // in the document title. | 347 // in the document title. |
333 // TODO(yoshiki): Remove this code with crbug.com/223304. | 348 // TODO(yoshiki): Remove this code with crbug.com/223304. |
334 remote_dest_path.RemoveExtension(), | 349 remote_dest_path.RemoveExtension(), |
335 callback); | 350 callback); |
336 } | 351 } |
337 | 352 |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 } | 570 } |
556 | 571 |
557 DCHECK_EQ(util::GetDriveMyDriveRootPath().value(), | 572 DCHECK_EQ(util::GetDriveMyDriveRootPath().value(), |
558 file_path->DirName().value()) << file_path->value(); | 573 file_path->DirName().value()) << file_path->value(); |
559 | 574 |
560 move_operation_->Move(*file_path, dest_path, callback); | 575 move_operation_->Move(*file_path, dest_path, callback); |
561 } | 576 } |
562 | 577 |
563 } // namespace file_system | 578 } // namespace file_system |
564 } // namespace drive | 579 } // namespace drive |
OLD | NEW |