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