Added ProfilePolicyConnector::IsFromCloudPolicy().

This helper method can be used to determine if a policy comes from the
profile cloud policy provider or from another source. It will be used
to determine the source of the Managed Bookmarks policy and set the
folder name accordingly.

BUG=49598

Review URL: https://codereview.chromium.org/304183007

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274142 0039d316-1c4b-4281-b951-d872f2087c98
parent b2985be6
...@@ -30,6 +30,18 @@ ...@@ -30,6 +30,18 @@
namespace policy { namespace policy {
namespace {
bool HasChromePolicy(ConfigurationPolicyProvider* provider,
const char* name) {
if (!provider)
return false;
PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
return provider->policies().Get(chrome_ns).Get(name) != NULL;
}
} // namespace
ProfilePolicyConnector::ProfilePolicyConnector() ProfilePolicyConnector::ProfilePolicyConnector()
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
: is_primary_user_(false), : is_primary_user_(false),
...@@ -58,6 +70,9 @@ void ProfilePolicyConnector::Init( ...@@ -58,6 +70,9 @@ void ProfilePolicyConnector::Init(
// use the policies exposed by the PolicyService! // use the policies exposed by the PolicyService!
// The default ConfigurationPolicyProvider::IsInitializationComplete() // The default ConfigurationPolicyProvider::IsInitializationComplete()
// result is true, so take care if a provider overrides that. // result is true, so take care if a provider overrides that.
//
// Note: if you append a new provider then make sure IsPolicyFromCloudPolicy()
// is also updated below.
std::vector<ConfigurationPolicyProvider*> providers; std::vector<ConfigurationPolicyProvider*> providers;
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
...@@ -144,4 +159,25 @@ std::string ProfilePolicyConnector::GetManagementDomain() const { ...@@ -144,4 +159,25 @@ std::string ProfilePolicyConnector::GetManagementDomain() const {
return ""; return "";
} }
bool ProfilePolicyConnector::IsPolicyFromCloudPolicy(const char* name) const {
if (!HasChromePolicy(user_cloud_policy_manager_, name))
return false;
// Check all the providers that have higher priority than the
// |user_cloud_policy_manager_|. These checks must be kept in sync with the
// order of the providers in Init().
if (HasChromePolicy(forwarding_policy_provider_.get(), name))
return false;
#if defined(OS_CHROMEOS)
BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
if (HasChromePolicy(connector->GetDeviceCloudPolicyManager(), name))
return false;
#endif
return true;
}
} // namespace policy } // namespace policy
...@@ -52,6 +52,10 @@ class ProfilePolicyConnector : public KeyedService { ...@@ -52,6 +52,10 @@ class ProfilePolicyConnector : public KeyedService {
// cloud policy management. Otherwise returns an empty string. // cloud policy management. Otherwise returns an empty string.
std::string GetManagementDomain() const; std::string GetManagementDomain() const;
// Returns true if the |name| Chrome policy is currently set via the
// CloudPolicyManager and isn't being overridden by a higher-level provider.
bool IsPolicyFromCloudPolicy(const char* name) const;
private: private:
#if defined(ENABLE_CONFIGURATION_POLICY) #if defined(ENABLE_CONFIGURATION_POLICY)
#if defined(OS_CHROMEOS) #if defined(OS_CHROMEOS)
......
...@@ -33,4 +33,8 @@ std::string ProfilePolicyConnector::GetManagementDomain() const { ...@@ -33,4 +33,8 @@ std::string ProfilePolicyConnector::GetManagementDomain() const {
return ""; return "";
} }
bool ProfilePolicyConnector::IsPolicyFromCloudPolicy(const char* name) const {
return false;
}
} // namespace policy } // namespace policy
// 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 "chrome/browser/policy/profile_policy_connector.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/values.h"
#include "chrome/test/base/testing_browser_process.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
#include "components/policy/core/common/mock_configuration_policy_provider.h"
#include "components/policy/core/common/policy_bundle.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_service.h"
#include "components/policy/core/common/schema_registry.h"
#include "policy/policy_constants.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::Return;
using testing::_;
namespace policy {
class ProfilePolicyConnectorTest : public testing::Test {
protected:
ProfilePolicyConnectorTest() {}
virtual ~ProfilePolicyConnectorTest() {}
virtual void SetUp() OVERRIDE {
// This must be set up before the TestingBrowserProcess is created.
BrowserPolicyConnector::SetPolicyProviderForTesting(&mock_provider_);
EXPECT_CALL(mock_provider_, IsInitializationComplete(_))
.WillRepeatedly(Return(true));
cloud_policy_store_.NotifyStoreLoaded();
cloud_policy_manager_.reset(
new CloudPolicyManager(PolicyNamespaceKey("", ""),
&cloud_policy_store_,
loop_.message_loop_proxy(),
loop_.message_loop_proxy(),
loop_.message_loop_proxy()));
}
virtual void TearDown() {
TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL);
cloud_policy_manager_->Shutdown();
}
base::MessageLoop loop_;
SchemaRegistry schema_registry_;
MockConfigurationPolicyProvider mock_provider_;
MockCloudPolicyStore cloud_policy_store_;
scoped_ptr<CloudPolicyManager> cloud_policy_manager_;
};
TEST_F(ProfilePolicyConnectorTest, IsPolicyFromCloudPolicy) {
ProfilePolicyConnector connector;
connector.Init(false,
#if defined(OS_CHROMEOS)
NULL,
#endif
&schema_registry_,
cloud_policy_manager_.get());
// No policy is set initially.
EXPECT_FALSE(
connector.IsPolicyFromCloudPolicy(autofill::prefs::kAutofillEnabled));
PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, "");
EXPECT_FALSE(connector.policy_service()->GetPolicies(chrome_ns).GetValue(
key::kAutoFillEnabled));
// Set the policy at the cloud provider.
cloud_policy_store_.policy_map_.Set(key::kAutoFillEnabled,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(false),
NULL);
cloud_policy_store_.NotifyStoreLoaded();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(connector.IsPolicyFromCloudPolicy(key::kAutoFillEnabled));
const base::Value* value =
connector.policy_service()->GetPolicies(chrome_ns).GetValue(
key::kAutoFillEnabled);
ASSERT_TRUE(value);
EXPECT_TRUE(base::FundamentalValue(false).Equals(value));
// Now test with a higher-priority provider also setting the policy.
PolicyMap map;
map.Set(key::kAutoFillEnabled,
POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER,
new base::FundamentalValue(true),
NULL);
mock_provider_.UpdateChromePolicy(map);
EXPECT_FALSE(connector.IsPolicyFromCloudPolicy(key::kAutoFillEnabled));
value = connector.policy_service()->GetPolicies(chrome_ns).GetValue(
key::kAutoFillEnabled);
ASSERT_TRUE(value);
EXPECT_TRUE(base::FundamentalValue(true).Equals(value));
// Cleanup.
connector.Shutdown();
}
} // namespace policy
...@@ -1163,6 +1163,7 @@ ...@@ -1163,6 +1163,7 @@
'browser/policy/javascript_policy_handler_unittest.cc', 'browser/policy/javascript_policy_handler_unittest.cc',
'browser/policy/managed_bookmarks_policy_handler_unittest.cc', 'browser/policy/managed_bookmarks_policy_handler_unittest.cc',
'browser/policy/policy_path_parser_unittest.cc', 'browser/policy/policy_path_parser_unittest.cc',
'browser/policy/profile_policy_connector_unittest.cc',
'browser/policy/url_blacklist_manager_unittest.cc', 'browser/policy/url_blacklist_manager_unittest.cc',
'browser/predictors/autocomplete_action_predictor_table_unittest.cc', 'browser/predictors/autocomplete_action_predictor_table_unittest.cc',
'browser/predictors/autocomplete_action_predictor_unittest.cc', 'browser/predictors/autocomplete_action_predictor_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment