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_system/operation_delegate.h"
12 #include "components/drive/file_change.h"
13 #include "components/drive/file_errors.h"
14 #include "components/drive/job_scheduler.h"
15 #include "components/drive/resource_metadata.h"
18 namespace file_system
{
22 // Updates the timestamps of the entry specified by |file_path|.
23 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
24 const base::FilePath
& file_path
,
25 const base::Time
& last_access_time
,
26 const base::Time
& last_modified_time
,
27 ResourceEntry
* entry
) {
28 FileError error
= metadata
->GetResourceEntryByPath(file_path
, entry
);
29 if (error
!= FILE_ERROR_OK
)
32 PlatformFileInfoProto
* file_info
= entry
->mutable_file_info();
33 if (!last_access_time
.is_null())
34 file_info
->set_last_accessed(last_access_time
.ToInternalValue());
35 if (!last_modified_time
.is_null())
36 file_info
->set_last_modified(last_modified_time
.ToInternalValue());
37 entry
->set_metadata_edit_state(ResourceEntry::DIRTY
);
38 entry
->set_modification_date(base::Time::Now().ToInternalValue());
39 return metadata
->RefreshEntry(*entry
);
44 TouchOperation::TouchOperation(base::SequencedTaskRunner
* blocking_task_runner
,
45 OperationDelegate
* delegate
,
46 internal::ResourceMetadata
* metadata
)
47 : blocking_task_runner_(blocking_task_runner
),
50 weak_ptr_factory_(this) {
53 TouchOperation::~TouchOperation() {
56 void TouchOperation::TouchFile(const base::FilePath
& file_path
,
57 const base::Time
& last_access_time
,
58 const base::Time
& last_modified_time
,
59 const FileOperationCallback
& callback
) {
60 DCHECK(thread_checker_
.CalledOnValidThread());
61 DCHECK(!callback
.is_null());
63 ResourceEntry
* entry
= new ResourceEntry
;
64 base::PostTaskAndReplyWithResult(
65 blocking_task_runner_
.get(), FROM_HERE
,
66 base::Bind(&UpdateLocalState
, metadata_
, file_path
, last_access_time
,
67 last_modified_time
, entry
),
68 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState
,
69 weak_ptr_factory_
.GetWeakPtr(), file_path
, callback
,
73 void TouchOperation::TouchFileAfterUpdateLocalState(
74 const base::FilePath
& file_path
,
75 const FileOperationCallback
& callback
,
76 const ResourceEntry
* entry
,
78 DCHECK(thread_checker_
.CalledOnValidThread());
79 DCHECK(!callback
.is_null());
81 FileChange changed_files
;
82 changed_files
.Update(file_path
, entry
->file_info().is_directory()
83 ? FileChange::FILE_TYPE_DIRECTORY
84 : FileChange::FILE_TYPE_FILE
,
85 FileChange::CHANGE_TYPE_ADD_OR_UPDATE
);
87 if (error
== FILE_ERROR_OK
) {
88 delegate_
->OnFileChangedByOperation(changed_files
);
89 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
95 } // namespace file_system