Index: chrome/browser/google_apis/drive_notification_manager.cc |
diff --git a/chrome/browser/google_apis/drive_notification_manager.cc b/chrome/browser/google_apis/drive_notification_manager.cc |
index 7ce0235d8a3a258e147aa208dd81bdd0c21bd058..198b87ac09553b2623c46355c00c4c98ac5ce4fb 100644 |
--- a/chrome/browser/google_apis/drive_notification_manager.cc |
+++ b/chrome/browser/google_apis/drive_notification_manager.cc |
@@ -4,6 +4,7 @@ |
#include "chrome/browser/google_apis/drive_notification_manager.h" |
+#include "base/memory/weak_ptr.h" |
#include "chrome/browser/google_apis/drive_notification_observer.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/sync/profile_sync_service.h" |
@@ -15,13 +16,23 @@ namespace google_apis { |
// The sync invalidation object ID for Google Drive. |
const char kDriveInvalidationObjectId[] = "CHANGELOG"; |
-// TODO(calvinlo): Constants for polling to go here. |
+// Incremental sync polling interval constants. Polling used when XMPP |
+// notifications are off and also with a long delay as a backup mechanism for |
+// possibly dropped XMPP notifications. |
+const int64 kPollingIntervalSeconds = 5; |
+const int64 kPollingIntervalSecondsWithXMPP = 4 * 60 * 60; // 4 hr |
DriveNotificationManager::DriveNotificationManager(Profile* profile) |
: profile_(profile), |
push_notification_registered_(false), |
push_notification_enabled_(false) { |
- // TODO(calvinlo): Initialize member variables for polling here. |
+ ProfileSyncService* profile_sync_service_ = |
+ ProfileSyncServiceFactory::GetForProfile(profile_); |
+ CHECK(profile_sync_service_); |
+ |
+ // Initially set polling timer as if XMPP notifications are off. Once XMPP is |
+ // enabled, it will adjust the polling period appropriately. |
+ SchedulePolling(kPollingIntervalSeconds); |
RegisterDriveNotifications(); |
} |
@@ -42,7 +53,14 @@ void DriveNotificationManager::Shutdown() { |
void DriveNotificationManager::OnInvalidatorStateChange( |
syncer::InvalidatorState state) { |
- SetPushNotificationEnabled(state); |
+ push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
+ if (push_notification_enabled_) { |
+ DVLOG(1) << "XMPP Notifications enabled"; |
+ SchedulePolling(kPollingIntervalSecondsWithXMPP); |
+ } else { |
+ DVLOG(1) << "XMPP Notifications disabled (state=" << state << ")"; |
+ SchedulePolling(kPollingIntervalSeconds); |
+ } |
} |
void DriveNotificationManager::OnIncomingInvalidation( |
@@ -56,10 +74,7 @@ void DriveNotificationManager::OnIncomingInvalidation( |
// TODO(dcheng): Only acknowledge the invalidation once the fetch has |
// completed. http://crbug.com/156843 |
- ProfileSyncService* profile_sync_service = |
- ProfileSyncServiceFactory::GetForProfile(profile_); |
- CHECK(profile_sync_service); |
- profile_sync_service->AcknowledgeInvalidation( |
+ profile_sync_service_->AcknowledgeInvalidation( |
invalidation_map.begin()->first, |
invalidation_map.begin()->second.ack_handle); |
@@ -76,6 +91,10 @@ void DriveNotificationManager::RemoveObserver( |
observers_.RemoveObserver(observer); |
} |
+bool DriveNotificationManager::IsPushNotificationEnabled() { |
+ return push_notification_enabled_; |
+} |
+ |
void DriveNotificationManager::NotifyObserversToUpdate() { |
FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, CheckForUpdates()); |
} |
@@ -84,7 +103,7 @@ void DriveNotificationManager::NotifyObserversToUpdate() { |
void DriveNotificationManager::RegisterDriveNotifications() { |
// Push notification registration might have already occurred if called from |
// a different extension. |
- if (!IsDriveNotificationSupported() || push_notification_registered_) |
+ if (push_notification_registered_) |
return; |
ProfileSyncService* profile_sync_service = |
@@ -99,33 +118,18 @@ void DriveNotificationManager::RegisterDriveNotifications() { |
kDriveInvalidationObjectId)); |
profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); |
push_notification_registered_ = true; |
- SetPushNotificationEnabled(profile_sync_service->GetInvalidatorState()); |
-} |
- |
-// TODO(calvinlo): Remove when all patches for http://crbug.com/173339 done. |
-bool DriveNotificationManager::IsDriveNotificationSupported() { |
- // TODO(calvinlo): A invalidation ID can only be registered to one handler. |
- // Therefore ChromeOS and SyncFS cannot both use XMPP notifications until |
- // (http://crbug.com/173339) is completed. |
- // For now, disable XMPP notifications for SyncFC on ChromeOS to guarantee |
- // that ChromeOS's file manager can register itself to the invalidationID. |
- |
-#if defined(OS_CHROMEOS) |
- return false; |
-#else |
- return true; |
-#endif |
+ OnInvalidatorStateChange(profile_sync_service->GetInvalidatorState()); |
} |
-void DriveNotificationManager::SetPushNotificationEnabled( |
- syncer::InvalidatorState state) { |
- DVLOG(1) << "SetPushNotificationEnabled() with state=" << state; |
- push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
- if (!push_notification_enabled_) |
- return; |
- |
- // Push notifications are enabled so reset polling timer. |
- //UpdatePollingDelay(kPollingDelaySecondsWithNotification); |
+void DriveNotificationManager::SchedulePolling(int64 delay_sec) { |
+ DCHECK_GT(delay_sec, 0); |
+ std::string schedule_type = (polling_timer_.IsRunning()) ? |
+ "updated" : "started"; |
+ DVLOG(1) << "Polling " << schedule_type << " (delay=" << delay_sec << "s)"; |
+ polling_timer_.Start( |
+ FROM_HERE, base::TimeDelta::FromSeconds(delay_sec), |
+ base::Bind(&DriveNotificationManager::NotifyObserversToUpdate, |
+ AsWeakPtr())); |
} |
} // namespace google_apis |