| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/drive/drive_notification_manager.h" | 5 #include "chrome/browser/drive/drive_notification_manager.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/browser/drive/drive_notification_observer.h" | 8 #include "chrome/browser/drive/drive_notification_observer.h" |
| 9 #include "chrome/browser/invalidation/invalidation_service.h" | 9 #include "chrome/browser/invalidation/invalidation_service.h" |
| 10 #include "chrome/browser/invalidation/invalidation_service_factory.h" | 10 #include "chrome/browser/invalidation/invalidation_service_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "google/cacheinvalidation/types.pb.h" | 11 #include "google/cacheinvalidation/types.pb.h" |
| 13 | 12 |
| 14 namespace drive { | 13 namespace drive { |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 // The polling interval time is used when XMPP is disabled. | 17 // The polling interval time is used when XMPP is disabled. |
| 19 const int kFastPollingIntervalInSecs = 60; | 18 const int kFastPollingIntervalInSecs = 60; |
| 20 | 19 |
| 21 // The polling interval time is used when XMPP is enabled. Theoretically | 20 // The polling interval time is used when XMPP is enabled. Theoretically |
| 22 // polling should be unnecessary if XMPP is enabled, but just in case. | 21 // polling should be unnecessary if XMPP is enabled, but just in case. |
| 23 const int kSlowPollingIntervalInSecs = 300; | 22 const int kSlowPollingIntervalInSecs = 300; |
| 24 | 23 |
| 25 // The sync invalidation object ID for Google Drive. | 24 // The sync invalidation object ID for Google Drive. |
| 26 const char kDriveInvalidationObjectId[] = "CHANGELOG"; | 25 const char kDriveInvalidationObjectId[] = "CHANGELOG"; |
| 27 | 26 |
| 28 } // namespace | 27 } // namespace |
| 29 | 28 |
| 30 DriveNotificationManager::DriveNotificationManager(Profile* profile) | 29 DriveNotificationManager::DriveNotificationManager( |
| 31 : profile_(profile), | 30 invalidation::InvalidationService* invalidation_service) |
| 31 : invalidation_service_(invalidation_service), |
| 32 push_notification_registered_(false), | 32 push_notification_registered_(false), |
| 33 push_notification_enabled_(false), | 33 push_notification_enabled_(false), |
| 34 observers_notified_(false), | 34 observers_notified_(false), |
| 35 polling_timer_(true /* retain_user_task */, false /* is_repeating */), | 35 polling_timer_(true /* retain_user_task */, false /* is_repeating */), |
| 36 weak_ptr_factory_(this) { | 36 weak_ptr_factory_(this) { |
| 37 DCHECK(invalidation_service_); |
| 37 RegisterDriveNotifications(); | 38 RegisterDriveNotifications(); |
| 38 RestartPollingTimer(); | 39 RestartPollingTimer(); |
| 39 } | 40 } |
| 40 | 41 |
| 41 DriveNotificationManager::~DriveNotificationManager() {} | 42 DriveNotificationManager::~DriveNotificationManager() {} |
| 42 | 43 |
| 43 void DriveNotificationManager::Shutdown() { | 44 void DriveNotificationManager::Shutdown() { |
| 44 // Unregister for Drive notifications. | 45 // Unregister for Drive notifications. |
| 45 invalidation::InvalidationService* invalidation_service = | 46 if (!invalidation_service_ || !push_notification_registered_) |
| 46 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
| 47 if (!invalidation_service || !push_notification_registered_) { | |
| 48 return; | 47 return; |
| 49 } | |
| 50 | 48 |
| 51 // We unregister the handler without updating unregistering our IDs on | 49 // We unregister the handler without updating unregistering our IDs on |
| 52 // purpose. See the class comment on the InvalidationService interface for | 50 // purpose. See the class comment on the InvalidationService interface for |
| 53 // more information. | 51 // more information. |
| 54 invalidation_service->UnregisterInvalidationHandler(this); | 52 invalidation_service_->UnregisterInvalidationHandler(this); |
| 53 invalidation_service_ = NULL; |
| 55 } | 54 } |
| 56 | 55 |
| 57 void DriveNotificationManager::OnInvalidatorStateChange( | 56 void DriveNotificationManager::OnInvalidatorStateChange( |
| 58 syncer::InvalidatorState state) { | 57 syncer::InvalidatorState state) { |
| 59 push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); | 58 push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
| 60 if (push_notification_enabled_) { | 59 if (push_notification_enabled_) { |
| 61 DVLOG(1) << "XMPP Notifications enabled"; | 60 DVLOG(1) << "XMPP Notifications enabled"; |
| 62 } else { | 61 } else { |
| 63 DVLOG(1) << "XMPP Notifications disabled (state=" << state << ")"; | 62 DVLOG(1) << "XMPP Notifications disabled (state=" << state << ")"; |
| 64 } | 63 } |
| 65 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, | 64 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, |
| 66 OnPushNotificationEnabled(push_notification_enabled_)); | 65 OnPushNotificationEnabled(push_notification_enabled_)); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void DriveNotificationManager::OnIncomingInvalidation( | 68 void DriveNotificationManager::OnIncomingInvalidation( |
| 70 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 69 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 71 DVLOG(2) << "XMPP Drive Notification Received"; | 70 DVLOG(2) << "XMPP Drive Notification Received"; |
| 72 DCHECK_EQ(1U, invalidation_map.size()); | 71 DCHECK_EQ(1U, invalidation_map.size()); |
| 73 const invalidation::ObjectId object_id( | 72 const invalidation::ObjectId object_id( |
| 74 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | 73 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
| 75 kDriveInvalidationObjectId); | 74 kDriveInvalidationObjectId); |
| 76 DCHECK_EQ(1U, invalidation_map.count(object_id)); | 75 DCHECK_EQ(1U, invalidation_map.count(object_id)); |
| 77 | 76 |
| 78 // TODO(dcheng): Only acknowledge the invalidation once the fetch has | 77 // TODO(dcheng): Only acknowledge the invalidation once the fetch has |
| 79 // completed. http://crbug.com/156843 | 78 // completed. http://crbug.com/156843 |
| 80 invalidation::InvalidationService* invalidation_service = | 79 DCHECK(invalidation_service_); |
| 81 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | 80 invalidation_service_->AcknowledgeInvalidation( |
| 82 DCHECK(invalidation_service); | |
| 83 invalidation_service->AcknowledgeInvalidation( | |
| 84 invalidation_map.begin()->first, | 81 invalidation_map.begin()->first, |
| 85 invalidation_map.begin()->second.ack_handle); | 82 invalidation_map.begin()->second.ack_handle); |
| 86 | 83 |
| 87 NotifyObserversToUpdate(NOTIFICATION_XMPP); | 84 NotifyObserversToUpdate(NOTIFICATION_XMPP); |
| 88 } | 85 } |
| 89 | 86 |
| 90 void DriveNotificationManager::AddObserver( | 87 void DriveNotificationManager::AddObserver( |
| 91 DriveNotificationObserver* observer) { | 88 DriveNotificationObserver* observer) { |
| 92 observers_.AddObserver(observer); | 89 observers_.AddObserver(observer); |
| 93 } | 90 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 123 | 120 |
| 124 // Note that polling_timer_ is not a repeating timer. Restarting manually | 121 // Note that polling_timer_ is not a repeating timer. Restarting manually |
| 125 // here is better as XMPP may be received right before the polling timer is | 122 // here is better as XMPP may be received right before the polling timer is |
| 126 // fired (i.e. we don't notify observers twice in a row). | 123 // fired (i.e. we don't notify observers twice in a row). |
| 127 RestartPollingTimer(); | 124 RestartPollingTimer(); |
| 128 } | 125 } |
| 129 | 126 |
| 130 void DriveNotificationManager::RegisterDriveNotifications() { | 127 void DriveNotificationManager::RegisterDriveNotifications() { |
| 131 DCHECK(!push_notification_enabled_); | 128 DCHECK(!push_notification_enabled_); |
| 132 | 129 |
| 133 invalidation::InvalidationService* invalidation_service = | 130 if (!invalidation_service_) |
| 134 invalidation::InvalidationServiceFactory::GetForProfile(profile_); | |
| 135 if (!invalidation_service) | |
| 136 return; | 131 return; |
| 137 | 132 |
| 138 invalidation_service->RegisterInvalidationHandler(this); | 133 invalidation_service_->RegisterInvalidationHandler(this); |
| 139 syncer::ObjectIdSet ids; | 134 syncer::ObjectIdSet ids; |
| 140 ids.insert(invalidation::ObjectId( | 135 ids.insert(invalidation::ObjectId( |
| 141 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | 136 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
| 142 kDriveInvalidationObjectId)); | 137 kDriveInvalidationObjectId)); |
| 143 invalidation_service->UpdateRegisteredInvalidationIds(this, ids); | 138 invalidation_service_->UpdateRegisteredInvalidationIds(this, ids); |
| 144 push_notification_registered_ = true; | 139 push_notification_registered_ = true; |
| 145 OnInvalidatorStateChange(invalidation_service->GetInvalidatorState()); | 140 OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState()); |
| 146 | 141 |
| 147 UMA_HISTOGRAM_BOOLEAN("Drive.PushNotificationRegistered", | 142 UMA_HISTOGRAM_BOOLEAN("Drive.PushNotificationRegistered", |
| 148 push_notification_registered_); | 143 push_notification_registered_); |
| 149 } | 144 } |
| 150 | 145 |
| 151 // static | 146 // static |
| 152 std::string DriveNotificationManager::NotificationSourceToString( | 147 std::string DriveNotificationManager::NotificationSourceToString( |
| 153 NotificationSource source) { | 148 NotificationSource source) { |
| 154 switch (source) { | 149 switch (source) { |
| 155 case NOTIFICATION_XMPP: | 150 case NOTIFICATION_XMPP: |
| 156 return "NOTIFICATION_XMPP"; | 151 return "NOTIFICATION_XMPP"; |
| 157 case NOTIFICATION_POLLING: | 152 case NOTIFICATION_POLLING: |
| 158 return "NOTIFICATION_POLLING"; | 153 return "NOTIFICATION_POLLING"; |
| 159 } | 154 } |
| 160 | 155 |
| 161 NOTREACHED(); | 156 NOTREACHED(); |
| 162 return ""; | 157 return ""; |
| 163 } | 158 } |
| 164 | 159 |
| 165 } // namespace drive | 160 } // namespace drive |
| OLD | NEW |