Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/settings/downloads_handler.h" | 5 #include "chrome/browser/ui/webui/settings/downloads_handler.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/download/download_prefs.h" | 8 #include "chrome/browser/download/download_prefs.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/chrome_select_file_policy.h" | 10 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 11 #include "chrome/common/pref_names.h" | 11 #include "chrome/common/pref_names.h" |
| 12 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
| 13 #include "components/prefs/pref_service.h" | 13 #include "components/prefs/pref_service.h" |
| 14 #include "components/prefs/scoped_user_pref_update.h" | 14 #include "components/prefs/scoped_user_pref_update.h" |
| 15 #include "content/public/browser/download_manager.h" | |
| 15 #include "content/public/browser/user_metrics.h" | 16 #include "content/public/browser/user_metrics.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 19 | 20 |
| 20 using base::UserMetricsAction; | 21 using base::UserMetricsAction; |
| 21 | 22 |
| 22 namespace settings { | 23 namespace settings { |
| 23 | 24 |
| 24 DownloadsHandler::DownloadsHandler() { | 25 DownloadsHandler::DownloadsHandler(Profile* profile) : profile_(profile) {} |
| 25 } | |
| 26 | 26 |
| 27 DownloadsHandler::~DownloadsHandler() { | 27 DownloadsHandler::~DownloadsHandler() { |
| 28 // There may be pending file dialogs, we need to tell them that we've gone | 28 // There may be pending file dialogs, we need to tell them that we've gone |
| 29 // away so they don't try and call back to us. | 29 // away so they don't try and call back to us. |
| 30 if (select_folder_dialog_.get()) | 30 if (select_folder_dialog_.get()) |
| 31 select_folder_dialog_->ListenerDestroyed(); | 31 select_folder_dialog_->ListenerDestroyed(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 void DownloadsHandler::RegisterMessages() { | 34 void DownloadsHandler::RegisterMessages() { |
| 35 web_ui()->RegisterMessageCallback( | 35 web_ui()->RegisterMessageCallback( |
| 36 "initializeDownloads", | |
| 37 base::Bind(&DownloadsHandler::HandleInitialize, base::Unretained(this))); | |
| 38 web_ui()->RegisterMessageCallback( | |
| 36 "selectDownloadLocation", | 39 "selectDownloadLocation", |
| 37 base::Bind(&DownloadsHandler::HandleSelectDownloadLocation, | 40 base::Bind(&DownloadsHandler::HandleSelectDownloadLocation, |
| 38 base::Unretained(this))); | 41 base::Unretained(this))); |
| 39 } | 42 } |
| 40 | 43 |
| 44 void DownloadsHandler::OnJavascriptAllowed() { | |
| 45 PrefService* prefs = profile_->GetPrefs(); | |
| 46 pref_registrar_.Init(prefs); | |
|
Dan Beam
2017/02/23 03:06:32
nit: pref_register_.Init(profile_->GetPrefs())
tommycli
2017/02/24 19:02:41
Done.
| |
| 47 | |
| 48 pref_registrar_.Add( | |
| 49 prefs::kDownloadExtensionsToOpen, | |
| 50 base::Bind(&DownloadsHandler::SendAutoOpenDownloadsToJavascript, | |
| 51 base::Unretained(this))); | |
| 52 } | |
| 53 | |
| 54 void DownloadsHandler::OnJavascriptDisallowed() { | |
| 55 pref_registrar_.RemoveAll(); | |
| 56 } | |
| 57 | |
| 58 void DownloadsHandler::HandleInitialize(const base::ListValue* args) { | |
| 59 AllowJavascript(); | |
| 60 SendAutoOpenDownloadsToJavascript(); | |
| 61 } | |
| 62 | |
| 63 void DownloadsHandler::SendAutoOpenDownloadsToJavascript() { | |
| 64 content::DownloadManager* manager = | |
| 65 content::BrowserContext::GetDownloadManager(profile_); | |
| 66 bool auto_open_downloads = | |
| 67 manager && DownloadPrefs::FromDownloadManager(manager)->IsAutoOpenUsed(); | |
| 68 CallJavascriptFunction("cr.webUIListenerCallback", | |
| 69 base::StringValue("auto-open-downloads-changed"), | |
| 70 base::FundamentalValue(auto_open_downloads)); | |
| 71 } | |
| 72 | |
| 41 void DownloadsHandler::HandleSelectDownloadLocation( | 73 void DownloadsHandler::HandleSelectDownloadLocation( |
| 42 const base::ListValue* args) { | 74 const base::ListValue* args) { |
| 43 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 75 PrefService* pref_service = profile_->GetPrefs(); |
| 44 select_folder_dialog_ = ui::SelectFileDialog::Create( | 76 select_folder_dialog_ = ui::SelectFileDialog::Create( |
| 45 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); | 77 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); |
| 46 ui::SelectFileDialog::FileTypeInfo info; | 78 ui::SelectFileDialog::FileTypeInfo info; |
| 47 info.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH; | 79 info.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH; |
| 48 select_folder_dialog_->SelectFile( | 80 select_folder_dialog_->SelectFile( |
| 49 ui::SelectFileDialog::SELECT_FOLDER, | 81 ui::SelectFileDialog::SELECT_FOLDER, |
| 50 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION), | 82 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION), |
| 51 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0, | 83 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0, |
| 52 base::FilePath::StringType(), | 84 base::FilePath::StringType(), |
| 53 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL); | 85 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL); |
| 54 } | 86 } |
| 55 | 87 |
| 56 void DownloadsHandler::FileSelected(const base::FilePath& path, | 88 void DownloadsHandler::FileSelected(const base::FilePath& path, |
| 57 int index, | 89 int index, |
| 58 void* params) { | 90 void* params) { |
| 59 content::RecordAction(UserMetricsAction("Options_SetDownloadDirectory")); | 91 content::RecordAction(UserMetricsAction("Options_SetDownloadDirectory")); |
| 60 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); | 92 PrefService* pref_service = profile_->GetPrefs(); |
| 61 pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path); | 93 pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path); |
| 62 pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path); | 94 pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path); |
| 63 } | 95 } |
| 64 | 96 |
| 65 } // namespace settings | 97 } // namespace settings |
| OLD | NEW |