1 // Copyright 2013 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/touch_operation.h"
8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h"
10 #include "base/time/time.h"
11 #include "chrome/browser/chromeos/drive/file_change.h"
12 #include "chrome/browser/chromeos/drive/file_errors.h"
13 #include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
14 #include "chrome/browser/chromeos/drive/resource_metadata.h"
15 #include "content/public/browser/browser_thread.h"
17 using content::BrowserThread;
20 namespace file_system {
24 // Updates the timestamps of the entry specified by |file_path|.
25 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
26 const base::FilePath& file_path,
27 const base::Time& last_access_time,
28 const base::Time& last_modified_time,
30 std::string* local_id) {
31 FileError error = metadata->GetResourceEntryByPath(file_path, entry);
32 if (error != FILE_ERROR_OK)
34 *local_id = entry->local_id();
36 PlatformFileInfoProto* file_info = entry->mutable_file_info();
37 if (!last_access_time.is_null())
38 file_info->set_last_accessed(last_access_time.ToInternalValue());
39 if (!last_modified_time.is_null())
40 file_info->set_last_modified(last_modified_time.ToInternalValue());
41 entry->set_metadata_edit_state(ResourceEntry::DIRTY);
42 entry->set_modification_date(base::Time::Now().ToInternalValue());
43 return metadata->RefreshEntry(*entry);
48 TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner,
49 OperationDelegate* delegate,
50 internal::ResourceMetadata* metadata)
51 : blocking_task_runner_(blocking_task_runner),
54 weak_ptr_factory_(this) {
57 TouchOperation::~TouchOperation() {
60 void TouchOperation::TouchFile(const base::FilePath& file_path,
61 const base::Time& last_access_time,
62 const base::Time& last_modified_time,
63 const FileOperationCallback& callback) {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65 DCHECK(!callback.is_null());
67 std::string* local_id = new std::string;
68 ResourceEntry* entry = new ResourceEntry;
69 base::PostTaskAndReplyWithResult(
70 blocking_task_runner_.get(),
72 base::Bind(&UpdateLocalState,
79 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState,
80 weak_ptr_factory_.GetWeakPtr(),
84 base::Owned(local_id)));
87 void TouchOperation::TouchFileAfterUpdateLocalState(
88 const base::FilePath& file_path,
89 const FileOperationCallback& callback,
90 const ResourceEntry* entry,
91 const std::string* local_id,
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
94 DCHECK(!callback.is_null());
96 FileChange changed_files;
99 entry->file_info().is_directory() ?
100 FileChange::FILE_TYPE_DIRECTORY : FileChange::FILE_TYPE_FILE,
101 FileChange::ADD_OR_UPDATE);
103 if (error == FILE_ERROR_OK) {
104 delegate_->OnFileChangedByOperation(changed_files);
105 delegate_->OnEntryUpdatedByOperation(*local_id);
110 } // namespace file_system