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" | 9 #include "chrome/browser/sync/profile_sync_service.h" |
10 #include "chrome/browser/sync/profile_sync_service_factory.h" | 10 #include "chrome/browser/sync/profile_sync_service_factory.h" |
11 #include "google/cacheinvalidation/types.pb.h" | 11 #include "google/cacheinvalidation/types.pb.h" |
12 | 12 |
13 namespace google_apis { | 13 namespace google_apis { |
14 | 14 |
15 // The sync invalidation object ID for Google Drive. | 15 // The sync invalidation object ID for Google Drive. |
16 const char kDriveInvalidationObjectId[] = "CHANGELOG"; | 16 const char kDriveInvalidationObjectId[] = "CHANGELOG"; |
17 | 17 |
18 // TODO(calvinlo): Constants for polling to go here. | |
19 | |
20 DriveNotificationManager::DriveNotificationManager(Profile* profile) | 18 DriveNotificationManager::DriveNotificationManager(Profile* profile) |
21 : profile_(profile), | 19 : profile_(profile), |
22 push_notification_registered_(false), | 20 push_notification_registered_(false), |
23 push_notification_enabled_(false) { | 21 push_notification_enabled_(false) { |
24 // TODO(calvinlo): Initialize member variables for polling here. | 22 ProfileSyncService* profile_sync_service_ = |
| 23 ProfileSyncServiceFactory::GetForProfile(profile_); |
| 24 CHECK(profile_sync_service_); |
| 25 |
25 RegisterDriveNotifications(); | 26 RegisterDriveNotifications(); |
26 } | 27 } |
27 | 28 |
28 DriveNotificationManager::~DriveNotificationManager() {} | 29 DriveNotificationManager::~DriveNotificationManager() {} |
29 | 30 |
30 void DriveNotificationManager::Shutdown() { | 31 void DriveNotificationManager::Shutdown() { |
31 // Unregister for Drive notifications. | 32 // Unregister for Drive notifications. |
32 ProfileSyncService* profile_sync_service = | 33 ProfileSyncService* profile_sync_service = |
33 ProfileSyncServiceFactory::GetForProfile(profile_); | 34 ProfileSyncServiceFactory::GetForProfile(profile_); |
34 if (!profile_sync_service || !push_notification_registered_) { | 35 if (!profile_sync_service || !push_notification_registered_) { |
35 return; | 36 return; |
36 } | 37 } |
37 | 38 |
38 profile_sync_service->UpdateRegisteredInvalidationIds( | 39 profile_sync_service->UpdateRegisteredInvalidationIds( |
39 this, syncer::ObjectIdSet()); | 40 this, syncer::ObjectIdSet()); |
40 profile_sync_service->UnregisterInvalidationHandler(this); | 41 profile_sync_service->UnregisterInvalidationHandler(this); |
41 } | 42 } |
42 | 43 |
43 void DriveNotificationManager::OnInvalidatorStateChange( | 44 void DriveNotificationManager::OnInvalidatorStateChange( |
44 syncer::InvalidatorState state) { | 45 syncer::InvalidatorState state) { |
45 SetPushNotificationEnabled(state); | 46 push_notification_enabled_ = (state == syncer::INVALIDATIONS_ENABLED); |
| 47 if (push_notification_enabled_) { |
| 48 DVLOG(1) << "XMPP Notifications enabled"; |
| 49 } else { |
| 50 DVLOG(1) << "XMPP Notifications disabled (state=" << state << ")"; |
| 51 } |
46 } | 52 } |
47 | 53 |
48 void DriveNotificationManager::OnIncomingInvalidation( | 54 void DriveNotificationManager::OnIncomingInvalidation( |
49 const syncer::ObjectIdInvalidationMap& invalidation_map) { | 55 const syncer::ObjectIdInvalidationMap& invalidation_map) { |
50 DVLOG(2) << "XMPP Drive Notification Received"; | 56 DVLOG(2) << "XMPP Drive Notification Received"; |
51 DCHECK_EQ(1U, invalidation_map.size()); | 57 DCHECK_EQ(1U, invalidation_map.size()); |
52 const invalidation::ObjectId object_id( | 58 const invalidation::ObjectId object_id( |
53 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | 59 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
54 kDriveInvalidationObjectId); | 60 kDriveInvalidationObjectId); |
55 DCHECK_EQ(1U, invalidation_map.count(object_id)); | 61 DCHECK_EQ(1U, invalidation_map.count(object_id)); |
56 | 62 |
57 // TODO(dcheng): Only acknowledge the invalidation once the fetch has | 63 // TODO(dcheng): Only acknowledge the invalidation once the fetch has |
58 // completed. http://crbug.com/156843 | 64 // completed. http://crbug.com/156843 |
59 ProfileSyncService* profile_sync_service = | 65 profile_sync_service_->AcknowledgeInvalidation( |
60 ProfileSyncServiceFactory::GetForProfile(profile_); | |
61 CHECK(profile_sync_service); | |
62 profile_sync_service->AcknowledgeInvalidation( | |
63 invalidation_map.begin()->first, | 66 invalidation_map.begin()->first, |
64 invalidation_map.begin()->second.ack_handle); | 67 invalidation_map.begin()->second.ack_handle); |
65 | 68 |
66 NotifyObserversToUpdate(); | 69 NotifyObserversToUpdate(); |
67 } | 70 } |
68 | 71 |
69 void DriveNotificationManager::AddObserver( | 72 void DriveNotificationManager::AddObserver( |
70 DriveNotificationObserver* observer) { | 73 DriveNotificationObserver* observer) { |
71 observers_.AddObserver(observer); | 74 observers_.AddObserver(observer); |
72 } | 75 } |
73 | 76 |
74 void DriveNotificationManager::RemoveObserver( | 77 void DriveNotificationManager::RemoveObserver( |
75 DriveNotificationObserver* observer) { | 78 DriveNotificationObserver* observer) { |
76 observers_.RemoveObserver(observer); | 79 observers_.RemoveObserver(observer); |
77 } | 80 } |
78 | 81 |
| 82 bool DriveNotificationManager::IsPushNotificationEnabled() { |
| 83 return push_notification_enabled_; |
| 84 } |
| 85 |
79 void DriveNotificationManager::NotifyObserversToUpdate() { | 86 void DriveNotificationManager::NotifyObserversToUpdate() { |
80 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, CheckForUpdates()); | 87 FOR_EACH_OBSERVER(DriveNotificationObserver, observers_, |
| 88 OnNotificationReceived()); |
81 } | 89 } |
82 | 90 |
83 // Register for Google Drive invalidation notifications through XMPP. | 91 // Register for Google Drive invalidation notifications through XMPP. |
84 void DriveNotificationManager::RegisterDriveNotifications() { | 92 void DriveNotificationManager::RegisterDriveNotifications() { |
85 // Push notification registration might have already occurred if called from | 93 // Push notification registration might have already occurred if called from |
86 // a different extension. | 94 // a different extension. |
87 if (!IsDriveNotificationSupported() || push_notification_registered_) | 95 if (push_notification_registered_) |
88 return; | 96 return; |
89 | 97 |
90 ProfileSyncService* profile_sync_service = | 98 ProfileSyncService* profile_sync_service = |
91 ProfileSyncServiceFactory::GetForProfile(profile_); | 99 ProfileSyncServiceFactory::GetForProfile(profile_); |
92 if (!profile_sync_service) | 100 if (!profile_sync_service) |
93 return; | 101 return; |
94 | 102 |
95 profile_sync_service->RegisterInvalidationHandler(this); | 103 profile_sync_service->RegisterInvalidationHandler(this); |
96 syncer::ObjectIdSet ids; | 104 syncer::ObjectIdSet ids; |
97 ids.insert(invalidation::ObjectId( | 105 ids.insert(invalidation::ObjectId( |
98 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, | 106 ipc::invalidation::ObjectSource::COSMO_CHANGELOG, |
99 kDriveInvalidationObjectId)); | 107 kDriveInvalidationObjectId)); |
100 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); | 108 profile_sync_service->UpdateRegisteredInvalidationIds(this, ids); |
101 push_notification_registered_ = true; | 109 push_notification_registered_ = true; |
102 SetPushNotificationEnabled(profile_sync_service->GetInvalidatorState()); | 110 OnInvalidatorStateChange(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 } | 111 } |
130 | 112 |
131 } // namespace google_apis | 113 } // namespace google_apis |
OLD | NEW |