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.
5 #include "chrome/browser/chromeos/drive/file_system/move_operation.h"
7 #include "base/sequenced_task_runner.h"
8 #include "chrome/browser/chromeos/drive/drive.pb.h"
9 #include "chrome/browser/chromeos/drive/file_change.h"
10 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
11 #include "chrome/browser/chromeos/drive/job_scheduler.h"
12 #include "chrome/browser/chromeos/drive/resource_metadata.h"
13 #include "content/public/browser/browser_thread.h"
15 using content::BrowserThread
;
18 namespace file_system
{
21 // Looks up ResourceEntry for source entry and the destination directory.
22 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
23 const base::FilePath
& src_path
,
24 const base::FilePath
& dest_path
,
25 FileChange
* changed_files
,
26 std::string
* local_id
) {
28 FileError error
= metadata
->GetResourceEntryByPath(src_path
, &entry
);
29 if (error
!= FILE_ERROR_OK
)
31 *local_id
= entry
.local_id();
33 ResourceEntry parent_entry
;
34 error
= metadata
->GetResourceEntryByPath(dest_path
.DirName(), &parent_entry
);
35 if (error
!= FILE_ERROR_OK
)
38 // The parent must be a directory.
39 if (!parent_entry
.file_info().is_directory())
40 return FILE_ERROR_NOT_A_DIRECTORY
;
42 changed_files
->Update(src_path
, entry
, FileChange::DELETE
);
44 // Strip the extension for a hosted document if necessary.
45 const std::string new_extension
=
46 base::FilePath(dest_path
.Extension()).AsUTF8Unsafe();
47 const bool has_hosted_document_extension
=
48 entry
.has_file_specific_info() &&
49 entry
.file_specific_info().is_hosted_document() &&
50 new_extension
== entry
.file_specific_info().document_extension();
51 const std::string new_title
=
52 has_hosted_document_extension
?
53 dest_path
.BaseName().RemoveExtension().AsUTF8Unsafe() :
54 dest_path
.BaseName().AsUTF8Unsafe();
56 entry
.set_title(new_title
);
57 entry
.set_parent_local_id(parent_entry
.local_id());
58 entry
.set_metadata_edit_state(ResourceEntry::DIRTY
);
59 entry
.set_modification_date(base::Time::Now().ToInternalValue());
60 error
= metadata
->RefreshEntry(entry
);
61 if (error
!= FILE_ERROR_OK
)
64 changed_files
->Update(dest_path
, entry
, FileChange::ADD_OR_UPDATE
);
70 MoveOperation::MoveOperation(base::SequencedTaskRunner
* blocking_task_runner
,
71 OperationDelegate
* delegate
,
72 internal::ResourceMetadata
* metadata
)
73 : blocking_task_runner_(blocking_task_runner
),
76 weak_ptr_factory_(this) {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
80 MoveOperation::~MoveOperation() {
81 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
84 void MoveOperation::Move(const base::FilePath
& src_file_path
,
85 const base::FilePath
& dest_file_path
,
86 const FileOperationCallback
& callback
) {
87 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
88 DCHECK(!callback
.is_null());
90 FileChange
* changed_files
= new FileChange
;
91 std::string
* local_id
= new std::string
;
92 base::PostTaskAndReplyWithResult(
93 blocking_task_runner_
.get(),
95 base::Bind(&UpdateLocalState
,
101 base::Bind(&MoveOperation::MoveAfterUpdateLocalState
,
102 weak_ptr_factory_
.GetWeakPtr(),
104 base::Owned(changed_files
),
105 base::Owned(local_id
)));
108 void MoveOperation::MoveAfterUpdateLocalState(
109 const FileOperationCallback
& callback
,
110 const FileChange
* changed_files
,
111 const std::string
* local_id
,
113 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
114 if (error
== FILE_ERROR_OK
) {
115 // Notify the change of directory.
116 delegate_
->OnFileChangedByOperation(*changed_files
);
117 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
123 } // namespace file_system