OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/download/download_dir_policy_handler.h" | 5 #include "chrome/browser/download/download_dir_policy_handler.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/prefs/pref_value_map.h" | 9 #include "base/prefs/pref_value_map.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/download/download_prefs.h" | 11 #include "chrome/browser/download/download_prefs.h" |
12 #include "chrome/browser/policy/policy_path_parser.h" | 12 #include "chrome/browser/policy/policy_path_parser.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 #include "components/policy/core/browser/configuration_policy_handler_parameters .h" | |
15 #include "components/policy/core/browser/policy_error_map.h" | |
14 #include "components/policy/core/common/policy_map.h" | 16 #include "components/policy/core/common/policy_map.h" |
17 #include "components/policy/core/common/policy_types.h" | |
18 #include "grit/component_strings.h" | |
15 #include "policy/policy_constants.h" | 19 #include "policy/policy_constants.h" |
16 | 20 |
21 #if defined(OS_CHROMEOS) | |
22 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
23 #endif | |
24 | |
25 namespace { | |
26 #if defined(OS_CHROMEOS) | |
27 const char* kDriveNamePolicyVariableName = "${google_drive}"; | |
28 | |
29 // Drive root folder relative to its mount point. | |
30 const base::FilePath::CharType* kRootRelativeToDriveMount = | |
31 FILE_PATH_LITERAL("/root/"); | |
32 #endif | |
33 } // namespace | |
34 | |
17 DownloadDirPolicyHandler::DownloadDirPolicyHandler() | 35 DownloadDirPolicyHandler::DownloadDirPolicyHandler() |
18 : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory, | 36 : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory, |
19 base::Value::TYPE_STRING) {} | 37 base::Value::TYPE_STRING) {} |
20 | 38 |
21 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {} | 39 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {} |
22 | 40 |
23 void DownloadDirPolicyHandler::ApplyPolicySettings( | 41 bool DownloadDirPolicyHandler::CheckPolicySettings( |
24 const policy::PolicyMap& policies, | 42 const policy::PolicyMap& policies, |
43 policy::PolicyErrorMap* errors) { | |
44 const base::Value* value = NULL; | |
45 if (!CheckAndGetValue(policies, errors, &value)) | |
46 return false; | |
47 | |
48 #if defined(OS_CHROMEOS) | |
49 // Download directory can only be set as a user policy. If it is set through | |
50 // platform policy for a chromeos=1 build, ignore it. | |
51 if (value && | |
52 policies.Get(policy_name())->scope != policy::POLICY_SCOPE_USER) { | |
53 errors->AddError(policy_name(), IDS_POLICY_SCOPE_ERROR); | |
54 return false; | |
55 } | |
56 #endif | |
57 | |
58 return true; | |
59 } | |
60 | |
61 void DownloadDirPolicyHandler::ApplyPolicySettingsWithParameters( | |
62 const policy::PolicyMap& policies, | |
63 const policy::PolicyHandlerParameters& parameters, | |
25 PrefValueMap* prefs) { | 64 PrefValueMap* prefs) { |
26 const base::Value* value = policies.GetValue(policy_name()); | 65 const base::Value* value = policies.GetValue(policy_name()); |
27 base::FilePath::StringType string_value; | 66 base::FilePath::StringType string_value; |
28 if (!value || !value->GetAsString(&string_value)) | 67 if (!value || !value->GetAsString(&string_value)) |
29 return; | 68 return; |
30 | 69 |
31 base::FilePath::StringType expanded_value = | 70 base::FilePath::StringType expanded_value; |
32 policy::path_parser::ExpandPathVariables(string_value); | 71 #if defined(OS_CHROMEOS) |
72 // TODO(kaliamoorthi): cleanup policy::path_parser and fold this code | |
bartfab (slow)
2014/03/14 16:16:45
Nit: s/cleanup/Clean up/
| |
73 // into it. http://crbug.com/352627 | |
74 size_t position = string_value.find(kDriveNamePolicyVariableName); | |
75 if (position != base::FilePath::StringType::npos) { | |
76 base::FilePath::StringType google_drive_root; | |
77 if (!parameters.user_id_hash.empty()) { | |
78 google_drive_root = drive::util::GetDriveMountPointPathForUserIdHash( | |
bartfab (slow)
2014/03/14 16:16:45
Nit: A better way to do this is GetDriveMountPoint
| |
79 parameters.user_id_hash).value() + | |
80 kRootRelativeToDriveMount; | |
81 } | |
82 expanded_value = string_value.replace( | |
83 position, | |
84 base::FilePath::StringType(kDriveNamePolicyVariableName).length(), | |
85 google_drive_root); | |
86 } | |
87 #else | |
88 expanded_value = policy::path_parser::ExpandPathVariables(string_value); | |
89 #endif | |
33 // Make sure the path isn't empty, since that will point to an undefined | 90 // Make sure the path isn't empty, since that will point to an undefined |
34 // location; the default location is used instead in that case. | 91 // location; the default location is used instead in that case. |
35 // This is checked after path expansion because a non-empty policy value can | 92 // This is checked after path expansion because a non-empty policy value can |
36 // lead to an empty path value after expansion (e.g. "\"\""). | 93 // lead to an empty path value after expansion (e.g. "\"\""). |
37 if (expanded_value.empty()) | 94 if (expanded_value.empty()) |
38 expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value(); | 95 expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value(); |
39 prefs->SetValue(prefs::kDownloadDefaultDirectory, | 96 prefs->SetValue(prefs::kDownloadDefaultDirectory, |
40 base::Value::CreateStringValue(expanded_value)); | 97 base::Value::CreateStringValue(expanded_value)); |
41 prefs->SetValue(prefs::kPromptForDownload, | 98 if (policies.Get(policy_name())->level == policy::POLICY_LEVEL_MANDATORY) |
bartfab (slow)
2014/03/14 16:16:45
Nit 1: Multi-line conditionals require curly brace
| |
42 base::Value::CreateBooleanValue(false)); | 99 prefs->SetValue(prefs::kPromptForDownload, |
100 base::Value::CreateBooleanValue(false)); | |
43 } | 101 } |
OLD | NEW |