summaryrefslogtreecommitdiffstats
path: root/chrome/browser/download/download_dir_policy_handler.cc
blob: be9ea9df3efd7d053d5404c7c6113a6ad7cf5e54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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/components_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() {}

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;
#if defined(OS_CHROMEOS)
  bool download_to_drive = false;
  // 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();
      download_to_drive = true;
    }
    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
  // 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
  // lead to an empty path value after expansion (e.g. "\"\"").
  if (expanded_value.empty())
    expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value();
  prefs->SetValue(prefs::kDownloadDefaultDirectory,
                  new base::StringValue(expanded_value));

  // If the policy is mandatory, prompt for download should be disabled.
  // Otherwise, it would enable a user to bypass the mandatory policy.
  if (policies.Get(policy_name())->level == policy::POLICY_LEVEL_MANDATORY) {
    prefs->SetValue(prefs::kPromptForDownload,
                    new base::FundamentalValue(false));
#if defined(OS_CHROMEOS)
    if (download_to_drive) {
      prefs->SetValue(prefs::kDisableDrive,
                      new base::FundamentalValue(false));
    }
#endif
  }
}