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

Side by Side Diff: chrome/browser/sync_file_system/drive_file_sync_service.cc

Issue 11421125: Implement polling part of DriveFileSyncService (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: modify comment Created 8 years 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/sync_file_system/drive_file_sync_service.h" 5 #include "chrome/browser/sync_file_system/drive_file_sync_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/location.h" 12 #include "base/location.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop_proxy.h" 14 #include "base/message_loop_proxy.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/google_apis/gdata_wapi_service.h" 17 #include "chrome/browser/google_apis/gdata_wapi_service.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/sync_file_system/drive_file_sync_client.h" 19 #include "chrome/browser/sync_file_system/drive_file_sync_client.h"
20 #include "chrome/browser/sync_file_system/drive_file_sync_util.h" 20 #include "chrome/browser/sync_file_system/drive_file_sync_util.h"
21 #include "chrome/browser/sync_file_system/drive_metadata_store.h" 21 #include "chrome/browser/sync_file_system/drive_metadata_store.h"
22 #include "chrome/browser/sync_file_system/remote_change_processor.h" 22 #include "chrome/browser/sync_file_system/remote_change_processor.h"
23 #include "chrome/browser/sync_file_system/sync_file_system.pb.h" 23 #include "chrome/browser/sync_file_system/sync_file_system.pb.h"
24 #include "chrome/common/extensions/extension.h"
24 #include "content/public/browser/browser_thread.h" 25 #include "content/public/browser/browser_thread.h"
25 #include "net/base/escape.h" 26 #include "extensions/common/constants.h"
26 #include "webkit/fileapi/file_system_url.h" 27 #include "webkit/fileapi/file_system_url.h"
27 #include "webkit/fileapi/syncable/sync_file_metadata.h" 28 #include "webkit/fileapi/syncable/sync_file_metadata.h"
28 #include "webkit/fileapi/syncable/sync_file_type.h" 29 #include "webkit/fileapi/syncable/sync_file_type.h"
29 #include "webkit/fileapi/syncable/syncable_file_system_util.h" 30 #include "webkit/fileapi/syncable/syncable_file_system_util.h"
30 31
31 namespace sync_file_system { 32 namespace sync_file_system {
32 33
33 namespace { 34 namespace {
34 35
35 const FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp"); 36 const FilePath::CharType kTempDirName[] = FILE_PATH_LITERAL("tmp");
36 const FilePath::CharType kSyncFileSystemDir[] = 37 const FilePath::CharType kSyncFileSystemDir[] =
37 FILE_PATH_LITERAL("Sync FileSystem"); 38 FILE_PATH_LITERAL("Sync FileSystem");
39 const int64 kMinimumPollingDelaySeconds = 10;
40 const int64 kMaximumPollingDelaySeconds = 60 * 60; // 1 hour
41 const int64 kDelayMultiplier = 2;
kinuko 2012/12/03 13:10:34 nit: if it's int we'll have 1, 2 or etc...? maybe
tzik 2012/12/04 04:59:50 Done.
38 42
39 bool CreateTemporaryFile(const FilePath& dir_path, FilePath* temp_file) { 43 bool CreateTemporaryFile(const FilePath& dir_path, FilePath* temp_file) {
40 return file_util::CreateDirectory(dir_path) && 44 return file_util::CreateDirectory(dir_path) &&
41 file_util::CreateTemporaryFileInDir(dir_path, temp_file); 45 file_util::CreateTemporaryFileInDir(dir_path, temp_file);
42 } 46 }
43 47
44 void DeleteTemporaryFile(const FilePath& file_path) { 48 void DeleteTemporaryFile(const FilePath& file_path) {
45 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)) { 49 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)) {
46 content::BrowserThread::PostTask( 50 content::BrowserThread::PostTask(
47 content::BrowserThread::FILE, FROM_HERE, 51 content::BrowserThread::FILE, FROM_HERE,
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 // Smaller changestamps have higher priorities (i.e. need to be processed 199 // Smaller changestamps have higher priorities (i.e. need to be processed
196 // earlier). 200 // earlier).
197 if (left.changestamp != right.changestamp) 201 if (left.changestamp != right.changestamp)
198 return left.changestamp > right.changestamp; 202 return left.changestamp > right.changestamp;
199 return false; 203 return false;
200 } 204 }
201 205
202 DriveFileSyncService::DriveFileSyncService(Profile* profile) 206 DriveFileSyncService::DriveFileSyncService(Profile* profile)
203 : last_operation_status_(fileapi::SYNC_STATUS_OK), 207 : last_operation_status_(fileapi::SYNC_STATUS_OK),
204 state_(REMOTE_SERVICE_OK), 208 state_(REMOTE_SERVICE_OK),
205 largest_changestamp_(0), 209 largest_fetched_changestamp_(0),
210 polling_delay_(base::TimeDelta::FromSeconds(kMinimumPollingDelaySeconds)),
211 polling_enabled_(true),
206 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 212 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
207 temporary_file_dir_ = 213 temporary_file_dir_ =
208 profile->GetPath().Append(kSyncFileSystemDir).Append(kTempDirName); 214 profile->GetPath().Append(kSyncFileSystemDir).Append(kTempDirName);
209 token_.reset(new TaskToken(AsWeakPtr())); 215 token_.reset(new TaskToken(AsWeakPtr()));
210 216
211 sync_client_.reset(new DriveFileSyncClient(profile)); 217 sync_client_.reset(new DriveFileSyncClient(profile));
212 218
213 metadata_store_.reset(new DriveMetadataStore( 219 metadata_store_.reset(new DriveMetadataStore(
214 profile->GetPath().Append(kSyncFileSystemDir), 220 profile->GetPath().Append(kSyncFileSystemDir),
215 content::BrowserThread::GetMessageLoopProxyForThread( 221 content::BrowserThread::GetMessageLoopProxyForThread(
(...skipping 25 matching lines...) Expand all
241 observers_.AddObserver(observer); 247 observers_.AddObserver(observer);
242 } 248 }
243 249
244 void DriveFileSyncService::RemoveObserver(Observer* observer) { 250 void DriveFileSyncService::RemoveObserver(Observer* observer) {
245 observers_.RemoveObserver(observer); 251 observers_.RemoveObserver(observer);
246 } 252 }
247 253
248 void DriveFileSyncService::RegisterOriginForTrackingChanges( 254 void DriveFileSyncService::RegisterOriginForTrackingChanges(
249 const GURL& origin, 255 const GURL& origin,
250 const fileapi::SyncStatusCallback& callback) { 256 const fileapi::SyncStatusCallback& callback) {
257 DCHECK(origin.SchemeIs(extensions::kExtensionScheme));
251 scoped_ptr<TaskToken> token(GetToken( 258 scoped_ptr<TaskToken> token(GetToken(
252 FROM_HERE, TASK_TYPE_DRIVE, "Retrieving origin metadata")); 259 FROM_HERE, TASK_TYPE_DRIVE, "Retrieving origin metadata"));
253 if (!token) { 260 if (!token) {
254 pending_tasks_.push_back(base::Bind( 261 pending_tasks_.push_back(base::Bind(
255 &DriveFileSyncService::RegisterOriginForTrackingChanges, 262 &DriveFileSyncService::RegisterOriginForTrackingChanges,
256 AsWeakPtr(), origin, callback)); 263 AsWeakPtr(), origin, callback));
257 return; 264 return;
258 } 265 }
259 266
260 if (state_ == REMOTE_SERVICE_DISABLED) { 267 if (state_ == REMOTE_SERVICE_DISABLED) {
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 423
417 DVLOG(1) << "ApplyLocalChange for " << url.DebugString() 424 DVLOG(1) << "ApplyLocalChange for " << url.DebugString()
418 << " local_change:" << local_file_change.DebugString() 425 << " local_change:" << local_file_change.DebugString()
419 << " ==> operation:" << operation; 426 << " ==> operation:" << operation;
420 427
421 switch (operation) { 428 switch (operation) {
422 case SYNC_OPERATION_UPLOAD_NEW_FILE: { 429 case SYNC_OPERATION_UPLOAD_NEW_FILE: {
423 sync_client_->UploadNewFile( 430 sync_client_->UploadNewFile(
424 metadata_store_->GetResourceIdForOrigin(url.origin()), 431 metadata_store_->GetResourceIdForOrigin(url.origin()),
425 local_file_path, 432 local_file_path,
426 net::EscapePath(url.path().AsUTF8Unsafe()), 433 url.path().AsUTF8Unsafe(),
427 local_file_metadata.size, 434 local_file_metadata.size,
428 base::Bind(&DriveFileSyncService::DidUploadNewFile, 435 base::Bind(&DriveFileSyncService::DidUploadNewFile,
429 AsWeakPtr(), base::Passed(&token), url, callback)); 436 AsWeakPtr(), base::Passed(&token), url, callback));
430 return; 437 return;
431 } 438 }
432 case SYNC_OPERATION_UPLOAD_EXISTING_FILE: { 439 case SYNC_OPERATION_UPLOAD_EXISTING_FILE: {
433 DriveMetadata metadata; 440 DriveMetadata metadata;
434 const fileapi::SyncStatusCode status = 441 const fileapi::SyncStatusCode status =
435 metadata_store_->ReadEntry(url, &metadata); 442 metadata_store_->ReadEntry(url, &metadata);
436 DCHECK_EQ(fileapi::SYNC_STATUS_OK, status); 443 DCHECK_EQ(fileapi::SYNC_STATUS_OK, status);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
496 callback, fileapi::SYNC_STATUS_FAILED); 503 callback, fileapi::SYNC_STATUS_FAILED);
497 } 504 }
498 505
499 // Called by CreateForTesting. 506 // Called by CreateForTesting.
500 DriveFileSyncService::DriveFileSyncService( 507 DriveFileSyncService::DriveFileSyncService(
501 const FilePath& base_dir, 508 const FilePath& base_dir,
502 scoped_ptr<DriveFileSyncClient> sync_client, 509 scoped_ptr<DriveFileSyncClient> sync_client,
503 scoped_ptr<DriveMetadataStore> metadata_store) 510 scoped_ptr<DriveMetadataStore> metadata_store)
504 : last_operation_status_(fileapi::SYNC_STATUS_OK), 511 : last_operation_status_(fileapi::SYNC_STATUS_OK),
505 state_(REMOTE_SERVICE_OK), 512 state_(REMOTE_SERVICE_OK),
506 largest_changestamp_(0), 513 largest_fetched_changestamp_(0),
514 polling_enabled_(false),
507 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 515 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
508 temporary_file_dir_ = base_dir.Append(kTempDirName); 516 temporary_file_dir_ = base_dir.Append(kTempDirName);
509 517
510 token_.reset(new TaskToken(AsWeakPtr())); 518 token_.reset(new TaskToken(AsWeakPtr()));
511 sync_client_ = sync_client.Pass(); 519 sync_client_ = sync_client.Pass();
512 metadata_store_ = metadata_store.Pass(); 520 metadata_store_ = metadata_store.Pass();
513 521
514 DidInitializeMetadataStore( 522 DidInitializeMetadataStore(
515 GetToken(FROM_HERE, TASK_TYPE_NONE, "Drive initialization for testing"), 523 GetToken(FROM_HERE, TASK_TYPE_NONE, "Drive initialization for testing"),
516 fileapi::SYNC_STATUS_OK, false); 524 fileapi::SYNC_STATUS_OK, false);
517 } 525 }
518 526
519 scoped_ptr<DriveFileSyncService::TaskToken> DriveFileSyncService::GetToken( 527 scoped_ptr<DriveFileSyncService::TaskToken> DriveFileSyncService::GetToken(
520 const tracked_objects::Location& from_here, 528 const tracked_objects::Location& from_here,
521 TaskType task_type, 529 TaskType task_type,
522 const std::string& description) { 530 const std::string& description) {
523 if (!token_) 531 if (!token_)
524 return scoped_ptr<TaskToken>(); 532 return scoped_ptr<TaskToken>();
525 token_->UpdateTask(from_here, task_type, description); 533 token_->UpdateTask(from_here, task_type, description);
526 return token_.Pass(); 534 return token_.Pass();
527 } 535 }
528 536
529 void DriveFileSyncService::NotifyTaskDone(fileapi::SyncStatusCode status, 537 void DriveFileSyncService::NotifyTaskDone(fileapi::SyncStatusCode status,
530 scoped_ptr<TaskToken> token) { 538 scoped_ptr<TaskToken> token) {
531 DCHECK(token); 539 DCHECK(token);
532 last_operation_status_ = status; 540 last_operation_status_ = status;
533 token_ = token.Pass(); 541 token_ = token.Pass();
534 542
535 RemoteServiceState old_state = state_;
536 if (token_->task_type() != TASK_TYPE_NONE) { 543 if (token_->task_type() != TASK_TYPE_NONE) {
537 DVLOG(1) << "NotifyTaskDone: " << token_->description() 544 DVLOG(1) << "NotifyTaskDone: " << token_->description()
538 << ": finished with status=" << status 545 << ": finished with status=" << status
539 << " " << token_->location().ToString(); 546 << " " << token_->location().ToString();
540 547
541 RemoteServiceState old_state = state_; 548 RemoteServiceState old_state = state_;
542 UpdateServiceState(); 549 UpdateServiceState();
543 550
544 // Notify remote sync service state if the state has been changed. 551 // Notify remote sync service state if the state has been changed.
545 if (!token_->description().empty() || old_state != state_) { 552 if (!token_->description().empty() || old_state != state_) {
546 FOR_EACH_OBSERVER( 553 FOR_EACH_OBSERVER(
547 Observer, observers_, 554 Observer, observers_,
548 OnRemoteServiceStateUpdated(state_, token_->done_description())); 555 OnRemoteServiceStateUpdated(state_, token_->done_description()));
549 } 556 }
550 } 557 }
551 558
552 token_->ResetTask(FROM_HERE); 559 token_->ResetTask(FROM_HERE);
553 if (!pending_tasks_.empty()) { 560 if (!pending_tasks_.empty()) {
554 base::Closure closure = pending_tasks_.front(); 561 base::Closure closure = pending_tasks_.front();
555 pending_tasks_.pop_front(); 562 pending_tasks_.pop_front();
556 closure.Run(); 563 closure.Run();
557 return; 564 return;
558 } 565 }
559 566
560 if (state_ != REMOTE_SERVICE_OK || old_state == state_) 567 if (state_ != REMOTE_SERVICE_OK)
561 return; 568 return;
562 569
563 // If the state has become OK and we have any pending batch sync origins 570 // If the state has become OK and we have any pending batch sync origins
564 // restart batch sync for them. 571 // restart batch sync for them.
565 if (!pending_batch_sync_origins_.empty()) { 572 if (!pending_batch_sync_origins_.empty()) {
566 GURL origin = *pending_batch_sync_origins_.begin(); 573 GURL origin = *pending_batch_sync_origins_.begin();
567 pending_batch_sync_origins_.erase(pending_batch_sync_origins_.begin()); 574 pending_batch_sync_origins_.erase(pending_batch_sync_origins_.begin());
568 std::string resource_id = metadata_store_->GetResourceIdForOrigin(origin); 575 std::string resource_id = metadata_store_->GetResourceIdForOrigin(origin);
569 StartBatchSyncForOrigin(origin, resource_id); 576 StartBatchSyncForOrigin(origin, resource_id);
577 return;
570 } 578 }
579
580 // Notify observer of the right timing to run ProcessRemoteChange.
581 FOR_EACH_OBSERVER(Observer, observers_,
582 OnRemoteChangeAvailable(pending_changes_.size()));
583
584 if (pending_changes_.empty() && polling_enabled_)
585 SchedulePolling();
kinuko 2012/12/03 13:10:34 I have a feeling that it might be ok to occasional
kinuko 2012/12/03 13:10:34 in OnAuthenticated or OnNetworkConnected should we
tzik 2012/12/04 04:59:50 Done.
tzik 2012/12/04 04:59:50 those patches are not landed yet. let me do it lat
571 } 586 }
572 587
573 void DriveFileSyncService::UpdateServiceState() { 588 void DriveFileSyncService::UpdateServiceState() {
574 switch (last_operation_status_) { 589 switch (last_operation_status_) {
575 // Possible regular operation errors. 590 // Possible regular operation errors.
576 case fileapi::SYNC_STATUS_OK: 591 case fileapi::SYNC_STATUS_OK:
577 case fileapi::SYNC_STATUS_FILE_BUSY: 592 case fileapi::SYNC_STATUS_FILE_BUSY:
578 case fileapi::SYNC_STATUS_HAS_CONFLICT: 593 case fileapi::SYNC_STATUS_HAS_CONFLICT:
579 case fileapi::SYNC_STATUS_NO_CONFLICT: 594 case fileapi::SYNC_STATUS_NO_CONFLICT:
580 case fileapi::SYNC_STATUS_NO_CHANGE_TO_SYNC: 595 case fileapi::SYNC_STATUS_NO_CHANGE_TO_SYNC:
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 637
623 void DriveFileSyncService::DidInitializeMetadataStore( 638 void DriveFileSyncService::DidInitializeMetadataStore(
624 scoped_ptr<TaskToken> token, 639 scoped_ptr<TaskToken> token,
625 fileapi::SyncStatusCode status, 640 fileapi::SyncStatusCode status,
626 bool created) { 641 bool created) {
627 if (status != fileapi::SYNC_STATUS_OK) { 642 if (status != fileapi::SYNC_STATUS_OK) {
628 NotifyTaskDone(status, token.Pass()); 643 NotifyTaskDone(status, token.Pass());
629 return; 644 return;
630 } 645 }
631 646
647 largest_fetched_changestamp_ = metadata_store_->GetLargestChangeStamp();
648
632 if (metadata_store_->sync_root_directory().empty()) { 649 if (metadata_store_->sync_root_directory().empty()) {
633 GetSyncRootDirectory(token.Pass(), base::Bind(&EmptyStatusCallback)); 650 GetSyncRootDirectory(token.Pass(), base::Bind(&EmptyStatusCallback));
634 return; 651 return;
635 } 652 }
636 653
637 NotifyTaskDone(status, token.Pass()); 654 NotifyTaskDone(status, token.Pass());
638 655
639 for (std::map<GURL, std::string>::const_iterator itr = 656 for (std::map<GURL, std::string>::const_iterator itr =
640 metadata_store_->batch_sync_origins().begin(); 657 metadata_store_->batch_sync_origins().begin();
641 itr != metadata_store_->batch_sync_origins().end(); 658 itr != metadata_store_->batch_sync_origins().end();
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
732 const std::string& resource_id, 749 const std::string& resource_id,
733 google_apis::GDataErrorCode error, 750 google_apis::GDataErrorCode error,
734 int64 largest_changestamp) { 751 int64 largest_changestamp) {
735 if (error != google_apis::HTTP_SUCCESS) { 752 if (error != google_apis::HTTP_SUCCESS) {
736 pending_batch_sync_origins_.insert(origin); 753 pending_batch_sync_origins_.insert(origin);
737 // TODO(tzik): Refine this error code. 754 // TODO(tzik): Refine this error code.
738 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass()); 755 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass());
739 return; 756 return;
740 } 757 }
741 758
759 if (metadata_store_->incremental_sync_origins().empty()) {
760 largest_fetched_changestamp_ = largest_changestamp;
761 metadata_store_->SetLargestChangeStamp(
762 largest_changestamp,
763 base::Bind(&EmptyStatusCallback));
764 }
765
742 DCHECK(token); 766 DCHECK(token);
743 token->UpdateTask(FROM_HERE, TASK_TYPE_DRIVE, "Retrieving remote files"); 767 token->UpdateTask(FROM_HERE, TASK_TYPE_DRIVE, "Retrieving remote files");
744 sync_client_->ListFiles( 768 sync_client_->ListFiles(
745 resource_id, 769 resource_id,
746 base::Bind( 770 base::Bind(
747 &DriveFileSyncService::DidGetDirectoryContentForBatchSync, 771 &DriveFileSyncService::DidGetDirectoryContentForBatchSync,
748 AsWeakPtr(), base::Passed(&token), origin, largest_changestamp)); 772 AsWeakPtr(), base::Passed(&token), origin, largest_changestamp));
749 } 773 }
750 774
751 void DriveFileSyncService::DidGetDirectoryContentForBatchSync( 775 void DriveFileSyncService::DidGetDirectoryContentForBatchSync(
752 scoped_ptr<TaskToken> token, 776 scoped_ptr<TaskToken> token,
753 const GURL& origin, 777 const GURL& origin,
754 int64 largest_changestamp, 778 int64 largest_changestamp,
755 google_apis::GDataErrorCode error, 779 google_apis::GDataErrorCode error,
756 scoped_ptr<google_apis::DocumentFeed> feed) { 780 scoped_ptr<google_apis::DocumentFeed> feed) {
757 if (error != google_apis::HTTP_SUCCESS) { 781 if (error != google_apis::HTTP_SUCCESS) {
758 pending_batch_sync_origins_.insert(origin); 782 pending_batch_sync_origins_.insert(origin);
759 // TODO(tzik): Refine this error code. 783 // TODO(tzik): Refine this error code.
760 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass()); 784 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass());
761 return; 785 return;
762 } 786 }
763 787
764 typedef ScopedVector<google_apis::DocumentEntry>::const_iterator iterator; 788 typedef ScopedVector<google_apis::DocumentEntry>::const_iterator iterator;
765 for (iterator itr = feed->entries().begin(); 789 for (iterator itr = feed->entries().begin();
766 itr != feed->entries().end(); ++itr) { 790 itr != feed->entries().end(); ++itr) {
767 AppendNewRemoteChange(origin, *itr, largest_changestamp, 791 AppendNewRemoteChange(origin, **itr, largest_changestamp,
768 REMOTE_SYNC_TYPE_BATCH); 792 REMOTE_SYNC_TYPE_BATCH);
769 } 793 }
770 794
771 GURL next_feed_url; 795 GURL next_feed_url;
772 if (feed->GetNextFeedURL(&next_feed_url)) { 796 if (feed->GetNextFeedURL(&next_feed_url)) {
773 sync_client_->ContinueListing( 797 sync_client_->ContinueListing(
774 next_feed_url, 798 next_feed_url,
775 base::Bind( 799 base::Bind(
776 &DriveFileSyncService::DidGetDirectoryContentForBatchSync, 800 &DriveFileSyncService::DidGetDirectoryContentForBatchSync,
777 AsWeakPtr(), base::Passed(&token), origin, largest_changestamp)); 801 AsWeakPtr(), base::Passed(&token), origin, largest_changestamp));
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 return SYNC_OPERATION_FAILED; 886 return SYNC_OPERATION_FAILED;
863 } 887 }
864 888
865 void DriveFileSyncService::DidApplyLocalChange( 889 void DriveFileSyncService::DidApplyLocalChange(
866 scoped_ptr<TaskToken> token, 890 scoped_ptr<TaskToken> token,
867 const fileapi::FileSystemURL& url, 891 const fileapi::FileSystemURL& url,
868 const google_apis::GDataErrorCode error, 892 const google_apis::GDataErrorCode error,
869 const fileapi::SyncStatusCallback& callback, 893 const fileapi::SyncStatusCallback& callback,
870 fileapi::SyncStatusCode status) { 894 fileapi::SyncStatusCode status) {
871 if (status == fileapi::SYNC_STATUS_OK) { 895 if (status == fileapi::SYNC_STATUS_OK) {
872 CancelRemoteChange(url); 896 RemoveRemoteChange(url);
873 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass()); 897 NotifyTaskDone(GDataErrorCodeToSyncStatusCodeWrapper(error), token.Pass());
874 callback.Run(GDataErrorCodeToSyncStatusCodeWrapper(error)); 898 callback.Run(GDataErrorCodeToSyncStatusCodeWrapper(error));
875 return; 899 return;
876 } 900 }
877 NotifyTaskDone(status, token.Pass()); 901 NotifyTaskDone(status, token.Pass());
878 callback.Run(status); 902 callback.Run(status);
879 } 903 }
880 904
881 void DriveFileSyncService::DidUploadNewFile( 905 void DriveFileSyncService::DidUploadNewFile(
882 scoped_ptr<TaskToken> token, 906 scoped_ptr<TaskToken> token,
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
997 const DriveMetadata& drive_metadata = param->drive_metadata; 1021 const DriveMetadata& drive_metadata = param->drive_metadata;
998 1022
999 status = metadata_store_->ReadEntry(param->remote_change.url, 1023 status = metadata_store_->ReadEntry(param->remote_change.url,
1000 &param->drive_metadata); 1024 &param->drive_metadata);
1001 DCHECK(status == fileapi::SYNC_STATUS_OK || 1025 DCHECK(status == fileapi::SYNC_STATUS_OK ||
1002 status == fileapi::SYNC_DATABASE_ERROR_NOT_FOUND); 1026 status == fileapi::SYNC_DATABASE_ERROR_NOT_FOUND);
1003 1027
1004 bool missing_db_entry = (status != fileapi::SYNC_STATUS_OK); 1028 bool missing_db_entry = (status != fileapi::SYNC_STATUS_OK);
1005 if (missing_db_entry) { 1029 if (missing_db_entry) {
1006 param->drive_metadata.set_resource_id(param->remote_change.resource_id); 1030 param->drive_metadata.set_resource_id(param->remote_change.resource_id);
1031 param->drive_metadata.set_md5_checksum(std::string());
1007 param->drive_metadata.set_conflicted(false); 1032 param->drive_metadata.set_conflicted(false);
1008 } 1033 }
1009 bool missing_local_file = 1034 bool missing_local_file =
1010 (metadata.file_type == fileapi::SYNC_FILE_TYPE_UNKNOWN); 1035 (metadata.file_type == fileapi::SYNC_FILE_TYPE_UNKNOWN);
1011 1036
1012 if (param->drive_metadata.conflicted()) { 1037 if (param->drive_metadata.conflicted()) {
1013 if (missing_local_file) { 1038 if (missing_local_file) {
1014 if (param->remote_change.change.IsAddOrUpdate()) { 1039 if (param->remote_change.change.IsAddOrUpdate()) {
1015 NOTIMPLEMENTED() << "ResolveToRemote()"; 1040 NOTIMPLEMENTED() << "ResolveToRemote()";
1016 AbortRemoteSync(param.Pass(), fileapi::SYNC_STATUS_FAILED); 1041 AbortRemoteSync(param.Pass(), fileapi::SYNC_STATUS_FAILED);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1076 1101
1077 DCHECK(param->remote_change.change.IsDelete()); 1102 DCHECK(param->remote_change.change.IsDelete());
1078 if (local_changes.empty()) { 1103 if (local_changes.empty()) {
1079 if (missing_local_file) { 1104 if (missing_local_file) {
1080 param->operation_type = fileapi::SYNC_OPERATION_NONE; 1105 param->operation_type = fileapi::SYNC_OPERATION_NONE;
1081 DeleteMetadataForRemoteSync(param.Pass()); 1106 DeleteMetadataForRemoteSync(param.Pass());
1082 return; 1107 return;
1083 } 1108 }
1084 DCHECK(!missing_local_file); 1109 DCHECK(!missing_local_file);
1085 param->operation_type = fileapi::SYNC_OPERATION_DELETE; 1110 param->operation_type = fileapi::SYNC_OPERATION_DELETE;
1111
1112 const fileapi::FileChange& file_change = param->remote_change.change;
1086 param->processor->ApplyRemoteChange( 1113 param->processor->ApplyRemoteChange(
1087 param->remote_change.change, FilePath(), url, 1114 file_change, FilePath(), url,
1088 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, AsWeakPtr(), 1115 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, AsWeakPtr(),
1089 base::Passed(&param))); 1116 base::Passed(&param)));
1090 return; 1117 return;
1091 } 1118 }
1092 1119
1093 DCHECK(!local_changes.empty()); 1120 DCHECK(!local_changes.empty());
1094 if (local_changes.list().back().IsAddOrUpdate()) { 1121 if (local_changes.list().back().IsAddOrUpdate()) {
1095 param->operation_type = fileapi::SYNC_OPERATION_NONE; 1122 param->operation_type = fileapi::SYNC_OPERATION_NONE;
1096 CompleteRemoteSync(param.Pass(), fileapi::SYNC_STATUS_OK); 1123 CompleteRemoteSync(param.Pass(), fileapi::SYNC_STATUS_OK);
1097 return; 1124 return;
(...skipping 18 matching lines...) Expand all
1116 } 1143 }
1117 1144
1118 void DriveFileSyncService::DidGetTemporaryFileForDownload( 1145 void DriveFileSyncService::DidGetTemporaryFileForDownload(
1119 scoped_ptr<ProcessRemoteChangeParam> param, 1146 scoped_ptr<ProcessRemoteChangeParam> param,
1120 bool success) { 1147 bool success) {
1121 if (!success) { 1148 if (!success) {
1122 AbortRemoteSync(param.Pass(), fileapi::SYNC_FILE_ERROR_FAILED); 1149 AbortRemoteSync(param.Pass(), fileapi::SYNC_FILE_ERROR_FAILED);
1123 return; 1150 return;
1124 } 1151 }
1125 1152
1126 DriveMetadata metadata;
1127 metadata_store_->ReadEntry(param->remote_change.url, &metadata);
1128
1129 const FilePath& temporary_file_path = param->temporary_file_path; 1153 const FilePath& temporary_file_path = param->temporary_file_path;
1130 std::string resource_id = param->remote_change.resource_id; 1154 std::string resource_id = param->remote_change.resource_id;
1155 const DriveMetadata& drive_metadata = param->drive_metadata;
1131 sync_client_->DownloadFile( 1156 sync_client_->DownloadFile(
1132 resource_id, metadata.md5_checksum(), 1157 resource_id, drive_metadata.md5_checksum(),
1133 temporary_file_path, 1158 temporary_file_path,
1134 base::Bind(&DriveFileSyncService::DidDownloadFile, 1159 base::Bind(&DriveFileSyncService::DidDownloadFile,
1135 AsWeakPtr(), base::Passed(&param))); 1160 AsWeakPtr(), base::Passed(&param)));
1136 } 1161 }
1137 1162
1138 void DriveFileSyncService::DidDownloadFile( 1163 void DriveFileSyncService::DidDownloadFile(
1139 scoped_ptr<ProcessRemoteChangeParam> param, 1164 scoped_ptr<ProcessRemoteChangeParam> param,
1140 google_apis::GDataErrorCode error, 1165 google_apis::GDataErrorCode error,
1141 const std::string& md5_checksum) { 1166 const std::string& md5_checksum) {
1142 if (error == google_apis::HTTP_NOT_MODIFIED) { 1167 if (error == google_apis::HTTP_NOT_MODIFIED) {
1143 param->operation_type = fileapi::SYNC_OPERATION_NONE; 1168 param->operation_type = fileapi::SYNC_OPERATION_NONE;
1144 CompleteRemoteSync(param.Pass(), fileapi::SYNC_STATUS_OK); 1169 CompleteRemoteSync(param.Pass(), fileapi::SYNC_STATUS_OK);
1145 return; 1170 return;
1146 } 1171 }
1147 1172
1148 fileapi::SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error); 1173 fileapi::SyncStatusCode status = GDataErrorCodeToSyncStatusCodeWrapper(error);
1149 if (status != fileapi::SYNC_STATUS_OK) { 1174 if (status != fileapi::SYNC_STATUS_OK) {
1150 AbortRemoteSync(param.Pass(), status); 1175 AbortRemoteSync(param.Pass(), status);
1151 return; 1176 return;
1152 } 1177 }
1153 1178
1154 param->md5_checksum = md5_checksum; 1179 param->drive_metadata.set_md5_checksum(md5_checksum);
1155 const fileapi::FileChange& change = param->remote_change.change; 1180 const fileapi::FileChange& change = param->remote_change.change;
1156 const FilePath& temporary_file_path = param->temporary_file_path; 1181 const FilePath& temporary_file_path = param->temporary_file_path;
1157 const fileapi::FileSystemURL& url = param->remote_change.url; 1182 const fileapi::FileSystemURL& url = param->remote_change.url;
1158 param->processor->ApplyRemoteChange( 1183 param->processor->ApplyRemoteChange(
1159 change, temporary_file_path, url, 1184 change, temporary_file_path, url,
1160 base::Bind(&DriveFileSyncService::DidApplyRemoteChange, 1185 base::Bind(&DriveFileSyncService::DidApplyRemoteChange,
1161 AsWeakPtr(), base::Passed(&param))); 1186 AsWeakPtr(), base::Passed(&param)));
1162 } 1187 }
1163 1188
1164 void DriveFileSyncService::DidApplyRemoteChange( 1189 void DriveFileSyncService::DidApplyRemoteChange(
1165 scoped_ptr<ProcessRemoteChangeParam> param, 1190 scoped_ptr<ProcessRemoteChangeParam> param,
1166 fileapi::SyncStatusCode status) { 1191 fileapi::SyncStatusCode status) {
1167 if (status != fileapi::SYNC_STATUS_OK) { 1192 if (status != fileapi::SYNC_STATUS_OK) {
1168 AbortRemoteSync(param.Pass(), status); 1193 AbortRemoteSync(param.Pass(), status);
1169 return; 1194 return;
1170 } 1195 }
1171 1196
1172 fileapi::FileSystemURL url = param->remote_change.url; 1197 fileapi::FileSystemURL url = param->remote_change.url;
1173 if (param->remote_change.change.IsDelete()) { 1198 if (param->remote_change.change.IsDelete()) {
1174 DeleteMetadataForRemoteSync(param.Pass()); 1199 DeleteMetadataForRemoteSync(param.Pass());
1175 return; 1200 return;
1176 } 1201 }
1177 1202
1178 DriveMetadata metadata; 1203 const DriveMetadata& drive_metadata = param->drive_metadata;
1179 metadata.set_resource_id(param->remote_change.resource_id); 1204 param->drive_metadata.set_resource_id(param->remote_change.resource_id);
1180 metadata.set_md5_checksum(param->md5_checksum); 1205 param->drive_metadata.set_conflicted(false);
1181 metadata.set_conflicted(false);
1182 1206
1183 metadata_store_->UpdateEntry( 1207 metadata_store_->UpdateEntry(
1184 url, metadata, 1208 url, drive_metadata,
1185 base::Bind(&DriveFileSyncService::CompleteRemoteSync, 1209 base::Bind(&DriveFileSyncService::CompleteRemoteSync,
1186 AsWeakPtr(), base::Passed(&param))); 1210 AsWeakPtr(), base::Passed(&param)));
1187 } 1211 }
1188 1212
1189 void DriveFileSyncService::DeleteMetadataForRemoteSync( 1213 void DriveFileSyncService::DeleteMetadataForRemoteSync(
1190 scoped_ptr<ProcessRemoteChangeParam> param) { 1214 scoped_ptr<ProcessRemoteChangeParam> param) {
1191 fileapi::FileSystemURL url = param->remote_change.url; 1215 fileapi::FileSystemURL url = param->remote_change.url;
1192 metadata_store_->DeleteEntry( 1216 metadata_store_->DeleteEntry(
1193 url, 1217 url,
1194 base::Bind(&DriveFileSyncService::CompleteRemoteSync, 1218 base::Bind(&DriveFileSyncService::CompleteRemoteSync,
1195 AsWeakPtr(), base::Passed(&param))); 1219 AsWeakPtr(), base::Passed(&param)));
1196 } 1220 }
1197 1221
1198 void DriveFileSyncService::CompleteRemoteSync( 1222 void DriveFileSyncService::CompleteRemoteSync(
1199 scoped_ptr<ProcessRemoteChangeParam> param, 1223 scoped_ptr<ProcessRemoteChangeParam> param,
1200 fileapi::SyncStatusCode status) { 1224 fileapi::SyncStatusCode status) {
1201 if (status != fileapi::SYNC_STATUS_OK) { 1225 if (status != fileapi::SYNC_STATUS_OK) {
1202 AbortRemoteSync(param.Pass(), status); 1226 AbortRemoteSync(param.Pass(), status);
1203 return; 1227 return;
1204 } 1228 }
1205 1229
1206 CancelRemoteChange(param->remote_change.url); 1230 RemoveRemoteChange(param->remote_change.url);
1207 1231
1208 GURL origin = param->remote_change.url.origin(); 1232 GURL origin = param->remote_change.url.origin();
1209 if (metadata_store_->IsIncrementalSyncOrigin(origin)) { 1233 if (metadata_store_->IsIncrementalSyncOrigin(origin)) {
1210 int64 changestamp = param->remote_change.changestamp; 1234 int64 changestamp = param->remote_change.changestamp;
1211 metadata_store_->SetLargestChangeStamp( 1235 metadata_store_->SetLargestChangeStamp(
1212 changestamp, 1236 changestamp,
1213 base::Bind(&DriveFileSyncService::FinalizeRemoteSync, 1237 base::Bind(&DriveFileSyncService::FinalizeRemoteSync,
1214 AsWeakPtr(), base::Passed(&param))); 1238 AsWeakPtr(), base::Passed(&param)));
1215 return; 1239 return;
1216 } 1240 }
(...skipping 20 matching lines...) Expand all
1237 param->callback.Run(status, param->remote_change.url, 1261 param->callback.Run(status, param->remote_change.url,
1238 param->operation_type); 1262 param->operation_type);
1239 } else { 1263 } else {
1240 param->callback.Run(status, param->remote_change.url, 1264 param->callback.Run(status, param->remote_change.url,
1241 fileapi::SYNC_OPERATION_NONE); 1265 fileapi::SYNC_OPERATION_NONE);
1242 } 1266 }
1243 } 1267 }
1244 1268
1245 void DriveFileSyncService::AppendNewRemoteChange( 1269 void DriveFileSyncService::AppendNewRemoteChange(
1246 const GURL& origin, 1270 const GURL& origin,
1247 google_apis::DocumentEntry* entry, 1271 const google_apis::DocumentEntry& entry,
1248 int64 changestamp, 1272 int64 changestamp,
1249 RemoteSyncType sync_type) { 1273 RemoteSyncType sync_type) {
1250 // TODO(tzik): Normalize the path here. 1274 // TODO(tzik): Normalize the path here.
1251 FilePath path = FilePath::FromUTF8Unsafe(UTF16ToUTF8(entry->title())); 1275 FilePath path = FilePath::FromUTF8Unsafe(UTF16ToUTF8(entry.title()));
1252 1276
1253 PathToChange* path_to_change = &url_to_change_[origin]; 1277 PathToChange* path_to_change = &url_to_change_[origin];
1254 PathToChange::iterator found = path_to_change->find(path); 1278 PathToChange::iterator found = path_to_change->find(path);
1255 if (found != path_to_change->end()) { 1279 if (found != path_to_change->end()) {
1256 if (found->second.changestamp >= changestamp) 1280 if (found->second.changestamp >= changestamp)
1257 return; 1281 return;
1258 pending_changes_.erase(found->second.position_in_queue); 1282 pending_changes_.erase(found->second.position_in_queue);
1259 } 1283 }
1260 1284
1261 fileapi::FileSystemURL url( 1285 fileapi::FileSystemURL url(
1262 fileapi::CreateSyncableFileSystemURL(origin, kServiceName, path)); 1286 fileapi::CreateSyncableFileSystemURL(origin, kServiceName, path));
1263 1287
1264 fileapi::FileChange::ChangeType change_type; 1288 fileapi::FileChange::ChangeType change_type;
1265 fileapi::SyncFileType file_type; 1289 fileapi::SyncFileType file_type;
1266 if (entry->deleted()) { 1290 if (entry.deleted()) {
1267 change_type = fileapi::FileChange::FILE_CHANGE_DELETE; 1291 change_type = fileapi::FileChange::FILE_CHANGE_DELETE;
1268 file_type = fileapi::SYNC_FILE_TYPE_UNKNOWN; 1292 file_type = fileapi::SYNC_FILE_TYPE_UNKNOWN;
1269 } else { 1293 } else {
1270 change_type = fileapi::FileChange::FILE_CHANGE_ADD_OR_UPDATE; 1294 change_type = fileapi::FileChange::FILE_CHANGE_ADD_OR_UPDATE;
1271 if (entry->kind() == google_apis::ENTRY_KIND_FOLDER) 1295 if (entry.is_folder())
1272 file_type = fileapi::SYNC_FILE_TYPE_DIRECTORY; 1296 file_type = fileapi::SYNC_FILE_TYPE_DIRECTORY;
1273 else 1297 else
1274 file_type = fileapi::SYNC_FILE_TYPE_FILE; 1298 file_type = fileapi::SYNC_FILE_TYPE_FILE;
1275 } 1299 }
1276 1300
1277 fileapi::FileChange file_change(change_type, file_type); 1301 fileapi::FileChange file_change(change_type, file_type);
1278 1302
1279 std::pair<PendingChangeQueue::iterator, bool> inserted_to_queue = 1303 std::pair<PendingChangeQueue::iterator, bool> inserted_to_queue =
1280 pending_changes_.insert(ChangeQueueItem(changestamp, sync_type, url)); 1304 pending_changes_.insert(ChangeQueueItem(changestamp, sync_type, url));
1281 DCHECK(inserted_to_queue.second); 1305 DCHECK(inserted_to_queue.second);
1282 1306
1283 (*path_to_change)[path] = RemoteChange( 1307 (*path_to_change)[path] = RemoteChange(
1284 changestamp, entry->resource_id(), url, file_change, 1308 changestamp, entry.resource_id(), url, file_change,
1285 inserted_to_queue.first); 1309 inserted_to_queue.first);
1286 } 1310 }
1287 1311
1288 void DriveFileSyncService::CancelRemoteChange( 1312 void DriveFileSyncService::RemoveRemoteChange(
1289 const fileapi::FileSystemURL& url) { 1313 const fileapi::FileSystemURL& url) {
1290 URLToChange::iterator found_origin = url_to_change_.find(url.origin()); 1314 URLToChange::iterator found_origin = url_to_change_.find(url.origin());
1291 if (found_origin == url_to_change_.end()) 1315 if (found_origin == url_to_change_.end())
1292 return; 1316 return;
1293 1317
1294 PathToChange* path_to_change = &found_origin->second; 1318 PathToChange* path_to_change = &found_origin->second;
1295 PathToChange::iterator found_change = path_to_change->find(url.path()); 1319 PathToChange::iterator found_change = path_to_change->find(url.path());
1296 if (found_change == path_to_change->end()) 1320 if (found_change == path_to_change->end())
1297 return; 1321 return;
1298 1322
1299 pending_changes_.erase(found_change->second.position_in_queue); 1323 pending_changes_.erase(found_change->second.position_in_queue);
1300 path_to_change->erase(found_change); 1324 path_to_change->erase(found_change);
1301 if (path_to_change->empty()) 1325 if (path_to_change->empty())
1302 url_to_change_.erase(found_origin); 1326 url_to_change_.erase(found_origin);
1303 1327
1304 MaybeMarkAsIncrementalSyncOrigin(url.origin()); 1328 if (metadata_store_->IsBatchSyncOrigin(url.origin()) &&
1305 } 1329 !ContainsKey(url_to_change_, url.origin())) {
1306 1330 metadata_store_->MoveBatchSyncOriginToIncremental(url.origin());
kinuko 2012/12/03 13:10:34 Shouldn't we call SchedulePolling() but not kick t
1307 void DriveFileSyncService::MaybeMarkAsIncrementalSyncOrigin( 1331 }
1308 const GURL& origin) {
1309 if (metadata_store_->IsBatchSyncOrigin(origin) &&
1310 !ContainsKey(url_to_change_, origin))
1311 metadata_store_->MoveBatchSyncOriginToIncremental(origin);
1312 } 1332 }
1313 1333
1314 bool DriveFileSyncService::GetPendingChangeForFileSystemURL( 1334 bool DriveFileSyncService::GetPendingChangeForFileSystemURL(
1315 const fileapi::FileSystemURL& url, 1335 const fileapi::FileSystemURL& url,
1316 RemoteChange* change) const { 1336 RemoteChange* change) const {
1317 DCHECK(change); 1337 DCHECK(change);
1318 URLToChange::const_iterator found_url = url_to_change_.find(url.origin()); 1338 URLToChange::const_iterator found_url = url_to_change_.find(url.origin());
1319 if (found_url == url_to_change_.end()) 1339 if (found_url == url_to_change_.end())
1320 return false; 1340 return false;
1321 const PathToChange& path_to_change = found_url->second; 1341 const PathToChange& path_to_change = found_url->second;
1322 PathToChange::const_iterator found_path = path_to_change.find(url.path()); 1342 PathToChange::const_iterator found_path = path_to_change.find(url.path());
1323 if (found_path == path_to_change.end()) 1343 if (found_path == path_to_change.end())
1324 return false; 1344 return false;
1325 *change = found_path->second; 1345 *change = found_path->second;
1326 return true; 1346 return true;
1327 } 1347 }
1328 1348
1349 void DriveFileSyncService::FetchChangesForIncrementalSync() {
1350 scoped_ptr<TaskToken> token(GetToken(FROM_HERE, TASK_TYPE_DRIVE,
1351 "Fetching remote change list"));
1352 if (!token) {
1353 pending_tasks_.push_back(base::Bind(
1354 &DriveFileSyncService::FetchChangesForIncrementalSync, AsWeakPtr()));
1355 return;
1356 }
1357
1358 if (metadata_store_->incremental_sync_origins().empty()) {
1359 NotifyTaskDone(fileapi::SYNC_STATUS_OK, token.Pass());
1360 return;
1361 }
kinuko 2012/12/03 13:10:34 Don't we call FetchChangesForIncrementalSync() rep
tzik 2012/12/04 04:59:50 Done.
1362
1363 sync_client_->ListChanges(
1364 largest_fetched_changestamp_,
1365 base::Bind(&DriveFileSyncService::DidFetchChangesForIncrementalSync,
1366 AsWeakPtr(), base::Passed(&token)));
1367 }
1368
1369 void DriveFileSyncService::DidFetchChangesForIncrementalSync(
1370 scoped_ptr<TaskToken> token,
1371 google_apis::GDataErrorCode error,
1372 scoped_ptr<google_apis::DocumentFeed> changes) {
1373 if (error != google_apis::HTTP_SUCCESS) {
1374 NotifyTaskDone(fileapi::SYNC_STATUS_FAILED, token.Pass());
1375 return;
1376 }
1377
1378 GURL next_feed;
kinuko 2012/12/03 13:10:34 nit: can you move this right before line 1392?
tzik 2012/12/04 04:59:50 Done.
1379
1380 typedef ScopedVector<google_apis::DocumentEntry>::const_iterator iterator;
1381 for (iterator itr = changes->entries().begin();
1382 itr != changes->entries().end(); ++itr) {
1383 const google_apis::DocumentEntry& entry = **itr;
1384 GURL origin;
1385 if (!GetOriginForEntry(entry, &origin))
1386 continue;
1387
1388 AppendNewRemoteChange(origin, entry, entry.changestamp(),
1389 REMOTE_SYNC_TYPE_INCREMENTAL);
1390 }
1391
1392 if (changes->GetNextFeedURL(&next_feed)) {
1393 sync_client_->ContinueListing(
1394 next_feed,
1395 base::Bind(&DriveFileSyncService::DidFetchChangesForIncrementalSync,
1396 AsWeakPtr(), base::Passed(&token)));
kinuko 2012/12/03 13:10:34 nit: can we return here and remove else on line 13
tzik 2012/12/04 04:59:50 Done.
1397 } else {
1398 largest_fetched_changestamp_ = changes->largest_changestamp();
1399
1400 if (changes->start_index() == 0 && changes->entries().empty()) {
1401 // If this set of changes is the first feed and its empty, update
kinuko 2012/12/03 13:10:34 nit: its empty -> it's empty
tzik 2012/12/04 04:59:50 Done.
1402 // the polling delay to wait longer.
1403 base::TimeDelta max_delay(
1404 base::TimeDelta::FromSeconds(kMaximumPollingDelaySeconds));
1405
1406 polling_delay_ *= kDelayMultiplier;
1407 if (polling_delay_ >= max_delay)
1408 polling_delay_ = max_delay;
1409 } else {
1410 polling_delay_ = base::TimeDelta::FromSeconds(
1411 kMinimumPollingDelaySeconds);
1412 }
1413
1414 NotifyTaskDone(fileapi::SYNC_STATUS_OK, token.Pass());
1415 }
1416 }
1417
1418 bool DriveFileSyncService::GetOriginForEntry(
1419 const google_apis::DocumentEntry& entry,
1420 GURL* origin_out) {
1421 typedef ScopedVector<google_apis::Link>::const_iterator iterator;
1422 for (iterator itr = entry.links().begin();
1423 itr != entry.links().end(); ++itr) {
1424 if ((*itr)->type() != google_apis::Link::LINK_PARENT)
1425 continue;
1426 GURL origin(UTF16ToUTF8((*itr)->title()));
1427 if (!origin.is_valid())
1428 continue;
1429
1430 if (!metadata_store_->IsBatchSyncOrigin(origin) &&
1431 !metadata_store_->IsIncrementalSyncOrigin(origin))
1432 continue;
1433 std::string resource_id(metadata_store_->GetResourceIdForOrigin(origin));
1434 GURL resource_link(sync_client_->ResourceIdToResourceLink(resource_id));
1435 if ((*itr)->href().GetOrigin() != resource_link.GetOrigin() ||
1436 (*itr)->href().path() != resource_link.path())
1437 continue;
1438
1439 *origin_out = origin;
1440 return true;
1441 }
1442 return false;
1443 }
1444
1445 void DriveFileSyncService::SchedulePolling() {
1446 if (!polling_timer_.IsRunning()) {
kinuko 2012/12/03 13:10:34 nit: maybe we can early return? if (polling_timer
tzik 2012/12/04 04:59:50 Done.
1447 DVLOG(1) << "Polling scheduled"
1448 << " (delay:" << polling_delay_.InSeconds() << ")";
1449
1450 polling_timer_.Start(
1451 FROM_HERE, polling_delay_,
1452 base::Bind(&DriveFileSyncService::FetchChangesForIncrementalSync,
1453 AsWeakPtr()));
1454 }
1455 }
1456
1329 fileapi::SyncStatusCode 1457 fileapi::SyncStatusCode
1330 DriveFileSyncService::GDataErrorCodeToSyncStatusCodeWrapper( 1458 DriveFileSyncService::GDataErrorCodeToSyncStatusCodeWrapper(
1331 google_apis::GDataErrorCode error) const { 1459 google_apis::GDataErrorCode error) const {
1332 fileapi::SyncStatusCode status = GDataErrorCodeToSyncStatusCode(error); 1460 fileapi::SyncStatusCode status = GDataErrorCodeToSyncStatusCode(error);
1333 if (status != fileapi::SYNC_STATUS_OK && !sync_client_->IsAuthenticated()) 1461 if (status != fileapi::SYNC_STATUS_OK && !sync_client_->IsAuthenticated())
1334 return fileapi::SYNC_STATUS_AUTHENTICATION_FAILED; 1462 return fileapi::SYNC_STATUS_AUTHENTICATION_FAILED;
1335 return status; 1463 return status;
1336 } 1464 }
1337 1465
1338 } // namespace sync_file_system 1466 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698