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/sync_file_system/drive_backend_v1/fake_api_util.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "chrome/browser/google_apis/drive_entry_kinds.h"
13 #include "webkit/common/blob/scoped_file.h"
15 namespace sync_file_system {
16 namespace drive_backend {
18 bool FakeAPIUtil::RemoteResourceComparator::operator()(
19 const RemoteResource& left,
20 const RemoteResource& right) {
21 if (left.parent_resource_id != right.parent_resource_id)
22 return left.parent_resource_id < right.parent_resource_id;
23 if (left.parent_title != right.parent_title)
24 return left.parent_title < right.parent_title;
25 if (left.title != right.title)
26 return left.title < right.title;
27 if (left.resource_id != right.resource_id)
28 return left.resource_id < right.resource_id;
29 if (left.md5_checksum != right.md5_checksum)
30 return left.md5_checksum < right.md5_checksum;
31 if (left.deleted != right.deleted)
32 return left.deleted < right.deleted;
33 return left.changestamp < right.changestamp;
36 struct FakeAPIUtil::ChangeStampComparator {
37 bool operator()(const google_apis::ResourceEntry* left,
38 const google_apis::ResourceEntry* right) {
39 return left->changestamp() < right->changestamp();
43 FakeAPIUtil::RemoteResource::RemoteResource()
44 : type(SYNC_FILE_TYPE_UNKNOWN), deleted(false), changestamp(0) {}
46 FakeAPIUtil::RemoteResource::RemoteResource(
47 const std::string& parent_resource_id,
48 const std::string& parent_title,
49 const std::string& title,
50 const std::string& resource_id,
51 const std::string& md5_checksum,
55 : parent_resource_id(parent_resource_id),
56 parent_title(parent_title),
58 resource_id(resource_id),
59 md5_checksum(md5_checksum),
62 changestamp(changestamp) {}
64 FakeAPIUtil::RemoteResource::~RemoteResource() {}
66 FakeAPIUtil::FakeAPIUtil()
67 : largest_changestamp_(0),
69 GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction),
70 GURL(google_apis::GDataWapiUrlGenerator::
71 kBaseDownloadUrlForProduction)) {}
73 FakeAPIUtil::~FakeAPIUtil() {}
75 void FakeAPIUtil::AddObserver(APIUtilObserver* observer) {}
77 void FakeAPIUtil::RemoveObserver(APIUtilObserver* observer) {}
79 void FakeAPIUtil::GetDriveDirectoryForSyncRoot(
80 const ResourceIdCallback& callback) {
81 base::MessageLoopProxy::current()->PostTask(
84 google_apis::HTTP_SUCCESS,
85 "folder: sync_root_resource_id"));
88 void FakeAPIUtil::GetDriveDirectoryForOrigin(
89 const std::string& sync_root_resource_id,
91 const ResourceIdCallback& callback) {
92 base::MessageLoopProxy::current()->PostTask(
95 google_apis::HTTP_SUCCESS,
96 "folder resource_id for " + origin.host()));
99 void FakeAPIUtil::GetLargestChangeStamp(const ChangeStampCallback& callback) {
100 base::MessageLoopProxy::current()->PostTask(
102 base::Bind(callback, google_apis::HTTP_SUCCESS, largest_changestamp_));
105 void FakeAPIUtil::GetResourceEntry(const std::string& resource_id,
106 const ResourceEntryCallback& callback) {
110 void FakeAPIUtil::ListFiles(const std::string& directory_resource_id,
111 const ResourceListCallback& callback) {
112 ListChanges(0, callback);
115 void FakeAPIUtil::ListChanges(int64 start_changestamp,
116 const ResourceListCallback& callback) {
117 scoped_ptr<google_apis::ResourceList> change_feed(
118 new google_apis::ResourceList());
120 ScopedVector<google_apis::ResourceEntry> entries;
121 typedef RemoteResourceByResourceId::const_iterator iterator;
122 for (iterator itr = remote_resources_.begin();
123 itr != remote_resources_.end(); ++itr) {
124 if (itr->second.changestamp < start_changestamp)
126 scoped_ptr<google_apis::ResourceEntry> entry(
127 CreateResourceEntry(itr->second));
128 entries.push_back(entry.release());
131 std::sort(entries.begin(), entries.end(), ChangeStampComparator());
133 change_feed->set_entries(entries.Pass());
134 change_feed->set_largest_changestamp(largest_changestamp_);
136 base::MessageLoopProxy::current()->PostTask(
139 callback, google_apis::HTTP_SUCCESS, base::Passed(&change_feed)));
142 void FakeAPIUtil::ContinueListing(const GURL& next_link,
143 const ResourceListCallback& callback) {
147 void FakeAPIUtil::DownloadFile(const std::string& resource_id,
148 const std::string& local_file_md5,
149 const DownloadFileCallback& callback) {
150 RemoteResourceByResourceId::iterator found =
151 remote_resources_.find(resource_id);
152 std::string file_md5;
154 base::Time updated_time;
155 google_apis::GDataErrorCode error = google_apis::HTTP_NOT_FOUND;
157 if (found != remote_resources_.end() && !found->second.deleted) {
158 scoped_ptr<google_apis::ResourceEntry> entry(
159 CreateResourceEntry(found->second));
160 file_md5 = entry->file_md5();
161 file_size = entry->file_size();
162 updated_time = entry->updated_time();
163 error = google_apis::HTTP_SUCCESS;
166 webkit_blob::ScopedFile dummy;
167 base::MessageLoopProxy::current()->PostTask(
169 base::Bind(callback, error, file_md5, file_size, updated_time,
170 base::Passed(&dummy)));
173 void FakeAPIUtil::UploadNewFile(const std::string& directory_resource_id,
174 const base::FilePath& local_file_path,
175 const std::string& title,
176 const UploadFileCallback& callback) {
180 void FakeAPIUtil::UploadExistingFile(const std::string& resource_id,
181 const std::string& remote_file_md5,
182 const base::FilePath& local_file_path,
183 const UploadFileCallback& callback) {
187 void FakeAPIUtil::CreateDirectory(const std::string& parent_resource_id,
188 const std::string& title,
189 const ResourceIdCallback& callback) {
193 bool FakeAPIUtil::IsAuthenticated() const { return true; }
195 void FakeAPIUtil::DeleteFile(const std::string& resource_id,
196 const std::string& remote_file_md5,
197 const GDataErrorCallback& callback) {
198 if (!ContainsKey(remote_resources_, resource_id)) {
199 base::MessageLoopProxy::current()->PostTask(
201 base::Bind(callback, google_apis::HTTP_NOT_FOUND));
205 const RemoteResource& deleted_directory = remote_resources_[resource_id];
206 PushRemoteChange(deleted_directory.parent_resource_id,
207 deleted_directory.parent_title,
208 deleted_directory.title,
209 deleted_directory.resource_id,
210 deleted_directory.md5_checksum,
211 SYNC_FILE_TYPE_UNKNOWN,
214 base::MessageLoopProxy::current()->PostTask(
216 base::Bind(callback, google_apis::HTTP_SUCCESS));
219 GURL FakeAPIUtil::ResourceIdToResourceLink(
220 const std::string& resource_id) const {
221 return url_generator_.GenerateEditUrl(resource_id);
224 void FakeAPIUtil::EnsureSyncRootIsNotInMyDrive(
225 const std::string& sync_root_resource_id) {
229 void FakeAPIUtil::PushRemoteChange(const std::string& parent_resource_id,
230 const std::string& parent_title,
231 const std::string& title,
232 const std::string& resource_id,
233 const std::string& md5,
236 remote_resources_[resource_id] = RemoteResource(
237 parent_resource_id, parent_title, title, resource_id,
238 md5, type, deleted, ++largest_changestamp_);
241 scoped_ptr<google_apis::ResourceEntry> FakeAPIUtil::CreateResourceEntry(
242 const RemoteResource& resource) const {
243 scoped_ptr<google_apis::ResourceEntry> entry(
244 new google_apis::ResourceEntry());
245 ScopedVector<google_apis::Link> parent_links;
246 scoped_ptr<google_apis::Link> link(new google_apis::Link());
248 link->set_type(google_apis::Link::LINK_PARENT);
249 link->set_href(ResourceIdToResourceLink(resource.parent_resource_id));
250 link->set_title(resource.parent_title);
251 parent_links.push_back(link.release());
253 entry->set_links(parent_links.Pass());
254 entry->set_title(resource.title);
255 entry->set_resource_id(resource.resource_id);
256 entry->set_file_md5(resource.md5_checksum);
257 entry->set_deleted(resource.deleted);
258 entry->set_changestamp(resource.changestamp);
260 switch (resource.type) {
261 case SYNC_FILE_TYPE_FILE:
262 entry->set_kind(google_apis::ENTRY_KIND_FILE);
264 case SYNC_FILE_TYPE_DIRECTORY:
265 entry->set_kind(google_apis::ENTRY_KIND_FOLDER);
267 case SYNC_FILE_TYPE_UNKNOWN:
268 entry->set_kind(google_apis::ENTRY_KIND_UNKNOWN);
275 } // namespace drive_backend
276 } // namespace sync_file_system