18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/download/download_dir_policy_handler.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/files/file_path.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/prefs/pref_value_map.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/values.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/download/download_prefs.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/policy/policy_path_parser.h"
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/pref_names.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/policy/core/browser/configuration_policy_handler_parameters.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/policy/core/browser/policy_error_map.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "components/policy/core/common/policy_map.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/policy/core/common/policy_types.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "grit/components_strings.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "policy/policy_constants.h"
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_CHROMEOS)
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/chromeos/drive/file_system_util.h"
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace {
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_CHROMEOS)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const char* kDriveNamePolicyVariableName = "${google_drive}";
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Drive root folder relative to its mount point.
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)const base::FilePath::CharType* kRootRelativeToDriveMount =
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    FILE_PATH_LITERAL("root");
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DownloadDirPolicyHandler::DownloadDirPolicyHandler()
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : TypeCheckingPolicyHandler(policy::key::kDownloadDirectory,
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                base::Value::TYPE_STRING) {}
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DownloadDirPolicyHandler::~DownloadDirPolicyHandler() {}
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool DownloadDirPolicyHandler::CheckPolicySettings(
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const policy::PolicyMap& policies,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    policy::PolicyErrorMap* errors) {
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const base::Value* value = NULL;
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!CheckAndGetValue(policies, errors, &value))
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_CHROMEOS)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Download directory can only be set as a user policy. If it is set through
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // platform policy for a chromeos=1 build, ignore it.
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (value &&
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      policies.Get(policy_name())->scope != policy::POLICY_SCOPE_USER) {
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    errors->AddError(policy_name(), IDS_POLICY_SCOPE_ERROR);
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return true;
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void DownloadDirPolicyHandler::ApplyPolicySettingsWithParameters(
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const policy::PolicyMap& policies,
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const policy::PolicyHandlerParameters& parameters,
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    PrefValueMap* prefs) {
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const base::Value* value = policies.GetValue(policy_name());
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::FilePath::StringType string_value;
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!value || !value->GetAsString(&string_value))
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::FilePath::StringType expanded_value;
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#if defined(OS_CHROMEOS)
72010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  bool download_to_drive = false;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TODO(kaliamoorthi): Clean up policy::path_parser and fold this code
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // into it. http://crbug.com/352627
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  size_t position = string_value.find(kDriveNamePolicyVariableName);
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (position != base::FilePath::StringType::npos) {
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::FilePath::StringType google_drive_root;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (!parameters.user_id_hash.empty()) {
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      google_drive_root = drive::util::GetDriveMountPointPathForUserIdHash(
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              parameters.user_id_hash)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              .Append(kRootRelativeToDriveMount)
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              .value();
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      download_to_drive = true;
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    expanded_value = string_value.replace(
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        position,
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::FilePath::StringType(kDriveNamePolicyVariableName).length(),
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        google_drive_root);
8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  } else {
9023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    expanded_value = string_value;
91a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#else
93a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  expanded_value = policy::path_parser::ExpandPathVariables(string_value);
94a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Make sure the path isn't empty, since that will point to an undefined
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // location; the default location is used instead in that case.
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // This is checked after path expansion because a non-empty policy value can
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // lead to an empty path value after expansion (e.g. "\"\"").
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (expanded_value.empty())
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    expanded_value = DownloadPrefs::GetDefaultDownloadDirectory().value();
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  prefs->SetValue(prefs::kDownloadDefaultDirectory,
102116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                  new base::StringValue(expanded_value));
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
10423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // If the policy is mandatory, prompt for download should be disabled.
10523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Otherwise, it would enable a user to bypass the mandatory policy.
10623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (policies.Get(policy_name())->level == policy::POLICY_LEVEL_MANDATORY) {
10723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)    prefs->SetValue(prefs::kPromptForDownload,
108010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                    new base::FundamentalValue(false));
109010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#if defined(OS_CHROMEOS)
110010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    if (download_to_drive) {
111010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      prefs->SetValue(prefs::kDisableDrive,
112010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)                      new base::FundamentalValue(false));
113010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    }
114010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#endif
11523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  }
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
117