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

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.cc

Issue 10823226: Get AboutResource as account metadata for Drive V2. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 4 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/chromeos/gdata/gdata_wapi_feed_loader.h" 5 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_loader.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/json/json_writer.h" 10 #include "base/json/json_writer.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/metrics/histogram.h" 12 #include "base/metrics/histogram.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/threading/sequenced_worker_pool.h" 14 #include "base/threading/sequenced_worker_pool.h"
15 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h" 15 #include "chrome/browser/chromeos/gdata/drive_webapps_registry.h"
16 #include "chrome/browser/chromeos/gdata/drive_api_parser.h"
16 #include "chrome/browser/chromeos/gdata/gdata_cache.h" 17 #include "chrome/browser/chromeos/gdata/gdata_cache.h"
17 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" 18 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h"
18 #include "chrome/browser/chromeos/gdata/gdata_util.h" 19 #include "chrome/browser/chromeos/gdata/gdata_util.h"
19 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_processor.h" 20 #include "chrome/browser/chromeos/gdata/gdata_wapi_feed_processor.h"
20 #include "chrome/common/chrome_switches.h" 21 #include "chrome/common/chrome_switches.h"
21 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
22 23
23 using content::BrowserThread; 24 using content::BrowserThread;
24 25
25 namespace gdata { 26 namespace gdata {
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 130 }
130 131
131 bool UseLevelDB() { 132 bool UseLevelDB() {
132 return CommandLine::ForCurrentProcess()->HasSwitch( 133 return CommandLine::ForCurrentProcess()->HasSwitch(
133 switches::kUseLevelDBForGData); 134 switches::kUseLevelDBForGData);
134 } 135 }
135 136
136 } // namespace 137 } // namespace
137 138
138 GetDocumentsParams::GetDocumentsParams( 139 GetDocumentsParams::GetDocumentsParams(
139 int start_changestamp, 140 int64 start_changestamp,
140 int root_feed_changestamp, 141 int64 root_feed_changestamp,
141 std::vector<DocumentFeed*>* feed_list, 142 std::vector<DocumentFeed*>* feed_list,
142 bool should_fetch_multiple_feeds, 143 bool should_fetch_multiple_feeds,
143 const FilePath& search_file_path, 144 const FilePath& search_file_path,
144 const std::string& search_query, 145 const std::string& search_query,
145 const std::string& directory_resource_id, 146 const std::string& directory_resource_id,
146 const FindEntryCallback& callback, 147 const FindEntryCallback& callback,
147 GetDocumentsUiState* ui_state) 148 GetDocumentsUiState* ui_state)
148 : start_changestamp(start_changestamp), 149 : start_changestamp(start_changestamp),
149 root_feed_changestamp(root_feed_changestamp), 150 root_feed_changestamp(root_feed_changestamp),
150 feed_list(feed_list), 151 feed_list(feed_list),
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 observers_.AddObserver(observer); 213 observers_.AddObserver(observer);
213 } 214 }
214 215
215 void GDataWapiFeedLoader::RemoveObserver(Observer* observer) { 216 void GDataWapiFeedLoader::RemoveObserver(Observer* observer) {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
217 observers_.RemoveObserver(observer); 218 observers_.RemoveObserver(observer);
218 } 219 }
219 220
220 void GDataWapiFeedLoader::ReloadFromServerIfNeeded( 221 void GDataWapiFeedLoader::ReloadFromServerIfNeeded(
221 ContentOrigin initial_origin, 222 ContentOrigin initial_origin,
222 int local_changestamp, 223 int64 local_changestamp,
223 const FilePath& search_file_path, 224 const FilePath& search_file_path,
224 const FindEntryCallback& callback) { 225 const FindEntryCallback& callback) {
225 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
226 227
227 DVLOG(1) << "ReloadFeedFromServerIfNeeded local_changestamp=" 228 DVLOG(1) << "ReloadFeedFromServerIfNeeded local_changestamp="
228 << local_changestamp << ", initial_origin=" << initial_origin; 229 << local_changestamp << ", initial_origin=" << initial_origin;
229 230
230 // First fetch the latest changestamp to see if there were any new changes 231 // First fetch the latest changestamp to see if there were any new changes
231 // there at all. 232 // there at all.
233 if (gdata::util::IsDriveV2ApiEnabled()) {
satorux1 2012/08/08 13:18:51 I named the file "gdata_wapi_feed_loader.cc", assu
kochi 2012/08/08 16:53:27 I could have a separate file but it turned out tha
234 documents_service_->GetAboutResource(
235 base::Bind(&GDataWapiFeedLoader::OnGetAboutResource,
236 weak_ptr_factory_.GetWeakPtr(),
237 initial_origin,
238 local_changestamp,
239 search_file_path,
240 callback));
241 return;
242 }
243
232 documents_service_->GetAccountMetadata( 244 documents_service_->GetAccountMetadata(
233 base::Bind(&GDataWapiFeedLoader::OnGetAccountMetadata, 245 base::Bind(&GDataWapiFeedLoader::OnGetAccountMetadata,
234 weak_ptr_factory_.GetWeakPtr(), 246 weak_ptr_factory_.GetWeakPtr(),
235 initial_origin, 247 initial_origin,
236 local_changestamp, 248 local_changestamp,
237 search_file_path, 249 search_file_path,
238 callback)); 250 callback));
239 } 251 }
240 252
241 void GDataWapiFeedLoader::OnGetAccountMetadata( 253 void GDataWapiFeedLoader::OnGetAccountMetadata(
242 ContentOrigin initial_origin, 254 ContentOrigin initial_origin,
243 int local_changestamp, 255 int64 local_changestamp,
244 const FilePath& search_file_path, 256 const FilePath& search_file_path,
245 const FindEntryCallback& callback, 257 const FindEntryCallback& callback,
246 GDataErrorCode status, 258 GDataErrorCode status,
247 scoped_ptr<base::Value> feed_data) { 259 scoped_ptr<base::Value> feed_data) {
248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
249 261
250 GDataFileError error = util::GDataToGDataFileError(status); 262 GDataFileError error = util::GDataToGDataFileError(status);
251 if (error != GDATA_FILE_OK) { 263 if (error != GDATA_FILE_OK) {
252 // Get changes starting from the next changestamp from what we have locally. 264 // Get changes starting from the next changestamp from what we have locally.
253 LoadFromServer(initial_origin, 265 LoadFromServer(initial_origin,
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 true, /* should_fetch_multiple_feeds */ 338 true, /* should_fetch_multiple_feeds */
327 search_file_path, 339 search_file_path,
328 std::string() /* no search query */, 340 std::string() /* no search query */,
329 GURL(), /* feed not explicitly set */ 341 GURL(), /* feed not explicitly set */
330 std::string() /* no directory resource ID */, 342 std::string() /* no directory resource ID */,
331 callback, 343 callback,
332 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded, 344 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
333 weak_ptr_factory_.GetWeakPtr())); 345 weak_ptr_factory_.GetWeakPtr()));
334 } 346 }
335 347
348 void GDataWapiFeedLoader::OnGetAboutResource(
349 ContentOrigin initial_origin,
350 int64 local_changestamp,
351 const FilePath& search_file_path,
352 const FindEntryCallback& callback,
353 GDataErrorCode status,
354 scoped_ptr<base::Value> feed_data) {
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
356
357 GDataFileError error = util::GDataToGDataFileError(status);
358 if (error != GDATA_FILE_OK) {
359 // Get changes starting from the next changestamp from what we have locally.
360 LoadFromServer(initial_origin,
satorux1 2012/08/08 13:18:51 LoadFromServer has too many parameters. Could you
kochi 2012/08/08 16:53:27 Will work on a separate CL.
361 local_changestamp + 1, 0,
362 true, /* should_fetch_multiple_feeds */
363 search_file_path,
364 std::string() /* no search query */,
365 GURL(), /* feed not explicitly set */
366 std::string() /* no directory resource ID */,
367 callback,
368 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
369 weak_ptr_factory_.GetWeakPtr()));
370 return;
371 }
372
373 scoped_ptr<AboutResource> about_resource;
374 if (feed_data.get())
375 about_resource = AboutResource::CreateFrom(*feed_data);
376
377 if (!about_resource.get()) {
378 LoadFromServer(initial_origin,
379 local_changestamp + 1, 0,
380 true, /* should_fetch_multiple_feeds */
381 search_file_path,
382 std::string() /* no search query */,
383 GURL(), /* feed not explicitly set */
384 std::string() /* no directory resource ID */,
385 callback,
386 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
387 weak_ptr_factory_.GetWeakPtr()));
388 return;
389 }
390
391 bool changes_detected = true;
392 int64 largest_changestamp = about_resource->largest_change_id();
393 directory_service_->SetRootResourceId(about_resource->root_folder_id());
394
395 if (local_changestamp >= largest_changestamp) {
396 if (local_changestamp > largest_changestamp) {
397 LOG(WARNING) << "Cached client feed is fresher than server, client = "
398 << local_changestamp
399 << ", server = "
400 << largest_changestamp;
401 }
402 // If our cache holds the latest state from the server, change the
403 // state to FROM_SERVER.
404 directory_service_->set_origin(
405 initial_origin == FROM_CACHE ? FROM_SERVER : initial_origin);
406 changes_detected = false;
407 }
408
409 // No changes detected, continue with search as planned.
410 if (!changes_detected) {
411 if (!callback.is_null()) {
412 directory_service_->FindEntryByPathAndRunSync(search_file_path,
413 callback);
414 }
415 return;
416 }
417
418 // Load changes from the server.
419 LoadFromServer(initial_origin,
420 local_changestamp > 0 ? local_changestamp + 1 : 0,
421 largest_changestamp,
422 true, /* should_fetch_multiple_feeds */
423 search_file_path,
424 std::string() /* no search query */,
425 GURL(), /* feed not explicitly set */
426 std::string() /* no directory resource ID */,
427 callback,
428 base::Bind(&GDataWapiFeedLoader::OnFeedFromServerLoaded,
429 weak_ptr_factory_.GetWeakPtr()));
430 }
431
336 void GDataWapiFeedLoader::LoadFromServer( 432 void GDataWapiFeedLoader::LoadFromServer(
337 ContentOrigin initial_origin, 433 ContentOrigin initial_origin,
338 int start_changestamp, 434 int64 start_changestamp,
339 int root_feed_changestamp, 435 int64 root_feed_changestamp,
340 bool should_fetch_multiple_feeds, 436 bool should_fetch_multiple_feeds,
341 const FilePath& search_file_path, 437 const FilePath& search_file_path,
342 const std::string& search_query, 438 const std::string& search_query,
343 const GURL& feed_to_load, 439 const GURL& feed_to_load,
344 const std::string& directory_resource_id, 440 const std::string& directory_resource_id,
345 const FindEntryCallback& entry_found_callback, 441 const FindEntryCallback& entry_found_callback,
346 const LoadDocumentFeedCallback& feed_load_callback) { 442 const LoadDocumentFeedCallback& feed_load_callback) {
347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 443 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
348 444
349 // |feed_list| will contain the list of all collected feed updates that 445 // |feed_list| will contain the list of all collected feed updates that
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 callback.Run(params, GDATA_FILE_ERROR_FAILED); 538 callback.Run(params, GDATA_FILE_ERROR_FAILED);
443 } 539 }
444 540
445 return; 541 return;
446 } 542 }
447 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url); 543 const bool has_next_feed_url = current_feed->GetNextFeedURL(&next_feed_url);
448 544
449 #ifndef NDEBUG 545 #ifndef NDEBUG
450 // Save initial root feed for analysis. 546 // Save initial root feed for analysis.
451 std::string file_name = 547 std::string file_name =
452 base::StringPrintf("DEBUG_feed_%d.json", 548 base::StringPrintf("DEBUG_feed_%ld.json",
453 params->start_changestamp); 549 params->start_changestamp);
454 util::PostBlockingPoolSequencedTask( 550 util::PostBlockingPoolSequencedTask(
455 FROM_HERE, 551 FROM_HERE,
456 blocking_task_runner_, 552 blocking_task_runner_,
457 base::Bind(&SaveFeedOnBlockingPoolForDebugging, 553 base::Bind(&SaveFeedOnBlockingPoolForDebugging,
458 cache_->GetCacheDirectoryPath( 554 cache_->GetCacheDirectoryPath(
459 GDataCache::CACHE_TYPE_META).Append(file_name), 555 GDataCache::CACHE_TYPE_META).Append(file_name),
460 base::Passed(&data))); 556 base::Passed(&data)));
461 #endif 557 #endif
462 558
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 util::PostBlockingPoolSequencedTask( 775 util::PostBlockingPoolSequencedTask(
680 FROM_HERE, 776 FROM_HERE,
681 blocking_task_runner_, 777 blocking_task_runner_,
682 base::Bind(&SaveProtoOnBlockingPool, path, 778 base::Bind(&SaveProtoOnBlockingPool, path,
683 base::Passed(serialized_proto.Pass()))); 779 base::Passed(serialized_proto.Pass())));
684 } 780 }
685 } 781 }
686 782
687 GDataFileError GDataWapiFeedLoader::UpdateFromFeed( 783 GDataFileError GDataWapiFeedLoader::UpdateFromFeed(
688 const std::vector<DocumentFeed*>& feed_list, 784 const std::vector<DocumentFeed*>& feed_list,
689 int start_changestamp, 785 int64 start_changestamp,
690 int root_feed_changestamp) { 786 int64 root_feed_changestamp) {
691 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 787 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
692 DVLOG(1) << "Updating directory with a feed"; 788 DVLOG(1) << "Updating directory with a feed";
693 789
694 std::set<FilePath> changed_dirs; 790 std::set<FilePath> changed_dirs;
695 791
696 GDataWapiFeedProcessor feed_processor(directory_service_); 792 GDataWapiFeedProcessor feed_processor(directory_service_);
697 const GDataFileError error = feed_processor.ApplyFeeds( 793 const GDataFileError error = feed_processor.ApplyFeeds(
698 feed_list, 794 feed_list,
699 start_changestamp, 795 start_changestamp,
700 root_feed_changestamp, 796 root_feed_changestamp,
701 &changed_dirs); 797 &changed_dirs);
702 798
703 // Don't send directory content change notification while performing 799 // Don't send directory content change notification while performing
704 // the initial content retrieval. 800 // the initial content retrieval.
705 const bool should_notify_directory_changed = (start_changestamp != 0); 801 const bool should_notify_directory_changed = (start_changestamp != 0);
706 if (should_notify_directory_changed) { 802 if (should_notify_directory_changed) {
707 for (std::set<FilePath>::iterator dir_iter = changed_dirs.begin(); 803 for (std::set<FilePath>::iterator dir_iter = changed_dirs.begin();
708 dir_iter != changed_dirs.end(); ++dir_iter) { 804 dir_iter != changed_dirs.end(); ++dir_iter) {
709 FOR_EACH_OBSERVER(Observer, observers_, 805 FOR_EACH_OBSERVER(Observer, observers_,
710 OnDirectoryChanged(*dir_iter)); 806 OnDirectoryChanged(*dir_iter));
711 } 807 }
712 } 808 }
713 809
714 return error; 810 return error;
715 } 811 }
716 812
717 } // namespace gdata 813 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698