From 4ec1766c6e48f235f2c67c705008cc73b61d5a31 Mon Sep 17 00:00:00 2001 From: "joaodasilva@chromium.org" Date: Thu, 26 Jan 2012 17:16:43 +0000 Subject: Added recommended Mac policy provider. BUG=108995 TEST=Set a policy as "once" or "often" in the Mac Workgroup Manager. It is loaded as a recommended policy by chromium. Review URL: http://codereview.chromium.org/9169084 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119249 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/policy/browser_policy_connector.cc | 11 +++--- .../policy/configuration_policy_provider_mac.cc | 32 ++++++++++------- .../policy/configuration_policy_provider_mac.h | 14 ++++++-- .../configuration_policy_provider_mac_unittest.cc | 42 +++++++++++++++------- 4 files changed, 68 insertions(+), 31 deletions(-) diff --git a/chrome/browser/policy/browser_policy_connector.cc b/chrome/browser/policy/browser_policy_connector.cc index afd2333..f2fd656 100644 --- a/chrome/browser/policy/browser_policy_connector.cc +++ b/chrome/browser/policy/browser_policy_connector.cc @@ -458,7 +458,8 @@ ConfigurationPolicyProvider* policy::kRegistryMandatorySubKey, POLICY_LEVEL_MANDATORY); #elif defined(OS_MACOSX) - return new ConfigurationPolicyProviderMac(policy_list); + return new ConfigurationPolicyProviderMac(policy_list, + POLICY_LEVEL_MANDATORY); #elif defined(OS_POSIX) FilePath config_dir_path; if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) { @@ -478,13 +479,15 @@ ConfigurationPolicyProvider* // static ConfigurationPolicyProvider* BrowserPolicyConnector::CreateRecommendedPlatformProvider() { -#if defined(OS_WIN) const PolicyDefinitionList* policy_list = GetChromePolicyDefinitionList(); +#if defined(OS_WIN) return new ConfigurationPolicyProviderWin(policy_list, policy::kRegistryRecommendedSubKey, POLICY_LEVEL_RECOMMENDED); -#elif defined(OS_POSIX) && !defined(OS_MACOSX) - const PolicyDefinitionList* policy_list = GetChromePolicyDefinitionList(); +#elif defined(OS_MACOSX) + return new ConfigurationPolicyProviderMac(policy_list, + POLICY_LEVEL_RECOMMENDED); +#elif defined(OS_POSIX) FilePath config_dir_path; if (PathService::Get(chrome::DIR_POLICY_FILES, &config_dir_path)) { return new ConfigDirPolicyProvider( diff --git a/chrome/browser/policy/configuration_policy_provider_mac.cc b/chrome/browser/policy/configuration_policy_provider_mac.cc index c5c051b..b81ac00 100644 --- a/chrome/browser/policy/configuration_policy_provider_mac.cc +++ b/chrome/browser/policy/configuration_policy_provider_mac.cc @@ -44,10 +44,12 @@ FilePath GetManagedPolicyPath() { MacPreferencesPolicyProviderDelegate::MacPreferencesPolicyProviderDelegate( MacPreferences* preferences, - const PolicyDefinitionList* policy_list) + const PolicyDefinitionList* policy_list, + PolicyLevel level) : FileBasedPolicyProvider::ProviderDelegate(GetManagedPolicyPath()), policy_list_(policy_list), - preferences_(preferences) {} + preferences_(preferences), + level_(level) {} MacPreferencesPolicyProviderDelegate::~MacPreferencesPolicyProviderDelegate() {} @@ -63,9 +65,12 @@ PolicyMap* MacPreferencesPolicyProviderDelegate::Load() { preferences_->CopyAppValue(name, kCFPreferencesCurrentApplication)); if (!value.get()) continue; - if (!preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication)) + bool forced = + preferences_->AppValueIsForced(name, kCFPreferencesCurrentApplication); + PolicyLevel level = forced ? POLICY_LEVEL_MANDATORY : + POLICY_LEVEL_RECOMMENDED; + if (level != level_) continue; - Value* policy_value = NULL; switch (current->value_type) { case Value::TYPE_STRING: @@ -117,11 +122,9 @@ PolicyMap* MacPreferencesPolicyProviderDelegate::Load() { default: NOTREACHED(); } - if (policy_value) { - // TODO(joaodasilva): determine the policy level and scope. - policy->Set(current->name, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, - policy_value); - } + // TODO(joaodasilva): figure the policy scope. + if (policy_value) + policy->Set(current->name, level_, POLICY_SCOPE_USER, policy_value); } return policy; @@ -138,17 +141,22 @@ base::Time MacPreferencesPolicyProviderDelegate::GetLastModification() { } ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac( - const PolicyDefinitionList* policy_list) + const PolicyDefinitionList* policy_list, + PolicyLevel level) : FileBasedPolicyProvider( policy_list, new MacPreferencesPolicyProviderDelegate(new MacPreferences, - policy_list)) {} + policy_list, + level)) {} ConfigurationPolicyProviderMac::ConfigurationPolicyProviderMac( const PolicyDefinitionList* policy_list, + PolicyLevel level, MacPreferences* preferences) : FileBasedPolicyProvider( policy_list, - new MacPreferencesPolicyProviderDelegate(preferences, policy_list)) {} + new MacPreferencesPolicyProviderDelegate(preferences, + policy_list, + level)) {} } // namespace policy diff --git a/chrome/browser/policy/configuration_policy_provider_mac.h b/chrome/browser/policy/configuration_policy_provider_mac.h index 2e21307..379b00d 100644 --- a/chrome/browser/policy/configuration_policy_provider_mac.h +++ b/chrome/browser/policy/configuration_policy_provider_mac.h @@ -8,6 +8,7 @@ #include "base/memory/scoped_ptr.h" #include "chrome/browser/policy/file_based_policy_provider.h" +#include "chrome/browser/policy/policy_map.h" class MacPreferences; @@ -20,7 +21,8 @@ class MacPreferencesPolicyProviderDelegate // Takes ownership of |preferences|. MacPreferencesPolicyProviderDelegate( MacPreferences* preferences, - const PolicyDefinitionList* policy_list); + const PolicyDefinitionList* policy_list, + PolicyLevel level); virtual ~MacPreferencesPolicyProviderDelegate(); // FileBasedPolicyLoader::Delegate implementation. @@ -37,6 +39,12 @@ class MacPreferencesPolicyProviderDelegate scoped_ptr preferences_; + // Determines the level of policies that this provider should load. This is + // a temporary restriction, until the policy system is ready to have providers + // loading policy at different levels. + // TODO(joaodasilva): remove this. + PolicyLevel level_; + DISALLOW_COPY_AND_ASSIGN(MacPreferencesPolicyProviderDelegate); }; @@ -44,10 +52,12 @@ class MacPreferencesPolicyProviderDelegate // provided by Mac OS X's managed preferences. class ConfigurationPolicyProviderMac : public FileBasedPolicyProvider { public: - ConfigurationPolicyProviderMac(const PolicyDefinitionList* policy_list); + ConfigurationPolicyProviderMac(const PolicyDefinitionList* policy_list, + PolicyLevel level); // For testing; takes ownership of |preferences|. ConfigurationPolicyProviderMac(const PolicyDefinitionList* policy_list, + PolicyLevel level, MacPreferences* preferences); DISALLOW_COPY_AND_ASSIGN(ConfigurationPolicyProviderMac); diff --git a/chrome/browser/policy/configuration_policy_provider_mac_unittest.cc b/chrome/browser/policy/configuration_policy_provider_mac_unittest.cc index 9522e7c..4919ed5 100644 --- a/chrome/browser/policy/configuration_policy_provider_mac_unittest.cc +++ b/chrome/browser/policy/configuration_policy_provider_mac_unittest.cc @@ -59,6 +59,7 @@ AsynchronousPolicyProvider* TestHarness::CreateProvider( const PolicyDefinitionList* policy_definition_list) { prefs_ = new MockPreferences(); return new ConfigurationPolicyProviderMac(policy_definition_list, + POLICY_LEVEL_MANDATORY, prefs_); } @@ -123,26 +124,37 @@ INSTANTIATE_TEST_CASE_P( class ConfigurationPolicyProviderMacTest : public AsynchronousPolicyTestBase { protected: ConfigurationPolicyProviderMacTest() - : prefs_(new MockPreferences()), - provider_(&test_policy_definitions::kList, - prefs_) {} + : mandatory_prefs_(new MockPreferences()), + recommended_prefs_(new MockPreferences()), + mandatory_provider_(&test_policy_definitions::kList, + POLICY_LEVEL_MANDATORY, + mandatory_prefs_), + recommended_provider_(&test_policy_definitions::kList, + POLICY_LEVEL_RECOMMENDED, + recommended_prefs_) {} virtual ~ConfigurationPolicyProviderMacTest() {} - MockPreferences* prefs_; - ConfigurationPolicyProviderMac provider_; + MockPreferences* mandatory_prefs_; + MockPreferences* recommended_prefs_; + ConfigurationPolicyProviderMac mandatory_provider_; + ConfigurationPolicyProviderMac recommended_provider_; }; TEST_F(ConfigurationPolicyProviderMacTest, Invalid) { ScopedCFTypeRef name( base::SysUTF8ToCFStringRef(test_policy_definitions::kKeyString)); ScopedCFTypeRef invalid_data(CFDataCreate(NULL, NULL, 0)); - prefs_->AddTestItem(name, invalid_data.get(), true); + mandatory_prefs_->AddTestItem(name, invalid_data.get(), true); + recommended_prefs_->AddTestItem(name, invalid_data.get(), false); - // Create the provider and have it read |prefs_|. - provider_.RefreshPolicies(); + // Create the provider and have it read |mandatory_prefs_|. + mandatory_provider_.RefreshPolicies(); + recommended_provider_.RefreshPolicies(); loop_.RunAllPending(); PolicyMap policy_map; - EXPECT_TRUE(provider_.Provide(&policy_map)); + EXPECT_TRUE(mandatory_provider_.Provide(&policy_map)); + EXPECT_TRUE(policy_map.empty()); + EXPECT_TRUE(recommended_provider_.Provide(&policy_map)); EXPECT_TRUE(policy_map.empty()); } @@ -152,14 +164,18 @@ TEST_F(ConfigurationPolicyProviderMacTest, TestNonForcedValue) { ScopedCFTypeRef test_value( base::SysUTF8ToCFStringRef("string value")); ASSERT_TRUE(test_value.get()); - prefs_->AddTestItem(name, test_value.get(), false); + mandatory_prefs_->AddTestItem(name, test_value.get(), false); + recommended_prefs_->AddTestItem(name, test_value.get(), false); - // Create the provider and have it read |prefs_|. - provider_.RefreshPolicies(); + // Create the provider and have it read |mandatory_prefs_|. + mandatory_provider_.RefreshPolicies(); + recommended_provider_.RefreshPolicies(); loop_.RunAllPending(); PolicyMap policy_map; - EXPECT_TRUE(provider_.Provide(&policy_map)); + EXPECT_TRUE(mandatory_provider_.Provide(&policy_map)); EXPECT_TRUE(policy_map.empty()); + EXPECT_TRUE(recommended_provider_.Provide(&policy_map)); + EXPECT_EQ(1U, policy_map.size()); } } // namespace policy -- cgit v1.1