Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / download / download_dir_policy_handler.cc
blobc027e44b440d9d7aaeb8c88169150ec21ff62bb9
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_dir_policy_handler.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/values.h"
11 #include "chrome/browser/download/download_prefs.h"
12 #include "chrome/browser/policy/policy_path_parser.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/drive/drive_pref_names.h"
15 #include "components/policy/core/browser/configuration_policy_handler_parameters.h"
16 #include "components/policy/core/browser/policy_error_map.h"
17 #include "components/policy/core/common/policy_map.h"
18 #include "components/policy/core/common/policy_types.h"
19 #include "grit/components_strings.h"
20 #include "policy/policy_constants.h"
22 #if defined(OS_CHROMEOS)
23 #include "chrome/browser/chromeos/drive/file_system_util.h"
24 #endif
26 namespace {
27 #if defined(OS_CHROMEOS)
28 const char* kDriveNamePolicyVariableName = "${google_drive}";
30 // Drive root folder relative to its mount point.
31 const base::FilePath::CharType* kRootRelativeToDriveMount =
32 FILE_PATH_LITERAL("root");
33 #endif
34 } // namespace
36 DownloadDirPolicyHandler::DownloadDirPolicyHandler()
37 : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory,
38 base::Value::TYPE_STRING) {}
40 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {}
42 bool DownloadDirPolicyHandler::CheckPolicySettings(
43 const policy::PolicyMap& policies,
44 policy::PolicyErrorMap* errors) {
45 const base::Value* value = NULL;
46 if (!CheckAndGetValue(policies, errors, &value))
47 return false;
49 #if defined(OS_CHROMEOS)
50 // Download directory can only be set as a user policy. If it is set through
51 // platform policy for a chromeos=1 build, ignore it.
52 if (value &&
53 policies.Get(policy_name())->scope != policy::POLICY_SCOPE_USER) {
54 errors->AddError(policy_name(), IDS_POLICY_SCOPE_ERROR);
55 return false;
57 #endif
59 return true;
62 void DownloadDirPolicyHandler::ApplyPolicySettingsWithParameters(
63 const policy::PolicyMap& policies,
64 const policy::PolicyHandlerParameters& parameters,
65 PrefValueMap* prefs) {
66 const base::Value* value = policies.GetValue(policy_name());
67 base::FilePath::StringType string_value;
68 if (!value || !value->GetAsString(&string_value))
69 return;
71 base::FilePath::StringType expanded_value;
72 #if defined(OS_CHROMEOS)
73 bool download_to_drive = false;
74 // TODO(kaliamoorthi): Clean up policy::path_parser and fold this code
75 // into it. http://crbug.com/352627
76 size_t position = string_value.find(kDriveNamePolicyVariableName);
77 if (position != base::FilePath::StringType::npos) {
78 base::FilePath::StringType google_drive_root;
79 if (!parameters.user_id_hash.empty()) {
80 google_drive_root = drive::util::GetDriveMountPointPathForUserIdHash(
81 parameters.user_id_hash)
82 .Append(kRootRelativeToDriveMount)
83 .value();
84 download_to_drive = true;
86 expanded_value = string_value.replace(
87 position,
88 base::FilePath::StringType(kDriveNamePolicyVariableName).length(),
89 google_drive_root);
90 } else {
91 expanded_value = string_value;
93 #else
94 expanded_value = policy::path_parser::ExpandPathVariables(string_value);
95 #endif
96 // Make sure the path isn't empty, since that will point to an undefined
97 // location; the default location is used instead in that case.
98 // This is checked after path expansion because a non-empty policy value can
99 // lead to an empty path value after expansion (e.g. "\"\"").
100 if (expanded_value.empty())
101 expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value();
102 prefs->SetValue(prefs::kDownloadDefaultDirectory,
103 make_scoped_ptr(new base::StringValue(expanded_value)));
105 // If the policy is mandatory, prompt for download should be disabled.
106 // Otherwise, it would enable a user to bypass the mandatory policy.
107 if (policies.Get(policy_name())->level == policy::POLICY_LEVEL_MANDATORY) {
108 prefs->SetBoolean(prefs::kPromptForDownload, false);
109 #if defined(OS_CHROMEOS)
110 if (download_to_drive) {
111 prefs->SetBoolean(drive::prefs::kDisableDrive, false);
113 #endif
117 void DownloadDirPolicyHandler::ApplyPolicySettings(
118 const policy::PolicyMap& /* policies */,
119 PrefValueMap* /* prefs */) {
120 NOTREACHED();