OLD | NEW |
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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return delay; | 161 return delay; |
162 } | 162 } |
163 }; | 163 }; |
164 | 164 |
165 SyncManagerImpl::SyncManagerImpl(const std::string& name) | 165 SyncManagerImpl::SyncManagerImpl(const std::string& name) |
166 : name_(name), | 166 : name_(name), |
167 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 167 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
168 change_delegate_(NULL), | 168 change_delegate_(NULL), |
169 initialized_(false), | 169 initialized_(false), |
170 observing_ip_address_changes_(false), | 170 observing_ip_address_changes_(false), |
171 notifications_disabled_reason_(TRANSIENT_NOTIFICATION_ERROR), | 171 invalidator_state_(DEFAULT_INVALIDATION_ERROR), |
172 throttled_data_type_tracker_(&allstatus_), | 172 throttled_data_type_tracker_(&allstatus_), |
173 traffic_recorder_(kMaxMessagesToRecord, kMaxMessageSizeToRecord), | 173 traffic_recorder_(kMaxMessagesToRecord, kMaxMessageSizeToRecord), |
174 encryptor_(NULL), | 174 encryptor_(NULL), |
175 unrecoverable_error_handler_(NULL), | 175 unrecoverable_error_handler_(NULL), |
176 report_unrecoverable_error_function_(NULL) { | 176 report_unrecoverable_error_function_(NULL) { |
177 // Pre-fill |notification_info_map_|. | 177 // Pre-fill |notification_info_map_|. |
178 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 178 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { |
179 notification_info_map_.insert( | 179 notification_info_map_.insert( |
180 std::make_pair(ModelTypeFromInt(i), NotificationInfo())); | 180 std::make_pair(ModelTypeFromInt(i), NotificationInfo())); |
181 } | 181 } |
(...skipping 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 // This is here for tests, which are still using p2p notifications. | 1003 // This is here for tests, which are still using p2p notifications. |
1004 // | 1004 // |
1005 // TODO(chron): Consider changing this back to track has_more_to_sync | 1005 // TODO(chron): Consider changing this back to track has_more_to_sync |
1006 // only notify peers if a successful commit has occurred. | 1006 // only notify peers if a successful commit has occurred. |
1007 bool is_notifiable_commit = | 1007 bool is_notifiable_commit = |
1008 (event.snapshot.model_neutral_state().num_successful_commits > 0); | 1008 (event.snapshot.model_neutral_state().num_successful_commits > 0); |
1009 if (is_notifiable_commit) { | 1009 if (is_notifiable_commit) { |
1010 if (invalidator_.get()) { | 1010 if (invalidator_.get()) { |
1011 const ObjectIdStateMap& id_state_map = | 1011 const ObjectIdStateMap& id_state_map = |
1012 ModelTypeStateMapToObjectIdStateMap(event.snapshot.source().types); | 1012 ModelTypeStateMapToObjectIdStateMap(event.snapshot.source().types); |
1013 invalidator_->SendNotification(id_state_map); | 1013 invalidator_->SendInvalidation(id_state_map); |
1014 } else { | 1014 } else { |
1015 DVLOG(1) << "Not sending notification: invalidator_ is NULL"; | 1015 DVLOG(1) << "Not sending invalidation: invalidator_ is NULL"; |
1016 } | 1016 } |
1017 } | 1017 } |
1018 } | 1018 } |
1019 | 1019 |
1020 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { | 1020 if (event.what_happened == SyncEngineEvent::STOP_SYNCING_PERMANENTLY) { |
1021 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, | 1021 FOR_EACH_OBSERVER(SyncManager::Observer, observers_, |
1022 OnStopSyncingPermanently()); | 1022 OnStopSyncingPermanently()); |
1023 return; | 1023 return; |
1024 } | 1024 } |
1025 | 1025 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1073 name, js_message_handler.Run(args)); | 1073 name, js_message_handler.Run(args)); |
1074 } | 1074 } |
1075 | 1075 |
1076 void SyncManagerImpl::BindJsMessageHandler( | 1076 void SyncManagerImpl::BindJsMessageHandler( |
1077 const std::string& name, | 1077 const std::string& name, |
1078 UnboundJsMessageHandler unbound_message_handler) { | 1078 UnboundJsMessageHandler unbound_message_handler) { |
1079 js_message_handlers_[name] = | 1079 js_message_handlers_[name] = |
1080 base::Bind(unbound_message_handler, base::Unretained(this)); | 1080 base::Bind(unbound_message_handler, base::Unretained(this)); |
1081 } | 1081 } |
1082 | 1082 |
1083 void SyncManagerImpl::OnNotificationStateChange( | |
1084 NotificationsDisabledReason reason) { | |
1085 const std::string& reason_str = NotificationsDisabledReasonToString(reason); | |
1086 notifications_disabled_reason_ = reason; | |
1087 DVLOG(1) << "Notification state changed to: " << reason_str; | |
1088 const bool notifications_enabled = | |
1089 (notifications_disabled_reason_ == NO_NOTIFICATION_ERROR); | |
1090 allstatus_.SetNotificationsEnabled(notifications_enabled); | |
1091 scheduler_->SetNotificationsEnabled(notifications_enabled); | |
1092 | |
1093 // TODO(akalin): Treat a CREDENTIALS_REJECTED state as an auth | |
1094 // error. | |
1095 | |
1096 if (js_event_handler_.IsInitialized()) { | |
1097 DictionaryValue details; | |
1098 details.Set("state", Value::CreateStringValue(reason_str)); | |
1099 js_event_handler_.Call(FROM_HERE, | |
1100 &JsEventHandler::HandleJsEvent, | |
1101 "onNotificationStateChange", | |
1102 JsEventDetails(&details)); | |
1103 } | |
1104 } | |
1105 | |
1106 DictionaryValue* SyncManagerImpl::NotificationInfoToValue( | 1083 DictionaryValue* SyncManagerImpl::NotificationInfoToValue( |
1107 const NotificationInfoMap& notification_info) { | 1084 const NotificationInfoMap& notification_info) { |
1108 DictionaryValue* value = new DictionaryValue(); | 1085 DictionaryValue* value = new DictionaryValue(); |
1109 | 1086 |
1110 for (NotificationInfoMap::const_iterator it = notification_info.begin(); | 1087 for (NotificationInfoMap::const_iterator it = notification_info.begin(); |
1111 it != notification_info.end(); ++it) { | 1088 it != notification_info.end(); ++it) { |
1112 const std::string& model_type_str = ModelTypeToString(it->first); | 1089 const std::string& model_type_str = ModelTypeToString(it->first); |
1113 value->Set(model_type_str, it->second.ToValue()); | 1090 value->Set(model_type_str, it->second.ToValue()); |
1114 } | 1091 } |
1115 | 1092 |
1116 return value; | 1093 return value; |
1117 } | 1094 } |
1118 | 1095 |
1119 std::string SyncManagerImpl::NotificationInfoToString( | 1096 std::string SyncManagerImpl::NotificationInfoToString( |
1120 const NotificationInfoMap& notification_info) { | 1097 const NotificationInfoMap& notification_info) { |
1121 scoped_ptr<DictionaryValue> value( | 1098 scoped_ptr<DictionaryValue> value( |
1122 NotificationInfoToValue(notification_info)); | 1099 NotificationInfoToValue(notification_info)); |
1123 std::string str; | 1100 std::string str; |
1124 base::JSONWriter::Write(value.get(), &str); | 1101 base::JSONWriter::Write(value.get(), &str); |
1125 return str; | 1102 return str; |
1126 } | 1103 } |
1127 | 1104 |
1128 JsArgList SyncManagerImpl::GetNotificationState( | 1105 JsArgList SyncManagerImpl::GetNotificationState( |
1129 const JsArgList& args) { | 1106 const JsArgList& args) { |
1130 const std::string& notification_state = | 1107 const std::string& notification_state = |
1131 NotificationsDisabledReasonToString(notifications_disabled_reason_); | 1108 InvalidatorStateToString(invalidator_state_); |
1132 DVLOG(1) << "GetNotificationState: " << notification_state; | 1109 DVLOG(1) << "GetNotificationState: " << notification_state; |
1133 ListValue return_args; | 1110 ListValue return_args; |
1134 return_args.Append(Value::CreateStringValue(notification_state)); | 1111 return_args.Append(Value::CreateStringValue(notification_state)); |
1135 return JsArgList(&return_args); | 1112 return JsArgList(&return_args); |
1136 } | 1113 } |
1137 | 1114 |
1138 JsArgList SyncManagerImpl::GetNotificationInfo( | 1115 JsArgList SyncManagerImpl::GetNotificationInfo( |
1139 const JsArgList& args) { | 1116 const JsArgList& args) { |
1140 DVLOG(1) << "GetNotificationInfo: " | 1117 DVLOG(1) << "GetNotificationInfo: " |
1141 << NotificationInfoToString(notification_info_map_); | 1118 << NotificationInfoToString(notification_info_map_); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1253 void SyncManagerImpl::UpdateNotificationInfo( | 1230 void SyncManagerImpl::UpdateNotificationInfo( |
1254 const ModelTypeStateMap& type_state_map) { | 1231 const ModelTypeStateMap& type_state_map) { |
1255 for (ModelTypeStateMap::const_iterator it = type_state_map.begin(); | 1232 for (ModelTypeStateMap::const_iterator it = type_state_map.begin(); |
1256 it != type_state_map.end(); ++it) { | 1233 it != type_state_map.end(); ++it) { |
1257 NotificationInfo* info = ¬ification_info_map_[it->first]; | 1234 NotificationInfo* info = ¬ification_info_map_[it->first]; |
1258 info->total_count++; | 1235 info->total_count++; |
1259 info->payload = it->second.payload; | 1236 info->payload = it->second.payload; |
1260 } | 1237 } |
1261 } | 1238 } |
1262 | 1239 |
1263 void SyncManagerImpl::OnNotificationsEnabled() { | 1240 void SyncManagerImpl::OnInvalidatorStateChange(InvalidatorState state) { |
1264 OnNotificationStateChange(NO_NOTIFICATION_ERROR); | 1241 const std::string& state_str = InvalidatorStateToString(state); |
| 1242 invalidator_state_ = state; |
| 1243 DVLOG(1) << "Invalidator state changed to: " << state_str; |
| 1244 const bool notifications_enabled = |
| 1245 (invalidator_state_ == INVALIDATIONS_ENABLED); |
| 1246 allstatus_.SetNotificationsEnabled(notifications_enabled); |
| 1247 scheduler_->SetNotificationsEnabled(notifications_enabled); |
| 1248 |
| 1249 // TODO(akalin): Treat a CREDENTIALS_REJECTED state as an auth |
| 1250 // error. |
| 1251 |
| 1252 if (js_event_handler_.IsInitialized()) { |
| 1253 DictionaryValue details; |
| 1254 details.SetString("state", state_str); |
| 1255 js_event_handler_.Call(FROM_HERE, |
| 1256 &JsEventHandler::HandleJsEvent, |
| 1257 "onNotificationStateChange", |
| 1258 JsEventDetails(&details)); |
| 1259 } |
1265 } | 1260 } |
1266 | 1261 |
1267 void SyncManagerImpl::OnNotificationsDisabled( | 1262 void SyncManagerImpl::OnIncomingInvalidation( |
1268 NotificationsDisabledReason reason) { | |
1269 OnNotificationStateChange(reason); | |
1270 } | |
1271 | |
1272 void SyncManagerImpl::OnIncomingNotification( | |
1273 const ObjectIdStateMap& id_state_map, | 1263 const ObjectIdStateMap& id_state_map, |
1274 IncomingNotificationSource source) { | 1264 IncomingInvalidationSource source) { |
1275 DCHECK(thread_checker_.CalledOnValidThread()); | 1265 DCHECK(thread_checker_.CalledOnValidThread()); |
1276 const ModelTypeStateMap& type_state_map = | 1266 const ModelTypeStateMap& type_state_map = |
1277 ObjectIdStateMapToModelTypeStateMap(id_state_map); | 1267 ObjectIdStateMapToModelTypeStateMap(id_state_map); |
1278 if (source == LOCAL_NOTIFICATION) { | 1268 if (source == LOCAL_INVALIDATION) { |
1279 scheduler_->ScheduleNudgeWithStatesAsync( | 1269 scheduler_->ScheduleNudgeWithStatesAsync( |
1280 TimeDelta::FromMilliseconds(kSyncRefreshDelayMsec), | 1270 TimeDelta::FromMilliseconds(kSyncRefreshDelayMsec), |
1281 NUDGE_SOURCE_LOCAL_REFRESH, | 1271 NUDGE_SOURCE_LOCAL_REFRESH, |
1282 type_state_map, FROM_HERE); | 1272 type_state_map, FROM_HERE); |
1283 } else if (!type_state_map.empty()) { | 1273 } else if (!type_state_map.empty()) { |
1284 scheduler_->ScheduleNudgeWithStatesAsync( | 1274 scheduler_->ScheduleNudgeWithStatesAsync( |
1285 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), | 1275 TimeDelta::FromMilliseconds(kSyncSchedulerDelayMsec), |
1286 NUDGE_SOURCE_NOTIFICATION, | 1276 NUDGE_SOURCE_NOTIFICATION, |
1287 type_state_map, FROM_HERE); | 1277 type_state_map, FROM_HERE); |
1288 allstatus_.IncrementNotificationsReceived(); | 1278 allstatus_.IncrementNotificationsReceived(); |
1289 UpdateNotificationInfo(type_state_map); | 1279 UpdateNotificationInfo(type_state_map); |
1290 debug_info_event_listener_.OnIncomingNotification(type_state_map); | 1280 debug_info_event_listener_.OnIncomingNotification(type_state_map); |
1291 } else { | 1281 } else { |
1292 LOG(WARNING) << "Sync received notification without any type information."; | 1282 LOG(WARNING) << "Sync received invalidation without any type information."; |
1293 } | 1283 } |
1294 | 1284 |
1295 if (js_event_handler_.IsInitialized()) { | 1285 if (js_event_handler_.IsInitialized()) { |
1296 DictionaryValue details; | 1286 DictionaryValue details; |
1297 ListValue* changed_types = new ListValue(); | 1287 ListValue* changed_types = new ListValue(); |
1298 details.Set("changedTypes", changed_types); | 1288 details.Set("changedTypes", changed_types); |
1299 for (ModelTypeStateMap::const_iterator it = type_state_map.begin(); | 1289 for (ModelTypeStateMap::const_iterator it = type_state_map.begin(); |
1300 it != type_state_map.end(); ++it) { | 1290 it != type_state_map.end(); ++it) { |
1301 const std::string& model_type_str = | 1291 const std::string& model_type_str = |
1302 ModelTypeToString(it->first); | 1292 ModelTypeToString(it->first); |
1303 changed_types->Append(Value::CreateStringValue(model_type_str)); | 1293 changed_types->Append(Value::CreateStringValue(model_type_str)); |
1304 } | 1294 } |
1305 details.SetString("source", (source == LOCAL_NOTIFICATION) ? | 1295 details.SetString("source", (source == LOCAL_INVALIDATION) ? |
1306 "LOCAL_NOTIFICATION" : "REMOTE_NOTIFICATION"); | 1296 "LOCAL_INVALIDATION" : "REMOTE_INVALIDATION"); |
1307 js_event_handler_.Call(FROM_HERE, | 1297 js_event_handler_.Call(FROM_HERE, |
1308 &JsEventHandler::HandleJsEvent, | 1298 &JsEventHandler::HandleJsEvent, |
1309 "onIncomingNotification", | 1299 "onIncomingNotification", |
1310 JsEventDetails(&details)); | 1300 JsEventDetails(&details)); |
1311 } | 1301 } |
1312 } | 1302 } |
1313 | 1303 |
1314 SyncStatus SyncManagerImpl::GetDetailedStatus() const { | 1304 SyncStatus SyncManagerImpl::GetDetailedStatus() const { |
1315 return allstatus_.status(); | 1305 return allstatus_.status(); |
1316 } | 1306 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1356 int SyncManagerImpl::GetDefaultNudgeDelay() { | 1346 int SyncManagerImpl::GetDefaultNudgeDelay() { |
1357 return kDefaultNudgeDelayMilliseconds; | 1347 return kDefaultNudgeDelayMilliseconds; |
1358 } | 1348 } |
1359 | 1349 |
1360 // static. | 1350 // static. |
1361 int SyncManagerImpl::GetPreferencesNudgeDelay() { | 1351 int SyncManagerImpl::GetPreferencesNudgeDelay() { |
1362 return kPreferencesNudgeDelayMilliseconds; | 1352 return kPreferencesNudgeDelayMilliseconds; |
1363 } | 1353 } |
1364 | 1354 |
1365 } // namespace syncer | 1355 } // namespace syncer |
OLD | NEW |