| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/chromeos/policy/affiliated_invalidation_service_provide
r.h" | 5 #include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provide
r.h" |
| 6 | 6 |
| 7 #include <vector> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/browser_process_platform_part_chromeos.h" | |
| 12 #include "chrome/browser/chrome_notification_types.h" | |
| 13 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 14 #include "chrome/browser/chromeos/policy/ticl_device_settings_provider.h" | |
| 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
| 16 #include "chrome/browser/chromeos/settings/device_identity_provider.h" | |
| 17 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h
" | |
| 18 #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/profiles/profile_manager.h" | |
| 21 #include "chrome/common/chrome_content_client.h" | |
| 22 #include "components/invalidation/invalidation_handler.h" | |
| 23 #include "components/invalidation/invalidation_service.h" | |
| 24 #include "components/invalidation/invalidation_state_tracker.h" | |
| 25 #include "components/invalidation/invalidator_state.h" | |
| 26 #include "components/invalidation/invalidator_storage.h" | |
| 27 #include "components/invalidation/profile_invalidation_provider.h" | |
| 28 #include "components/invalidation/ticl_invalidation_service.h" | |
| 29 #include "components/invalidation/ticl_settings_provider.h" | |
| 30 #include "components/policy/core/common/cloud/cloud_policy_constants.h" | |
| 31 #include "components/user_manager/user.h" | |
| 32 #include "content/public/browser/notification_details.h" | |
| 33 #include "content/public/browser/notification_service.h" | |
| 34 #include "google_apis/gaia/identity_provider.h" | |
| 35 | |
| 36 namespace policy { | 7 namespace policy { |
| 37 | 8 |
| 38 AffiliatedInvalidationServiceProvider::Consumer::~Consumer() { | 9 AffiliatedInvalidationServiceProvider::Consumer::~Consumer() { |
| 39 } | 10 } |
| 40 | 11 |
| 41 class AffiliatedInvalidationServiceProvider::InvalidationServiceObserver | 12 AffiliatedInvalidationServiceProvider::AffiliatedInvalidationServiceProvider() { |
| 42 : public syncer::InvalidationHandler { | |
| 43 public: | |
| 44 explicit InvalidationServiceObserver( | |
| 45 AffiliatedInvalidationServiceProvider* parent, | |
| 46 invalidation::InvalidationService* invalidation_service); | |
| 47 ~InvalidationServiceObserver() override; | |
| 48 | |
| 49 invalidation::InvalidationService* GetInvalidationService(); | |
| 50 bool IsServiceConnected() const; | |
| 51 | |
| 52 // public syncer::InvalidationHandler: | |
| 53 void OnInvalidatorStateChange(syncer::InvalidatorState state) override; | |
| 54 void OnIncomingInvalidation( | |
| 55 const syncer::ObjectIdInvalidationMap& invalidation_map) override; | |
| 56 std::string GetOwnerName() const override; | |
| 57 | |
| 58 private: | |
| 59 AffiliatedInvalidationServiceProvider* parent_; | |
| 60 invalidation::InvalidationService* invalidation_service_; | |
| 61 bool is_service_connected_; | |
| 62 bool is_observer_ready_; | |
| 63 | |
| 64 DISALLOW_COPY_AND_ASSIGN(InvalidationServiceObserver); | |
| 65 }; | |
| 66 | |
| 67 AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 68 InvalidationServiceObserver( | |
| 69 AffiliatedInvalidationServiceProvider* parent, | |
| 70 invalidation::InvalidationService* invalidation_service) | |
| 71 : parent_(parent), | |
| 72 invalidation_service_(invalidation_service), | |
| 73 is_service_connected_(false), | |
| 74 is_observer_ready_(false) { | |
| 75 invalidation_service_->RegisterInvalidationHandler(this); | |
| 76 is_service_connected_ = invalidation_service->GetInvalidatorState() == | |
| 77 syncer::INVALIDATIONS_ENABLED; | |
| 78 is_observer_ready_ = true; | |
| 79 } | |
| 80 | |
| 81 AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 82 ~InvalidationServiceObserver() { | |
| 83 is_observer_ready_ = false; | |
| 84 invalidation_service_->UnregisterInvalidationHandler(this); | |
| 85 } | |
| 86 | |
| 87 invalidation::InvalidationService* | |
| 88 AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 89 GetInvalidationService() { | |
| 90 return invalidation_service_; | |
| 91 } | |
| 92 | |
| 93 bool AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 94 IsServiceConnected() const { | |
| 95 return is_service_connected_; | |
| 96 } | |
| 97 | |
| 98 void AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 99 OnInvalidatorStateChange(syncer::InvalidatorState state) { | |
| 100 if (!is_observer_ready_) | |
| 101 return; | |
| 102 | |
| 103 const bool is_service_connected = (state == syncer::INVALIDATIONS_ENABLED); | |
| 104 if (is_service_connected == is_service_connected_) | |
| 105 return; | |
| 106 | |
| 107 is_service_connected_ = is_service_connected; | |
| 108 if (is_service_connected_) | |
| 109 parent_->OnInvalidationServiceConnected(invalidation_service_); | |
| 110 else | |
| 111 parent_->OnInvalidationServiceDisconnected(invalidation_service_); | |
| 112 } | |
| 113 | |
| 114 void AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 115 OnIncomingInvalidation( | |
| 116 const syncer::ObjectIdInvalidationMap& invalidation_map) { | |
| 117 } | |
| 118 | |
| 119 std::string AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: | |
| 120 GetOwnerName() const { | |
| 121 return "AffiliatedInvalidationService"; | |
| 122 } | |
| 123 | |
| 124 AffiliatedInvalidationServiceProvider::AffiliatedInvalidationServiceProvider() | |
| 125 : invalidation_service_(nullptr), | |
| 126 consumer_count_(0), | |
| 127 is_shut_down_(false) { | |
| 128 // The AffiliatedInvalidationServiceProvider should be created before any user | |
| 129 // Profiles. | |
| 130 DCHECK(g_browser_process->profile_manager()->GetLoadedProfiles().empty()); | |
| 131 | |
| 132 // Subscribe to notification about new user profiles becoming available. | |
| 133 registrar_.Add(this, | |
| 134 chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, | |
| 135 content::NotificationService::AllSources()); | |
| 136 } | 13 } |
| 137 | 14 |
| 138 AffiliatedInvalidationServiceProvider:: | 15 AffiliatedInvalidationServiceProvider:: |
| 139 ~AffiliatedInvalidationServiceProvider() { | 16 ~AffiliatedInvalidationServiceProvider() { |
| 140 // Verify that the provider was shut down first. | |
| 141 DCHECK(is_shut_down_); | |
| 142 } | |
| 143 | |
| 144 void AffiliatedInvalidationServiceProvider::Observe( | |
| 145 int type, | |
| 146 const content::NotificationSource& source, | |
| 147 const content::NotificationDetails& details) { | |
| 148 DCHECK_EQ(chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, type); | |
| 149 DCHECK(!is_shut_down_); | |
| 150 Profile* profile = content::Details<Profile>(details).ptr(); | |
| 151 invalidation::ProfileInvalidationProvider* invalidation_provider = | |
| 152 invalidation::ProfileInvalidationProviderFactory::GetForProfile(profile); | |
| 153 if (!invalidation_provider) { | |
| 154 // If the Profile does not support invalidation (e.g. guest, incognito), | |
| 155 // ignore it. | |
| 156 return; | |
| 157 } | |
| 158 user_manager::User* user = | |
| 159 chromeos::ProfileHelper::Get()->GetUserByProfile(profile); | |
| 160 if (!user || | |
| 161 g_browser_process->platform_part()->browser_policy_connector_chromeos()-> | |
| 162 GetUserAffiliation(user->email()) != USER_AFFILIATION_MANAGED) { | |
| 163 // If the Profile belongs to a user who is not affiliated with the device's | |
| 164 // enrollment domain, ignore it. | |
| 165 return; | |
| 166 } | |
| 167 | |
| 168 // Create a state observer for the user's invalidation service. | |
| 169 invalidation::InvalidationService* invalidation_service = | |
| 170 invalidation_provider->GetInvalidationService(); | |
| 171 profile_invalidation_service_observers_.push_back( | |
| 172 new InvalidationServiceObserver(this, invalidation_service)); | |
| 173 | |
| 174 if (profile_invalidation_service_observers_.back()->IsServiceConnected()) { | |
| 175 // If the invalidation service is connected, check whether to switch to it. | |
| 176 OnInvalidationServiceConnected(invalidation_service); | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 void AffiliatedInvalidationServiceProvider::RegisterConsumer( | |
| 181 Consumer* consumer) { | |
| 182 if (consumers_.HasObserver(consumer) || is_shut_down_) | |
| 183 return; | |
| 184 | |
| 185 consumers_.AddObserver(consumer); | |
| 186 ++consumer_count_; | |
| 187 | |
| 188 if (invalidation_service_) | |
| 189 consumer->OnInvalidationServiceSet(invalidation_service_); | |
| 190 else if (consumer_count_ == 1) | |
| 191 FindConnectedInvalidationService(); | |
| 192 } | |
| 193 | |
| 194 void AffiliatedInvalidationServiceProvider::UnregisterConsumer( | |
| 195 Consumer* consumer) { | |
| 196 if (!consumers_.HasObserver(consumer)) | |
| 197 return; | |
| 198 | |
| 199 consumers_.RemoveObserver(consumer); | |
| 200 --consumer_count_; | |
| 201 | |
| 202 if (invalidation_service_ && consumer_count_ == 0) { | |
| 203 invalidation_service_ = nullptr; | |
| 204 DestroyDeviceInvalidationService(); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 void AffiliatedInvalidationServiceProvider::Shutdown() { | |
| 209 is_shut_down_ = true; | |
| 210 | |
| 211 registrar_.RemoveAll(); | |
| 212 profile_invalidation_service_observers_.clear(); | |
| 213 DestroyDeviceInvalidationService(); | |
| 214 | |
| 215 if (invalidation_service_) { | |
| 216 invalidation_service_ = nullptr; | |
| 217 // Explicitly notify consumers that the invalidation service they were using | |
| 218 // is no longer available. | |
| 219 SetInvalidationService(nullptr); | |
| 220 } | |
| 221 } | |
| 222 | |
| 223 invalidation::TiclInvalidationService* | |
| 224 AffiliatedInvalidationServiceProvider:: | |
| 225 GetDeviceInvalidationServiceForTest() const { | |
| 226 return device_invalidation_service_.get(); | |
| 227 } | |
| 228 | |
| 229 void AffiliatedInvalidationServiceProvider::OnInvalidationServiceConnected( | |
| 230 invalidation::InvalidationService* invalidation_service) { | |
| 231 DCHECK(!is_shut_down_); | |
| 232 | |
| 233 if (consumer_count_ == 0) { | |
| 234 // If there are no consumers, no invalidation service is required. | |
| 235 return; | |
| 236 } | |
| 237 | |
| 238 if (!device_invalidation_service_) { | |
| 239 // The lack of a device-global invalidation service implies that another | |
| 240 // connected invalidation service is being made available to consumers | |
| 241 // already. There is no need to switch from that to the service which just | |
| 242 // connected. | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 if (invalidation_service != device_invalidation_service_.get()) { | |
| 247 // If an invalidation service other than the device-global one connected, | |
| 248 // destroy the device-global service. | |
| 249 invalidation_service_ = nullptr; | |
| 250 DestroyDeviceInvalidationService(); | |
| 251 } | |
| 252 | |
| 253 // Make the invalidation service that just connected available to consumers. | |
| 254 SetInvalidationService(invalidation_service); | |
| 255 } | |
| 256 | |
| 257 void AffiliatedInvalidationServiceProvider::OnInvalidationServiceDisconnected( | |
| 258 invalidation::InvalidationService* invalidation_service) { | |
| 259 DCHECK(!is_shut_down_); | |
| 260 | |
| 261 if (invalidation_service != invalidation_service_) { | |
| 262 // If the invalidation service which disconnected was not being made | |
| 263 // available to consumers, return. | |
| 264 return; | |
| 265 } | |
| 266 | |
| 267 // The invalidation service which disconnected was being made available to | |
| 268 // consumers. Stop making it available. | |
| 269 DCHECK(consumer_count_); | |
| 270 invalidation_service_ = nullptr; | |
| 271 | |
| 272 // Try to make another invalidation service available to consumers. | |
| 273 FindConnectedInvalidationService(); | |
| 274 | |
| 275 // If no other connected invalidation service was found, explicitly notify | |
| 276 // consumers that the invalidation service they were using is no longer | |
| 277 // available. | |
| 278 if (!invalidation_service_) | |
| 279 SetInvalidationService(nullptr); | |
| 280 } | |
| 281 | |
| 282 void AffiliatedInvalidationServiceProvider::FindConnectedInvalidationService() { | |
| 283 DCHECK(!invalidation_service_); | |
| 284 DCHECK(consumer_count_); | |
| 285 DCHECK(!is_shut_down_); | |
| 286 | |
| 287 for (ScopedVector<InvalidationServiceObserver>::const_iterator it = | |
| 288 profile_invalidation_service_observers_.begin(); | |
| 289 it != profile_invalidation_service_observers_.end(); ++it) { | |
| 290 if ((*it)->IsServiceConnected()) { | |
| 291 // If a connected invalidation service belonging to an affiliated | |
| 292 // logged-in user is found, make it available to consumers. | |
| 293 DestroyDeviceInvalidationService(); | |
| 294 SetInvalidationService((*it)->GetInvalidationService()); | |
| 295 return; | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 if (!device_invalidation_service_) { | |
| 300 // If no other connected invalidation service was found and no device-global | |
| 301 // invalidation service exists, create one. | |
| 302 device_invalidation_service_.reset( | |
| 303 new invalidation::TiclInvalidationService( | |
| 304 GetUserAgent(), | |
| 305 scoped_ptr<IdentityProvider>(new chromeos::DeviceIdentityProvider( | |
| 306 chromeos::DeviceOAuth2TokenServiceFactory::Get())), | |
| 307 scoped_ptr<invalidation::TiclSettingsProvider>( | |
| 308 new TiclDeviceSettingsProvider), | |
| 309 g_browser_process->gcm_driver(), | |
| 310 g_browser_process->system_request_context())); | |
| 311 device_invalidation_service_->Init( | |
| 312 scoped_ptr<syncer::InvalidationStateTracker>( | |
| 313 new invalidation::InvalidatorStorage( | |
| 314 g_browser_process->local_state()))); | |
| 315 device_invalidation_service_observer_.reset( | |
| 316 new InvalidationServiceObserver( | |
| 317 this, | |
| 318 device_invalidation_service_.get())); | |
| 319 } | |
| 320 | |
| 321 if (device_invalidation_service_observer_->IsServiceConnected()) { | |
| 322 // If the device-global invalidation service is connected already, make it | |
| 323 // available to consumers immediately. Otherwise, the invalidation service | |
| 324 // will be made available to clients when it successfully connects. | |
| 325 OnInvalidationServiceConnected(device_invalidation_service_.get()); | |
| 326 } | |
| 327 } | |
| 328 | |
| 329 void AffiliatedInvalidationServiceProvider::SetInvalidationService( | |
| 330 invalidation::InvalidationService* invalidation_service) { | |
| 331 DCHECK(!invalidation_service_); | |
| 332 invalidation_service_ = invalidation_service; | |
| 333 FOR_EACH_OBSERVER(Consumer, | |
| 334 consumers_, | |
| 335 OnInvalidationServiceSet(invalidation_service_)); | |
| 336 } | |
| 337 | |
| 338 void AffiliatedInvalidationServiceProvider::DestroyDeviceInvalidationService() { | |
| 339 device_invalidation_service_observer_.reset(); | |
| 340 device_invalidation_service_.reset(); | |
| 341 } | 17 } |
| 342 | 18 |
| 343 } // namespace policy | 19 } // namespace policy |
| OLD | NEW |