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 | |
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')
6 files changed, 198 insertions, 11 deletions
diff --git a/chrome/browser/chromeos/drive/file_system_util.cc b/chrome/browser/chromeos/drive/file_system_util.cc index 283772e..f8ca3b3 100644 --- a/chrome/browser/chromeos/drive/file_system_util.cc +++ b/chrome/browser/chromeos/drive/file_system_util.cc @@ -127,6 +127,13 @@ const base::FilePath& GetDriveMyDriveRootPath() { return drive_root_path; } +base::FilePath GetDriveMountPointPathForUserIdHash( + const std::string user_id_hash) { + return base::FilePath(kSpecialMountPointRoot).AppendASCII( + net::EscapePath(kDriveMountPointNameBase + + (user_id_hash.empty() ? "" : "-" + user_id_hash))); +} + base::FilePath GetDriveMountPointPath(Profile* profile) { std::string id = chromeos::ProfileHelper::GetUserIdHashFromProfile(profile); if (id.empty()) { @@ -141,8 +148,7 @@ base::FilePath GetDriveMountPointPath(Profile* profile) { if (user) id = user->username_hash(); } - return base::FilePath(kSpecialMountPointRoot).AppendASCII( - net::EscapePath(kDriveMountPointNameBase + (id.empty() ? "" : "-" + id))); + return GetDriveMountPointPathForUserIdHash(id); } FileSystemInterface* GetFileSystemByProfile(Profile* profile) { diff --git a/chrome/browser/chromeos/drive/file_system_util.h b/chrome/browser/chromeos/drive/file_system_util.h index 43ae4c5..278ecbc 100644 --- a/chrome/browser/chromeos/drive/file_system_util.h +++ b/chrome/browser/chromeos/drive/file_system_util.h @@ -59,6 +59,10 @@ const base::FilePath& GetDriveMyDriveRootPath(); // Returns the Drive mount point path, which looks like "/special/drive-<hash>". base::FilePath GetDriveMountPointPath(Profile* profile); +// Returns the Drive mount point path, which looks like +// "/special/drive-<username_hash>", when provided with the |user_id_hash|. +base::FilePath GetDriveMountPointPathForUserIdHash(std::string user_id_hash); + // Returns the FileSystem for the |profile|. If not available (not mounted // or disabled), returns NULL. FileSystemInterface* GetFileSystemByProfile(Profile* profile); 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 diff --git a/chrome/browser/policy/configuration_policy_handler_list_factory.cc b/chrome/browser/policy/configuration_policy_handler_list_factory.cc index 6e59679..3681ae2 100644 --- a/chrome/browser/policy/configuration_policy_handler_list_factory.cc +++ b/chrome/browser/policy/configuration_policy_handler_list_factory.cc @@ -16,6 +16,7 @@ #include "components/policy/core/browser/autofill_policy_handler.h" #include "components/policy/core/browser/configuration_policy_handler.h" #include "components/policy/core/browser/configuration_policy_handler_list.h" +#include "components/policy/core/browser/configuration_policy_handler_parameters.h" #include "components/policy/core/browser/url_blacklist_policy_handler.h" #include "components/policy/core/common/policy_details.h" #include "components/policy/core/common/policy_map.h" @@ -39,6 +40,8 @@ #if defined(OS_CHROMEOS) #include "ash/magnifier/magnifier_constants.h" +#include "chrome/browser/chromeos/login/user.h" +#include "chrome/browser/chromeos/login/user_manager.h" #include "chrome/browser/chromeos/policy/configuration_policy_handler_chromeos.h" #include "chromeos/dbus/power_policy_controller.h" #endif @@ -47,7 +50,7 @@ #include "chrome/browser/policy/managed_bookmarks_policy_handler.h" #endif -#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS) +#if !defined(OS_ANDROID) && !defined(OS_IOS) #include "chrome/browser/download/download_dir_policy_handler.h" #endif @@ -489,10 +492,22 @@ StringToIntEnumListPolicyHandler::MappingEntry kExtensionAllowedTypesMap[] = { } // namespace +void PopulatePolicyHandlerParameters(PolicyHandlerParameters* parameters) { +#if defined(OS_CHROMEOS) + if (chromeos::UserManager::IsInitialized()) { + const chromeos::User* user = chromeos::UserManager::Get()->GetActiveUser(); + if (user) + parameters->user_id_hash = user->username_hash(); + } +#endif +} + scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList( const Schema& chrome_schema) { scoped_ptr<ConfigurationPolicyHandlerList> handlers( - new ConfigurationPolicyHandlerList(base::Bind(&GetChromePolicyDetails))); + new ConfigurationPolicyHandlerList( + base::Bind(&PopulatePolicyHandlerParameters), + base::Bind(&GetChromePolicyDetails))); for (size_t i = 0; i < arraysize(kSimplePolicyMap); ++i) { handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( new SimplePolicyHandler(kSimplePolicyMap[i].policy_name, @@ -548,8 +563,6 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList( #if !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS) handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( new DiskCacheDirPolicyHandler())); - handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( - new DownloadDirPolicyHandler)); handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( new extensions::NativeMessagingHostListPolicyHandler( @@ -563,6 +576,11 @@ scoped_ptr<ConfigurationPolicyHandlerList> BuildHandlerList( true))); #endif // !defined(OS_CHROMEOS) && !defined(OS_ANDROID) && !defined(OS_IOS) +#if !defined(OS_ANDROID) && !defined(OS_IOS) + handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( + new DownloadDirPolicyHandler)); +#endif + #if defined(OS_CHROMEOS) handlers->AddHandler(make_scoped_ptr<ConfigurationPolicyHandler>( new extensions::ExtensionListPolicyHandler( |