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/google_apis/drive_notification_manager.h" | 5 #include "chrome/browser/google_apis/drive_notification_manager.h" |
6 | 6 |
7 #include "chrome/browser/google_apis/drive_notification_observer.h" | 7 #include "chrome/browser/google_apis/drive_notification_observer.h" |
8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/sync/profile_sync_service.h" |
| 10 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 11 #include "google/cacheinvalidation/types.pb.h" |
9 | 12 |
10 namespace google_apis { | 13 namespace google_apis { |
11 | 14 |
12 // TODO(calvinlo): Constants for polling and XMPP sync mechanisms go here. | 15 // The sync invalidation object ID for Google Drive. |
| 16 const char kDriveInvalidationObjectId[] = "CHANGELOG"; |
| 17 |
| 18 // TODO(calvinlo): Constants for polling to go here. |
13 | 19 |
14 DriveNotificationManager::DriveNotificationManager(Profile* profile) | 20 DriveNotificationManager::DriveNotificationManager(Profile* profile) |
15 : profile_(profile) { | 21 : profile_(profile), |
16 // TODO(calvinlo): Initialize member variables for polling and XMPP here. | 22 push_notification_registered_(false), |
| 23 push_notification_enabled_(false) { |
| 24 // TODO(calvinlo): Initialize member variables for polling here. |
| 25 RegisterDriveNotifications(); |
17 } | 26 } |
18 | 27 |
19 DriveNotificationManager::~DriveNotificationManager() {} | 28 DriveNotificationManager::~DriveNotificationManager() {} |
20 | 29 |
21 void DriveNotificationManager::Shutdown() { | 30 void DriveNotificationManager::Shutdown() { |
22 // TODO(calvinlo): Unregister for Drive notifications goes here. | 31 // Unregister for Drive notifications. |
| 32 ProfileSyncService* profile_sync_service = |
| 33 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 34 if (!profile_sync_service || !push_notification_registered_) { |
| 35 return; |
| 36 } |
| 37 |
| 38 profile_sync_service->UpdateRegisteredInvalidationIds( |
| 39 this, syncer::ObjectIdSet()); |
| 40 profile_sync_service->UnregisterInvalidationHandler(this); |
23 } | 41 } |
24 | 42 |
25 // TODO(calvinlo): Implementations for syncer::InvalidationHandler go here. | 43 void DriveNotificationManager::OnInvalidatorStateChange( |
26 // Implement OnInvalidatorStateChange() and OnIncomingInvalidation(). | 44 syncer::InvalidatorState state) { |
| 45 SetPushNotificationEnabled(state); |
| 46 } |
| 47 |
| 48 void DriveNotificationManager::OnIncomingInvalidation( |
| 49 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| 50 DVLOG(2) << "XMPP Drive Notification Received"; |
| 51 DCHECK_EQ(1U, invalidation_map.size()); |
| 52 const invalidation::ObjectId object_id( |
| 53 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
| 54 kDriveInvalidationObjectId); |
| 55 DCHECK_EQ(1U, invalidation_map.count(object_id)); |
| 56 |
| 57 // TODO(dcheng): Only acknowledge the invalidation once the fetch has |
| 58 // completed. http://crbug.com/156843 |
| 59 ProfileSyncService* profile_sync_service = |
| 60 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 61 CHECK(profile_sync_service); |
| 62 profile_sync_service->AcknowledgeInvalidation( |
| 63 invalidation_map.begin()->first, |
| 64 invalidation_map.begin()->second.ack_handle); |
| 65 |
| 66 NotifyObserversToUpdate(); |
| 67 } |
27 | 68 |
28 void DriveNotificationManager::AddObserver( | 69 void DriveNotificationManager::AddObserver( |
29 DriveNotificationObserver* observer) { | 70 DriveNotificationObserver* observer) { |
30 observers_.AddObserver(observer); | 71 observers_.AddObserver(observer); |
31 } | 72 } |
32 | 73 |
33 void DriveNotificationManager::RemoveObserver( | 74 void DriveNotificationManager::RemoveObserver( |
34 DriveNotificationObserver* observer) { | 75 DriveNotificationObserver* observer) { |
35 observers_.RemoveObserver(observer); | 76 observers_.RemoveObserver(observer); |
36 } | 77 } |
37 | 78 |
38 void DriveNotificationManager::NotifyObserversToUpdate() { | 79 void DriveNotificationManager::NotifyObserversToUpdate() { |
39 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, CheckForUpdates()); | 80 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, CheckForUpdates()); |
40 } | 81 } |
41 | 82 |
| 83 // Register for Google Drive invalidation notifications through XMPP. |
| 84 void DriveNotificationManager::RegisterDriveNotifications() { |
| 85 // Push notification registration might have already occurred if called from |
| 86 // a different extension. |
| 87 if (!IsDriveNotificationSupported() || push_notification_registered_) |
| 88 return; |
| 89 |
| 90 ProfileSyncService* profile_sync_service = |
| 91 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 92 if (!profile_sync_service) |
| 93 return; |
| 94 |
| 95 profile_sync_service->RegisterInvalidationHandler(this); |
| 96 syncer::ObjectIdSet ids; |
| 97 ids.insert(invalidation::ObjectId( |
| 98 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
| 99 kDriveInvalidationObjectId)); |
| 100 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); |
| 101 push_notification_registered_ = true; |
| 102 SetPushNotificationEnabled(profile_sync_service->GetInvalidatorState()); |
| 103 } |
| 104 |
| 105 // TODO(calvinlo): Remove when all patches for http://crbug.com/173339 done. |
| 106 bool DriveNotificationManager::IsDriveNotificationSupported() { |
| 107 // TODO(calvinlo): A invalidation ID can only be registered to one handler. |
| 108 // Therefore ChromeOS and SyncFS cannot both use XMPP notifications until |
| 109 // (http://crbug.com/173339) is completed. |
| 110 // For now, disable XMPP notifications for SyncFC on ChromeOS to guarantee |
| 111 // that ChromeOS's file manager can register itself to the invalidationID. |
| 112 |
| 113 #if defined(OS_CHROMEOS) |
| 114 return false; |
| 115 #else |
| 116 return true; |
| 117 #endif |
| 118 } |
| 119 |
| 120 void DriveNotificationManager::SetPushNotificationEnabled( |
| 121 syncer::InvalidatorState state) { |
| 122 DVLOG(1) << "SetPushNotificationEnabled() with state=" << state; |
| 123 push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
| 124 if (!push_notification_enabled_) |
| 125 return; |
| 126 |
| 127 // Push notifications are enabled so reset polling timer. |
| 128 //UpdatePollingDelay(kPollingDelaySecondsWithNotification); |
| 129 } |
| 130 |
42 } // namespace google_apis | 131 } // namespace google_apis |
OLD | NEW |