Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: chrome/browser/download/download_dir_policy_handler.cc

Issue 197013007: Set drive as the default download folder (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes build errors Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // The relative path of the drive download folder with respect to the path
bartfab (slow) 2014/03/14 13:43:45 Is this really the path to the *download* folder?
30 // returned by GetDriveMountPointPathUsingUserHash.
31 const base::FilePath::CharType* kPathRelativeToDriveRoot =
32 FILE_PATH_LITERAL("/root/");
33 #endif
34 } // namespace
35
17 DownloadDirPolicyHandler::DownloadDirPolicyHandler() 36 DownloadDirPolicyHandler::DownloadDirPolicyHandler()
18 : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory, 37 : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory,
19 base::Value::TYPE_STRING) {} 38 base::Value::TYPE_STRING) {}
20 39
21 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {} 40 DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {}
22 41
23 void DownloadDirPolicyHandler::ApplyPolicySettings( 42 bool DownloadDirPolicyHandler::CheckPolicySettings(
24 const policy::PolicyMap& policies, 43 const policy::PolicyMap& policies,
44 policy::PolicyErrorMap* errors) {
45 const base::Value* value = NULL;
46 if (!CheckAndGetValue(policies, errors, &value))
47 return false;
48
49 #if defined(OS_CHROMEOS)
50 // On chromeos, setting Download directory is supported for the current user,
bartfab (slow) 2014/03/14 13:43:45 Nit 1: s/chromeos/Chrome OS/ Nit 2: s/Download/the
51 // but not supported for the whole device.
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;
56 }
57 #endif
58
59 return true;
60 }
61
62 void DownloadDirPolicyHandler::ApplyPolicySettingsWithParameters(
63 const policy::PolicyMap& policies,
64 const policy::PolicyHandlerParameters& parameters,
25 PrefValueMap* prefs) { 65 PrefValueMap* prefs) {
26 const base::Value* value = policies.GetValue(policy_name()); 66 const base::Value* value = policies.GetValue(policy_name());
27 base::FilePath::StringType string_value; 67 base::FilePath::StringType string_value;
28 if (!value || !value->GetAsString(&string_value)) 68 if (!value || !value->GetAsString(&string_value))
29 return; 69 return;
30 70
31 base::FilePath::StringType expanded_value = 71 base::FilePath::StringType expanded_value;
32 policy::path_parser::ExpandPathVariables(string_value); 72 #if defined(OS_CHROMEOS)
bartfab (slow) 2014/03/14 13:43:45 Nit: File a bug and add a TODO to fold this Chrome
73 size_t position = string_value.find(kDriveNamePolicyVariableName);
74 if (position != std::string::npos) {
bartfab (slow) 2014/03/14 13:43:45 Nit: s/std::string/base::FilePath::StringType/
75 base::FilePath::StringType drive_mount;
bartfab (slow) 2014/03/14 13:43:45 Nit: What you store in this variable is not the pa
76 if (!parameters.user_id_hash.empty())
bartfab (slow) 2014/03/14 13:43:45 Nit: Multi-line conditionals require curly braces.
77 drive_mount = drive::util::GetDriveMountPointPathUsingUserHash(
78 parameters.user_id_hash).value() +
79 kPathRelativeToDriveRoot;
80 expanded_value = string_value.replace(
81 position, strlen(kDriveNamePolicyVariableName), drive_mount);
bartfab (slow) 2014/03/14 13:43:45 Once you go for base::FilePath::CharType (which ca
82 }
83 #else
84 expanded_value = policy::path_parser::ExpandPathVariables(string_value);
85 #endif
33 // Make sure the path isn't empty, since that will point to an undefined 86 // 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. 87 // location; the default location is used instead in that case.
35 // This is checked after path expansion because a non-empty policy value can 88 // This is checked after path expansion because a non-empty policy value can
36 // lead to an empty path value after expansion (e.g. "\"\""). 89 // lead to an empty path value after expansion (e.g. "\"\"").
37 if (expanded_value.empty()) 90 if (expanded_value.empty())
38 expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value(); 91 expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value();
39 prefs->SetValue(prefs::kDownloadDefaultDirectory, 92 prefs->SetValue(prefs::kDownloadDefaultDirectory,
40 base::Value::CreateStringValue(expanded_value)); 93 base::Value::CreateStringValue(expanded_value));
94 #if !defined(OS_CHROMEOS)
bartfab (slow) 2014/03/14 13:43:45 The policy is documented as: "If this policy is l
41 prefs->SetValue(prefs::kPromptForDownload, 95 prefs->SetValue(prefs::kPromptForDownload,
42 base::Value::CreateBooleanValue(false)); 96 base::Value::CreateBooleanValue(false));
97 #endif
43 } 98 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698