diff options
author | kaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-14 21:26:55 +0000 |
---|---|---|
committer | kaliamoorthi@chromium.org <kaliamoorthi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-14 21:26:55 +0000 |
commit | 1a195de02b72ed483f593edf5623e330cb8e2908 (patch) | |
tree | 17e40a4a2893d64807df78d347baf288b3ed9845 /chrome/browser/download | |
parent | a5c490548643e65ff72b9cf3769c422e5c6f887d (diff) | |
download | chromium_src-1a195de02b72ed483f593edf5623e330cb8e2908.zip chromium_src-1a195de02b72ed483f593edf5623e330cb8e2908.tar.gz chromium_src-1a195de02b72ed483f593edf5623e330cb8e2908.tar.bz2 |
Set drive as the default download folder
This CL modifies download directory policy handler to enable drive to be set as the default download folder in chromebooks.
BUG=340052
Review URL: https://codereview.chromium.org/197013007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257206 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download')
3 files changed, 164 insertions, 5 deletions
diff --git a/chrome/browser/download/download_dir_policy_handler.cc b/chrome/browser/download/download_dir_policy_handler.cc index 3f79ff5..c510299 100644 --- a/chrome/browser/download/download_dir_policy_handler.cc +++ b/chrome/browser/download/download_dir_policy_handler.cc @@ -11,25 +11,83 @@ #include "chrome/browser/download/download_prefs.h" #include "chrome/browser/policy/policy_path_parser.h" #include "chrome/common/pref_names.h" +#include "components/policy/core/browser/configuration_policy_handler_parameters.h" +#include "components/policy/core/browser/policy_error_map.h" #include "components/policy/core/common/policy_map.h" +#include "components/policy/core/common/policy_types.h" +#include "grit/component_strings.h" #include "policy/policy_constants.h" +#if defined(OS_CHROMEOS) +#include "chrome/browser/chromeos/drive/file_system_util.h" +#endif + +namespace { +#if defined(OS_CHROMEOS) +const char* kDriveNamePolicyVariableName = "${google_drive}"; + +// Drive root folder relative to its mount point. +const base::FilePath::CharType* kRootRelativeToDriveMount = + FILE_PATH_LITERAL("root"); +#endif +} // namespace + DownloadDirPolicyHandler::DownloadDirPolicyHandler() : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory, base::Value::TYPE_STRING) {} DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {} -void DownloadDirPolicyHandler::ApplyPolicySettings( +bool DownloadDirPolicyHandler::CheckPolicySettings( const policy::PolicyMap& policies, + policy::PolicyErrorMap* errors) { + const base::Value* value = NULL; + if (!CheckAndGetValue(policies, errors, &value)) + return false; + +#if defined(OS_CHROMEOS) + // Download directory can only be set as a user policy. If it is set through + // platform policy for a chromeos=1 build, ignore it. + if (value && + policies.Get(policy_name())->scope != policy::POLICY_SCOPE_USER) { + errors->AddError(policy_name(), IDS_POLICY_SCOPE_ERROR); + return false; + } +#endif + + return true; +} + +void DownloadDirPolicyHandler::ApplyPolicySettingsWithParameters( + const policy::PolicyMap& policies, + const policy::PolicyHandlerParameters& parameters, PrefValueMap* prefs) { const base::Value* value = policies.GetValue(policy_name()); base::FilePath::StringType string_value; if (!value || !value->GetAsString(&string_value)) return; - base::FilePath::StringType expanded_value = - policy::path_parser::ExpandPathVariables(string_value); + base::FilePath::StringType expanded_value; +#if defined(OS_CHROMEOS) + // TODO(kaliamoorthi): Clean up policy::path_parser and fold this code + // into it. http://crbug.com/352627 + size_t position = string_value.find(kDriveNamePolicyVariableName); + if (position != base::FilePath::StringType::npos) { + base::FilePath::StringType google_drive_root; + if (!parameters.user_id_hash.empty()) { + google_drive_root = drive::util::GetDriveMountPointPathForUserIdHash( + parameters.user_id_hash) + .Append(kRootRelativeToDriveMount) + .value(); + } + expanded_value = string_value.replace( + position, + base::FilePath::StringType(kDriveNamePolicyVariableName).length(), + google_drive_root); + } +#else + expanded_value = policy::path_parser::ExpandPathVariables(string_value); +#endif // Make sure the path isn't empty, since that will point to an undefined // location; the default location is used instead in that case. // This is checked after path expansion because a non-empty policy value can @@ -38,6 +96,8 @@ void DownloadDirPolicyHandler::ApplyPolicySettings( expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value(); prefs->SetValue(prefs::kDownloadDefaultDirectory, base::Value::CreateStringValue(expanded_value)); + + // TODO(kaliamoorthi): Do not set this pref when the policy is recommended. prefs->SetValue(prefs::kPromptForDownload, base::Value::CreateBooleanValue(false)); } diff --git a/chrome/browser/download/download_dir_policy_handler.h b/chrome/browser/download/download_dir_policy_handler.h index 2b26689..b449442 100644 --- a/chrome/browser/download/download_dir_policy_handler.h +++ b/chrome/browser/download/download_dir_policy_handler.h @@ -5,6 +5,8 @@ #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_DIR_POLICY_HANDLER_H_ #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_DIR_POLICY_HANDLER_H_ +#include "base/basictypes.h" +#include "base/compiler_specific.h" #include "components/policy/core/browser/configuration_policy_handler.h" class PrefValueMap; @@ -20,8 +22,13 @@ class DownloadDirPolicyHandler : public policy::TypeCheckingPolicyHandler { virtual ~DownloadDirPolicyHandler(); // ConfigurationPolicyHandler methods: - virtual void ApplyPolicySettings(const policy::PolicyMap& policies, - PrefValueMap* prefs) OVERRIDE; + virtual bool CheckPolicySettings(const policy::PolicyMap& policies, + policy::PolicyErrorMap* errors) OVERRIDE; + + virtual void ApplyPolicySettingsWithParameters( + const policy::PolicyMap& policies, + const policy::PolicyHandlerParameters& parameters, + PrefValueMap* prefs) OVERRIDE; private: DISALLOW_COPY_AND_ASSIGN(DownloadDirPolicyHandler); diff --git a/chrome/browser/download/download_dir_policy_handler_unittest.cc b/chrome/browser/download/download_dir_policy_handler_unittest.cc index 1b5e8b7..32bf1c8 100644 --- a/chrome/browser/download/download_dir_policy_handler_unittest.cc +++ b/chrome/browser/download/download_dir_policy_handler_unittest.cc @@ -2,25 +2,69 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include <string> + +#include "base/compiler_specific.h" +#include "base/files/file_path.h" #include "base/values.h" #include "chrome/browser/download/download_dir_policy_handler.h" #include "chrome/browser/download/download_prefs.h" #include "chrome/common/pref_names.h" +#include "components/policy/core/browser/configuration_policy_handler_parameters.h" #include "components/policy/core/browser/configuration_policy_pref_store.h" #include "components/policy/core/browser/configuration_policy_pref_store_test.h" +#include "components/policy/core/common/policy_details.h" #include "components/policy/core/common/policy_map.h" +#include "components/policy/core/common/policy_types.h" #include "policy/policy_constants.h" +#if defined(OS_CHROMEOS) +#include "chrome/browser/chromeos/drive/file_system_util.h" +#endif + +namespace { + +const char* kUserIDHash = "deadbeef"; + +#if defined(OS_CHROMEOS) +const char* kDriveNamePolicyVariableName = "${google_drive}"; +const base::FilePath::CharType* kRootRelativeToDriveMount = + FILE_PATH_LITERAL("root"); +const char* kRelativeToDriveRoot = "/home/"; + +std::string GetExpectedDownloadDirectory() { + return drive::util::GetDriveMountPointPathForUserIdHash(kUserIDHash) + .Append(kRootRelativeToDriveMount) + .value(); +} + +#endif + +} // namespace + class DownloadDirPolicyHandlerTest : public policy::ConfigurationPolicyPrefStoreTest { public: virtual void SetUp() OVERRIDE { + recommended_store_ = new policy::ConfigurationPolicyPrefStore( + policy_service_.get(), + &handler_list_, + policy::POLICY_LEVEL_RECOMMENDED); handler_list_.AddHandler( make_scoped_ptr<policy::ConfigurationPolicyHandler>( new DownloadDirPolicyHandler)); } + + virtual void PopulatePolicyHandlerParameters( + policy::PolicyHandlerParameters* parameters) OVERRIDE { + parameters->user_id_hash = kUserIDHash; + } + + protected: + scoped_refptr<policy::ConfigurationPolicyPrefStore> recommended_store_; }; +#if !defined(OS_CHROMEOS) TEST_F(DownloadDirPolicyHandlerTest, SetDownloadDirectory) { policy::PolicyMap policy; EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); @@ -40,3 +84,51 @@ TEST_F(DownloadDirPolicyHandlerTest, SetDownloadDirectory) { ASSERT_TRUE(result); EXPECT_FALSE(prompt_for_download); } +#endif + +#if defined(OS_CHROMEOS) +TEST_F(DownloadDirPolicyHandlerTest, SetDownloadToDrive) { + EXPECT_FALSE(store_->GetValue(prefs::kPromptForDownload, NULL)); + + policy::PolicyMap policy; + policy.Set(policy::key::kDownloadDirectory, + policy::POLICY_LEVEL_MANDATORY, + policy::POLICY_SCOPE_USER, + new base::StringValue(kDriveNamePolicyVariableName), + NULL); + UpdateProviderPolicy(policy); + + const base::Value* value = NULL; + bool prompt_for_download; + EXPECT_TRUE(store_->GetValue(prefs::kPromptForDownload, &value)); + EXPECT_TRUE(value); + EXPECT_TRUE(value->GetAsBoolean(&prompt_for_download)); + EXPECT_FALSE(prompt_for_download); + + std::string download_directory; + EXPECT_TRUE(store_->GetValue(prefs::kDownloadDefaultDirectory, &value)); + EXPECT_TRUE(value); + EXPECT_TRUE(value->GetAsString(&download_directory)); + EXPECT_EQ(GetExpectedDownloadDirectory(), download_directory); + + policy.Set(policy::key::kDownloadDirectory, + policy::POLICY_LEVEL_RECOMMENDED, + policy::POLICY_SCOPE_USER, + new base::StringValue(std::string(kDriveNamePolicyVariableName) + + kRelativeToDriveRoot), + NULL); + UpdateProviderPolicy(policy); + + EXPECT_TRUE(recommended_store_->GetValue(prefs::kPromptForDownload, &value)); + EXPECT_TRUE(value); + EXPECT_TRUE(value->GetAsBoolean(&prompt_for_download)); + EXPECT_FALSE(prompt_for_download); + + EXPECT_TRUE( + recommended_store_->GetValue(prefs::kDownloadDefaultDirectory, &value)); + EXPECT_TRUE(value); + EXPECT_TRUE(value->GetAsString(&download_directory)); + EXPECT_EQ(GetExpectedDownloadDirectory() + kRelativeToDriveRoot, + download_directory); +} +#endif |