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_errors.h"
12 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
13 #include "chrome/browser/chromeos/drive/resource_metadata.h"
14 #include "content/public/browser/browser_thread.h"
16 using content::BrowserThread
;
19 namespace file_system
{
23 // Updates the timestamps of the entry specified by |file_path|.
24 FileError
UpdateLocalState(internal::ResourceMetadata
* metadata
,
25 const base::FilePath
& file_path
,
26 const base::Time
& last_access_time
,
27 const base::Time
& last_modified_time
,
28 std::string
* local_id
) {
30 FileError error
= metadata
->GetResourceEntryByPath(file_path
, &entry
);
31 if (error
!= FILE_ERROR_OK
)
33 *local_id
= entry
.local_id();
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 return metadata
->RefreshEntry(entry
);
46 TouchOperation::TouchOperation(base::SequencedTaskRunner
* blocking_task_runner
,
47 OperationObserver
* observer
,
48 internal::ResourceMetadata
* metadata
)
49 : blocking_task_runner_(blocking_task_runner
),
52 weak_ptr_factory_(this) {
55 TouchOperation::~TouchOperation() {
58 void TouchOperation::TouchFile(const base::FilePath
& file_path
,
59 const base::Time
& last_access_time
,
60 const base::Time
& last_modified_time
,
61 const FileOperationCallback
& callback
) {
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
63 DCHECK(!callback
.is_null());
65 std::string
* local_id
= new std::string
;
66 base::PostTaskAndReplyWithResult(
67 blocking_task_runner_
.get(),
69 base::Bind(&UpdateLocalState
,
75 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState
,
76 weak_ptr_factory_
.GetWeakPtr(),
79 base::Owned(local_id
)));
82 void TouchOperation::TouchFileAfterUpdateLocalState(
83 const base::FilePath
& file_path
,
84 const FileOperationCallback
& callback
,
85 const std::string
* local_id
,
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
88 DCHECK(!callback
.is_null());
90 if (error
== FILE_ERROR_OK
) {
91 observer_
->OnDirectoryChangedByOperation(file_path
.DirName());
92 observer_
->OnEntryUpdatedByOperation(*local_id
);
97 } // namespace file_system