Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/chromeos/drive/file_system/touch_operation.cc

Issue 101073002: drive: Support offline touch (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chromeos/drive/file_system/touch_operation.h" 5 #include "chrome/browser/chromeos/drive/file_system/touch_operation.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file_path.h" 8 #include "base/files/file_path.h"
9 #include "base/sequenced_task_runner.h" 9 #include "base/sequenced_task_runner.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "chrome/browser/chromeos/drive/file_errors.h" 11 #include "chrome/browser/chromeos/drive/file_errors.h"
12 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h" 12 #include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
13 #include "chrome/browser/chromeos/drive/file_system_util.h"
14 #include "chrome/browser/chromeos/drive/job_scheduler.h"
15 #include "chrome/browser/chromeos/drive/resource_entry_conversion.h"
16 #include "chrome/browser/chromeos/drive/resource_metadata.h" 13 #include "chrome/browser/chromeos/drive/resource_metadata.h"
17 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
18 15
19 using content::BrowserThread; 16 using content::BrowserThread;
20 17
21 namespace drive { 18 namespace drive {
22 namespace file_system { 19 namespace file_system {
23 20
24 namespace { 21 namespace {
25 22
26 // Refreshes the entry specified by |local_id| with the contents of 23 // Updates the timestamps of the entry specified by |file_path|.
27 // |resource_entry|. 24 FileError UpdateLocalState(internal::ResourceMetadata* metadata,
28 FileError RefreshEntry(internal::ResourceMetadata* metadata, 25 const base::FilePath& file_path,
29 const std::string& local_id, 26 const base::Time& last_access_time,
30 scoped_ptr<google_apis::ResourceEntry> resource_entry, 27 const base::Time& last_modified_time,
31 base::FilePath* file_path) { 28 std::string* local_id) {
32 DCHECK(resource_entry);
33
34 ResourceEntry entry; 29 ResourceEntry entry;
35 std::string parent_resource_id; 30 FileError error = metadata->GetResourceEntryByPath(file_path, &entry);
36 if (!ConvertToResourceEntry(*resource_entry, &entry, &parent_resource_id))
37 return FILE_ERROR_NOT_A_FILE;
38
39 std::string parent_local_id;
40 FileError error = metadata->GetIdByResourceId(parent_resource_id,
41 &parent_local_id);
42 if (error != FILE_ERROR_OK) 31 if (error != FILE_ERROR_OK)
43 return error; 32 return error;
33 *local_id = entry.local_id();
44 34
45 entry.set_local_id(local_id); 35 PlatformFileInfoProto* file_info = entry.mutable_file_info();
46 entry.set_parent_local_id(parent_local_id); 36 if (!last_access_time.is_null())
47 37 file_info->set_last_accessed(last_access_time.ToInternalValue());
48 error = metadata->RefreshEntry(entry); 38 if (!last_modified_time.is_null())
49 if (error != FILE_ERROR_OK) 39 file_info->set_last_modified(last_modified_time.ToInternalValue());
50 return error; 40 entry.set_metadata_edit_state(ResourceEntry::DIRTY);
51 41 return metadata->RefreshEntry(entry);
52 *file_path = metadata->GetFilePath(local_id);
53 return file_path->empty() ? FILE_ERROR_FAILED : FILE_ERROR_OK;
54 } 42 }
55 43
56 } // namespace 44 } // namespace
57 45
58 TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner, 46 TouchOperation::TouchOperation(base::SequencedTaskRunner* blocking_task_runner,
59 OperationObserver* observer, 47 OperationObserver* observer,
60 JobScheduler* scheduler,
61 internal::ResourceMetadata* metadata) 48 internal::ResourceMetadata* metadata)
62 : blocking_task_runner_(blocking_task_runner), 49 : blocking_task_runner_(blocking_task_runner),
63 observer_(observer), 50 observer_(observer),
64 scheduler_(scheduler),
65 metadata_(metadata), 51 metadata_(metadata),
66 weak_ptr_factory_(this) { 52 weak_ptr_factory_(this) {
67 } 53 }
68 54
69 TouchOperation::~TouchOperation() { 55 TouchOperation::~TouchOperation() {
70 } 56 }
71 57
72 void TouchOperation::TouchFile(const base::FilePath& file_path, 58 void TouchOperation::TouchFile(const base::FilePath& file_path,
73 const base::Time& last_access_time, 59 const base::Time& last_access_time,
74 const base::Time& last_modified_time, 60 const base::Time& last_modified_time,
75 const FileOperationCallback& callback) { 61 const FileOperationCallback& callback) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
77 DCHECK(!callback.is_null()); 63 DCHECK(!callback.is_null());
78 64
79 ResourceEntry* entry = new ResourceEntry; 65 std::string* local_id = new std::string;
80 base::PostTaskAndReplyWithResult( 66 base::PostTaskAndReplyWithResult(
81 blocking_task_runner_.get(), 67 blocking_task_runner_.get(),
82 FROM_HERE, 68 FROM_HERE,
83 base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath, 69 base::Bind(&UpdateLocalState,
84 base::Unretained(metadata_), 70 metadata_,
85 file_path, 71 file_path,
86 entry),
87 base::Bind(&TouchOperation::TouchFileAfterGetResourceEntry,
88 weak_ptr_factory_.GetWeakPtr(),
89 last_access_time, 72 last_access_time,
90 last_modified_time, 73 last_modified_time,
74 local_id),
75 base::Bind(&TouchOperation::TouchFileAfterUpdateLocalState,
76 weak_ptr_factory_.GetWeakPtr(),
77 file_path,
91 callback, 78 callback,
92 base::Owned(entry))); 79 base::Owned(local_id)));
93 } 80 }
94 81
95 void TouchOperation::TouchFileAfterGetResourceEntry( 82 void TouchOperation::TouchFileAfterUpdateLocalState(
96 const base::Time& last_access_time, 83 const base::FilePath& file_path,
97 const base::Time& last_modified_time,
98 const FileOperationCallback& callback, 84 const FileOperationCallback& callback,
99 ResourceEntry* entry, 85 const std::string* local_id,
100 FileError error) {
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
102 DCHECK(!callback.is_null());
103 DCHECK(entry);
104
105 if (error != FILE_ERROR_OK) {
106 callback.Run(error);
107 return;
108 }
109
110 // Note: |last_modified_time| is mapped to modifiedDate, |last_access_time|
111 // is mapped to lastViewedByMeDate. See also ConvertToResourceEntry().
112 scheduler_->TouchResource(
113 entry->resource_id(), last_modified_time, last_access_time,
114 base::Bind(&TouchOperation::TouchFileAfterServerTimeStampUpdated,
115 weak_ptr_factory_.GetWeakPtr(),
116 entry->local_id(), callback));
117 }
118
119 void TouchOperation::TouchFileAfterServerTimeStampUpdated(
120 const std::string& local_id,
121 const FileOperationCallback& callback,
122 google_apis::GDataErrorCode gdata_error,
123 scoped_ptr<google_apis::ResourceEntry> resource_entry) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
125 DCHECK(!callback.is_null());
126
127 FileError error = GDataToFileError(gdata_error);
128 if (error != FILE_ERROR_OK) {
129 callback.Run(error);
130 return;
131 }
132
133 base::FilePath* file_path = new base::FilePath;
134 base::PostTaskAndReplyWithResult(
135 blocking_task_runner_.get(),
136 FROM_HERE,
137 base::Bind(&RefreshEntry,
138 base::Unretained(metadata_),
139 local_id,
140 base::Passed(&resource_entry),
141 file_path),
142 base::Bind(&TouchOperation::TouchFileAfterRefreshMetadata,
143 weak_ptr_factory_.GetWeakPtr(),
144 base::Owned(file_path),
145 callback));
146 }
147
148 void TouchOperation::TouchFileAfterRefreshMetadata(
149 const base::FilePath* file_path,
150 const FileOperationCallback& callback,
151 FileError error) { 86 FileError error) {
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
153 DCHECK(!callback.is_null()); 88 DCHECK(!callback.is_null());
154 89
155 if (error == FILE_ERROR_OK) 90 if (error == FILE_ERROR_OK) {
156 observer_->OnDirectoryChangedByOperation(file_path->DirName()); 91 observer_->OnDirectoryChangedByOperation(file_path.DirName());
157 92 observer_->OnEntryUpdatedByOperation(*local_id);
93 }
158 callback.Run(error); 94 callback.Run(error);
159 } 95 }
160 96
161 } // namespace file_system 97 } // namespace file_system
162 } // namespace drive 98 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698