Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync_file_system / drive_backend_v1 / fake_api_util.cc
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.
4
5 #include "chrome/browser/sync_file_system/drive_backend_v1/fake_api_util.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "google_apis/drive/drive_entry_kinds.h"
13 #include "webkit/common/blob/scoped_file.h"
14
15 namespace sync_file_system {
16 namespace drive_backend {
17
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;
34 }
35
36 struct FakeAPIUtil::ChangeStampComparator {
37   bool operator()(const google_apis::ResourceEntry* left,
38                   const google_apis::ResourceEntry* right) {
39     return left->changestamp() < right->changestamp();
40   }
41 };
42
43 FakeAPIUtil::RemoteResource::RemoteResource()
44     : type(SYNC_FILE_TYPE_UNKNOWN), deleted(false), changestamp(0) {}
45
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,
52     SyncFileType type,
53     bool deleted,
54     int64 changestamp)
55     : parent_resource_id(parent_resource_id),
56       parent_title(parent_title),
57       title(title),
58       resource_id(resource_id),
59       md5_checksum(md5_checksum),
60       type(type),
61       deleted(deleted),
62       changestamp(changestamp) {}
63
64 FakeAPIUtil::RemoteResource::~RemoteResource() {}
65
66 FakeAPIUtil::FakeAPIUtil()
67     : largest_changestamp_(0),
68       url_generator_(
69           GURL(google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction)) {}
70
71 FakeAPIUtil::~FakeAPIUtil() {}
72
73 void FakeAPIUtil::AddObserver(APIUtilObserver* observer) {}
74
75 void FakeAPIUtil::RemoveObserver(APIUtilObserver* observer) {}
76
77 void FakeAPIUtil::GetDriveDirectoryForSyncRoot(
78     const ResourceIdCallback& callback) {
79   base::MessageLoopProxy::current()->PostTask(
80       FROM_HERE,
81       base::Bind(callback,
82                  google_apis::HTTP_SUCCESS,
83                  "folder: sync_root_resource_id"));
84 }
85
86 void FakeAPIUtil::GetDriveDirectoryForOrigin(
87     const std::string& sync_root_resource_id,
88     const GURL& origin,
89     const ResourceIdCallback& callback) {
90   base::MessageLoopProxy::current()->PostTask(
91       FROM_HERE,
92       base::Bind(callback,
93                  google_apis::HTTP_SUCCESS,
94                  "folder resource_id for " + origin.host()));
95 }
96
97 void FakeAPIUtil::GetLargestChangeStamp(const ChangeStampCallback& callback) {
98   base::MessageLoopProxy::current()->PostTask(
99       FROM_HERE,
100       base::Bind(callback, google_apis::HTTP_SUCCESS, largest_changestamp_));
101 }
102
103 void FakeAPIUtil::GetResourceEntry(const std::string& resource_id,
104                                    const ResourceEntryCallback& callback) {
105   NOTREACHED();
106 }
107
108 void FakeAPIUtil::ListFiles(const std::string& directory_resource_id,
109                             const ResourceListCallback& callback) {
110   ListChanges(0, callback);
111 }
112
113 void FakeAPIUtil::ListChanges(int64 start_changestamp,
114                               const ResourceListCallback& callback) {
115   scoped_ptr<google_apis::ResourceList> change_feed(
116       new google_apis::ResourceList());
117
118   ScopedVector<google_apis::ResourceEntry> entries;
119   typedef RemoteResourceByResourceId::const_iterator iterator;
120   for (iterator itr = remote_resources_.begin();
121        itr != remote_resources_.end(); ++itr) {
122     if (itr->second.changestamp < start_changestamp)
123       continue;
124     scoped_ptr<google_apis::ResourceEntry> entry(
125         CreateResourceEntry(itr->second));
126     entries.push_back(entry.release());
127   }
128
129   std::sort(entries.begin(), entries.end(), ChangeStampComparator());
130
131   change_feed->set_entries(entries.Pass());
132   change_feed->set_largest_changestamp(largest_changestamp_);
133
134   base::MessageLoopProxy::current()->PostTask(
135       FROM_HERE,
136       base::Bind(
137           callback, google_apis::HTTP_SUCCESS, base::Passed(&change_feed)));
138 }
139
140 void FakeAPIUtil::ContinueListing(const GURL& next_link,
141                                   const ResourceListCallback& callback) {
142   NOTREACHED();
143 }
144
145 void FakeAPIUtil::DownloadFile(const std::string& resource_id,
146                                const std::string& local_file_md5,
147                                const DownloadFileCallback& callback) {
148   RemoteResourceByResourceId::iterator found =
149       remote_resources_.find(resource_id);
150   std::string file_md5;
151   int64 file_size = 0;
152   base::Time updated_time;
153   google_apis::GDataErrorCode error = google_apis::HTTP_NOT_FOUND;
154
155   if (found != remote_resources_.end() && !found->second.deleted) {
156     scoped_ptr<google_apis::ResourceEntry> entry(
157         CreateResourceEntry(found->second));
158     file_md5 = entry->file_md5();
159     file_size = entry->file_size();
160     updated_time = entry->updated_time();
161     error = google_apis::HTTP_SUCCESS;
162   }
163
164   webkit_blob::ScopedFile dummy;
165   base::MessageLoopProxy::current()->PostTask(
166       FROM_HERE,
167       base::Bind(callback, error, file_md5, file_size, updated_time,
168                  base::Passed(&dummy)));
169 }
170
171 void FakeAPIUtil::UploadNewFile(const std::string& directory_resource_id,
172                                 const base::FilePath& local_file_path,
173                                 const std::string& title,
174                                 const UploadFileCallback& callback) {
175   NOTREACHED();
176 }
177
178 void FakeAPIUtil::UploadExistingFile(const std::string& resource_id,
179                                      const std::string& remote_file_md5,
180                                      const base::FilePath& local_file_path,
181                                      const UploadFileCallback& callback) {
182   NOTREACHED();
183 }
184
185 void FakeAPIUtil::CreateDirectory(const std::string& parent_resource_id,
186                                   const std::string& title,
187                                   const ResourceIdCallback& callback) {
188   NOTREACHED();
189 }
190
191 bool FakeAPIUtil::IsAuthenticated() const { return true; }
192
193 void FakeAPIUtil::DeleteFile(const std::string& resource_id,
194                              const std::string& remote_file_md5,
195                              const GDataErrorCallback& callback) {
196   if (!ContainsKey(remote_resources_, resource_id)) {
197     base::MessageLoopProxy::current()->PostTask(
198         FROM_HERE,
199         base::Bind(callback, google_apis::HTTP_NOT_FOUND));
200     return;
201   }
202
203   const RemoteResource& deleted_directory = remote_resources_[resource_id];
204   PushRemoteChange(deleted_directory.parent_resource_id,
205                    deleted_directory.parent_title,
206                    deleted_directory.title,
207                    deleted_directory.resource_id,
208                    deleted_directory.md5_checksum,
209                    SYNC_FILE_TYPE_UNKNOWN,
210                    true /* deleted */);
211
212   base::MessageLoopProxy::current()->PostTask(
213       FROM_HERE,
214       base::Bind(callback, google_apis::HTTP_SUCCESS));
215 }
216
217 void FakeAPIUtil::EnsureSyncRootIsNotInMyDrive(
218     const std::string& sync_root_resource_id) {
219   // Nothing to do.
220 }
221
222 void FakeAPIUtil::PushRemoteChange(const std::string& parent_resource_id,
223                                    const std::string& parent_title,
224                                    const std::string& title,
225                                    const std::string& resource_id,
226                                    const std::string& md5,
227                                    SyncFileType type,
228                                    bool deleted) {
229   remote_resources_[resource_id] = RemoteResource(
230       parent_resource_id, parent_title, title, resource_id,
231       md5, type, deleted, ++largest_changestamp_);
232 }
233
234 scoped_ptr<google_apis::ResourceEntry> FakeAPIUtil::CreateResourceEntry(
235     const RemoteResource& resource) const {
236   scoped_ptr<google_apis::ResourceEntry> entry(
237       new google_apis::ResourceEntry());
238   ScopedVector<google_apis::Link> parent_links;
239   scoped_ptr<google_apis::Link> link(new google_apis::Link());
240
241   link->set_type(google_apis::Link::LINK_PARENT);
242   link->set_href(ResourceIdToResourceLink(resource.parent_resource_id));
243   link->set_title(resource.parent_title);
244   parent_links.push_back(link.release());
245
246   entry->set_links(parent_links.Pass());
247   entry->set_title(resource.title);
248   entry->set_resource_id(resource.resource_id);
249   entry->set_file_md5(resource.md5_checksum);
250   entry->set_deleted(resource.deleted);
251   entry->set_changestamp(resource.changestamp);
252
253   switch (resource.type) {
254     case SYNC_FILE_TYPE_FILE:
255       entry->set_kind(google_apis::ENTRY_KIND_FILE);
256       break;
257     case SYNC_FILE_TYPE_DIRECTORY:
258       entry->set_kind(google_apis::ENTRY_KIND_FOLDER);
259       break;
260     case SYNC_FILE_TYPE_UNKNOWN:
261       entry->set_kind(google_apis::ENTRY_KIND_UNKNOWN);
262       break;
263   }
264
265   return entry.Pass();
266 }
267
268 GURL FakeAPIUtil::ResourceIdToResourceLink(
269     const std::string& resource_id) const {
270   return url_generator_.GenerateEditUrl(resource_id);
271 }
272
273 }  // namespace drive_backend
274 }  // namespace sync_file_system