Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / settings / downloads_handler.cc
blob6a6a9ec0af72d4512cc22b665863c2618b9058dc
1 // 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.
5 #include "chrome/browser/ui/webui/settings/downloads_handler.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/prefs/scoped_user_pref_update.h"
9 #include "base/values.h"
10 #include "chrome/browser/download/download_prefs.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/ui/chrome_select_file_policy.h"
13 #include "chrome/common/pref_names.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "chrome/grit/generated_resources.h"
16 #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"
21 using base::UserMetricsAction;
23 namespace settings {
25 DownloadsHandler::DownloadsHandler() {
28 DownloadsHandler::~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();
35 void DownloadsHandler::RegisterMessages() {
36 web_ui()->RegisterMessageCallback(
37 "selectDownloadLocation",
38 base::Bind(&DownloadsHandler::HandleSelectDownloadLocation,
39 base::Unretained(this)));
42 void 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,
51 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION),
52 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0,
53 base::FilePath::StringType(),
54 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL);
57 void 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);
66 } // namespace settings