diff options
author | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 16:03:54 +0000 |
---|---|---|
committer | mnissler@chromium.org <mnissler@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-06 16:03:54 +0000 |
commit | 75492771bc5aa1969501937be22319d7f05eece5 (patch) | |
tree | 3e90adde8732c2d46ca913f35c9bd936e5835ef3 | |
parent | f6edd789acfaac00d0bd90adfb5f54813fd72fd3 (diff) | |
download | chromium_src-75492771bc5aa1969501937be22319d7f05eece5.zip chromium_src-75492771bc5aa1969501937be22319d7f05eece5.tar.gz chromium_src-75492771bc5aa1969501937be22319d7f05eece5.tar.bz2 |
Split up CloudPolicyManager.
This introduces a new class CloudPolicyCore with the sole purpose of
managing the essential cloud policy pieces. CloudPolicyManager is now
mostly down to implementing ConfigurationPolicyProvider and delegates to
CloudPolicyCore.
BUG=chromium:108928
TEST=unit tests
TBR=ben@chromium.org
Review URL: https://codereview.chromium.org/11452005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171507 0039d316-1c4b-4281-b951-d872f2087c98
24 files changed, 437 insertions, 285 deletions
diff --git a/chrome/browser/policy/cloud_policy_browsertest.cc b/chrome/browser/policy/cloud_policy_browsertest.cc index 2f450d6..d848680 100644 --- a/chrome/browser/policy/cloud_policy_browsertest.cc +++ b/chrome/browser/policy/cloud_policy_browsertest.cc @@ -147,23 +147,23 @@ void SetUpNewStackAfterCreatingBrowser(Browser* browser) { UserCloudPolicyManager* policy_manager = UserCloudPolicyManagerFactory::GetForProfile(browser->profile()); ASSERT_TRUE(policy_manager); - policy_manager->Initialize(g_browser_process->local_state(), - connector->device_management_service()); + policy_manager->Connect(g_browser_process->local_state(), + connector->device_management_service()); #endif // defined(OS_CHROMEOS) - ASSERT_TRUE(policy_manager->cloud_policy_client()); + ASSERT_TRUE(policy_manager->core()->client()); base::RunLoop run_loop; MockCloudPolicyClientObserver observer; EXPECT_CALL(observer, OnRegistrationStateChanged(_)).WillOnce( InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); - policy_manager->cloud_policy_client()->AddObserver(&observer); + policy_manager->core()->client()->AddObserver(&observer); // Give a bogus OAuth token to the |policy_manager|. This should make its // CloudPolicyClient fetch the DMToken. policy_manager->RegisterClient("bogus"); run_loop.Run(); Mock::VerifyAndClearExpectations(&observer); - policy_manager->cloud_policy_client()->RemoveObserver(&observer); + policy_manager->core()->client()->RemoveObserver(&observer); } struct TestSetup { diff --git a/chrome/browser/policy/cloud_policy_core.cc b/chrome/browser/policy/cloud_policy_core.cc new file mode 100644 index 0000000..f8fe21c --- /dev/null +++ b/chrome/browser/policy/cloud_policy_core.cc @@ -0,0 +1,63 @@ +// Copyright (c) 2012 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/policy/cloud_policy_core.h" + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/message_loop.h" +#include "chrome/browser/policy/cloud_policy_client.h" +#include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" +#include "chrome/browser/policy/cloud_policy_service.h" +#include "chrome/browser/policy/cloud_policy_store.h" +#include "chrome/browser/prefs/pref_service.h" + +namespace policy { + +CloudPolicyCore::CloudPolicyCore(CloudPolicyStore* store) + : store_(store) {} + +CloudPolicyCore::~CloudPolicyCore() {} + +void CloudPolicyCore::Connect(scoped_ptr<CloudPolicyClient> client) { + CHECK(!client_.get()); + CHECK(client.get()); + client_ = client.Pass(); + service_.reset(new CloudPolicyService(client_.get(), store_)); +} + +void CloudPolicyCore::Disconnect() { + refresh_delay_.reset(); + refresh_scheduler_.reset(); + service_.reset(); + client_.reset(); +} + +void CloudPolicyCore::StartRefreshScheduler() { + if (!refresh_scheduler_.get()) { + refresh_scheduler_.reset( + new CloudPolicyRefreshScheduler( + client_.get(), store_, + MessageLoop::current()->message_loop_proxy())); + UpdateRefreshDelayFromPref(); + } +} + +void CloudPolicyCore::TrackRefreshDelayPref( + PrefService* pref_service, + const std::string& refresh_pref_name) { + refresh_delay_.reset(new IntegerPrefMember()); + refresh_delay_->Init( + refresh_pref_name.c_str(), pref_service, + base::Bind(&CloudPolicyCore::UpdateRefreshDelayFromPref, + base::Unretained(this))); + UpdateRefreshDelayFromPref(); +} + +void CloudPolicyCore::UpdateRefreshDelayFromPref() { + if (refresh_scheduler_.get() && refresh_delay_.get()) + refresh_scheduler_->SetRefreshDelay(refresh_delay_->GetValue()); +} + +} // namespace policy diff --git a/chrome/browser/policy/cloud_policy_core.h b/chrome/browser/policy/cloud_policy_core.h new file mode 100644 index 0000000..536691b --- /dev/null +++ b/chrome/browser/policy/cloud_policy_core.h @@ -0,0 +1,79 @@ +// Copyright (c) 2012 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. + +#ifndef CHROME_BROWSER_POLICY_CLOUD_POLICY_CORE_H_ +#define CHROME_BROWSER_POLICY_CLOUD_POLICY_CORE_H_ + +#include <string> + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/api/prefs/pref_member.h" + +class PrefService; + +namespace policy { + +class CloudPolicyClient; +class CloudPolicyRefreshScheduler; +class CloudPolicyService; +class CloudPolicyStore; + +// CloudPolicyCore glues together the ingredients that are essential for +// obtaining a fully-functional cloud policy system: CloudPolicyClient and +// CloudPolicyStore, which are responsible for fetching policy from the cloud +// and storing it locally, respectively, as well as a CloudPolicyService +// instance that moves data between the two former components, and +// CloudPolicyRefreshScheduler which triggers periodic refreshes. +class CloudPolicyCore { + public: + explicit CloudPolicyCore(CloudPolicyStore* store); + ~CloudPolicyCore(); + + CloudPolicyClient* client() { return client_.get(); } + const CloudPolicyClient* client() const { return client_.get(); } + + CloudPolicyStore* store() { return store_; } + const CloudPolicyStore* store() const { return store_; } + + CloudPolicyService* service() { return service_.get(); } + const CloudPolicyService* service() const { return service_.get(); } + + CloudPolicyRefreshScheduler* refresh_scheduler() { + return refresh_scheduler_.get(); + } + const CloudPolicyRefreshScheduler* refresh_scheduler() const { + return refresh_scheduler_.get(); + } + + // Initializes the cloud connection. + void Connect(scoped_ptr<CloudPolicyClient> client); + + // Shuts down the cloud connection. + void Disconnect(); + + // Starts a refresh scheduler in case none is running yet. + void StartRefreshScheduler(); + + // Watches the pref named |refresh_pref_name| in |pref_service| and adjusts + // |refresh_scheduler_|'s refresh delay accordingly. + void TrackRefreshDelayPref(PrefService* pref_service, + const std::string& refresh_pref_name); + + private: + // Updates the refresh scheduler on refresh delay changes. + void UpdateRefreshDelayFromPref(); + + CloudPolicyStore* store_; + scoped_ptr<CloudPolicyClient> client_; + scoped_ptr<CloudPolicyService> service_; + scoped_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_; + scoped_ptr<IntegerPrefMember> refresh_delay_; + + DISALLOW_COPY_AND_ASSIGN(CloudPolicyCore); +}; + +} // namespace policy + +#endif // CHROME_BROWSER_POLICY_CLOUD_POLICY_CORE_H_ diff --git a/chrome/browser/policy/cloud_policy_core_unittest.cc b/chrome/browser/policy/cloud_policy_core_unittest.cc new file mode 100644 index 0000000..0080cbf --- /dev/null +++ b/chrome/browser/policy/cloud_policy_core_unittest.cc @@ -0,0 +1,78 @@ +// Copyright (c) 2012 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/policy/cloud_policy_core.h" + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" +#include "chrome/browser/policy/mock_cloud_policy_client.h" +#include "chrome/browser/policy/mock_cloud_policy_store.h" +#include "chrome/browser/prefs/browser_prefs.h" +#include "chrome/common/pref_names.h" +#include "chrome/test/base/testing_pref_service.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace policy { + +class CloudPolicyCoreTest : public testing::Test { + protected: + CloudPolicyCoreTest() + : core_(&store_) { + chrome::RegisterLocalState(&prefs_); + } + + MessageLoop loop_; + + TestingPrefService prefs_; + MockCloudPolicyStore store_; + CloudPolicyCore core_; + + private: + DISALLOW_COPY_AND_ASSIGN(CloudPolicyCoreTest); +}; + +TEST_F(CloudPolicyCoreTest, ConnectAndDisconnect) { + EXPECT_TRUE(core_.store()); + EXPECT_FALSE(core_.client()); + EXPECT_FALSE(core_.service()); + EXPECT_FALSE(core_.refresh_scheduler()); + + // Connect() brings up client and service. + core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient())); + EXPECT_TRUE(core_.client()); + EXPECT_TRUE(core_.service()); + EXPECT_FALSE(core_.refresh_scheduler()); + + // Disconnect() goes back to no client and service. + core_.Disconnect(); + EXPECT_FALSE(core_.client()); + EXPECT_FALSE(core_.service()); + EXPECT_FALSE(core_.refresh_scheduler()); + + // Calling Disconnect() twice doesn't do bad things. + core_.Disconnect(); + EXPECT_FALSE(core_.client()); + EXPECT_FALSE(core_.service()); + EXPECT_FALSE(core_.refresh_scheduler()); +} + +TEST_F(CloudPolicyCoreTest, RefreshScheduler) { + EXPECT_FALSE(core_.refresh_scheduler()); + core_.Connect(scoped_ptr<CloudPolicyClient>(new MockCloudPolicyClient())); + core_.StartRefreshScheduler(); + ASSERT_TRUE(core_.refresh_scheduler()); + + int default_refresh_delay = core_.refresh_scheduler()->refresh_delay(); + + const int kRefreshRate = 1000 * 60 * 60; + prefs_.SetInteger(prefs::kUserPolicyRefreshRate, kRefreshRate); + core_.TrackRefreshDelayPref(&prefs_, prefs::kUserPolicyRefreshRate); + EXPECT_EQ(kRefreshRate, core_.refresh_scheduler()->refresh_delay()); + + prefs_.ClearPref(prefs::kUserPolicyRefreshRate); + EXPECT_EQ(default_refresh_delay, core_.refresh_scheduler()->refresh_delay()); +} + +} // namespace policy diff --git a/chrome/browser/policy/cloud_policy_manager.cc b/chrome/browser/policy/cloud_policy_manager.cc index f4f6d71..53cece0 100644 --- a/chrome/browser/policy/cloud_policy_manager.cc +++ b/chrome/browser/policy/cloud_policy_manager.cc @@ -7,7 +7,6 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "base/logging.h" -#include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" #include "chrome/browser/policy/cloud_policy_service.h" #include "chrome/browser/policy/policy_bundle.h" #include "chrome/browser/policy/policy_map.h" @@ -15,39 +14,35 @@ namespace policy { -CloudPolicyManager::CloudPolicyManager(CloudPolicyStore* store) - : store_(store), +CloudPolicyManager::CloudPolicyManager(CloudPolicyStore* cloud_policy_store) + : core_(cloud_policy_store), waiting_for_policy_refresh_(false) { - store_->AddObserver(this); + store()->AddObserver(this); // If the underlying store is already initialized, publish the loaded // policy. Otherwise, request a load now. - if (store_->is_initialized()) + if (store()->is_initialized()) CheckAndPublishPolicy(); else - store_->Load(); + store()->Load(); } -CloudPolicyManager::~CloudPolicyManager() { - DCHECK(!store_) << "Shutdown() must be called before destruction!"; -} +CloudPolicyManager::~CloudPolicyManager() {} void CloudPolicyManager::Shutdown() { - ShutdownService(); - if (store_) - store_->RemoveObserver(this); + core_.Disconnect(); + store()->RemoveObserver(this); ConfigurationPolicyProvider::Shutdown(); - store_ = NULL; } bool CloudPolicyManager::IsInitializationComplete() const { - return store_->is_initialized(); + return store()->is_initialized(); } void CloudPolicyManager::RefreshPolicies() { - if (service_.get()) { + if (service()) { waiting_for_policy_refresh_ = true; - service_->RefreshPolicy( + service()->RefreshPolicy( base::Bind(&CloudPolicyManager::OnRefreshComplete, base::Unretained(this))); } else { @@ -55,52 +50,21 @@ void CloudPolicyManager::RefreshPolicies() { } } -void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore* store) { - DCHECK_EQ(store_, store); +void CloudPolicyManager::OnStoreLoaded(CloudPolicyStore* cloud_policy_store) { + DCHECK_EQ(store(), cloud_policy_store); CheckAndPublishPolicy(); } -void CloudPolicyManager::OnStoreError(CloudPolicyStore* store) { - DCHECK_EQ(store_, store); +void CloudPolicyManager::OnStoreError(CloudPolicyStore* cloud_policy_store) { + DCHECK_EQ(store(), cloud_policy_store); // No action required, the old policy is still valid. } -void CloudPolicyManager::InitializeService( - scoped_ptr<CloudPolicyClient> client) { - CHECK(!client_.get()); - CHECK(client.get()); - client_ = client.Pass(); - service_.reset(new CloudPolicyService(client_.get(), store_)); -} - -void CloudPolicyManager::ShutdownService() { - refresh_delay_.reset(); - refresh_scheduler_.reset(); - service_.reset(); - client_.reset(); -} - -void CloudPolicyManager::StartRefreshScheduler( - PrefService* local_state, - const std::string& refresh_rate_pref) { - if (!refresh_scheduler_.get()) { - refresh_delay_.reset(new IntegerPrefMember()); - refresh_delay_->Init(refresh_rate_pref.c_str(), local_state, - base::Bind(&CloudPolicyManager::UpdateRefreshDelay, - base::Unretained(this))); - refresh_scheduler_.reset( - new CloudPolicyRefreshScheduler( - client_.get(), store_, - MessageLoop::current()->message_loop_proxy())); - UpdateRefreshDelay(); - } -} - void CloudPolicyManager::CheckAndPublishPolicy() { if (IsInitializationComplete() && !waiting_for_policy_refresh_) { scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( - store_->policy_map()); + store()->policy_map()); UpdatePolicy(bundle.Pass()); } } @@ -110,8 +74,4 @@ void CloudPolicyManager::OnRefreshComplete() { CheckAndPublishPolicy(); } -void CloudPolicyManager::UpdateRefreshDelay() { - refresh_scheduler_->SetRefreshDelay(refresh_delay_->GetValue()); -} - } // namespace policy diff --git a/chrome/browser/policy/cloud_policy_manager.h b/chrome/browser/policy/cloud_policy_manager.h index 07050c1..f181b85 100644 --- a/chrome/browser/policy/cloud_policy_manager.h +++ b/chrome/browser/policy/cloud_policy_manager.h @@ -9,23 +9,14 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" -#include "base/memory/scoped_ptr.h" -#include "chrome/browser/api/prefs/pref_member.h" +#include "chrome/browser/policy/cloud_policy_core.h" #include "chrome/browser/policy/cloud_policy_store.h" #include "chrome/browser/policy/configuration_policy_provider.h" -class PrefService; - namespace policy { -class CloudPolicyClient; -class CloudPolicyRefreshScheduler; -class CloudPolicyService; - // CloudPolicyManager is the main switching central between cloud policy and the -// upper layers of the policy stack. It owns CloudPolicyClient and -// CloudPolicyService, is responsible for receiving and keeping policy from the -// cloud and exposes the decoded policy from a CloudPolicyStore via the +// upper layers of the policy stack. It wires up a CloudPolicyCore to the // ConfigurationPolicyProvider interface. // // This class contains the base functionality, there are subclasses that add @@ -34,19 +25,11 @@ class CloudPolicyService; class CloudPolicyManager : public ConfigurationPolicyProvider, public CloudPolicyStore::Observer { public: - explicit CloudPolicyManager(CloudPolicyStore* store); + explicit CloudPolicyManager(CloudPolicyStore* cloud_policy_store); virtual ~CloudPolicyManager(); - CloudPolicyClient* cloud_policy_client() { return client_.get(); } - const CloudPolicyClient* cloud_policy_client() const { return client_.get(); } - - CloudPolicyStore* cloud_policy_store() { return store_; } - const CloudPolicyStore* cloud_policy_store() const { return store_; } - - CloudPolicyService* cloud_policy_service() { return service_.get(); } - const CloudPolicyService* cloud_policy_service() const { - return service_.get(); - } + CloudPolicyCore* core() { return &core_; } + const CloudPolicyCore* core() const { return &core_; } // ConfigurationPolicyProvider: virtual void Shutdown() OVERRIDE; @@ -54,44 +37,32 @@ class CloudPolicyManager : public ConfigurationPolicyProvider, virtual void RefreshPolicies() OVERRIDE; // CloudPolicyStore::Observer: - virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; - virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; + virtual void OnStoreLoaded(CloudPolicyStore* cloud_policy_store) OVERRIDE; + virtual void OnStoreError(CloudPolicyStore* cloud_policy_store) OVERRIDE; protected: - // Initializes the cloud connection. - void InitializeService(scoped_ptr<CloudPolicyClient> client); - - // Shuts down the cloud connection. - void ShutdownService(); - - // Starts a refresh scheduler in case none is running yet. |local_state| must - // stay valid until ShutdownService() gets called. - void StartRefreshScheduler(PrefService* local_state, - const std::string& refresh_rate_pref); - // Check whether fully initialized and if so, publish policy by calling // ConfigurationPolicyStore::UpdatePolicy(). void CheckAndPublishPolicy(); + // Convenience accessors to core() components. + CloudPolicyClient* client() { return core_.client(); } + const CloudPolicyClient* client() const { return core_.client(); } + CloudPolicyStore* store() { return core_.store(); } + const CloudPolicyStore* store() const { return core_.store(); } + CloudPolicyService* service() { return core_.service(); } + const CloudPolicyService* service() const { return core_.service(); } + private: // Completion handler for policy refresh operations. void OnRefreshComplete(); - // Updates the refresh scheduler on refresh delay changes. - void UpdateRefreshDelay(); - - CloudPolicyStore* store_; - scoped_ptr<CloudPolicyClient> client_; - scoped_ptr<CloudPolicyService> service_; - scoped_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_; + CloudPolicyCore core_; // Whether there's a policy refresh operation pending, in which case all // policy update notifications are deferred until after it completes. bool waiting_for_policy_refresh_; - // Keeps track of the refresh delay pref. - scoped_ptr<IntegerPrefMember> refresh_delay_; - DISALLOW_COPY_AND_ASSIGN(CloudPolicyManager); }; diff --git a/chrome/browser/policy/cloud_policy_manager_unittest.cc b/chrome/browser/policy/cloud_policy_manager_unittest.cc index 92c1b2f..0bbf390 100644 --- a/chrome/browser/policy/cloud_policy_manager_unittest.cc +++ b/chrome/browser/policy/cloud_policy_manager_unittest.cc @@ -131,9 +131,9 @@ class TestCloudPolicyManager : public CloudPolicyManager { virtual ~TestCloudPolicyManager() {} // Publish the protected members for testing. - using CloudPolicyManager::InitializeService; - using CloudPolicyManager::ShutdownService; - using CloudPolicyManager::StartRefreshScheduler; + using CloudPolicyManager::client; + using CloudPolicyManager::store; + using CloudPolicyManager::service; using CloudPolicyManager::CheckAndPublishPolicy; private: @@ -207,18 +207,18 @@ TEST_F(CloudPolicyManagerTest, InitAndShutdown) { MockCloudPolicyClient* client = new MockCloudPolicyClient(); EXPECT_CALL(*client, SetupRegistration(_, _)); - manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); + manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); Mock::VerifyAndClearExpectations(client); - EXPECT_TRUE(manager_->cloud_policy_client()); - EXPECT_TRUE(manager_->cloud_policy_service()); + EXPECT_TRUE(manager_->client()); + EXPECT_TRUE(manager_->service()); EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); manager_->CheckAndPublishPolicy(); Mock::VerifyAndClearExpectations(&observer_); - manager_->ShutdownService(); - EXPECT_FALSE(manager_->cloud_policy_client()); - EXPECT_FALSE(manager_->cloud_policy_service()); + manager_->core()->Disconnect(); + EXPECT_FALSE(manager_->client()); + EXPECT_FALSE(manager_->service()); } TEST_F(CloudPolicyManagerTest, RegistrationAndFetch) { @@ -228,7 +228,7 @@ TEST_F(CloudPolicyManagerTest, RegistrationAndFetch) { EXPECT_TRUE(manager_->IsInitializationComplete()); MockCloudPolicyClient* client = new MockCloudPolicyClient(); - manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); + manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); client->SetDMToken(policy_.policy_data().request_token()); client->NotifyRegistrationStateChanged(); @@ -263,7 +263,7 @@ TEST_F(CloudPolicyManagerTest, Update) { TEST_F(CloudPolicyManagerTest, RefreshNotRegistered) { MockCloudPolicyClient* client = new MockCloudPolicyClient(); - manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); + manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); store_.NotifyStoreLoaded(); @@ -277,7 +277,7 @@ TEST_F(CloudPolicyManagerTest, RefreshNotRegistered) { TEST_F(CloudPolicyManagerTest, RefreshSuccessful) { MockCloudPolicyClient* client = new MockCloudPolicyClient(); - manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); + manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); // Simulate a store load. store_.policy_.reset(new em::PolicyData(policy_.policy_data())); diff --git a/chrome/browser/policy/cloud_policy_refresh_scheduler.h b/chrome/browser/policy/cloud_policy_refresh_scheduler.h index 61a7c69..7068676 100644 --- a/chrome/browser/policy/cloud_policy_refresh_scheduler.h +++ b/chrome/browser/policy/cloud_policy_refresh_scheduler.h @@ -43,6 +43,8 @@ class CloudPolicyRefreshScheduler const scoped_refptr<base::TaskRunner>& task_runner); virtual ~CloudPolicyRefreshScheduler(); + int64 refresh_delay() const { return refresh_delay_ms_; } + // Sets the refresh delay to |refresh_delay| (subject to min/max clamping). void SetRefreshDelay(int64 refresh_delay); diff --git a/chrome/browser/policy/device_cloud_policy_manager_chromeos.cc b/chrome/browser/policy/device_cloud_policy_manager_chromeos.cc index 1ba3020..67736fd 100644 --- a/chrome/browser/policy/device_cloud_policy_manager_chromeos.cc +++ b/chrome/browser/policy/device_cloud_policy_manager_chromeos.cc @@ -7,6 +7,7 @@ #include "base/bind.h" #include "base/bind_helpers.h" #include "chrome/browser/chromeos/system/statistics_provider.h" +#include "chrome/browser/policy/cloud_policy_store.h" #include "chrome/browser/policy/device_cloud_policy_store_chromeos.h" #include "chrome/browser/policy/device_management_service.h" #include "chrome/browser/policy/enrollment_handler_chromeos.h" @@ -76,7 +77,7 @@ void DeviceCloudPolicyManagerChromeOS::StartEnrollment( const AllowedDeviceModes& allowed_device_modes, const EnrollmentCallback& callback) { CHECK(device_management_service_); - ShutdownService(); + core()->Disconnect(); enrollment_handler_.reset( new EnrollmentHandlerChromeOS( @@ -144,8 +145,10 @@ void DeviceCloudPolicyManagerChromeOS::EnrollmentCompleted( const EnrollmentCallback& callback, EnrollmentStatus status) { if (status.status() == EnrollmentStatus::STATUS_SUCCESS) { - InitializeService(enrollment_handler_->ReleaseClient()); - StartRefreshScheduler(local_state_, prefs::kDevicePolicyRefreshRate); + core()->Connect(enrollment_handler_->ReleaseClient()); + core()->StartRefreshScheduler(); + core()->TrackRefreshDelayPref(local_state_, + prefs::kDevicePolicyRefreshRate); } else { StartIfManaged(); } @@ -158,11 +161,13 @@ void DeviceCloudPolicyManagerChromeOS::EnrollmentCompleted( void DeviceCloudPolicyManagerChromeOS::StartIfManaged() { if (device_management_service_ && local_state_ && - cloud_policy_store()->is_initialized() && - cloud_policy_store()->is_managed() && - !cloud_policy_service()) { - InitializeService(CreateClient()); - StartRefreshScheduler(local_state_, prefs::kDevicePolicyRefreshRate); + store()->is_initialized() && + store()->is_managed() && + !service()) { + core()->Connect(CreateClient()); + core()->StartRefreshScheduler(); + core()->TrackRefreshDelayPref(local_state_, + prefs::kDevicePolicyRefreshRate); } } diff --git a/chrome/browser/policy/device_cloud_policy_manager_chromeos_unittest.cc b/chrome/browser/policy/device_cloud_policy_manager_chromeos_unittest.cc index 6435cef..ed6f41c 100644 --- a/chrome/browser/policy/device_cloud_policy_manager_chromeos_unittest.cc +++ b/chrome/browser/policy/device_cloud_policy_manager_chromeos_unittest.cc @@ -181,8 +181,8 @@ class DeviceCloudPolicyManagerChromeOSEnrollmentTest EXPECT_EQ(DEVICE_MODE_ENTERPRISE, install_attributes_.GetMode()); EXPECT_TRUE(store_->has_policy()); EXPECT_TRUE(store_->is_managed()); - ASSERT_TRUE(manager_.cloud_policy_client()); - EXPECT_TRUE(manager_.cloud_policy_client()->is_registered()); + ASSERT_TRUE(manager_.core()->client()); + EXPECT_TRUE(manager_.core()->client()->is_registered()); bundle.Get(POLICY_DOMAIN_CHROME, std::string()).Set( key::kDeviceMetricsReportingEnabled, POLICY_LEVEL_MANDATORY, diff --git a/chrome/browser/policy/device_local_account_policy_provider.cc b/chrome/browser/policy/device_local_account_policy_provider.cc index ad8da9d..55518d27 100644 --- a/chrome/browser/policy/device_local_account_policy_provider.cc +++ b/chrome/browser/policy/device_local_account_policy_provider.cc @@ -5,6 +5,7 @@ #include "chrome/browser/policy/device_local_account_policy_provider.h" #include "base/bind.h" +#include "chrome/browser/policy/cloud_policy_service.h" #include "chrome/browser/policy/policy_bundle.h" #include "chrome/browser/policy/policy_service.h" @@ -33,9 +34,9 @@ bool DeviceLocalAccountPolicyProvider::IsInitializationComplete() const { void DeviceLocalAccountPolicyProvider::RefreshPolicies() { DeviceLocalAccountPolicyBroker* broker = GetBroker(); - if (broker) { + if (broker && broker->core()->service()) { waiting_for_policy_refresh_ = true; - broker->RefreshPolicy( + broker->core()->service()->RefreshPolicy( base::Bind(&DeviceLocalAccountPolicyProvider::ReportPolicyRefresh, weak_factory_.GetWeakPtr())); } else { @@ -66,11 +67,11 @@ void DeviceLocalAccountPolicyProvider::UpdateFromBroker() { DeviceLocalAccountPolicyBroker* broker = GetBroker(); scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); if (broker) { - store_initialized_ |= broker->store()->is_initialized(); + store_initialized_ |= broker->core()->store()->is_initialized(); if (!waiting_for_policy_refresh_) { // Copy policy from the broker. bundle->Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( - broker->store()->policy_map()); + broker->core()->store()->policy_map()); } else { // Wait for the refresh to finish. return; diff --git a/chrome/browser/policy/device_local_account_policy_service.cc b/chrome/browser/policy/device_local_account_policy_service.cc index e44384e..ca6aa22 100644 --- a/chrome/browser/policy/device_local_account_policy_service.cc +++ b/chrome/browser/policy/device_local_account_policy_service.cc @@ -8,7 +8,7 @@ #include "base/message_loop.h" #include "chrome/browser/policy/cloud_policy_client.h" #include "chrome/browser/policy/cloud_policy_refresh_scheduler.h" -#include "chrome/browser/policy/cloud_policy_service.h" +#include "chrome/browser/policy/device_local_account_policy_store.h" #include "chrome/browser/policy/device_management_service.h" #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" #include "chrome/browser/policy/proto/device_management_backend.pb.h" @@ -20,48 +20,35 @@ namespace em = enterprise_management; namespace policy { DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( - const std::string& account_id, - chromeos::SessionManagerClient* session_manager_client, - chromeos::DeviceSettingsService* device_settings_service) - : account_id_(account_id), - store_(account_id, session_manager_client, device_settings_service) {} + scoped_ptr<DeviceLocalAccountPolicyStore> store) + : store_(store.Pass()), + core_(store_.get()) { +} DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {} -void DeviceLocalAccountPolicyBroker::RefreshPolicy( - const base::Closure& callback) { - if (service_.get()) - service_->RefreshPolicy(callback); - else - callback.Run(); +const std::string& DeviceLocalAccountPolicyBroker::account_id() const { + return store_->account_id(); } void DeviceLocalAccountPolicyBroker::Connect( scoped_ptr<CloudPolicyClient> client) { - DCHECK(!client_.get()); - client_ = client.Pass(); - service_.reset(new CloudPolicyService(client_.get(), &store_)); - refresh_scheduler_.reset( - new CloudPolicyRefreshScheduler( - client_.get(), &store_, - MessageLoop::current()->message_loop_proxy())); + core_.Connect(client.Pass()); + core_.StartRefreshScheduler(); UpdateRefreshDelay(); } void DeviceLocalAccountPolicyBroker::Disconnect() { - DCHECK(client_.get()); - refresh_scheduler_.reset(); - service_.reset(); - client_.reset(); + core_.Disconnect(); } void DeviceLocalAccountPolicyBroker::UpdateRefreshDelay() { - if (refresh_scheduler_) { + if (core_.refresh_scheduler()) { const Value* policy_value = - store_.policy_map().GetValue(key::kPolicyRefreshRate); + store_->policy_map().GetValue(key::kPolicyRefreshRate); int delay = 0; if (policy_value && policy_value->GetAsInteger(&delay)) - refresh_scheduler_->SetRefreshDelay(delay); + core_.refresh_scheduler()->SetRefreshDelay(delay); } } @@ -88,7 +75,7 @@ void DeviceLocalAccountPolicyService::Connect( // Connect the brokers. for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); broker != policy_brokers_.end(); ++broker) { - DCHECK(!broker->second->client()); + DCHECK(!broker->second->core()->client()); broker->second->Connect( CreateClientForAccount(broker->second->account_id()).Pass()); } @@ -172,7 +159,7 @@ void DeviceLocalAccountPolicyService::UpdateAccountList( // Fire up the cloud connection for fetching policy for the account from // the cloud if this is an enterprise-managed device. - if (!new_broker || !new_broker->client()) { + if (!new_broker || !new_broker->core()->client()) { scoped_ptr<CloudPolicyClient> client( CreateClientForAccount(entry->id())); if (client.get()) { @@ -192,11 +179,13 @@ void DeviceLocalAccountPolicyService::UpdateAccountList( scoped_ptr<DeviceLocalAccountPolicyBroker> DeviceLocalAccountPolicyService::CreateBroker( const std::string& account_id) { + scoped_ptr<DeviceLocalAccountPolicyStore> store( + new DeviceLocalAccountPolicyStore(account_id, session_manager_client_, + device_settings_service_)); scoped_ptr<DeviceLocalAccountPolicyBroker> broker( - new DeviceLocalAccountPolicyBroker(account_id, session_manager_client_, - device_settings_service_)); - broker->store()->AddObserver(this); - broker->store()->Load(); + new DeviceLocalAccountPolicyBroker(store.Pass())); + broker->core()->store()->AddObserver(this); + broker->core()->store()->Load(); return broker.Pass(); } @@ -204,7 +193,7 @@ void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { for (PolicyBrokerMap::iterator broker = map->begin(); broker != map->end(); ++broker) { if (broker->second) { - broker->second->store()->RemoveObserver(this); + broker->second->core()->store()->RemoveObserver(this); delete broker->second; } } @@ -216,7 +205,7 @@ DeviceLocalAccountPolicyBroker* CloudPolicyStore* store) { for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); broker != policy_brokers_.end(); ++broker) { - if (broker->second->store() == store) + if (broker->second->core()->store() == store) return broker->second; } return NULL; diff --git a/chrome/browser/policy/device_local_account_policy_service.h b/chrome/browser/policy/device_local_account_policy_service.h index c81aad2..a2061ba 100644 --- a/chrome/browser/policy/device_local_account_policy_service.h +++ b/chrome/browser/policy/device_local_account_policy_service.h @@ -14,8 +14,8 @@ #include "base/memory/scoped_ptr.h" #include "base/observer_list.h" #include "chrome/browser/chromeos/settings/device_settings_service.h" +#include "chrome/browser/policy/cloud_policy_core.h" #include "chrome/browser/policy/cloud_policy_store.h" -#include "chrome/browser/policy/device_local_account_policy_store.h" namespace chromeos { class SessionManagerClient; @@ -24,31 +24,21 @@ class SessionManagerClient; namespace policy { class CloudPolicyClient; -class CloudPolicyRefreshScheduler; -class CloudPolicyService; +class DeviceLocalAccountPolicyStore; class DeviceManagementService; -// This class manages the policy settings for a single device-local account, -// hosting the corresponding DeviceLocalAccountPolicyStore as well as the -// CloudPolicyClient (for updating the policy from the cloud) if applicable. +// The main switching central that downloads, caches, refreshes, etc. policy for +// a single device-local account. class DeviceLocalAccountPolicyBroker { public: - DeviceLocalAccountPolicyBroker( - const std::string& account_id, - chromeos::SessionManagerClient* session_manager_client, - chromeos::DeviceSettingsService* device_settings_service); + explicit DeviceLocalAccountPolicyBroker( + scoped_ptr<DeviceLocalAccountPolicyStore> store); ~DeviceLocalAccountPolicyBroker(); - CloudPolicyStore* store() { return &store_; } - const CloudPolicyStore* store() const { return &store_; } - - CloudPolicyClient* client() { return client_.get(); } - const CloudPolicyClient* client() const { return client_.get(); } + const std::string& account_id() const; - const std::string& account_id() const { return account_id_; } - - // Refreshes policy (if applicable) and invokes |callback| when done. - void RefreshPolicy(const base::Closure& callback); + CloudPolicyCore* core() { return &core_; } + const CloudPolicyCore* core() const { return &core_; } // Establish a cloud connection for the service. void Connect(scoped_ptr<CloudPolicyClient> client); @@ -56,17 +46,13 @@ class DeviceLocalAccountPolicyBroker { // Destroy the cloud connection, stopping policy refreshes. void Disconnect(); - // Updates the refresh scheduler's delay from the key::kPolicyRefreshRate - // policy in |store_|. + // Reads the refresh delay from policy and configures the refresh scheduler. void UpdateRefreshDelay(); private: const std::string account_id_; - - DeviceLocalAccountPolicyStore store_; - scoped_ptr<CloudPolicyClient> client_; - scoped_ptr<CloudPolicyService> service_; - scoped_ptr<CloudPolicyRefreshScheduler> refresh_scheduler_; + scoped_ptr<DeviceLocalAccountPolicyStore> store_; + CloudPolicyCore core_; DISALLOW_COPY_AND_ASSIGN(DeviceLocalAccountPolicyBroker); }; diff --git a/chrome/browser/policy/device_local_account_policy_service_unittest.cc b/chrome/browser/policy/device_local_account_policy_service_unittest.cc index e2187a0..879c9c0 100644 --- a/chrome/browser/policy/device_local_account_policy_service_unittest.cc +++ b/chrome/browser/policy/device_local_account_policy_service_unittest.cc @@ -9,6 +9,7 @@ #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" #include "chrome/browser/policy/cloud_policy_client.h" #include "chrome/browser/policy/cloud_policy_constants.h" +#include "chrome/browser/policy/cloud_policy_service.h" #include "chrome/browser/policy/device_local_account_policy_provider.h" #include "chrome/browser/policy/mock_configuration_policy_provider.h" #include "chrome/browser/policy/mock_device_management_service.h" @@ -96,10 +97,10 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, GetBroker) { service_.GetBrokerForAccount(PolicyBuilder::kFakeUsername); ASSERT_TRUE(broker); EXPECT_EQ(PolicyBuilder::kFakeUsername, broker->account_id()); - ASSERT_TRUE(broker->store()); - EXPECT_EQ(CloudPolicyStore::STATUS_OK, broker->store()->status()); - EXPECT_FALSE(broker->client()); - EXPECT_TRUE(broker->store()->policy_map().empty()); + ASSERT_TRUE(broker->core()->store()); + EXPECT_EQ(CloudPolicyStore::STATUS_OK, broker->core()->store()->status()); + EXPECT_FALSE(broker->core()->client()); + EXPECT_TRUE(broker->core()->store()->policy_map().empty()); } TEST_F(DeviceLocalAccountPolicyServiceTest, LoadNoPolicy) { @@ -112,9 +113,10 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, LoadNoPolicy) { FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); - ASSERT_TRUE(broker->store()); - EXPECT_EQ(CloudPolicyStore::STATUS_LOAD_ERROR, broker->store()->status()); - EXPECT_TRUE(broker->store()->policy_map().empty()); + ASSERT_TRUE(broker->core()->store()); + EXPECT_EQ(CloudPolicyStore::STATUS_LOAD_ERROR, + broker->core()->store()->status()); + EXPECT_TRUE(broker->core()->store()->policy_map().empty()); } TEST_F(DeviceLocalAccountPolicyServiceTest, LoadValidationFailure) { @@ -132,10 +134,10 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, LoadValidationFailure) { FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); - ASSERT_TRUE(broker->store()); + ASSERT_TRUE(broker->core()->store()); EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, - broker->store()->status()); - EXPECT_TRUE(broker->store()->policy_map().empty()); + broker->core()->store()->status()); + EXPECT_TRUE(broker->core()->store()->policy_map().empty()); } TEST_F(DeviceLocalAccountPolicyServiceTest, LoadPolicy) { @@ -150,12 +152,14 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, LoadPolicy) { FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); - ASSERT_TRUE(broker->store()); - EXPECT_EQ(CloudPolicyStore::STATUS_OK, broker->store()->status()); - ASSERT_TRUE(broker->store()->policy()); + ASSERT_TRUE(broker->core()->store()); + EXPECT_EQ(CloudPolicyStore::STATUS_OK, + broker->core()->store()->status()); + ASSERT_TRUE(broker->core()->store()->policy()); EXPECT_EQ(device_local_account_policy_.policy_data().SerializeAsString(), - broker->store()->policy()->SerializeAsString()); - EXPECT_TRUE(expected_policy_map_.Equals(broker->store()->policy_map())); + broker->core()->store()->policy()->SerializeAsString()); + EXPECT_TRUE(expected_policy_map_.Equals( + broker->core()->store()->policy_map())); } TEST_F(DeviceLocalAccountPolicyServiceTest, StoreValidationFailure) { @@ -168,16 +172,16 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, StoreValidationFailure) { DeviceLocalAccountPolicyBroker* broker = service_.GetBrokerForAccount(PolicyBuilder::kFakeUsername); ASSERT_TRUE(broker); - ASSERT_TRUE(broker->store()); - broker->store()->Store(device_local_account_policy_.policy()); + ASSERT_TRUE(broker->core()->store()); + broker->core()->store()->Store(device_local_account_policy_.policy()); FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); - ASSERT_TRUE(broker->store()); + ASSERT_TRUE(broker->core()->store()); EXPECT_EQ(CloudPolicyStore::STATUS_VALIDATION_ERROR, - broker->store()->status()); + broker->core()->store()->status()); EXPECT_EQ(CloudPolicyValidatorBase::VALIDATION_WRONG_POLICY_TYPE, - broker->store()->validation_status()); + broker->core()->store()->validation_status()); } TEST_F(DeviceLocalAccountPolicyServiceTest, StorePolicy) { @@ -187,8 +191,8 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, StorePolicy) { DeviceLocalAccountPolicyBroker* broker = service_.GetBrokerForAccount(PolicyBuilder::kFakeUsername); ASSERT_TRUE(broker); - ASSERT_TRUE(broker->store()); - broker->store()->Store(device_local_account_policy_.policy()); + ASSERT_TRUE(broker->core()->store()); + broker->core()->store()->Store(device_local_account_policy_.policy()); FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); @@ -222,7 +226,7 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, FetchPolicy) { ASSERT_TRUE(broker); service_.Connect(&mock_device_management_service_); - EXPECT_TRUE(broker->client()); + EXPECT_TRUE(broker->core()->client()); em::DeviceManagementRequest request; em::DeviceManagementResponse response; @@ -240,7 +244,7 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, FetchPolicy) { _)) .WillOnce(SaveArg<6>(&request)); EXPECT_CALL(service_observer_, OnPolicyUpdated(PolicyBuilder::kFakeUsername)); - broker->client()->FetchPolicy(); + broker->core()->client()->FetchPolicy(); FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&service_observer_); Mock::VerifyAndClearExpectations(&mock_device_management_service_); @@ -252,17 +256,19 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, FetchPolicy) { EXPECT_EQ(PolicyBuilder::kFakeUsername, request.policy_request().request(0).settings_entity_id()); - ASSERT_TRUE(broker->store()); - EXPECT_EQ(CloudPolicyStore::STATUS_OK, broker->store()->status()); - ASSERT_TRUE(broker->store()->policy()); + ASSERT_TRUE(broker->core()->store()); + EXPECT_EQ(CloudPolicyStore::STATUS_OK, + broker->core()->store()->status()); + ASSERT_TRUE(broker->core()->store()->policy()); EXPECT_EQ(device_local_account_policy_.policy_data().SerializeAsString(), - broker->store()->policy()->SerializeAsString()); - EXPECT_TRUE(expected_policy_map_.Equals(broker->store()->policy_map())); + broker->core()->store()->policy()->SerializeAsString()); + EXPECT_TRUE(expected_policy_map_.Equals( + broker->core()->store()->policy_map())); EXPECT_CALL(service_observer_, OnPolicyUpdated(PolicyBuilder::kFakeUsername)).Times(0); service_.Disconnect(); - EXPECT_FALSE(broker->client()); + EXPECT_FALSE(broker->core()->client()); Mock::VerifyAndClearExpectations(&service_observer_); } @@ -276,6 +282,7 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, RefreshPolicy) { ASSERT_TRUE(broker); service_.Connect(&mock_device_management_service_); + ASSERT_TRUE(broker->core()->service()); em::DeviceManagementResponse response; response.mutable_policy_response()->add_response()->CopyFrom( @@ -285,7 +292,7 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, RefreshPolicy) { EXPECT_CALL(mock_device_management_service_, StartJob(_, _, _, _, _, _, _)); EXPECT_CALL(*this, OnRefreshDone()).Times(1); EXPECT_CALL(service_observer_, OnPolicyUpdated(PolicyBuilder::kFakeUsername)); - broker->RefreshPolicy( + broker->core()->service()->RefreshPolicy( base::Bind(&DeviceLocalAccountPolicyServiceTest::OnRefreshDone, base::Unretained(this))); FlushDeviceSettings(); @@ -293,9 +300,11 @@ TEST_F(DeviceLocalAccountPolicyServiceTest, RefreshPolicy) { Mock::VerifyAndClearExpectations(this); Mock::VerifyAndClearExpectations(&mock_device_management_service_); - ASSERT_TRUE(broker->store()); - EXPECT_EQ(CloudPolicyStore::STATUS_OK, broker->store()->status()); - EXPECT_TRUE(expected_policy_map_.Equals(broker->store()->policy_map())); + ASSERT_TRUE(broker->core()->store()); + EXPECT_EQ(CloudPolicyStore::STATUS_OK, + broker->core()->store()->status()); + EXPECT_TRUE(expected_policy_map_.Equals( + broker->core()->store()->policy_map())); } class DeviceLocalAccountPolicyProviderTest @@ -376,7 +385,7 @@ TEST_F(DeviceLocalAccountPolicyProviderTest, Policy) { DeviceLocalAccountPolicyBroker* broker = service_.GetBrokerForAccount(PolicyBuilder::kFakeUsername); ASSERT_TRUE(broker); - broker->store()->Load(); + broker->core()->store()->Load(); FlushDeviceSettings(); Mock::VerifyAndClearExpectations(&provider_observer_); @@ -416,7 +425,7 @@ TEST_F(DeviceLocalAccountPolicyProviderTest, RefreshPolicies) { DeviceLocalAccountPolicyBroker* broker = service_.GetBrokerForAccount(PolicyBuilder::kFakeUsername); ASSERT_TRUE(broker); - EXPECT_FALSE(broker->client()); + EXPECT_FALSE(broker->core()->client()); EXPECT_CALL(provider_observer_, OnUpdatePolicy(&provider_)).Times(AtLeast(1)); provider_.RefreshPolicies(); Mock::VerifyAndClearExpectations(&provider_observer_); diff --git a/chrome/browser/policy/device_local_account_policy_store.h b/chrome/browser/policy/device_local_account_policy_store.h index 4388b6a..5a987d5 100644 --- a/chrome/browser/policy/device_local_account_policy_store.h +++ b/chrome/browser/policy/device_local_account_policy_store.h @@ -39,6 +39,8 @@ class DeviceLocalAccountPolicyStore chromeos::DeviceSettingsService* device_settings_service); virtual ~DeviceLocalAccountPolicyStore(); + const std::string& account_id() const { return account_id_; } + // CloudPolicyStore: virtual void Store( const enterprise_management::PolicyFetchResponse& policy) OVERRIDE; diff --git a/chrome/browser/policy/user_cloud_policy_manager.cc b/chrome/browser/policy/user_cloud_policy_manager.cc index 53ae4fe..70009fb 100644 --- a/chrome/browser/policy/user_cloud_policy_manager.cc +++ b/chrome/browser/policy/user_cloud_policy_manager.cc @@ -27,31 +27,32 @@ UserCloudPolicyManager::~UserCloudPolicyManager() { UserCloudPolicyManagerFactory::GetInstance()->Unregister(profile_, this); } -void UserCloudPolicyManager::Initialize( +void UserCloudPolicyManager::Connect( PrefService* local_state, DeviceManagementService* device_management_service) { - InitializeService( + core()->Connect( make_scoped_ptr(new CloudPolicyClient(std::string(), std::string(), USER_AFFILIATION_NONE, CloudPolicyClient::POLICY_TYPE_USER, NULL, device_management_service))); - StartRefreshScheduler(local_state, prefs::kUserPolicyRefreshRate); + core()->StartRefreshScheduler(); + core()->TrackRefreshDelayPref(local_state, prefs::kUserPolicyRefreshRate); } -void UserCloudPolicyManager::ShutdownAndRemovePolicy() { - ShutdownService(); +void UserCloudPolicyManager::DisconnectAndRemovePolicy() { + core()->Disconnect(); store_->Clear(); } bool UserCloudPolicyManager::IsClientRegistered() const { - return cloud_policy_client() && cloud_policy_client()->is_registered(); + return client() && client()->is_registered(); } void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { - DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; - if (!cloud_policy_client()->is_registered()) { + DCHECK(client()) << "Callers must invoke Initialize() first"; + if (!client()->is_registered()) { DVLOG(1) << "Registering client with access token: " << access_token; - cloud_policy_client()->Register(access_token); + client()->Register(access_token); } } diff --git a/chrome/browser/policy/user_cloud_policy_manager.h b/chrome/browser/policy/user_cloud_policy_manager.h index 2b95c80..c2f5b99 100644 --- a/chrome/browser/policy/user_cloud_policy_manager.h +++ b/chrome/browser/policy/user_cloud_policy_manager.h @@ -17,7 +17,6 @@ class Profile; namespace policy { -class CloudPolicyClient; class DeviceManagementService; class UserCloudPolicyStore; @@ -32,14 +31,14 @@ class UserCloudPolicyManager : public CloudPolicyManager { // Initializes the cloud connection. |local_state| and // |device_management_service| must stay valid until this object is deleted or // ShutdownAndRemovePolicy() gets called. Virtual for mocking. - virtual void Initialize(PrefService* local_state, - DeviceManagementService* device_management_service); + virtual void Connect(PrefService* local_state, + DeviceManagementService* device_management_service); // Shuts down the UserCloudPolicyManager (removes and stops refreshing the // cached cloud policy). This is typically called when a profile is being // disassociated from a given user (e.g. during signout). No policy will be // provided by this object until the next time Initialize() is invoked. - void ShutdownAndRemovePolicy(); + void DisconnectAndRemovePolicy(); // Returns true if the underlying CloudPolicyClient is already registered. // Virtual for mocking. diff --git a/chrome/browser/policy/user_cloud_policy_manager_chromeos.cc b/chrome/browser/policy/user_cloud_policy_manager_chromeos.cc index 15df5ef..51feede 100644 --- a/chrome/browser/policy/user_cloud_policy_manager_chromeos.cc +++ b/chrome/browser/policy/user_cloud_policy_manager_chromeos.cc @@ -27,12 +27,12 @@ void UserCloudPolicyManagerChromeOS::Connect( DCHECK(device_management_service); DCHECK(local_state); local_state_ = local_state; - scoped_ptr<CloudPolicyClient> client( + scoped_ptr<CloudPolicyClient> cloud_policy_client( new CloudPolicyClient(std::string(), std::string(), user_affiliation, CloudPolicyClient::POLICY_TYPE_USER, NULL, device_management_service)); - InitializeService(client.Pass()); - cloud_policy_client()->AddObserver(this); + core()->Connect(cloud_policy_client.Pass()); + client()->AddObserver(this); if (wait_for_policy_fetch_) { // If we are supposed to wait for a policy fetch, we trigger an explicit @@ -40,8 +40,8 @@ void UserCloudPolicyManagerChromeOS::Connect( // done. The refresh scheduler only gets started once that refresh // completes. Note that we might have to wait for registration to happen, // see OnRegistrationStateChanged() below. - if (cloud_policy_client()->is_registered()) { - cloud_policy_service()->RefreshPolicy( + if (client()->is_registered()) { + service()->RefreshPolicy( base::Bind( &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, base::Unretained(this))); @@ -57,26 +57,28 @@ void UserCloudPolicyManagerChromeOS::CancelWaitForPolicyFetch() { // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler // can be started. - if (cloud_policy_service() && local_state_) - StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate); + if (service() && local_state_) { + core()->StartRefreshScheduler(); + core()->TrackRefreshDelayPref(local_state_, prefs::kUserPolicyRefreshRate); + } } bool UserCloudPolicyManagerChromeOS::IsClientRegistered() const { - return cloud_policy_client() && cloud_policy_client()->is_registered(); + return client() && client()->is_registered(); } void UserCloudPolicyManagerChromeOS::RegisterClient( const std::string& access_token) { - DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; - if (!cloud_policy_client()->is_registered()) { + DCHECK(client()) << "Callers must invoke Initialize() first"; + if (!client()->is_registered()) { DVLOG(1) << "Registering client with access token: " << access_token; - cloud_policy_client()->Register(access_token); + client()->Register(access_token); } } void UserCloudPolicyManagerChromeOS::Shutdown() { - if (cloud_policy_client()) - cloud_policy_client()->RemoveObserver(this); + if (client()) + client()->RemoveObserver(this); CloudPolicyManager::Shutdown(); } @@ -92,12 +94,12 @@ void UserCloudPolicyManagerChromeOS::OnPolicyFetched( } void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( - CloudPolicyClient* client) { - DCHECK_EQ(cloud_policy_client(), client); + CloudPolicyClient* cloud_policy_client) { + DCHECK_EQ(client(), cloud_policy_client); if (wait_for_policy_fetch_) { // If we're blocked on the policy fetch, now is a good time to issue it. - if (cloud_policy_client()->is_registered()) { - cloud_policy_service()->RefreshPolicy( + if (client()->is_registered()) { + service()->RefreshPolicy( base::Bind( &UserCloudPolicyManagerChromeOS::OnInitialPolicyFetchComplete, base::Unretained(this))); @@ -109,8 +111,9 @@ void UserCloudPolicyManagerChromeOS::OnRegistrationStateChanged( } } -void UserCloudPolicyManagerChromeOS::OnClientError(CloudPolicyClient* client) { - DCHECK_EQ(cloud_policy_client(), client); +void UserCloudPolicyManagerChromeOS::OnClientError( + CloudPolicyClient* cloud_policy_client) { + DCHECK_EQ(client(), cloud_policy_client); CancelWaitForPolicyFetch(); } diff --git a/chrome/browser/policy/user_cloud_policy_manager_chromeos_unittest.cc b/chrome/browser/policy/user_cloud_policy_manager_chromeos_unittest.cc index 8e0a0b8..207df7c 100644 --- a/chrome/browser/policy/user_cloud_policy_manager_chromeos_unittest.cc +++ b/chrome/browser/policy/user_cloud_policy_manager_chromeos_unittest.cc @@ -105,7 +105,7 @@ TEST_F(UserCloudPolicyManagerChromeOSTest, WaitForPolicyFetch) { EXPECT_CALL(device_management_service_, CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); - manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id"); + manager_->core()->client()->SetupRegistration("dm_token", "client_id"); ASSERT_TRUE(fetch_request); EXPECT_FALSE(manager_->IsInitializationComplete()); Mock::VerifyAndClearExpectations(&observer_); @@ -134,7 +134,7 @@ TEST_F(UserCloudPolicyManagerChromeOSTest, WaitForPolicyFetchError) { EXPECT_CALL(device_management_service_, CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); - manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id"); + manager_->core()->client()->SetupRegistration("dm_token", "client_id"); ASSERT_TRUE(fetch_request); EXPECT_FALSE(manager_->IsInitializationComplete()); Mock::VerifyAndClearExpectations(&observer_); diff --git a/chrome/browser/policy/user_cloud_policy_manager_unittest.cc b/chrome/browser/policy/user_cloud_policy_manager_unittest.cc index 32042c3..08520db 100644 --- a/chrome/browser/policy/user_cloud_policy_manager_unittest.cc +++ b/chrome/browser/policy/user_cloud_policy_manager_unittest.cc @@ -69,8 +69,9 @@ class UserCloudPolicyManagerTest : public testing::Test { DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); }; -TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { - // Load policy, make sure it goes away when ShutdownAndRemove() is called. +TEST_F(UserCloudPolicyManagerTest, DisconnectAndRemovePolicy) { + // Load policy, make sure it goes away when DisconnectAndRemovePolicy() is + // called. CreateManager(); store_->policy_map_.CopyFrom(policy_map_); EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); @@ -78,8 +79,8 @@ TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); EXPECT_TRUE(manager_->IsInitializationComplete()); EXPECT_CALL(*store_, Clear()); - manager_->ShutdownAndRemovePolicy(); - EXPECT_FALSE(manager_->cloud_policy_service()); + manager_->DisconnectAndRemovePolicy(); + EXPECT_FALSE(manager_->core()->service()); } } // namespace diff --git a/chrome/browser/policy/user_policy_signin_service.cc b/chrome/browser/policy/user_policy_signin_service.cc index 2eb6812..7b1cd28 100644 --- a/chrome/browser/policy/user_policy_signin_service.cc +++ b/chrome/browser/policy/user_policy_signin_service.cc @@ -64,8 +64,8 @@ UserPolicySigninService::~UserPolicySigninService() {} void UserPolicySigninService::StopObserving() { UserCloudPolicyManager* manager = GetManager(); - if (manager && manager->cloud_policy_service()) - manager->cloud_policy_service()->RemoveObserver(this); + if (manager && manager->core()->service()) + manager->core()->service()->RemoveObserver(this); } void UserPolicySigninService::Observe( @@ -114,10 +114,10 @@ void UserPolicySigninService::ConfigureUserCloudPolicyManager() { if (signin_manager->GetAuthenticatedUsername().empty()) { // User has signed out - remove existing policy. StopObserving(); - manager->ShutdownAndRemovePolicy(); + manager->DisconnectAndRemovePolicy(); } else { // Initialize the UserCloudPolicyManager if it isn't already initialized. - if (!manager->cloud_policy_service()) { + if (!manager->core()->service()) { // Make sure we've initialized the DeviceManagementService. It's OK to // call this multiple times so we do it every time we initialize the // UserCloudPolicyManager. @@ -128,14 +128,14 @@ void UserPolicySigninService::ConfigureUserCloudPolicyManager() { // the OnInitializationCompleted() callback is invoked. policy::DeviceManagementService* service = g_browser_process-> browser_policy_connector()->device_management_service(); - manager->Initialize(g_browser_process->local_state(), service); - DCHECK(manager->cloud_policy_service()); - manager->cloud_policy_service()->AddObserver(this); + manager->Connect(g_browser_process->local_state(), service); + DCHECK(manager->core()->service()); + manager->core()->service()->AddObserver(this); } // If the CloudPolicyService is initialized, but the CloudPolicyClient still // needs to be registered, kick off registration. - if (manager->cloud_policy_service()->IsInitializationComplete() && + if (manager->core()->service()->IsInitializationComplete() && !manager->IsClientRegistered()) { RegisterCloudPolicyService(); } @@ -145,7 +145,7 @@ void UserPolicySigninService::ConfigureUserCloudPolicyManager() { void UserPolicySigninService::OnInitializationCompleted( CloudPolicyService* service) { UserCloudPolicyManager* manager = GetManager(); - DCHECK_EQ(service, manager->cloud_policy_service()); + DCHECK_EQ(service, manager->core()->service()); DCHECK(service->IsInitializationComplete()); // The service is now initialized - if the client is not yet registered, then // it means that there is no cached policy and so we need to initiate a new diff --git a/chrome/browser/policy/user_policy_signin_service_unittest.cc b/chrome/browser/policy/user_policy_signin_service_unittest.cc index f407452..0689e55 100644 --- a/chrome/browser/policy/user_policy_signin_service_unittest.cc +++ b/chrome/browser/policy/user_policy_signin_service_unittest.cc @@ -118,7 +118,7 @@ TEST_F(UserPolicySigninServiceTest, InitWhileSignedOut) { content::NotificationService::NoDetails()); // UserCloudPolicyManager should not be initialized. - ASSERT_FALSE(manager_->cloud_policy_service()); + ASSERT_FALSE(manager_->core()->service()); } TEST_F(UserPolicySigninServiceTest, InitWhileSignedIn) { @@ -133,7 +133,7 @@ TEST_F(UserPolicySigninServiceTest, InitWhileSignedIn) { content::NotificationService::NoDetails()); // UserCloudPolicyManager should be initialized. - ASSERT_TRUE(manager_->cloud_policy_service()); + ASSERT_TRUE(manager_->core()->service()); // Complete initialization of the store. mock_store_->NotifyStoreLoaded(); @@ -159,7 +159,7 @@ TEST_F(UserPolicySigninServiceTest, SignInAfterInit) { // UserCloudPolicyManager should not be initialized since there is no // signed-in user. - ASSERT_FALSE(manager_->cloud_policy_service()); + ASSERT_FALSE(manager_->core()->service()); // Now sign in the user. SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( @@ -173,7 +173,7 @@ TEST_F(UserPolicySigninServiceTest, SignInAfterInit) { GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); // UserCloudPolicyManager should be initialized. - ASSERT_TRUE(manager_->cloud_policy_service()); + ASSERT_TRUE(manager_->core()->service()); // Client registration should be in progress since we have an oauth token. ASSERT_TRUE(IsRequestActive()); @@ -189,7 +189,7 @@ TEST_F(UserPolicySigninServiceTest, UnregisteredClient) { // UserCloudPolicyManager should not be initialized since there is no // signed-in user. - ASSERT_FALSE(manager_->cloud_policy_service()); + ASSERT_FALSE(manager_->core()->service()); // Now sign in the user. SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( @@ -200,7 +200,7 @@ TEST_F(UserPolicySigninServiceTest, UnregisteredClient) { GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); // UserCloudPolicyManager should be initialized. - ASSERT_TRUE(manager_->cloud_policy_service()); + ASSERT_TRUE(manager_->core()->service()); // Client registration should not be in progress since the store is not // yet initialized. @@ -223,7 +223,7 @@ TEST_F(UserPolicySigninServiceTest, RegisteredClient) { // UserCloudPolicyManager should not be initialized since there is no // signed-in user. - ASSERT_FALSE(manager_->cloud_policy_service()); + ASSERT_FALSE(manager_->core()->service()); // Now sign in the user. SigninManagerFactory::GetForProfile(profile_.get())->SetAuthenticatedUsername( @@ -234,7 +234,7 @@ TEST_F(UserPolicySigninServiceTest, RegisteredClient) { GaiaConstants::kGaiaOAuth2LoginRefreshToken, "oauth_login_refresh_token"); // UserCloudPolicyManager should be initialized. - ASSERT_TRUE(manager_->cloud_policy_service()); + ASSERT_TRUE(manager_->core()->service()); // Client registration should not be in progress since the store is not // yet initialized. @@ -267,13 +267,13 @@ TEST_F(UserPolicySigninServiceTest, SignOutAfterInit) { content::NotificationService::NoDetails()); // UserCloudPolicyManager should be initialized. - ASSERT_TRUE(manager_->cloud_policy_service()); + ASSERT_TRUE(manager_->core()->service()); // Now sign out. SigninManagerFactory::GetForProfile(profile_.get())->SignOut(); // UserCloudPolicyManager should be shut down. - ASSERT_FALSE(manager_->cloud_policy_service()); + ASSERT_FALSE(manager_->core()->service()); } } // namespace diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index 11c37d9..92f9389 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1311,6 +1311,8 @@ 'browser/policy/cloud_policy_constants.h', 'browser/policy/cloud_policy_controller.cc', 'browser/policy/cloud_policy_controller.h', + 'browser/policy/cloud_policy_core.cc', + 'browser/policy/cloud_policy_core.h', 'browser/policy/cloud_policy_data_store.cc', 'browser/policy/cloud_policy_data_store.h', 'browser/policy/cloud_policy_manager.cc', diff --git a/chrome/chrome_tests_unit.gypi b/chrome/chrome_tests_unit.gypi index e1c8567..e58fc98 100644 --- a/chrome/chrome_tests_unit.gypi +++ b/chrome/chrome_tests_unit.gypi @@ -894,6 +894,7 @@ 'browser/policy/auto_enrollment_client_unittest.cc', 'browser/policy/cloud_policy_client_unittest.cc', 'browser/policy/cloud_policy_controller_unittest.cc', + 'browser/policy/cloud_policy_core_unittest.cc', 'browser/policy/cloud_policy_manager_unittest.cc', 'browser/policy/cloud_policy_provider_unittest.cc', 'browser/policy/cloud_policy_refresh_scheduler_unittest.cc', |