This source file includes following definitions.
- weak_ptr_factory_
- Shutdown
- OnInvalidatorStateChange
- OnIncomingInvalidation
- GetOwnerName
- AddObserver
- RemoveObserver
- RestartPollingTimer
- NotifyObserversToUpdate
- RegisterDriveNotifications
- NotificationSourceToString
#include "chrome/browser/drive/drive_notification_manager.h"
#include "base/metrics/histogram.h"
#include "chrome/browser/drive/drive_notification_observer.h"
#include "chrome/browser/invalidation/invalidation_service.h"
#include "chrome/browser/invalidation/invalidation_service_factory.h"
#include "google/cacheinvalidation/types.pb.h"
#include "sync/notifier/object_id_invalidation_map.h"
namespace drive {
namespace {
const int kFastPollingIntervalInSecs = 60;
const int kSlowPollingIntervalInSecs = 300;
const char kDriveInvalidationObjectId[] = "CHANGELOG";
}
DriveNotificationManager::DriveNotificationManager(
invalidation::InvalidationService* invalidation_service)
: invalidation_service_(invalidation_service),
push_notification_registered_(false),
push_notification_enabled_(false),
observers_notified_(false),
polling_timer_(true , false ),
weak_ptr_factory_(this) {
DCHECK(invalidation_service_);
RegisterDriveNotifications();
RestartPollingTimer();
}
DriveNotificationManager::~DriveNotificationManager() {}
void DriveNotificationManager::Shutdown() {
if (!invalidation_service_ || !push_notification_registered_)
return;
invalidation_service_->UnregisterInvalidationHandler(this);
invalidation_service_ = NULL;
}
void DriveNotificationManager::OnInvalidatorStateChange(
syncer::InvalidatorState state) {
push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED);
if (push_notification_enabled_) {
DVLOG(1) << "XMPP Notifications enabled";
} else {
DVLOG(1) << "XMPP Notifications disabled (state=" << state << ")";
}
FOR_EACH_OBSERVER(DriveNotificationObserver, observers_,
OnPushNotificationEnabled(push_notification_enabled_));
}
void DriveNotificationManager::OnIncomingInvalidation(
const syncer::ObjectIdInvalidationMap& invalidation_map) {
DVLOG(2) << "XMPP Drive Notification Received";
syncer::ObjectIdSet ids = invalidation_map.GetObjectIds();
DCHECK_EQ(1U, ids.size());
const invalidation::ObjectId object_id(
ipc::invalidation::ObjectSource::COSMO_CHANGELOG,
kDriveInvalidationObjectId);
DCHECK_EQ(1U, ids.count(object_id));
invalidation_map.AcknowledgeAll();
NotifyObserversToUpdate(NOTIFICATION_XMPP);
}
std::string DriveNotificationManager::GetOwnerName() const { return "Drive"; }
void DriveNotificationManager::AddObserver(
DriveNotificationObserver* observer) {
observers_.AddObserver(observer);
}
void DriveNotificationManager::RemoveObserver(
DriveNotificationObserver* observer) {
observers_.RemoveObserver(observer);
}
void DriveNotificationManager::RestartPollingTimer() {
const int interval_secs = (push_notification_enabled_ ?
kSlowPollingIntervalInSecs :
kFastPollingIntervalInSecs);
polling_timer_.Stop();
polling_timer_.Start(
FROM_HERE,
base::TimeDelta::FromSeconds(interval_secs),
base::Bind(&DriveNotificationManager::NotifyObserversToUpdate,
weak_ptr_factory_.GetWeakPtr(),
NOTIFICATION_POLLING));
}
void DriveNotificationManager::NotifyObserversToUpdate(
NotificationSource source) {
DVLOG(1) << "Notifying observers: " << NotificationSourceToString(source);
FOR_EACH_OBSERVER(DriveNotificationObserver, observers_,
OnNotificationReceived());
if (!observers_notified_) {
UMA_HISTOGRAM_BOOLEAN("Drive.PushNotificationInitiallyEnabled",
push_notification_enabled_);
}
observers_notified_ = true;
RestartPollingTimer();
}
void DriveNotificationManager::RegisterDriveNotifications() {
DCHECK(!push_notification_enabled_);
if (!invalidation_service_)
return;
invalidation_service_->RegisterInvalidationHandler(this);
syncer::ObjectIdSet ids;
ids.insert(invalidation::ObjectId(
ipc::invalidation::ObjectSource::COSMO_CHANGELOG,
kDriveInvalidationObjectId));
invalidation_service_->UpdateRegisteredInvalidationIds(this, ids);
push_notification_registered_ = true;
OnInvalidatorStateChange(invalidation_service_->GetInvalidatorState());
UMA_HISTOGRAM_BOOLEAN("Drive.PushNotificationRegistered",
push_notification_registered_);
}
std::string DriveNotificationManager::NotificationSourceToString(
NotificationSource source) {
switch (source) {
case NOTIFICATION_XMPP:
return "NOTIFICATION_XMPP";
case NOTIFICATION_POLLING:
return "NOTIFICATION_POLLING";
}
NOTREACHED();
return "";
}
}