orenb | 6850b0d | 2015-05-28 21:35:24 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/ui/webui/settings/downloads_handler.h" |
| 6 | |
orenb | 6850b0d | 2015-05-28 21:35:24 | [diff] [blame] | 7 | #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" |
dschuyler | 7b8e80f | 2015-10-01 21:24:25 | [diff] [blame] | 13 | #include "chrome/grit/settings_strings.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame^] | 14 | #include "components/prefs/pref_service.h" |
| 15 | #include "components/prefs/scoped_user_pref_update.h" |
orenb | 6850b0d | 2015-05-28 21:35:24 | [diff] [blame] | 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" |
| 20 | |
| 21 | using base::UserMetricsAction; |
| 22 | |
| 23 | namespace settings { |
| 24 | |
| 25 | DownloadsHandler::DownloadsHandler() { |
| 26 | } |
| 27 | |
| 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(); |
| 33 | } |
| 34 | |
| 35 | void DownloadsHandler::RegisterMessages() { |
| 36 | web_ui()->RegisterMessageCallback( |
| 37 | "selectDownloadLocation", |
| 38 | base::Bind(&DownloadsHandler::HandleSelectDownloadLocation, |
| 39 | base::Unretained(this))); |
| 40 | } |
| 41 | |
| 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, |
James Hawkins | 01cfa0f | 2015-06-05 16:48:54 | [diff] [blame] | 51 | l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION), |
orenb | 6850b0d | 2015-05-28 21:35:24 | [diff] [blame] | 52 | pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0, |
| 53 | base::FilePath::StringType(), |
| 54 | web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL); |
| 55 | } |
| 56 | |
| 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); |
| 64 | } |
| 65 | |
| 66 | } // namespace settings |