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 "google_apis/drive/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
)) {}
71 FakeAPIUtil::~FakeAPIUtil() {}
73 void FakeAPIUtil::AddObserver(APIUtilObserver
* observer
) {}
75 void FakeAPIUtil::RemoveObserver(APIUtilObserver
* observer
) {}
77 void FakeAPIUtil::GetDriveDirectoryForSyncRoot(
78 const ResourceIdCallback
& callback
) {
79 base::MessageLoopProxy::current()->PostTask(
82 google_apis::HTTP_SUCCESS
,
83 "folder: sync_root_resource_id"));
86 void FakeAPIUtil::GetDriveDirectoryForOrigin(
87 const std::string
& sync_root_resource_id
,
89 const ResourceIdCallback
& callback
) {
90 base::MessageLoopProxy::current()->PostTask(
93 google_apis::HTTP_SUCCESS
,
94 "folder resource_id for " + origin
.host()));
97 void FakeAPIUtil::GetLargestChangeStamp(const ChangeStampCallback
& callback
) {
98 base::MessageLoopProxy::current()->PostTask(
100 base::Bind(callback
, google_apis::HTTP_SUCCESS
, largest_changestamp_
));
103 void FakeAPIUtil::GetResourceEntry(const std::string
& resource_id
,
104 const ResourceEntryCallback
& callback
) {
108 void FakeAPIUtil::ListFiles(const std::string
& directory_resource_id
,
109 const ResourceListCallback
& callback
) {
110 ListChanges(0, callback
);
113 void FakeAPIUtil::ListChanges(int64 start_changestamp
,
114 const ResourceListCallback
& callback
) {
115 scoped_ptr
<google_apis::ResourceList
> change_feed(
116 new google_apis::ResourceList());
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
)
124 scoped_ptr
<google_apis::ResourceEntry
> entry(
125 CreateResourceEntry(itr
->second
));
126 entries
.push_back(entry
.release());
129 std::sort(entries
.begin(), entries
.end(), ChangeStampComparator());
131 change_feed
->set_entries(entries
.Pass());
132 change_feed
->set_largest_changestamp(largest_changestamp_
);
134 base::MessageLoopProxy::current()->PostTask(
137 callback
, google_apis::HTTP_SUCCESS
, base::Passed(&change_feed
)));
140 void FakeAPIUtil::ContinueListing(const GURL
& next_link
,
141 const ResourceListCallback
& callback
) {
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
;
152 base::Time updated_time
;
153 google_apis::GDataErrorCode error
= google_apis::HTTP_NOT_FOUND
;
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
;
164 webkit_blob::ScopedFile dummy
;
165 base::MessageLoopProxy::current()->PostTask(
167 base::Bind(callback
, error
, file_md5
, file_size
, updated_time
,
168 base::Passed(&dummy
)));
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
) {
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
) {
185 void FakeAPIUtil::CreateDirectory(const std::string
& parent_resource_id
,
186 const std::string
& title
,
187 const ResourceIdCallback
& callback
) {
191 bool FakeAPIUtil::IsAuthenticated() const { return true; }
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(
199 base::Bind(callback
, google_apis::HTTP_NOT_FOUND
));
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
,
212 base::MessageLoopProxy::current()->PostTask(
214 base::Bind(callback
, google_apis::HTTP_SUCCESS
));
217 void FakeAPIUtil::EnsureSyncRootIsNotInMyDrive(
218 const std::string
& sync_root_resource_id
) {
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
,
229 remote_resources_
[resource_id
] = RemoteResource(
230 parent_resource_id
, parent_title
, title
, resource_id
,
231 md5
, type
, deleted
, ++largest_changestamp_
);
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());
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());
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
);
253 switch (resource
.type
) {
254 case SYNC_FILE_TYPE_FILE
:
255 entry
->set_kind(google_apis::ENTRY_KIND_FILE
);
257 case SYNC_FILE_TYPE_DIRECTORY
:
258 entry
->set_kind(google_apis::ENTRY_KIND_FOLDER
);
260 case SYNC_FILE_TYPE_UNKNOWN
:
261 entry
->set_kind(google_apis::ENTRY_KIND_UNKNOWN
);
268 GURL
FakeAPIUtil::ResourceIdToResourceLink(
269 const std::string
& resource_id
) const {
270 return url_generator_
.GenerateEditUrl(resource_id
);
273 } // namespace drive_backend
274 } // namespace sync_file_system