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

Side by Side Diff: sync/internal_api/sync_manager_impl.cc

Issue 488843002: [Sync] Add support for server controlled nudge delays (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 6 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 "sync/internal_api/sync_manager_impl.h" 5 #include "sync/internal_api/sync_manager_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 namespace syncer { 55 namespace syncer {
56 56
57 using sessions::SyncSessionContext; 57 using sessions::SyncSessionContext;
58 using syncable::ImmutableWriteTransactionInfo; 58 using syncable::ImmutableWriteTransactionInfo;
59 using syncable::SPECIFICS; 59 using syncable::SPECIFICS;
60 using syncable::UNIQUE_POSITION; 60 using syncable::UNIQUE_POSITION;
61 61
62 namespace { 62 namespace {
63 63
64 // Delays for syncer nudges.
65 static const int kDefaultNudgeDelayMilliseconds = 200;
66 static const int kSlowNudgeDelayMilliseconds = 2000;
67 static const int kSyncRefreshDelayMsec = 500;
68 static const int kSyncSchedulerDelayMsec = 250;
69
70 GetUpdatesCallerInfo::GetUpdatesSource GetSourceFromReason( 64 GetUpdatesCallerInfo::GetUpdatesSource GetSourceFromReason(
71 ConfigureReason reason) { 65 ConfigureReason reason) {
72 switch (reason) { 66 switch (reason) {
73 case CONFIGURE_REASON_RECONFIGURATION: 67 case CONFIGURE_REASON_RECONFIGURATION:
74 return GetUpdatesCallerInfo::RECONFIGURATION; 68 return GetUpdatesCallerInfo::RECONFIGURATION;
75 case CONFIGURE_REASON_MIGRATION: 69 case CONFIGURE_REASON_MIGRATION:
76 return GetUpdatesCallerInfo::MIGRATION; 70 return GetUpdatesCallerInfo::MIGRATION;
77 case CONFIGURE_REASON_NEW_CLIENT: 71 case CONFIGURE_REASON_NEW_CLIENT:
78 return GetUpdatesCallerInfo::NEW_CLIENT; 72 return GetUpdatesCallerInfo::NEW_CLIENT;
79 case CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE: 73 case CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE:
80 case CONFIGURE_REASON_CRYPTO: 74 case CONFIGURE_REASON_CRYPTO:
81 return GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE; 75 return GetUpdatesCallerInfo::NEWLY_SUPPORTED_DATATYPE;
82 default: 76 default:
83 NOTREACHED(); 77 NOTREACHED();
84 } 78 }
85 return GetUpdatesCallerInfo::UNKNOWN; 79 return GetUpdatesCallerInfo::UNKNOWN;
86 } 80 }
87 81
88 } // namespace 82 } // namespace
89 83
90 // A class to calculate nudge delays for types.
91 class NudgeStrategy {
rlarocque 2014/08/20 00:13:32 Woo-hoo! I'm glad to see this being deleted.
Nicolas Zea 2014/08/20 22:49:43 Acknowledged.
92 public:
93 static TimeDelta GetNudgeDelayTimeDelta(const ModelType& model_type,
94 SyncManagerImpl* core) {
95 NudgeDelayStrategy delay_type = GetNudgeDelayStrategy(model_type);
96 return GetNudgeDelayTimeDeltaFromType(delay_type,
97 model_type,
98 core);
99 }
100
101 private:
102 // Possible types of nudge delay for datatypes.
103 // Note: These are just hints. If a sync happens then all dirty entries
104 // would be committed as part of the sync.
105 enum NudgeDelayStrategy {
106 // Sync right away.
107 IMMEDIATE,
108
109 // Sync this change while syncing another change.
110 ACCOMPANY_ONLY,
111
112 // The datatype does not use one of the predefined wait times but defines
113 // its own wait time logic for nudge.
114 CUSTOM,
115 };
116
117 static NudgeDelayStrategy GetNudgeDelayStrategy(const ModelType& type) {
118 switch (type) {
119 case AUTOFILL:
120 return ACCOMPANY_ONLY;
121 case BOOKMARKS:
122 case PREFERENCES:
123 case SESSIONS:
124 case FAVICON_IMAGES:
125 case FAVICON_TRACKING:
126 return CUSTOM;
127 default:
128 return IMMEDIATE;
129 }
130 }
131
132 static TimeDelta GetNudgeDelayTimeDeltaFromType(
133 const NudgeDelayStrategy& delay_type, const ModelType& model_type,
134 const SyncManagerImpl* core) {
135 CHECK(core);
136 TimeDelta delay = TimeDelta::FromMilliseconds(
137 kDefaultNudgeDelayMilliseconds);
138 switch (delay_type) {
139 case IMMEDIATE:
140 delay = TimeDelta::FromMilliseconds(
141 kDefaultNudgeDelayMilliseconds);
142 break;
143 case ACCOMPANY_ONLY:
144 delay = TimeDelta::FromSeconds(kDefaultShortPollIntervalSeconds);
145 break;
146 case CUSTOM:
147 switch (model_type) {
148 case BOOKMARKS:
149 case PREFERENCES:
150 delay = TimeDelta::FromMilliseconds(kSlowNudgeDelayMilliseconds);
151 break;
152 case SESSIONS:
153 case FAVICON_IMAGES:
154 case FAVICON_TRACKING:
155 delay = core->scheduler()->GetSessionsCommitDelay();
156 break;
157 default:
158 NOTREACHED();
159 }
160 break;
161 default:
162 NOTREACHED();
163 }
164 return delay;
165 }
166 };
167
168 SyncManagerImpl::SyncManagerImpl(const std::string& name) 84 SyncManagerImpl::SyncManagerImpl(const std::string& name)
169 : name_(name), 85 : name_(name),
170 change_delegate_(NULL), 86 change_delegate_(NULL),
171 initialized_(false), 87 initialized_(false),
172 observing_network_connectivity_changes_(false), 88 observing_network_connectivity_changes_(false),
173 report_unrecoverable_error_function_(NULL), 89 report_unrecoverable_error_function_(NULL),
174 weak_ptr_factory_(this) { 90 weak_ptr_factory_(this) {
175 // Pre-fill |notification_info_map_|. 91 // Pre-fill |notification_info_map_|.
176 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { 92 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) {
177 notification_info_map_.insert( 93 notification_info_map_.insert(
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 &(change_records_[i]))) { 784 &(change_records_[i]))) {
869 for (size_t j = 0; j < change_records_[i].Get().size(); ++j) 785 for (size_t j = 0; j < change_records_[i].Get().size(); ++j)
870 entries_changed->push_back((change_records_[i].Get())[j].id); 786 entries_changed->push_back((change_records_[i].Get())[j].id);
871 } 787 }
872 if (change_records_[i].Get().empty()) 788 if (change_records_[i].Get().empty())
873 change_records_.erase(i); 789 change_records_.erase(i);
874 } 790 }
875 } 791 }
876 } 792 }
877 793
878 TimeDelta SyncManagerImpl::GetNudgeDelayTimeDelta(
879 const ModelType& model_type) {
880 return NudgeStrategy::GetNudgeDelayTimeDelta(model_type, this);
881 }
882
883 void SyncManagerImpl::RequestNudgeForDataTypes( 794 void SyncManagerImpl::RequestNudgeForDataTypes(
884 const tracked_objects::Location& nudge_location, 795 const tracked_objects::Location& nudge_location,
885 ModelTypeSet types) { 796 ModelTypeSet types) {
886 debug_info_event_listener_.OnNudgeFromDatatype(types.First().Get()); 797 debug_info_event_listener_.OnNudgeFromDatatype(types.First().Get());
887 798
888 // TODO(lipalani) : Calculate the nudge delay based on all types. 799 scheduler_->ScheduleLocalNudge(types, nudge_location);
889 base::TimeDelta nudge_delay = NudgeStrategy::GetNudgeDelayTimeDelta(
890 types.First().Get(),
891 this);
892 scheduler_->ScheduleLocalNudge(nudge_delay,
893 types,
894 nudge_location);
895 } 800 }
896 801
897 void SyncManagerImpl::NudgeForInitialDownload(syncer::ModelType type) { 802 void SyncManagerImpl::NudgeForInitialDownload(syncer::ModelType type) {
898 DCHECK(thread_checker_.CalledOnValidThread()); 803 DCHECK(thread_checker_.CalledOnValidThread());
899 scheduler_->ScheduleInitialSyncNudge(type); 804 scheduler_->ScheduleInitialSyncNudge(type);
900 } 805 }
901 806
902 void SyncManagerImpl::NudgeForCommit(syncer::ModelType type) { 807 void SyncManagerImpl::NudgeForCommit(syncer::ModelType type) {
903 DCHECK(thread_checker_.CalledOnValidThread()); 808 DCHECK(thread_checker_.CalledOnValidThread());
904 RequestNudgeForDataTypes(FROM_HERE, ModelTypeSet(type)); 809 RequestNudgeForDataTypes(FROM_HERE, ModelTypeSet(type));
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
984 allstatus_.SetNotificationsEnabled(invalidator_enabled); 889 allstatus_.SetNotificationsEnabled(invalidator_enabled);
985 scheduler_->SetNotificationsEnabled(invalidator_enabled); 890 scheduler_->SetNotificationsEnabled(invalidator_enabled);
986 } 891 }
987 892
988 void SyncManagerImpl::OnIncomingInvalidation( 893 void SyncManagerImpl::OnIncomingInvalidation(
989 syncer::ModelType type, 894 syncer::ModelType type,
990 scoped_ptr<InvalidationInterface> invalidation) { 895 scoped_ptr<InvalidationInterface> invalidation) {
991 DCHECK(thread_checker_.CalledOnValidThread()); 896 DCHECK(thread_checker_.CalledOnValidThread());
992 897
993 scheduler_->ScheduleInvalidationNudge( 898 scheduler_->ScheduleInvalidationNudge(
994 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec),
995 type, 899 type,
996 invalidation.Pass(), 900 invalidation.Pass(),
997 FROM_HERE); 901 FROM_HERE);
998 } 902 }
999 903
1000 void SyncManagerImpl::RefreshTypes(ModelTypeSet types) { 904 void SyncManagerImpl::RefreshTypes(ModelTypeSet types) {
1001 DCHECK(thread_checker_.CalledOnValidThread()); 905 DCHECK(thread_checker_.CalledOnValidThread());
1002 if (types.Empty()) { 906 if (types.Empty()) {
1003 LOG(WARNING) << "Sync received refresh request with no types specified."; 907 LOG(WARNING) << "Sync received refresh request with no types specified.";
1004 } else { 908 } else {
1005 scheduler_->ScheduleLocalRefreshRequest( 909 scheduler_->ScheduleLocalRefreshRequest(
1006 TimeDelta::FromMilliseconds(kSyncRefreshDelayMsec),
1007 types, FROM_HERE); 910 types, FROM_HERE);
1008 } 911 }
1009 } 912 }
1010 913
1011 SyncStatus SyncManagerImpl::GetDetailedStatus() const { 914 SyncStatus SyncManagerImpl::GetDetailedStatus() const {
1012 return allstatus_.status(); 915 return allstatus_.status();
1013 } 916 }
1014 917
1015 void SyncManagerImpl::SaveChanges() { 918 void SyncManagerImpl::SaveChanges() {
1016 directory()->SaveChanges(); 919 directory()->SaveChanges();
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 1033
1131 bool SyncManagerImpl::HasDirectoryTypeDebugInfoObserver( 1034 bool SyncManagerImpl::HasDirectoryTypeDebugInfoObserver(
1132 syncer::TypeDebugInfoObserver* observer) { 1035 syncer::TypeDebugInfoObserver* observer) {
1133 return model_type_registry_->HasDirectoryTypeDebugInfoObserver(observer); 1036 return model_type_registry_->HasDirectoryTypeDebugInfoObserver(observer);
1134 } 1037 }
1135 1038
1136 void SyncManagerImpl::RequestEmitDebugInfo() { 1039 void SyncManagerImpl::RequestEmitDebugInfo() {
1137 model_type_registry_->RequestEmitDebugInfo(); 1040 model_type_registry_->RequestEmitDebugInfo();
1138 } 1041 }
1139 1042
1140 // static.
1141 int SyncManagerImpl::GetDefaultNudgeDelay() {
1142 return kDefaultNudgeDelayMilliseconds;
1143 }
1144
1145 // static.
1146 int SyncManagerImpl::GetSlowNudgeDelay() {
1147 return kSlowNudgeDelayMilliseconds;
1148 }
1149
1150 } // namespace syncer 1043 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698