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

Side by Side Diff: chrome/browser/google_apis/fake_drive_service.cc

Issue 11827045: google_apis: Add set_offline() to FakeDriveService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 7 years, 11 months 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/google_apis/fake_drive_service.h" 5 #include "chrome/browser/google_apis/fake_drive_service.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/message_loop.h" 9 #include "base/message_loop.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "chrome/browser/google_apis/gdata_wapi_parser.h" 11 #include "chrome/browser/google_apis/gdata_wapi_parser.h"
12 #include "chrome/browser/google_apis/test_util.h" 12 #include "chrome/browser/google_apis/test_util.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 14
15 using content::BrowserThread; 15 using content::BrowserThread;
16 16
17 namespace google_apis { 17 namespace google_apis {
18 18
19 FakeDriveService::FakeDriveService() 19 FakeDriveService::FakeDriveService()
20 : resource_id_count_(0) { 20 : resource_id_count_(0),
21 offline_(false) {
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
22 } 23 }
23 24
24 FakeDriveService::~FakeDriveService() { 25 FakeDriveService::~FakeDriveService() {
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
26 } 27 }
27 28
28 bool FakeDriveService::LoadResourceListForWapi( 29 bool FakeDriveService::LoadResourceListForWapi(
29 const std::string& relative_path) { 30 const std::string& relative_path) {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 31 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 void FakeDriveService::GetResourceList( 102 void FakeDriveService::GetResourceList(
102 const GURL& feed_url, 103 const GURL& feed_url,
103 int64 start_changestamp, 104 int64 start_changestamp,
104 const std::string& search_query, 105 const std::string& search_query,
105 bool shared_with_me, 106 bool shared_with_me,
106 const std::string& directory_resource_id, 107 const std::string& directory_resource_id,
107 const GetResourceListCallback& callback) { 108 const GetResourceListCallback& callback) {
108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
109 DCHECK(!callback.is_null()); 110 DCHECK(!callback.is_null());
110 111
112 if (offline_) {
113 scoped_ptr<ResourceList> null;
114 MessageLoop::current()->PostTask(
115 FROM_HERE,
116 base::Bind(callback,
117 GDATA_NO_CONNECTION,
118 base::Passed(&null)));
119 return;
120 }
121
111 scoped_ptr<ResourceList> resource_list = 122 scoped_ptr<ResourceList> resource_list =
112 ResourceList::CreateFrom(*resource_list_value_); 123 ResourceList::CreateFrom(*resource_list_value_);
113 MessageLoop::current()->PostTask( 124 MessageLoop::current()->PostTask(
114 FROM_HERE, 125 FROM_HERE,
115 base::Bind(callback, 126 base::Bind(callback,
116 HTTP_SUCCESS, 127 HTTP_SUCCESS,
117 base::Passed(&resource_list))); 128 base::Passed(&resource_list)));
118 } 129 }
119 130
120 void FakeDriveService::GetResourceEntry( 131 void FakeDriveService::GetResourceEntry(
121 const std::string& resource_id, 132 const std::string& resource_id,
122 const GetResourceEntryCallback& callback) { 133 const GetResourceEntryCallback& callback) {
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
124 DCHECK(!callback.is_null()); 135 DCHECK(!callback.is_null());
125 136
137 if (offline_) {
138 scoped_ptr<ResourceEntry> null;
139 MessageLoop::current()->PostTask(
140 FROM_HERE,
141 base::Bind(callback,
142 GDATA_NO_CONNECTION,
143 base::Passed(&null)));
144 return;
145 }
146
126 base::DictionaryValue* entry = FindEntryByResourceId(resource_id); 147 base::DictionaryValue* entry = FindEntryByResourceId(resource_id);
127 if (entry) { 148 if (entry) {
128 scoped_ptr<ResourceEntry> resource_entry = 149 scoped_ptr<ResourceEntry> resource_entry =
129 ResourceEntry::CreateFrom(*entry); 150 ResourceEntry::CreateFrom(*entry);
130 MessageLoop::current()->PostTask( 151 MessageLoop::current()->PostTask(
131 FROM_HERE, 152 FROM_HERE,
132 base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_entry))); 153 base::Bind(callback, HTTP_SUCCESS, base::Passed(&resource_entry)));
133 return; 154 return;
134 } 155 }
135 156
136 scoped_ptr<ResourceEntry> null; 157 scoped_ptr<ResourceEntry> null;
137 MessageLoop::current()->PostTask( 158 MessageLoop::current()->PostTask(
138 FROM_HERE, 159 FROM_HERE,
139 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null))); 160 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
140 } 161 }
141 162
142 void FakeDriveService::GetAccountMetadata( 163 void FakeDriveService::GetAccountMetadata(
143 const GetAccountMetadataCallback& callback) { 164 const GetAccountMetadataCallback& callback) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 DCHECK(!callback.is_null()); 166 DCHECK(!callback.is_null());
146 167
168 if (offline_) {
169 scoped_ptr<AccountMetadataFeed> null;
170 MessageLoop::current()->PostTask(
171 FROM_HERE,
172 base::Bind(callback,
173 GDATA_NO_CONNECTION,
174 base::Passed(&null)));
175 return;
176 }
147 scoped_ptr<AccountMetadataFeed> account_metadata = 177 scoped_ptr<AccountMetadataFeed> account_metadata =
148 AccountMetadataFeed::CreateFrom(*account_metadata_value_); 178 AccountMetadataFeed::CreateFrom(*account_metadata_value_);
149 MessageLoop::current()->PostTask( 179 MessageLoop::current()->PostTask(
150 FROM_HERE, 180 FROM_HERE,
151 base::Bind(callback, 181 base::Bind(callback,
152 HTTP_SUCCESS, 182 HTTP_SUCCESS,
153 base::Passed(&account_metadata))); 183 base::Passed(&account_metadata)));
154 } 184 }
155 185
156 void FakeDriveService::GetApplicationInfo( 186 void FakeDriveService::GetApplicationInfo(
157 const GetDataCallback& callback) { 187 const GetDataCallback& callback) {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
159 DCHECK(!callback.is_null()); 189 DCHECK(!callback.is_null());
160 190
191 if (offline_) {
192 scoped_ptr<base::Value> null;
193 MessageLoop::current()->PostTask(
194 FROM_HERE,
195 base::Bind(callback,
196 GDATA_NO_CONNECTION,
197 base::Passed(&null)));
198 return;
199 }
200
161 scoped_ptr<base::Value> copied_app_info_value( 201 scoped_ptr<base::Value> copied_app_info_value(
162 app_info_value_->DeepCopy()); 202 app_info_value_->DeepCopy());
163 MessageLoop::current()->PostTask( 203 MessageLoop::current()->PostTask(
164 FROM_HERE, 204 FROM_HERE,
165 base::Bind(callback, 205 base::Bind(callback,
166 HTTP_SUCCESS, 206 HTTP_SUCCESS,
167 base::Passed(&copied_app_info_value))); 207 base::Passed(&copied_app_info_value)));
168 208
169 } 209 }
170 210
171 void FakeDriveService::DeleteResource( 211 void FakeDriveService::DeleteResource(
172 const GURL& edit_url, 212 const GURL& edit_url,
173 const EntryActionCallback& callback) { 213 const EntryActionCallback& callback) {
174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
175 DCHECK(!callback.is_null()); 215 DCHECK(!callback.is_null());
176 216
217 if (offline_) {
218 MessageLoop::current()->PostTask(
219 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
220 return;
221 }
222
177 base::DictionaryValue* resource_list_dict = NULL; 223 base::DictionaryValue* resource_list_dict = NULL;
178 base::ListValue* entries = NULL; 224 base::ListValue* entries = NULL;
179 // Go through entries and remove the one that matches |edit_url|. 225 // Go through entries and remove the one that matches |edit_url|.
180 if (resource_list_value_->GetAsDictionary(&resource_list_dict) && 226 if (resource_list_value_->GetAsDictionary(&resource_list_dict) &&
181 resource_list_dict->GetList("entry", &entries)) { 227 resource_list_dict->GetList("entry", &entries)) {
182 for (size_t i = 0; i < entries->GetSize(); ++i) { 228 for (size_t i = 0; i < entries->GetSize(); ++i) {
183 base::DictionaryValue* entry = NULL; 229 base::DictionaryValue* entry = NULL;
184 base::ListValue* links = NULL; 230 base::ListValue* links = NULL;
185 if (entries->GetDictionary(i, &entry) && 231 if (entries->GetDictionary(i, &entry) &&
186 entry->GetList("link", &links)) { 232 entry->GetList("link", &links)) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 265
220 void FakeDriveService::DownloadFile( 266 void FakeDriveService::DownloadFile(
221 const FilePath& virtual_path, 267 const FilePath& virtual_path,
222 const FilePath& local_cache_path, 268 const FilePath& local_cache_path,
223 const GURL& content_url, 269 const GURL& content_url,
224 const DownloadActionCallback& download_action_callback, 270 const DownloadActionCallback& download_action_callback,
225 const GetContentCallback& get_content_callback) { 271 const GetContentCallback& get_content_callback) {
226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
227 DCHECK(!download_action_callback.is_null()); 273 DCHECK(!download_action_callback.is_null());
228 274
275 if (offline_) {
276 MessageLoop::current()->PostTask(
277 FROM_HERE,
278 base::Bind(download_action_callback,
279 GDATA_NO_CONNECTION,
280 FilePath()));
281 return;
282 }
283
229 base::DictionaryValue* entry = FindEntryByContentUrl(content_url); 284 base::DictionaryValue* entry = FindEntryByContentUrl(content_url);
230 if (!entry) { 285 if (!entry) {
231 base::MessageLoopProxy::current()->PostTask( 286 base::MessageLoopProxy::current()->PostTask(
232 FROM_HERE, 287 FROM_HERE,
233 base::Bind(download_action_callback, HTTP_NOT_FOUND, FilePath())); 288 base::Bind(download_action_callback, HTTP_NOT_FOUND, FilePath()));
234 return; 289 return;
235 } 290 }
236 291
237 // Write the content URL as the content of the file. 292 // Write the content URL as the content of the file.
238 if (static_cast<int>(content_url.spec().size()) != 293 if (static_cast<int>(content_url.spec().size()) !=
(...skipping 11 matching lines...) Expand all
250 base::Bind(download_action_callback, HTTP_SUCCESS, local_cache_path)); 305 base::Bind(download_action_callback, HTTP_SUCCESS, local_cache_path));
251 } 306 }
252 307
253 void FakeDriveService::CopyHostedDocument( 308 void FakeDriveService::CopyHostedDocument(
254 const std::string& resource_id, 309 const std::string& resource_id,
255 const FilePath::StringType& new_name, 310 const FilePath::StringType& new_name,
256 const GetResourceEntryCallback& callback) { 311 const GetResourceEntryCallback& callback) {
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
258 DCHECK(!callback.is_null()); 313 DCHECK(!callback.is_null());
259 314
315 if (offline_) {
316 scoped_ptr<ResourceEntry> null;
317 MessageLoop::current()->PostTask(
318 FROM_HERE,
319 base::Bind(callback,
320 GDATA_NO_CONNECTION,
321 base::Passed(&null)));
322 return;
323 }
324
260 base::DictionaryValue* resource_list_dict = NULL; 325 base::DictionaryValue* resource_list_dict = NULL;
261 base::ListValue* entries = NULL; 326 base::ListValue* entries = NULL;
262 // Go through entries and copy the one that matches |resource_id|. 327 // Go through entries and copy the one that matches |resource_id|.
263 if (resource_list_value_->GetAsDictionary(&resource_list_dict) && 328 if (resource_list_value_->GetAsDictionary(&resource_list_dict) &&
264 resource_list_dict->GetList("entry", &entries)) { 329 resource_list_dict->GetList("entry", &entries)) {
265 for (size_t i = 0; i < entries->GetSize(); ++i) { 330 for (size_t i = 0; i < entries->GetSize(); ++i) {
266 base::DictionaryValue* entry = NULL; 331 base::DictionaryValue* entry = NULL;
267 base::DictionaryValue* resource_id_dict = NULL; 332 base::DictionaryValue* resource_id_dict = NULL;
268 base::ListValue* categories = NULL; 333 base::ListValue* categories = NULL;
269 std::string current_resource_id; 334 std::string current_resource_id;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null))); 378 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
314 } 379 }
315 380
316 void FakeDriveService::RenameResource( 381 void FakeDriveService::RenameResource(
317 const GURL& edit_url, 382 const GURL& edit_url,
318 const FilePath::StringType& new_name, 383 const FilePath::StringType& new_name,
319 const EntryActionCallback& callback) { 384 const EntryActionCallback& callback) {
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
321 DCHECK(!callback.is_null()); 386 DCHECK(!callback.is_null());
322 387
388 if (offline_) {
389 MessageLoop::current()->PostTask(
390 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
391 return;
392 }
393
323 base::DictionaryValue* entry = FindEntryByEditUrl(edit_url); 394 base::DictionaryValue* entry = FindEntryByEditUrl(edit_url);
324 if (entry) { 395 if (entry) {
325 entry->SetString("title.$t", 396 entry->SetString("title.$t",
326 FilePath(new_name).AsUTF8Unsafe()); 397 FilePath(new_name).AsUTF8Unsafe());
327 MessageLoop::current()->PostTask( 398 MessageLoop::current()->PostTask(
328 FROM_HERE, base::Bind(callback, HTTP_SUCCESS)); 399 FROM_HERE, base::Bind(callback, HTTP_SUCCESS));
329 return; 400 return;
330 } 401 }
331 402
332 MessageLoop::current()->PostTask( 403 MessageLoop::current()->PostTask(
333 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 404 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
334 } 405 }
335 406
336 void FakeDriveService::AddResourceToDirectory( 407 void FakeDriveService::AddResourceToDirectory(
337 const GURL& parent_content_url, 408 const GURL& parent_content_url,
338 const GURL& edit_url, 409 const GURL& edit_url,
339 const EntryActionCallback& callback) { 410 const EntryActionCallback& callback) {
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 411 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
341 DCHECK(!callback.is_null()); 412 DCHECK(!callback.is_null());
342 413
414 if (offline_) {
415 MessageLoop::current()->PostTask(
416 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
417 return;
418 }
419
343 base::DictionaryValue* entry = FindEntryByEditUrl(edit_url); 420 base::DictionaryValue* entry = FindEntryByEditUrl(edit_url);
344 if (entry) { 421 if (entry) {
345 base::ListValue* links = NULL; 422 base::ListValue* links = NULL;
346 if (entry->GetList("link", &links)) { 423 if (entry->GetList("link", &links)) {
347 bool parent_link_found = false; 424 bool parent_link_found = false;
348 for (size_t i = 0; i < links->GetSize(); ++i) { 425 for (size_t i = 0; i < links->GetSize(); ++i) {
349 base::DictionaryValue* link = NULL; 426 base::DictionaryValue* link = NULL;
350 std::string rel; 427 std::string rel;
351 if (links->GetDictionary(i, &link) && 428 if (links->GetDictionary(i, &link) &&
352 link->GetString("rel", &rel) && 429 link->GetString("rel", &rel) &&
(...skipping 21 matching lines...) Expand all
374 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 451 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
375 } 452 }
376 453
377 void FakeDriveService::RemoveResourceFromDirectory( 454 void FakeDriveService::RemoveResourceFromDirectory(
378 const GURL& parent_content_url, 455 const GURL& parent_content_url,
379 const std::string& resource_id, 456 const std::string& resource_id,
380 const EntryActionCallback& callback) { 457 const EntryActionCallback& callback) {
381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 458 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
382 DCHECK(!callback.is_null()); 459 DCHECK(!callback.is_null());
383 460
461 if (offline_) {
462 MessageLoop::current()->PostTask(
463 FROM_HERE, base::Bind(callback, GDATA_NO_CONNECTION));
464 return;
465 }
466
384 base::DictionaryValue* entry = FindEntryByResourceId(resource_id); 467 base::DictionaryValue* entry = FindEntryByResourceId(resource_id);
385 if (entry) { 468 if (entry) {
386 base::ListValue* links = NULL; 469 base::ListValue* links = NULL;
387 if (entry->GetList("link", &links)) { 470 if (entry->GetList("link", &links)) {
388 for (size_t i = 0; i < links->GetSize(); ++i) { 471 for (size_t i = 0; i < links->GetSize(); ++i) {
389 base::DictionaryValue* link = NULL; 472 base::DictionaryValue* link = NULL;
390 std::string rel; 473 std::string rel;
391 std::string href; 474 std::string href;
392 if (links->GetDictionary(i, &link) && 475 if (links->GetDictionary(i, &link) &&
393 link->GetString("rel", &rel) && 476 link->GetString("rel", &rel) &&
(...skipping 13 matching lines...) Expand all
407 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND)); 490 FROM_HERE, base::Bind(callback, HTTP_NOT_FOUND));
408 } 491 }
409 492
410 void FakeDriveService::AddNewDirectory( 493 void FakeDriveService::AddNewDirectory(
411 const GURL& parent_content_url, 494 const GURL& parent_content_url,
412 const FilePath::StringType& directory_name, 495 const FilePath::StringType& directory_name,
413 const GetResourceEntryCallback& callback) { 496 const GetResourceEntryCallback& callback) {
414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 497 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
415 DCHECK(!callback.is_null()); 498 DCHECK(!callback.is_null());
416 499
500 if (offline_) {
501 scoped_ptr<ResourceEntry> null;
502 MessageLoop::current()->PostTask(
503 FROM_HERE,
504 base::Bind(callback,
505 GDATA_NO_CONNECTION,
506 base::Passed(&null)));
507 return;
508 }
509
417 // If the parent content URL is not empty, the parent should exist. 510 // If the parent content URL is not empty, the parent should exist.
418 if (!parent_content_url.is_empty()) { 511 if (!parent_content_url.is_empty()) {
419 base::DictionaryValue* parent_entry = 512 base::DictionaryValue* parent_entry =
420 FindEntryByContentUrl(parent_content_url); 513 FindEntryByContentUrl(parent_content_url);
421 if (!parent_entry) { 514 if (!parent_entry) {
422 scoped_ptr<ResourceEntry> null; 515 scoped_ptr<ResourceEntry> null;
423 MessageLoop::current()->PostTask( 516 MessageLoop::current()->PostTask(
424 FROM_HERE, 517 FROM_HERE,
425 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null))); 518 base::Bind(callback, HTTP_NOT_FOUND, base::Passed(&null)));
426 return; 519 return;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 content_url == GURL(current_content_url)) { 676 content_url == GURL(current_content_url)) {
584 return entry; 677 return entry;
585 } 678 }
586 } 679 }
587 } 680 }
588 681
589 return NULL; 682 return NULL;
590 } 683 }
591 684
592 std::string FakeDriveService::GetNewResourceId() { 685 std::string FakeDriveService::GetNewResourceId() {
686 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
687
593 ++resource_id_count_; 688 ++resource_id_count_;
594 return base::StringPrintf("resource_id_%d", resource_id_count_); 689 return base::StringPrintf("resource_id_%d", resource_id_count_);
595 } 690 }
596 691
597 } // namespace google_apis 692 } // namespace google_apis
OLDNEW
« no previous file with comments | « chrome/browser/google_apis/fake_drive_service.h ('k') | chrome/browser/google_apis/fake_drive_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698