diff options
author | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 08:17:16 +0000 |
---|---|---|
committer | bauerb@chromium.org <bauerb@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-07 08:17:16 +0000 |
commit | 8c85cb6bc3289aa8aaf1439cd4684c5c496c9e4a (patch) | |
tree | 8302d93689876c2061ad100edf4c2ead01e6e202 /components | |
parent | 86203925b50b728135317868bda6a8337cdd347a (diff) | |
download | chromium_src-8c85cb6bc3289aa8aaf1439cd4684c5c496c9e4a.zip chromium_src-8c85cb6bc3289aa8aaf1439cd4684c5c496c9e4a.tar.gz chromium_src-8c85cb6bc3289aa8aaf1439cd4684c5c496c9e4a.tar.bz2 |
Add a platform policy provider for Android.
BUG=340704
TBR=ben@chromium.org
Review URL: https://codereview.chromium.org/155923002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249618 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
6 files changed, 267 insertions, 0 deletions
diff --git a/components/components_tests.gyp b/components/components_tests.gyp index f371b2e..ac5db07 100644 --- a/components/components_tests.gyp +++ b/components/components_tests.gyp @@ -276,6 +276,7 @@ 'policy/core/common/policy_loader_mac_unittest.cc', 'policy/core/common/policy_loader_win_unittest.cc', 'policy/core/common/policy_map_unittest.cc', + 'policy/core/common/policy_provider_android_unittest.cc', 'policy/core/common/policy_service_impl_unittest.cc', 'policy/core/common/policy_statistics_collector_unittest.cc', 'policy/core/common/preg_parser_win_unittest.cc', diff --git a/components/policy/core/common/policy_provider_android.cc b/components/policy/core/common/policy_provider_android.cc new file mode 100644 index 0000000..d70fbe6 --- /dev/null +++ b/components/policy/core/common/policy_provider_android.cc @@ -0,0 +1,61 @@ +// Copyright 2014 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/policy_provider_android.h" +#include "components/policy/core/common/policy_provider_android_delegate.h" + +namespace policy { + +namespace { + +bool g_wait_for_policies = false; + +} // namespace + +PolicyProviderAndroid::PolicyProviderAndroid() + : delegate_(NULL), + initialized_(!g_wait_for_policies) {} + +PolicyProviderAndroid::~PolicyProviderAndroid() {} + +// static +void PolicyProviderAndroid::SetShouldWaitForPolicy( + bool should_wait_for_policy) { + g_wait_for_policies = should_wait_for_policy; +} + +void PolicyProviderAndroid::SetDelegate( + PolicyProviderAndroidDelegate* delegate) { + delegate_ = delegate; +} + +void PolicyProviderAndroid::SetPolicies(scoped_ptr<PolicyBundle> policy) { + initialized_ = true; + UpdatePolicy(policy.Pass()); +} + +void PolicyProviderAndroid::Shutdown() { + if (delegate_) + delegate_->PolicyProviderShutdown(); + + ConfigurationPolicyProvider::Shutdown(); +} + +bool PolicyProviderAndroid::IsInitializationComplete( + PolicyDomain domain) const { + return initialized_; +} + +void PolicyProviderAndroid::RefreshPolicies() { + if (delegate_) { + delegate_->RefreshPolicies(); + } else { + // If we don't have a delegate, pass a copy of the current policies. + scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); + bundle->CopyFrom(policies()); + UpdatePolicy(bundle.Pass()); + } +} + +} // namespace policy diff --git a/components/policy/core/common/policy_provider_android.h b/components/policy/core/common/policy_provider_android.h new file mode 100644 index 0000000..f2fbefb --- /dev/null +++ b/components/policy/core/common/policy_provider_android.h @@ -0,0 +1,45 @@ +// Copyright 2014 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_POLICY_PROVIDER_ANDROID_H_ +#define COMPONENTS_POLICY_CORE_COMMON_POLICY_PROVIDER_ANDROID_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "components/policy/core/common/configuration_policy_provider.h" + +namespace policy { + +class PolicyProviderAndroidDelegate; + +// Provider for policies set by the Android platform. +class POLICY_EXPORT PolicyProviderAndroid : public ConfigurationPolicyProvider { + public: + PolicyProviderAndroid(); + virtual ~PolicyProviderAndroid(); + + // Call this method to tell the policy system whether it should wait for + // policies to be loaded by this provider. If this method is called, + // IsInitializationComplete() will only return true after SetPolicies() has + // been called at least once, otherwise it will return true immediately. + static void SetShouldWaitForPolicy(bool should_wait_for_policy); + + void SetDelegate(PolicyProviderAndroidDelegate* delegate); + void SetPolicies(scoped_ptr<PolicyBundle> policy); + + // ConfigurationPolicyProvider: + virtual void Shutdown() OVERRIDE; + virtual bool IsInitializationComplete(PolicyDomain domain) const OVERRIDE; + virtual void RefreshPolicies() OVERRIDE; + + private: + PolicyProviderAndroidDelegate* delegate_; + bool initialized_; + + DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroid); +}; + +} // namespace policy + +#endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_PROVIDER_ANDROID_H_ diff --git a/components/policy/core/common/policy_provider_android_delegate.h b/components/policy/core/common/policy_provider_android_delegate.h new file mode 100644 index 0000000..94f8c4b --- /dev/null +++ b/components/policy/core/common/policy_provider_android_delegate.h @@ -0,0 +1,29 @@ +// Copyright 2014 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_POLICY_PROVIDER_ANDROID_DELEGATE_H_ +#define COMPONENTS_POLICY_CORE_COMMON_POLICY_PROVIDER_ANDROID_DELEGATE_H_ + +#include "base/compiler_specific.h" + +namespace policy { + +// A delegate for the Android policy provider. This class is responsible for +// setting policies on the PolicyProviderAndroid and refreshing them on demand. +class POLICY_EXPORT PolicyProviderAndroidDelegate { + public: + // Called to refresh policies. If this method is called, the delegate must + // eventually call SetPolicies on the provider. + virtual void RefreshPolicies() = 0; + + // Called before the provider is destroyed. + virtual void PolicyProviderShutdown() = 0; + + protected: + virtual ~PolicyProviderAndroidDelegate() {} +}; + +} // namespace policy + +#endif // COMPONENTS_POLICY_CORE_COMMON_POLICY_PROVIDER_ANDROID_DELEGATE_H_ diff --git a/components/policy/core/common/policy_provider_android_unittest.cc b/components/policy/core/common/policy_provider_android_unittest.cc new file mode 100644 index 0000000..7107316 --- /dev/null +++ b/components/policy/core/common/policy_provider_android_unittest.cc @@ -0,0 +1,128 @@ +// Copyright 2014 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/memory/scoped_ptr.h" +#include "components/policy/core/common/policy_provider_android.h" +#include "components/policy/core/common/policy_provider_android_delegate.h" +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace policy { + +namespace { + +// Helper to write a policy in |bundle| with less code. +void SetPolicy(PolicyBundle* bundle, + const std::string& name, + const std::string& value) { + bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) + .Set(name, + POLICY_LEVEL_MANDATORY, + POLICY_SCOPE_USER, + base::Value::CreateStringValue(value), + NULL); +} + +class MockPolicyProviderAndroidDelegate : public PolicyProviderAndroidDelegate { + public: + MockPolicyProviderAndroidDelegate() {} + virtual ~MockPolicyProviderAndroidDelegate() {} + + MOCK_METHOD0(RefreshPolicies, void()); + MOCK_METHOD0(PolicyProviderShutdown, void()); + + private: + DISALLOW_COPY_AND_ASSIGN(MockPolicyProviderAndroidDelegate); +}; + +// Test fixture that makes sure that we always call Shutdown() before destroying +// the policy provider. Allocate this just like a PolicyProviderAndroid and use +// Get() to get the policy provider. +class PolicyProviderAndroidTestFixture { + public: + PolicyProviderAndroidTestFixture() {} + ~PolicyProviderAndroidTestFixture() { + provider_.Shutdown(); + } + + PolicyProviderAndroid* Get() { + return &provider_; + } + + private: + PolicyProviderAndroid provider_; + DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTestFixture); +}; + +} // namespace + +class PolicyProviderAndroidTest : public ::testing::Test { + protected: + PolicyProviderAndroidTest(); + virtual ~PolicyProviderAndroidTest(); + + virtual void SetUp() OVERRIDE; + virtual void TearDown() OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(PolicyProviderAndroidTest); +}; + +PolicyProviderAndroidTest::PolicyProviderAndroidTest() {} +PolicyProviderAndroidTest::~PolicyProviderAndroidTest() {} + +void PolicyProviderAndroidTest::SetUp() {} + +void PolicyProviderAndroidTest::TearDown() { + PolicyProviderAndroid::SetShouldWaitForPolicy(false); +} + +TEST_F(PolicyProviderAndroidTest, InitializationCompleted) { + PolicyProviderAndroidTestFixture provider; + EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); + + const PolicyBundle kEmptyBundle; + EXPECT_TRUE(provider.Get()->policies().Equals(kEmptyBundle)); +} + +TEST_F(PolicyProviderAndroidTest, WaitForInitialization) { + PolicyProviderAndroid::SetShouldWaitForPolicy(true); + PolicyProviderAndroidTestFixture provider; + EXPECT_FALSE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); + + scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle); + SetPolicy(policy_bundle.get(), "key", "value"); + PolicyBundle expected_policy_bundle; + expected_policy_bundle.CopyFrom(*policy_bundle); + provider.Get()->SetPolicies(policy_bundle.Pass()); + EXPECT_TRUE(provider.Get()->IsInitializationComplete(POLICY_DOMAIN_CHROME)); + EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); +} + +TEST_F(PolicyProviderAndroidTest, RefreshPolicies) { + MockPolicyProviderAndroidDelegate delegate; + PolicyProviderAndroidTestFixture provider; + + provider.Get()->SetDelegate(&delegate); + + scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle); + SetPolicy(policy_bundle.get(), "key", "old_value"); + PolicyBundle expected_policy_bundle; + expected_policy_bundle.CopyFrom(*policy_bundle); + provider.Get()->SetPolicies(policy_bundle.Pass()); + EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); + + EXPECT_CALL(delegate, RefreshPolicies()).Times(1); + provider.Get()->RefreshPolicies(); + + policy_bundle.reset(new PolicyBundle); + SetPolicy(policy_bundle.get(), "key", "new_value"); + expected_policy_bundle.CopyFrom(*policy_bundle); + provider.Get()->SetPolicies(policy_bundle.Pass()); + EXPECT_TRUE(provider.Get()->policies().Equals(expected_policy_bundle)); + + EXPECT_CALL(delegate, PolicyProviderShutdown()).Times(1); +} + +} // namespace policy diff --git a/components/policy/policy_common.gypi b/components/policy/policy_common.gypi index c343a6a..0b75ebe 100644 --- a/components/policy/policy_common.gypi +++ b/components/policy/policy_common.gypi @@ -107,6 +107,9 @@ 'core/common/policy_namespace.h', 'core/common/policy_pref_names.cc', 'core/common/policy_pref_names.h', + 'core/common/policy_provider_android.cc', + 'core/common/policy_provider_android.h', + 'core/common/policy_provider_android_delegate.h', 'core/common/policy_service.cc', 'core/common/policy_service.h', 'core/common/policy_service_impl.cc', |