Chromium Code Reviews| Index: chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.cc |
| diff --git a/chrome/browser/chromeos/policy/device_cloud_policy_invalidator.cc b/chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.cc |
| similarity index 57% |
| copy from chrome/browser/chromeos/policy/device_cloud_policy_invalidator.cc |
| copy to chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.cc |
| index 0f62250173b13889f551af53cc4c1fa1090c4cd6..66aea21143f007300e00de4d598275d778582ba8 100644 |
| --- a/chrome/browser/chromeos/policy/device_cloud_policy_invalidator.cc |
| +++ b/chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.cc |
| @@ -1,27 +1,22 @@ |
| -// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/chromeos/policy/device_cloud_policy_invalidator.h" |
| +#include "chrome/browser/chromeos/policy/affiliated_invalidation_service_provider.h" |
| -#include <string> |
| #include <vector> |
| #include "base/logging.h" |
| -#include "base/message_loop/message_loop_proxy.h" |
| -#include "base/time/clock.h" |
| -#include "base/time/default_clock.h" |
| #include "chrome/browser/browser_process.h" |
| #include "chrome/browser/browser_process_platform_part_chromeos.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" |
| -#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h" |
| #include "chrome/browser/chromeos/policy/ticl_device_settings_provider.h" |
| #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| #include "chrome/browser/chromeos/settings/device_identity_provider.h" |
| #include "chrome/browser/chromeos/settings/device_oauth2_token_service_factory.h" |
| #include "chrome/browser/invalidation/profile_invalidation_provider_factory.h" |
| -#include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" |
| +#include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/profiles/profile_manager.h" |
| #include "chrome/common/chrome_content_client.h" |
| #include "components/invalidation/invalidation_handler.h" |
| @@ -37,18 +32,17 @@ |
| #include "content/public/browser/notification_details.h" |
| #include "content/public/browser/notification_service.h" |
| #include "google_apis/gaia/identity_provider.h" |
| -#include "net/url_request/url_request_context_getter.h" |
| -#include "policy/proto/device_management_backend.pb.h" |
| - |
| -class Profile; |
| namespace policy { |
| -class DeviceCloudPolicyInvalidator::InvalidationServiceObserver |
| +AffiliatedInvalidationServiceProvider::Consumer::~Consumer() { |
| +} |
| + |
| +class AffiliatedInvalidationServiceProvider::InvalidationServiceObserver |
| : public syncer::InvalidationHandler { |
| public: |
| explicit InvalidationServiceObserver( |
| - DeviceCloudPolicyInvalidator* parent, |
| + AffiliatedInvalidationServiceProvider* parent, |
| invalidation::InvalidationService* invalidation_service); |
| virtual ~InvalidationServiceObserver(); |
| @@ -63,41 +57,43 @@ class DeviceCloudPolicyInvalidator::InvalidationServiceObserver |
| virtual std::string GetOwnerName() const override; |
| private: |
| - DeviceCloudPolicyInvalidator* parent_; |
| + AffiliatedInvalidationServiceProvider* parent_; |
| invalidation::InvalidationService* invalidation_service_; |
| bool is_service_connected_; |
| DISALLOW_COPY_AND_ASSIGN(InvalidationServiceObserver); |
| }; |
| -DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| InvalidationServiceObserver( |
| - DeviceCloudPolicyInvalidator* parent, |
| + AffiliatedInvalidationServiceProvider* parent, |
| invalidation::InvalidationService* invalidation_service) |
| : parent_(parent), |
| invalidation_service_(invalidation_service), |
| is_service_connected_(invalidation_service->GetInvalidatorState() == |
| syncer::INVALIDATIONS_ENABLED) { |
| invalidation_service_->RegisterInvalidationHandler(this); |
| + if (IsServiceConnected()) |
| + parent->OnInvalidationServiceConnected(invalidation_service_); |
| } |
| -DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| ~InvalidationServiceObserver() { |
| invalidation_service_->UnregisterInvalidationHandler(this); |
| } |
| invalidation::InvalidationService* |
| -DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| GetInvalidationService() { |
| return invalidation_service_; |
| } |
| -bool DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +bool AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| IsServiceConnected() const { |
| return is_service_connected_; |
| } |
| -void DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +void AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| OnInvalidatorStateChange(syncer::InvalidatorState state) { |
| const bool is_service_connected = (state == syncer::INVALIDATIONS_ENABLED); |
| if (is_service_connected == is_service_connected_) |
| @@ -110,20 +106,20 @@ void DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| parent_->OnInvalidationServiceDisconnected(invalidation_service_); |
| } |
| -void DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +void AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| OnIncomingInvalidation( |
| const syncer::ObjectIdInvalidationMap& invalidation_map) { |
| } |
| -std::string DeviceCloudPolicyInvalidator::InvalidationServiceObserver:: |
| +std::string AffiliatedInvalidationServiceProvider::InvalidationServiceObserver:: |
| GetOwnerName() const { |
| - return "DevicePolicy"; |
| + return "AffiliatedInvalidationService"; |
| } |
| -DeviceCloudPolicyInvalidator::DeviceCloudPolicyInvalidator() |
| - : invalidation_service_(NULL), |
| - highest_handled_invalidation_version_(0) { |
| - // The DeviceCloudPolicyInvalidator should be created before any user |
| +AffiliatedInvalidationServiceProvider::AffiliatedInvalidationServiceProvider() |
| + : invalidation_service_(nullptr), |
| + consumer_count_(0) { |
| + // The AffiliatedInvalidationServiceProvider should be created before any user |
| // Profiles. |
| DCHECK(g_browser_process->profile_manager()->GetLoadedProfiles().empty()); |
| @@ -131,15 +127,13 @@ DeviceCloudPolicyInvalidator::DeviceCloudPolicyInvalidator() |
| registrar_.Add(this, |
| chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
| content::NotificationService::AllSources()); |
| - |
| - TryToCreateInvalidator(); |
| } |
| -DeviceCloudPolicyInvalidator::~DeviceCloudPolicyInvalidator() { |
| - DestroyInvalidator(); |
| +AffiliatedInvalidationServiceProvider:: |
| +~AffiliatedInvalidationServiceProvider() { |
| } |
| -void DeviceCloudPolicyInvalidator::Observe( |
| +void AffiliatedInvalidationServiceProvider::Observe( |
| int type, |
| const content::NotificationSource& source, |
| const content::NotificationDetails& details) { |
| @@ -157,24 +151,64 @@ void DeviceCloudPolicyInvalidator::Observe( |
| if (!user || |
| g_browser_process->platform_part()->browser_policy_connector_chromeos()-> |
| GetUserAffiliation(user->email()) != USER_AFFILIATION_MANAGED) { |
| - // If the Profile belongs to a user who is not affiliated with the domain |
| - // the device is enrolled into, ignore it. |
| + // If the Profile belongs to a user who is not affiliated with the device's |
| + // enrollment domain, ignore it. |
| return; |
| } |
| - // Create a state observer for the user's invalidation service. |
| + |
| + // Create a state observer for the user's invalidation service. If the |
| + // invalidation service is connected, the observer will trigger a check |
| + // whether to switch to the service. |
| + invalidation::InvalidationService* invalidation_service = |
| + invalidation_provider->GetInvalidationService(); |
| profile_invalidation_service_observers_.push_back( |
| - new InvalidationServiceObserver( |
| - this, |
| - invalidation_provider->GetInvalidationService())); |
| + new InvalidationServiceObserver(this, invalidation_service)); |
| +} |
| + |
| +void AffiliatedInvalidationServiceProvider::RegisterConsumer( |
| + Consumer* consumer) { |
| + if (consumers_.HasObserver(consumer)) |
| + return; |
| + |
| + consumers_.AddObserver(consumer); |
| + ++consumer_count_; |
| + |
| + if (invalidation_service_) |
| + consumer->OnInvalidationServiceSet(invalidation_service_); |
| + else |
| + FindConnectedInvalidationService(); |
| +} |
| + |
| +void AffiliatedInvalidationServiceProvider::UnregisterConsumer( |
| + Consumer* consumer) { |
| + if (!consumers_.HasObserver(consumer)) |
| + return; |
| - TryToCreateInvalidator(); |
| + consumers_.RemoveObserver(consumer); |
| + --consumer_count_; |
| + |
| + if (invalidation_service_ &&consumer_count_ == 0) { |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
nit: space after &&
bartfab (slow)
2015/01/20 13:22:47
Done.
|
| + ResetInvalidationService(); |
| + DestroyDeviceInvalidationService(); |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
This pattern may be dangerous as ResetInvalidation
bartfab (slow)
2015/01/20 13:22:47
This is actually not dangerous, because we are gua
|
| + } |
| } |
| -void DeviceCloudPolicyInvalidator::OnInvalidationServiceConnected( |
| +invalidation::TiclInvalidationService* |
| +AffiliatedInvalidationServiceProvider:: |
| + GetDeviceInvalidationServiceForTest() const { |
| + return device_invalidation_service_.get(); |
| +} |
| + |
| +void AffiliatedInvalidationServiceProvider::OnInvalidationServiceConnected( |
| invalidation::InvalidationService* invalidation_service) { |
| + if (consumer_count_ == 0) { |
| + // If there are no consumers, no invalidation service is required. |
| + return; |
| + } |
| + |
| if (!device_invalidation_service_) { |
| - // The lack of a device-global invalidation service implies that a |
| - // |CloudPolicyInvalidator| backed by another connected service exists |
| + // The lack of a device-global invalidation service implies that another |
| + // connected invalidation service is being made available to consumers |
| // already. There is no need to switch from that to the service which just |
| // connected. |
| return; |
| @@ -182,37 +216,40 @@ void DeviceCloudPolicyInvalidator::OnInvalidationServiceConnected( |
| if (invalidation_service != device_invalidation_service_.get()) { |
| // If an invalidation service other than the device-global one connected, |
| - // destroy the device-global service and the |CloudPolicyInvalidator| backed |
| - // by it, if any. |
| - DestroyInvalidator(); |
| + // destroy the device-global service. |
| + ResetInvalidationService(); |
| DestroyDeviceInvalidationService(); |
| } |
| - // Create a |CloudPolicyInvalidator| backed by the invalidation service which |
| - // just connected. |
| - CreateInvalidator(invalidation_service); |
| + // Make the invalidation service that just connected available to consumers. |
| + SetInvalidationService(invalidation_service); |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
Two callbacks to consumers in this function - not
bartfab (slow)
2015/01/20 13:22:47
Changed to a single callback.
|
| } |
| -void DeviceCloudPolicyInvalidator::OnInvalidationServiceDisconnected( |
| +void AffiliatedInvalidationServiceProvider::OnInvalidationServiceDisconnected( |
| invalidation::InvalidationService* invalidation_service) { |
| if (invalidation_service != invalidation_service_) { |
| - // If the invalidation service which disconnected is not backing the current |
| - // |CloudPolicyInvalidator|, return. |
| + // If the invalidation service which disconnected was not being made |
| + // available to consumers, return. |
| return; |
| } |
| - // Destroy the |CloudPolicyInvalidator| backed by the invalidation service |
| - // which just disconnected. |
| - DestroyInvalidator(); |
| + // Notify consumers that the invalidation service they were using is no longer |
| + // available. |
| + ResetInvalidationService(); |
| - // Try to create a |CloudPolicyInvalidator| backed by another invalidation |
| - // service. |
| - TryToCreateInvalidator(); |
| + // Try to make another invalidation service available to consumers. |
| + FindConnectedInvalidationService(); |
| } |
| -void DeviceCloudPolicyInvalidator::TryToCreateInvalidator() { |
| - if (invalidator_) { |
| - // If a |CloudPolicyInvalidator| exists already, return. |
| +void AffiliatedInvalidationServiceProvider::FindConnectedInvalidationService() { |
| + if (consumer_count_ == 0) { |
| + // If there are no consumers, no invalidation service is required. |
| + return; |
| + } |
| + |
| + if (invalidation_service_) { |
| + // If a connected invalidation service is being made available to consumers |
| + // already, there is nothing to do. |
| return; |
| } |
| @@ -221,17 +258,17 @@ void DeviceCloudPolicyInvalidator::TryToCreateInvalidator() { |
| it != profile_invalidation_service_observers_.end(); ++it) { |
| if ((*it)->IsServiceConnected()) { |
| // If a connected invalidation service belonging to an affiliated |
| - // logged-in user is found, create a |CloudPolicyInvalidator| backed by |
| - // that service and destroy the device-global service, if any. |
| + // logged-in user is found, make it available to consumers. |
| DestroyDeviceInvalidationService(); |
| - CreateInvalidator((*it)->GetInvalidationService()); |
| + SetInvalidationService((*it)->GetInvalidationService()); |
| return; |
| } |
| } |
| if (!device_invalidation_service_) { |
| - // If no other connected invalidation service was found, ensure that a |
| - // device-global service is running. |
| + // If no other connected invalidation service was found and no device-global |
| + // invalidation service exists, create one. The invalidation service will be |
| + // made available to consumers when it successfully connects. |
| device_invalidation_service_.reset( |
| new invalidation::TiclInvalidationService( |
| GetUserAgent(), |
| @@ -249,42 +286,31 @@ void DeviceCloudPolicyInvalidator::TryToCreateInvalidator() { |
| new InvalidationServiceObserver( |
| this, |
| device_invalidation_service_.get())); |
| - } |
| - if (device_invalidation_service_observer_->IsServiceConnected()) { |
| - // If the device-global invalidation service is connected, create a |
| - // |CloudPolicyInvalidator| backed by it. Otherwise, a |
| - // |CloudPolicyInvalidator| will be created later when a connected service |
| - // becomes available. |
| - CreateInvalidator(device_invalidation_service_.get()); |
| + } else if (device_invalidation_service_observer_->IsServiceConnected()) { |
| + // If a connected device-global invalidation service exists already, make it |
| + // available to consumers immediately. Otherwise, the invalidation service |
| + // will be made available to clients when it successfully connects. |
| + SetInvalidationService(device_invalidation_service_.get()); |
| } |
| } |
| -void DeviceCloudPolicyInvalidator::CreateInvalidator( |
| +void AffiliatedInvalidationServiceProvider::SetInvalidationService( |
| invalidation::InvalidationService* invalidation_service) { |
| + DCHECK(!invalidation_service_); |
| invalidation_service_ = invalidation_service; |
| - DCHECK(!invalidator_); |
| - invalidator_.reset(new CloudPolicyInvalidator( |
| - enterprise_management::DeviceRegisterRequest::DEVICE, |
| - g_browser_process->platform_part()->browser_policy_connector_chromeos()-> |
| - GetDeviceCloudPolicyManager()->core(), |
| - base::MessageLoopProxy::current(), |
| - scoped_ptr<base::Clock>(new base::DefaultClock()), |
| - highest_handled_invalidation_version_)); |
| - invalidator_->Initialize(invalidation_service); |
| + FOR_EACH_OBSERVER(Consumer, |
| + consumers_, |
| + OnInvalidationServiceSet(invalidation_service_)); |
| } |
| -void DeviceCloudPolicyInvalidator::DestroyInvalidator() { |
| - if (invalidator_) { |
| - highest_handled_invalidation_version_ = |
| - invalidator_->highest_handled_invalidation_version(); |
| - invalidator_->Shutdown(); |
| - } |
| - invalidator_.reset(); |
| - invalidation_service_ = NULL; |
| +void AffiliatedInvalidationServiceProvider::ResetInvalidationService() { |
| + if (invalidation_service_) |
| + FOR_EACH_OBSERVER(Consumer, consumers_, OnInvalidationServiceSet(nullptr)); |
|
Mattias Nissler (ping if slow)
2015/01/16 13:24:29
nit: It's probably more robust to reset |invalidat
bartfab (slow)
2015/01/20 13:22:47
Done.
|
| + invalidation_service_ = nullptr; |
| } |
| -void DeviceCloudPolicyInvalidator::DestroyDeviceInvalidationService() { |
| +void AffiliatedInvalidationServiceProvider::DestroyDeviceInvalidationService() { |
| device_invalidation_service_observer_.reset(); |
| device_invalidation_service_.reset(); |
| } |