blob: 58eee71ac0f08b9f1e196dda7cef377c9c14f418 [file] [log] [blame]
orenb6850b0d2015-05-28 21:35:241// Copyright (c) 2015 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.
4
5#include "chrome/browser/ui/webui/settings/downloads_handler.h"
6
orenb6850b0d2015-05-28 21:35:247#include "base/values.h"
8#include "chrome/browser/download/download_prefs.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/ui/chrome_select_file_policy.h"
11#include "chrome/common/pref_names.h"
12#include "chrome/grit/chromium_strings.h"
dschuyler7b8e80f2015-10-01 21:24:2513#include "chrome/grit/settings_strings.h"
brettwb1fc1b82016-02-02 00:19:0814#include "components/prefs/pref_service.h"
15#include "components/prefs/scoped_user_pref_update.h"
orenb6850b0d2015-05-28 21:35:2416#include "content/public/browser/user_metrics.h"
17#include "content/public/browser/web_contents.h"
18#include "content/public/browser/web_ui.h"
19#include "ui/base/l10n/l10n_util.h"
20
21using base::UserMetricsAction;
22
23namespace settings {
24
25DownloadsHandler::DownloadsHandler() {
26}
27
28DownloadsHandler::~DownloadsHandler() {
29 // There may be pending file dialogs, we need to tell them that we've gone
30 // away so they don't try and call back to us.
31 if (select_folder_dialog_.get())
32 select_folder_dialog_->ListenerDestroyed();
33}
34
35void DownloadsHandler::RegisterMessages() {
36 web_ui()->RegisterMessageCallback(
37 "selectDownloadLocation",
38 base::Bind(&DownloadsHandler::HandleSelectDownloadLocation,
39 base::Unretained(this)));
40}
41
42void DownloadsHandler::HandleSelectDownloadLocation(
43 const base::ListValue* args) {
44 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
45 select_folder_dialog_ = ui::SelectFileDialog::Create(
46 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
47 ui::SelectFileDialog::FileTypeInfo info;
48 info.support_drive = true;
49 select_folder_dialog_->SelectFile(
50 ui::SelectFileDialog::SELECT_FOLDER,
James Hawkins01cfa0f2015-06-05 16:48:5451 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION),
orenb6850b0d2015-05-28 21:35:2452 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0,
53 base::FilePath::StringType(),
54 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL);
55}
56
57void DownloadsHandler::FileSelected(const base::FilePath& path,
58 int index,
59 void* params) {
60 content::RecordAction(UserMetricsAction("Options_SetDownloadDirectory"));
61 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs();
62 pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path);
63 pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path);
64}
65
66} // namespace settings