Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(430)

Side by Side Diff: chrome/browser/ui/webui/settings/downloads_handler.cc

Issue 2711823002: MD Settings: Add "Auto Open" setting to Downloads C++ handler. (Closed)
Patch Set: fix teardown Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 pref_registrar_.Init(profile_->GetPrefs());
46 pref_registrar_.Add(
47 prefs::kDownloadExtensionsToOpen,
48 base::Bind(&DownloadsHandler::SendAutoOpenDownloadsToJavascript,
49 base::Unretained(this)));
50 }
51
52 void DownloadsHandler::OnJavascriptDisallowed() {
53 pref_registrar_.RemoveAll();
54 }
55
56 void DownloadsHandler::HandleInitialize(const base::ListValue* args) {
57 AllowJavascript();
58 SendAutoOpenDownloadsToJavascript();
59 }
60
61 void DownloadsHandler::SendAutoOpenDownloadsToJavascript() {
62 content::DownloadManager* manager =
63 content::BrowserContext::GetDownloadManager(profile_);
64 bool auto_open_downloads =
65 manager && DownloadPrefs::FromDownloadManager(manager)->IsAutoOpenUsed();
66 CallJavascriptFunction("cr.webUIListenerCallback",
67 base::StringValue("auto-open-downloads-changed"),
68 base::FundamentalValue(auto_open_downloads));
69 }
70
41 void DownloadsHandler::HandleSelectDownloadLocation( 71 void DownloadsHandler::HandleSelectDownloadLocation(
42 const base::ListValue* args) { 72 const base::ListValue* args) {
43 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); 73 PrefService* pref_service = profile_->GetPrefs();
44 select_folder_dialog_ = ui::SelectFileDialog::Create( 74 select_folder_dialog_ = ui::SelectFileDialog::Create(
45 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents())); 75 this, new ChromeSelectFilePolicy(web_ui()->GetWebContents()));
46 ui::SelectFileDialog::FileTypeInfo info; 76 ui::SelectFileDialog::FileTypeInfo info;
47 info.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH; 77 info.allowed_paths = ui::SelectFileDialog::FileTypeInfo::NATIVE_OR_DRIVE_PATH;
48 select_folder_dialog_->SelectFile( 78 select_folder_dialog_->SelectFile(
49 ui::SelectFileDialog::SELECT_FOLDER, 79 ui::SelectFileDialog::SELECT_FOLDER,
50 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION), 80 l10n_util::GetStringUTF16(IDS_SETTINGS_DOWNLOAD_LOCATION),
51 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0, 81 pref_service->GetFilePath(prefs::kDownloadDefaultDirectory), &info, 0,
52 base::FilePath::StringType(), 82 base::FilePath::StringType(),
53 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL); 83 web_ui()->GetWebContents()->GetTopLevelNativeWindow(), NULL);
54 } 84 }
55 85
56 void DownloadsHandler::FileSelected(const base::FilePath& path, 86 void DownloadsHandler::FileSelected(const base::FilePath& path,
57 int index, 87 int index,
58 void* params) { 88 void* params) {
59 content::RecordAction(UserMetricsAction("Options_SetDownloadDirectory")); 89 content::RecordAction(UserMetricsAction("Options_SetDownloadDirectory"));
60 PrefService* pref_service = Profile::FromWebUI(web_ui())->GetPrefs(); 90 PrefService* pref_service = profile_->GetPrefs();
61 pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path); 91 pref_service->SetFilePath(prefs::kDownloadDefaultDirectory, path);
62 pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path); 92 pref_service->SetFilePath(prefs::kSaveFileDefaultDirectory, path);
63 } 93 }
64 94
65 } // namespace settings 95 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/downloads_handler.h ('k') | chrome/browser/ui/webui/settings/downloads_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698