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