Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / drive / file_system / touch_operation.cc
blob914312c4a665a1fc3bb5add400a931193794fce8
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"
7 #include "base/bind.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;
18 namespace drive {
19 namespace file_system {
21 namespace {
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) {
29 ResourceEntry entry;
30 FileError error = metadata->GetResourceEntryByPath(file_path, &entry);
31 if (error != FILE_ERROR_OK)
32 return error;
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);
44 } // namespace
46 TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner,
47 OperationObserver* observer,
48 internal::ResourceMetadata* metadata)
49 : blocking_task_runner_(blocking_task_runner),
50 observer_(observer),
51 metadata_(metadata),
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(),
68 FROM_HERE,
69 base::Bind(&UpdateLocalState,
70 metadata_,
71 file_path,
72 last_access_time,
73 last_modified_time,
74 local_id),
75 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState,
76 weak_ptr_factory_.GetWeakPtr(),
77 file_path,
78 callback,
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,
86 FileError error) {
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);
94 callback.Run(error);
97 } // namespace file_system
98 } // namespace drive