1 // Copyright (c) 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/fake_drive_file_sync_client.h"
10 #include "base/location.h"
11 #include "base/message_loop_proxy.h"
13 namespace sync_file_system
{
15 bool FakeDriveFileSyncClient::RemoteResourceComparator::operator()(
16 const RemoteResource
& left
,
17 const RemoteResource
& right
) {
18 if (left
.parent_resource_id
!= right
.parent_resource_id
)
19 return left
.parent_resource_id
< right
.parent_resource_id
;
20 if (left
.parent_title
!= right
.parent_title
)
21 return left
.parent_title
< right
.parent_title
;
22 if (left
.title
!= right
.title
)
23 return left
.title
< right
.title
;
24 if (left
.resource_id
!= right
.resource_id
)
25 return left
.resource_id
< right
.resource_id
;
26 if (left
.md5_checksum
!= right
.md5_checksum
)
27 return left
.md5_checksum
< right
.md5_checksum
;
28 if (left
.deleted
!= right
.deleted
)
29 return left
.deleted
< right
.deleted
;
30 return left
.changestamp
< right
.changestamp
;
33 struct FakeDriveFileSyncClient::ChangeStampComparator
{
34 bool operator()(const google_apis::ResourceEntry
* left
,
35 const google_apis::ResourceEntry
* right
) {
36 return left
->changestamp() < right
->changestamp();
40 FakeDriveFileSyncClient::RemoteResource::RemoteResource()
45 FakeDriveFileSyncClient::RemoteResource::RemoteResource(
46 const std::string
& parent_resource_id
,
47 const std::string
& parent_title
,
48 const std::string
& title
,
49 const std::string
& resource_id
,
50 const std::string
& md5_checksum
,
53 : parent_resource_id(parent_resource_id
),
54 parent_title(parent_title
),
56 resource_id(resource_id
),
57 md5_checksum(md5_checksum
),
59 changestamp(changestamp
) {
62 FakeDriveFileSyncClient::RemoteResource::~RemoteResource() {
65 FakeDriveFileSyncClient::FakeDriveFileSyncClient()
66 : largest_changestamp_(0),
68 google_apis::GDataWapiUrlGenerator::kBaseUrlForProduction
)) {
71 FakeDriveFileSyncClient::~FakeDriveFileSyncClient() {
74 void FakeDriveFileSyncClient::AddObserver(
75 DriveFileSyncClientObserver
* observer
) {
78 void FakeDriveFileSyncClient::RemoveObserver(
79 DriveFileSyncClientObserver
* observer
) {
82 void FakeDriveFileSyncClient::GetDriveDirectoryForSyncRoot(
83 const ResourceIdCallback
& callback
) {
84 base::MessageLoopProxy::current()->PostTask(
86 base::Bind(callback
, google_apis::HTTP_SUCCESS
,
87 "folder: sync_root_resource_id"));
90 void FakeDriveFileSyncClient::GetDriveDirectoryForOrigin(
91 const std::string
& sync_root_resource_id
,
93 const ResourceIdCallback
& callback
) {
94 base::MessageLoopProxy::current()->PostTask(
96 base::Bind(callback
, google_apis::HTTP_SUCCESS
,
97 "folder resource_id for " + origin
.host()));
100 void FakeDriveFileSyncClient::GetLargestChangeStamp(
101 const ChangeStampCallback
& callback
) {
102 base::MessageLoopProxy::current()->PostTask(
104 base::Bind(callback
, google_apis::HTTP_SUCCESS
, largest_changestamp_
));
107 void FakeDriveFileSyncClient::GetResourceEntry(
108 const std::string
& resource_id
,
109 const ResourceEntryCallback
& callback
) {
113 void FakeDriveFileSyncClient::ListFiles(
114 const std::string
& directory_resource_id
,
115 const ResourceListCallback
& callback
) {
116 ListChanges(0, callback
);
119 void FakeDriveFileSyncClient::ListChanges(
120 int64 start_changestamp
,
121 const ResourceListCallback
& callback
) {
122 scoped_ptr
<google_apis::ResourceList
> change_feed(
123 new google_apis::ResourceList());
125 ScopedVector
<google_apis::ResourceEntry
> entries
;
126 typedef RemoteResourceByResourceId::const_iterator iterator
;
127 for (iterator itr
= remote_resources_
.begin();
128 itr
!= remote_resources_
.end(); ++itr
) {
129 if (itr
->second
.changestamp
< start_changestamp
)
131 scoped_ptr
<google_apis::ResourceEntry
> entry(
132 CreateResourceEntry(itr
->second
));
133 entries
.push_back(entry
.release());
136 std::sort(entries
.begin(), entries
.end(),
137 ChangeStampComparator());
139 change_feed
->set_entries(&entries
);
140 change_feed
->set_largest_changestamp(largest_changestamp_
);
142 base::MessageLoopProxy::current()->PostTask(
144 base::Bind(callback
, google_apis::HTTP_SUCCESS
,
145 base::Passed(&change_feed
)));
148 void FakeDriveFileSyncClient::ContinueListing(
149 const GURL
& feed_url
,
150 const ResourceListCallback
& callback
) {
154 void FakeDriveFileSyncClient::DownloadFile(
155 const std::string
& resource_id
,
156 const std::string
& local_file_md5
,
157 const base::FilePath
& local_file_path
,
158 const DownloadFileCallback
& callback
) {
159 RemoteResourceByResourceId::iterator found
=
160 remote_resources_
.find(resource_id
);
161 std::string file_md5
;
162 google_apis::GDataErrorCode error
= google_apis::HTTP_NOT_FOUND
;
164 if (found
!= remote_resources_
.end() && !found
->second
.deleted
) {
165 scoped_ptr
<google_apis::ResourceEntry
> entry(
166 CreateResourceEntry(found
->second
));
167 file_md5
= entry
->file_md5();
168 error
= google_apis::HTTP_SUCCESS
;
171 base::MessageLoopProxy::current()->PostTask(
173 base::Bind(callback
, error
, file_md5
));
176 void FakeDriveFileSyncClient::UploadNewFile(
177 const std::string
& directory_resource_id
,
178 const base::FilePath
& local_file_path
,
179 const std::string
& title
,
180 const UploadFileCallback
& callback
) {
184 void FakeDriveFileSyncClient::UploadExistingFile(
185 const std::string
& resource_id
,
186 const std::string
& remote_file_md5
,
187 const base::FilePath
& local_file_path
,
188 const UploadFileCallback
& callback
) {
192 bool FakeDriveFileSyncClient::IsAuthenticated() const {
196 void FakeDriveFileSyncClient::DeleteFile(
197 const std::string
& resource_id
,
198 const std::string
& remote_file_md5
,
199 const GDataErrorCallback
& callback
) {
200 google_apis::GDataErrorCode error
= google_apis::HTTP_NOT_FOUND
;
201 DCHECK(ContainsKey(remote_resources_
, resource_id
));
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
,
211 error
= google_apis::HTTP_SUCCESS
;
212 base::MessageLoopProxy::current()->PostTask(
214 base::Bind(callback
, error
));
217 GURL
FakeDriveFileSyncClient::ResourceIdToResourceLink(
218 const std::string
& resource_id
) const {
219 return url_generator_
.GenerateContentUrl(resource_id
);
222 void FakeDriveFileSyncClient::EnsureSyncRootIsNotInMyDrive(
223 const std::string
& sync_root_resource_id
) const {
227 void FakeDriveFileSyncClient::PushRemoteChange(
228 const std::string
& parent_resource_id
,
229 const std::string
& parent_title
,
230 const std::string
& title
,
231 const std::string
& resource_id
,
232 const std::string
& md5
,
234 remote_resources_
[resource_id
] = RemoteResource(
235 parent_resource_id
, parent_title
, title
, resource_id
,
236 md5
, deleted
, ++largest_changestamp_
);
239 scoped_ptr
<google_apis::ResourceEntry
>
240 FakeDriveFileSyncClient::CreateResourceEntry(
241 const RemoteResource
& resource
) const {
242 scoped_ptr
<google_apis::ResourceEntry
> entry(
243 new google_apis::ResourceEntry());
244 ScopedVector
<google_apis::Link
> parent_links
;
245 scoped_ptr
<google_apis::Link
> link(new google_apis::Link());
247 link
->set_type(google_apis::Link::LINK_PARENT
);
248 link
->set_href(url_generator_
.GenerateContentUrl(
249 resource
.parent_resource_id
));
250 link
->set_title(resource
.parent_title
);
251 parent_links
.push_back(link
.release());
253 entry
->set_links(&parent_links
);
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
);
263 } // namespace sync_file_system