diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-13 13:32:27 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-12-13 13:32:27 +0000 |
commit | bc9eb133c83b85edce41755938e49d34e2f6c1d7 (patch) | |
tree | af5740e1475ccb6ffd01c4ae745b0e307ad6fbfa /components | |
parent | 1060d1c1f62bcef207aa7da4d722e71cf01f2acf (diff) | |
download | chromium_src-bc9eb133c83b85edce41755938e49d34e2f6c1d7.zip chromium_src-bc9eb133c83b85edce41755938e49d34e2f6c1d7.tar.gz chromium_src-bc9eb133c83b85edce41755938e49d34e2f6c1d7.tar.bz2 |
Move PolicyHeaderService/PolicyHeaderIOHelper to policy/core/common/cloud.
BUG=326799
TBR=jochen
Review URL: https://codereview.chromium.org/109993007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@240612 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r-- | components/components_tests.gyp | 3 | ||||
-rw-r--r-- | components/policy.gypi | 6 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_io_helper.cc (renamed from components/policy/core/browser/policy_header_io_helper.cc) | 2 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_io_helper.h (renamed from components/policy/core/browser/policy_header_io_helper.h) | 6 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_io_helper_unittest.cc (renamed from components/policy/core/browser/policy_header_io_helper_unittest.cc) | 5 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_service.cc | 59 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_service.h | 70 | ||||
-rw-r--r-- | components/policy/core/common/cloud/policy_header_service_unittest.cc | 122 |
8 files changed, 264 insertions, 9 deletions
diff --git a/components/components_tests.gyp b/components/components_tests.gyp index 2229538..294970f 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -197,7 +197,6 @@ 'components.gyp:policy_component_test_support', ], 'sources': [ - 'policy/core/browser/policy_header_io_helper_unittest.cc', 'policy/core/common/async_policy_provider_unittest.cc', 'policy/core/common/cloud/cloud_policy_client_unittest.cc', 'policy/core/common/cloud/cloud_policy_core_unittest.cc', @@ -211,6 +210,8 @@ 'policy/core/common/cloud/device_management_service_unittest.cc', 'policy/core/common/cloud/external_policy_data_fetcher_unittest.cc', 'policy/core/common/cloud/external_policy_data_updater_unittest.cc', + 'policy/core/common/cloud/policy_header_io_helper_unittest.cc', + 'policy/core/common/cloud/policy_header_service_unittest.cc', 'policy/core/common/cloud/rate_limiter_unittest.cc', 'policy/core/common/cloud/resource_cache_unittest.cc', 'policy/core/common/cloud/user_cloud_policy_manager_unittest.cc', diff --git a/components/policy.gypi b/components/policy.gypi index a69a1f1..4e05b54 100644 --- a/components/policy.gypi +++ b/components/policy.gypi @@ -41,8 +41,6 @@ 'policy/core/browser/configuration_policy_pref_store.h', 'policy/core/browser/policy_error_map.cc', 'policy/core/browser/policy_error_map.h', - 'policy/core/browser/policy_header_io_helper.cc', - 'policy/core/browser/policy_header_io_helper.h', 'policy/core/common/cloud/cloud_external_data_manager.cc', 'policy/core/common/cloud/cloud_external_data_manager.h', 'policy/core/common/cloud/cloud_policy_client.cc', @@ -77,6 +75,10 @@ 'policy/core/common/cloud/external_policy_data_fetcher.h', 'policy/core/common/cloud/external_policy_data_updater.cc', 'policy/core/common/cloud/external_policy_data_updater.h', + 'policy/core/common/cloud/policy_header_io_helper.cc', + 'policy/core/common/cloud/policy_header_io_helper.h', + 'policy/core/common/cloud/policy_header_service.cc', + 'policy/core/common/cloud/policy_header_service.h', 'policy/core/common/cloud/rate_limiter.cc', 'policy/core/common/cloud/rate_limiter.h', 'policy/core/common/cloud/resource_cache.cc', diff --git a/components/policy/core/browser/policy_header_io_helper.cc b/components/policy/core/common/cloud/policy_header_io_helper.cc index 1eb1898..a749dda 100644 --- a/components/policy/core/browser/policy_header_io_helper.cc +++ b/components/policy/core/common/cloud/policy_header_io_helper.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "components/policy/core/browser/policy_header_io_helper.h" +#include "components/policy/core/common/cloud/policy_header_io_helper.h" #include "base/bind.h" #include "base/location.h" diff --git a/components/policy/core/browser/policy_header_io_helper.h b/components/policy/core/common/cloud/policy_header_io_helper.h index 345b9cc..01a0765 100644 --- a/components/policy/core/browser/policy_header_io_helper.h +++ b/components/policy/core/common/cloud/policy_header_io_helper.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef COMPONENTS_POLICY_CORE_BROWSER_POLICY_HEADER_IO_HELPER_H_ -#define COMPONENTS_POLICY_CORE_BROWSER_POLICY_HEADER_IO_HELPER_H_ +#ifndef COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_ +#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_ #include <string> @@ -59,4 +59,4 @@ class POLICY_EXPORT PolicyHeaderIOHelper { } // namespace policy -#endif // COMPONENTS_POLICY_CORE_BROWSER_POLICY_HEADER_IO_HELPER_H_ +#endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_IO_HELPER_H_ diff --git a/components/policy/core/browser/policy_header_io_helper_unittest.cc b/components/policy/core/common/cloud/policy_header_io_helper_unittest.cc index 054168d..b86b260 100644 --- a/components/policy/core/browser/policy_header_io_helper_unittest.cc +++ b/components/policy/core/common/cloud/policy_header_io_helper_unittest.cc @@ -3,7 +3,7 @@ // found in the LICENSE file. #include "base/test/test_simple_task_runner.h" -#include "components/policy/core/browser/policy_header_io_helper.h" +#include "components/policy/core/common/cloud/policy_header_io_helper.h" #include "net/http/http_request_headers.h" #include "net/url_request/url_request_test_util.h" #include "testing/gtest/include/gtest/gtest.h" @@ -14,7 +14,6 @@ namespace { const char kDMServerURL[] = "http://server_url"; const char kPolicyHeaderName[] = "Chrome-Policy-Posture"; const char kInitialPolicyHeader[] = "initial_header"; -} // namespace class PolicyHeaderIOHelperTest : public testing::Test { public: @@ -47,6 +46,8 @@ class PolicyHeaderIOHelperTest : public testing::Test { scoped_refptr<base::TestSimpleTaskRunner> task_runner_; }; +} // namespace + TEST_F(PolicyHeaderIOHelperTest, InitialHeader) { net::TestURLRequest request( GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context_); diff --git a/components/policy/core/common/cloud/policy_header_service.cc b/components/policy/core/common/cloud/policy_header_service.cc new file mode 100644 index 0000000..af261f8 --- /dev/null +++ b/components/policy/core/common/cloud/policy_header_service.cc @@ -0,0 +1,59 @@ +// Copyright 2013 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 "components/policy/core/common/cloud/policy_header_service.h" + +#include "components/policy/core/common/cloud/cloud_policy_store.h" +#include "components/policy/core/common/cloud/policy_header_io_helper.h" + +namespace policy { + +PolicyHeaderService::PolicyHeaderService(const std::string& server_url, + CloudPolicyStore* user_policy_store, + CloudPolicyStore* device_policy_store) + : server_url_(server_url), + user_policy_store_(user_policy_store), + device_policy_store_(device_policy_store) { + user_policy_store_->AddObserver(this); + if (device_policy_store_) + device_policy_store_->AddObserver(this); +} + +PolicyHeaderService::~PolicyHeaderService() { + user_policy_store_->RemoveObserver(this); + if (device_policy_store_) + device_policy_store_->RemoveObserver(this); +} + +scoped_ptr<PolicyHeaderIOHelper> +PolicyHeaderService::CreatePolicyHeaderIOHelper( + scoped_refptr<base::SequencedTaskRunner> task_runner) { + std::string initial_header_value = CreateHeaderValue(); + scoped_ptr<PolicyHeaderIOHelper> helper = make_scoped_ptr( + new PolicyHeaderIOHelper(server_url_, initial_header_value, task_runner)); + helpers_.push_back(helper.get()); + return helper.Pass(); +} + +std::string PolicyHeaderService::CreateHeaderValue() { + // TODO(atwilson): Extract policy information and generate correct header. + return ""; +} + +void PolicyHeaderService::OnStoreLoaded(CloudPolicyStore* store) { + // If we have a PolicyHeaderIOHelper, notify it of the new header value. + if (!helpers_.empty()) { + std::string new_header = CreateHeaderValue(); + for (std::vector<PolicyHeaderIOHelper*>::const_iterator it = + helpers_.begin(); it != helpers_.end(); ++it) { + (*it)->UpdateHeader(new_header); + } + } +} + +void PolicyHeaderService::OnStoreError(CloudPolicyStore* store) { + // Do nothing on errors. +} + +} // namespace policy diff --git a/components/policy/core/common/cloud/policy_header_service.h b/components/policy/core/common/cloud/policy_header_service.h new file mode 100644 index 0000000..1d4346b --- /dev/null +++ b/components/policy/core/common/cloud/policy_header_service.h @@ -0,0 +1,70 @@ +// Copyright 2013 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 COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_SERVICE_H_ +#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_SERVICE_H_ + +#include <string> +#include <vector> + +#include "base/memory/ref_counted.h" +#include "base/memory/scoped_ptr.h" +#include "components/policy/core/common/cloud/cloud_policy_store.h" +#include "components/policy/policy_export.h" + +namespace base { +class SequencedTaskRunner; +} + +namespace policy { + +class PolicyHeaderIOHelper; + +// Per-profile service used to generate PolicyHeaderIOHelper objects, and +// keep them up-to-date as policy changes. +// TODO(atwilson): Move to components/policy once CloudPolicyStore is moved. +class POLICY_EXPORT PolicyHeaderService : public CloudPolicyStore::Observer { + public: + // |device_policy_store| can be null on platforms that do not support + // device policy. Both |user_policy_store| and |device_policy_store| must + // outlive this object. + PolicyHeaderService(const std::string& server_url, + CloudPolicyStore* user_policy_store, + CloudPolicyStore* device_policy_store); + virtual ~PolicyHeaderService(); + + // Creates a PolicyHeaderIOHelper object to be run on the IO thread and + // add policy headers to outgoing requests. The caller takes ownership of + // this object and must ensure it outlives ProfileHeaderService (in practice, + // this is called by ProfileIOData, which is shutdown *after* all + // ProfileKeyedServices are shutdown). + scoped_ptr<PolicyHeaderIOHelper> CreatePolicyHeaderIOHelper( + scoped_refptr<base::SequencedTaskRunner> task_runner); + + // Overridden CloudPolicyStore::Observer methods: + virtual void OnStoreLoaded(CloudPolicyStore* store) OVERRIDE; + virtual void OnStoreError(CloudPolicyStore* store) OVERRIDE; + + protected: + // Generate a policy header based on the currently loaded policy. + // Virtual to allow mocking in tests. + virtual std::string CreateHeaderValue(); + + private: + // Weak pointer to created PolicyHeaderIOHelper objects. + std::vector<PolicyHeaderIOHelper*> helpers_; + + // URL of the policy server. + std::string server_url_; + + // Weak pointers to User-/Device-level policy stores. + CloudPolicyStore* user_policy_store_; + CloudPolicyStore* device_policy_store_; + + DISALLOW_COPY_AND_ASSIGN(PolicyHeaderService); +}; + +} // namespace policy + +#endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_POLICY_HEADER_SERVICE_H_ diff --git a/components/policy/core/common/cloud/policy_header_service_unittest.cc b/components/policy/core/common/cloud/policy_header_service_unittest.cc new file mode 100644 index 0000000..d15725b --- /dev/null +++ b/components/policy/core/common/cloud/policy_header_service_unittest.cc @@ -0,0 +1,122 @@ +// Copyright 2013 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 "base/test/test_simple_task_runner.h" +#include "components/policy/core/common/cloud/mock_cloud_policy_store.h" +#include "components/policy/core/common/cloud/policy_header_io_helper.h" +#include "components/policy/core/common/cloud/policy_header_service.h" +#include "net/http/http_request_headers.h" +#include "net/url_request/url_request_test_util.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace policy { +using enterprise_management::PolicyData; + +namespace { +const char kDMServerURL[] = "http://server_url"; +const char kPolicyHeaderName[] = "Chrome-Policy-Posture"; +const char kExpectedPolicyHeader[] = "expected_header"; + +// Test version of the PolicyHeaderService that allows the tests to inject +// their own header values. +// TODO(atwilson): Remove this once PolicyHeaderService extracts the header +// directly from policy. +class TestPolicyHeaderService : public PolicyHeaderService { + public: + TestPolicyHeaderService(CloudPolicyStore* user_store, + CloudPolicyStore* device_store) + : PolicyHeaderService(kDMServerURL, user_store, device_store) { + } + + virtual ~TestPolicyHeaderService() {} + + void set_header(const std::string& header) { header_ = header; } + + protected: + virtual std::string CreateHeaderValue() OVERRIDE { return header_; } + + private: + std::string header_; +}; + +class TestCloudPolicyStore : public MockCloudPolicyStore { + public: + void SetPolicy(scoped_ptr<PolicyData> policy) { + policy_ = policy.Pass(); + // Notify observers. + NotifyStoreLoaded(); + } +}; + +class PolicyHeaderServiceTest : public testing::Test { + public: + PolicyHeaderServiceTest() { + task_runner_ = make_scoped_refptr(new base::TestSimpleTaskRunner()); + } + virtual ~PolicyHeaderServiceTest() {} + + virtual void SetUp() OVERRIDE { + service_.reset(new TestPolicyHeaderService(&user_store_, &device_store_)); + service_->set_header(kExpectedPolicyHeader); + helper_ = service_->CreatePolicyHeaderIOHelper(task_runner_).Pass(); + } + + virtual void TearDown() OVERRIDE { + task_runner_->RunUntilIdle(); + // Helper should outlive the service. + service_.reset(); + helper_.reset(); + } + + void ValidateHeader(const net::HttpRequestHeaders& headers, + bool should_exist) { + if (should_exist) { + std::string header; + EXPECT_TRUE(headers.GetHeader(kPolicyHeaderName, &header)); + EXPECT_EQ(header, kExpectedPolicyHeader); + } else { + EXPECT_TRUE(headers.IsEmpty()); + } + } + + base::MessageLoop loop_; + scoped_ptr<TestPolicyHeaderService> service_; + TestCloudPolicyStore user_store_; + TestCloudPolicyStore device_store_; + scoped_ptr<PolicyHeaderIOHelper> helper_; + scoped_refptr<base::TestSimpleTaskRunner> task_runner_; +}; + +} // namespace + +TEST_F(PolicyHeaderServiceTest, TestCreationAndShutdown) { + // Just tests that the objects can be created and shutdown properly. + EXPECT_TRUE(service_); + EXPECT_TRUE(helper_); +} + +TEST_F(PolicyHeaderServiceTest, TestWithAndWithoutPolicyHeader) { + // Set policy - this should push a header to the PolicyHeaderIOHelper. + scoped_ptr<PolicyData> policy(new PolicyData()); + user_store_.SetPolicy(policy.Pass()); + task_runner_->RunUntilIdle(); + + net::TestURLRequestContext context; + net::TestURLRequest request( + GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context); + helper_->AddPolicyHeaders(&request); + ValidateHeader(request.extra_request_headers(), true); + + // Now blow away the policy data. + service_->set_header(""); + user_store_.SetPolicy(scoped_ptr<PolicyData>()); + task_runner_->RunUntilIdle(); + + net::TestURLRequest request2( + GURL(kDMServerURL), net::DEFAULT_PRIORITY, NULL, &context); + helper_->AddPolicyHeaders(&request2); + ValidateHeader(request2.extra_request_headers(), false); +} + +} // namespace policy |