This source file includes following definitions.
- CheckPolicySettings
- ApplyPolicySettingsWithParameters
#include "chrome/browser/download/download_dir_policy_handler.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_value_map.h"
#include "base/values.h"
#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}";
const base::FilePath::CharType* kRootRelativeToDriveMount =
FILE_PATH_LITERAL("root");
#endif
}
DownloadDirPolicyHandler::DownloadDirPolicyHandler()
: TypeCheckingPolicyHandler(policy::key::kDownloadDirectory,
base::Value::TYPE_STRING) {}
DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {}
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)
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;
#if defined(OS_CHROMEOS)
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 = string_value;
}
#else
expanded_value = policy::path_parser::ExpandPathVariables(string_value);
#endif
if (expanded_value.empty())
expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value();
prefs->SetValue(prefs::kDownloadDefaultDirectory,
base::Value::CreateStringValue(expanded_value));
if (policies.Get(policy_name())->level == policy::POLICY_LEVEL_MANDATORY) {
prefs->SetValue(prefs::kPromptForDownload,
base::Value::CreateBooleanValue(false));
}
}