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/job_scheduler.h"
15 #include "chrome/browser/chromeos/drive/resource_metadata.h"
16 #include "content/public/browser/browser_thread.h"
18 using content::BrowserThread
;
21 namespace file_system
{
25 // Updates the timestamps of the entry specified by |file_path|.
26 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
27 const base::FilePath
& file_path
,
28 const base::Time
& last_access_time
,
29 const base::Time
& last_modified_time
,
30 ResourceEntry
* entry
) {
31 FileError error
= metadata
->GetResourceEntryByPath(file_path
, entry
);
32 if (error
!= FILE_ERROR_OK
)
35 PlatformFileInfoProto
* file_info
= entry
->mutable_file_info();
36 if (!last_access_time
.is_null())
37 file_info
->set_last_accessed(last_access_time
.ToInternalValue());
38 if (!last_modified_time
.is_null())
39 file_info
->set_last_modified(last_modified_time
.ToInternalValue());
40 entry
->set_metadata_edit_state(ResourceEntry::DIRTY
);
41 entry
->set_modification_date(base::Time::Now().ToInternalValue());
42 return metadata
->RefreshEntry(*entry
);
47 TouchOperation::TouchOperation(base::SequencedTaskRunner
* blocking_task_runner
,
48 OperationDelegate
* delegate
,
49 internal::ResourceMetadata
* metadata
)
50 : blocking_task_runner_(blocking_task_runner
),
53 weak_ptr_factory_(this) {
56 TouchOperation::~TouchOperation() {
59 void TouchOperation::TouchFile(const base::FilePath
& file_path
,
60 const base::Time
& last_access_time
,
61 const base::Time
& last_modified_time
,
62 const FileOperationCallback
& callback
) {
63 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
64 DCHECK(!callback
.is_null());
66 ResourceEntry
* entry
= new ResourceEntry
;
67 base::PostTaskAndReplyWithResult(
68 blocking_task_runner_
.get(), FROM_HERE
,
69 base::Bind(&UpdateLocalState
, metadata_
, file_path
, last_access_time
,
70 last_modified_time
, entry
),
71 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState
,
72 weak_ptr_factory_
.GetWeakPtr(), file_path
, callback
,
76 void TouchOperation::TouchFileAfterUpdateLocalState(
77 const base::FilePath
& file_path
,
78 const FileOperationCallback
& callback
,
79 const ResourceEntry
* entry
,
81 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
82 DCHECK(!callback
.is_null());
84 FileChange changed_files
;
87 entry
->file_info().is_directory() ?
88 FileChange::FILE_TYPE_DIRECTORY
: FileChange::FILE_TYPE_FILE
,
89 FileChange::ADD_OR_UPDATE
);
91 if (error
== FILE_ERROR_OK
) {
92 delegate_
->OnFileChangedByOperation(changed_files
);
93 delegate_
->OnEntryUpdatedByOperation(ClientContext(USER_INITIATED
),
99 } // namespace file_system