1 // Copyright 2014 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/views/download/download_feedback_dialog_view.h"
7 #include "base/metrics/histogram.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/supports_user_data.h"
10 #include "chrome/browser/platform_util.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/grit/chromium_strings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/constrained_window/constrained_window_views.h"
16 #include "content/public/browser/page_navigator.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/views/controls/link.h"
19 #include "ui/views/controls/message_box_view.h"
20 #include "ui/views/widget/widget.h"
22 using content::OpenURLParams;
26 const void* kDialogStatusKey = &kDialogStatusKey;
28 class DialogStatusData : public base::SupportsUserData::Data {
30 DialogStatusData() : currently_shown_(false) {}
31 ~DialogStatusData() override {}
32 bool currently_shown() const { return currently_shown_; }
33 void set_currently_shown(bool shown) { currently_shown_ = shown; }
35 bool currently_shown_;
41 void DownloadFeedbackDialogView::Show(
42 gfx::NativeWindow parent_window,
44 content::PageNavigator* navigator,
45 const UserDecisionCallback& callback) {
46 // This dialog should only be shown if it hasn't been shown before.
47 DCHECK(!profile->GetPrefs()->HasPrefPath(
48 prefs::kSafeBrowsingExtendedReportingEnabled));
50 // Only one dialog should be shown at a time, so check to see if another one
51 // is open. If another one is open, treat this parallel call as if reporting
52 // is disabled (to be conservative).
53 DialogStatusData* data =
54 static_cast<DialogStatusData*>(profile->GetUserData(kDialogStatusKey));
56 data = new DialogStatusData();
57 profile->SetUserData(kDialogStatusKey, data);
59 if (data->currently_shown() == false) {
60 data->set_currently_shown(true);
61 DownloadFeedbackDialogView* window =
62 new DownloadFeedbackDialogView(profile, navigator, callback);
63 CreateBrowserModalDialogViews(window, parent_window)->Show();
69 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
71 content::PageNavigator* navigator,
72 const UserDecisionCallback& callback)
74 navigator_(navigator),
76 explanation_box_view_(new views::MessageBoxView(
77 views::MessageBoxView::InitParams(l10n_util::GetStringUTF16(
78 IDS_FEEDBACK_SERVICE_DIALOG_EXPLANATION)))),
79 link_view_(new views::Link(l10n_util::GetStringUTF16(
80 IDS_SAFE_BROWSING_PRIVACY_POLICY_PAGE))),
81 title_text_(l10n_util::GetStringUTF16(IDS_FEEDBACK_SERVICE_DIALOG_TITLE)),
82 ok_button_text_(l10n_util::GetStringUTF16(
83 IDS_FEEDBACK_SERVICE_DIALOG_OK_BUTTON_LABEL)),
84 cancel_button_text_(l10n_util::GetStringUTF16(
85 IDS_FEEDBACK_SERVICE_DIALOG_CANCEL_BUTTON_LABEL)) {
86 link_view_->set_listener(this);
89 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
91 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
92 return ui::DIALOG_BUTTON_CANCEL;
95 base::string16 DownloadFeedbackDialogView::GetDialogButtonLabel(
96 ui::DialogButton button) const {
97 return (button == ui::DIALOG_BUTTON_OK) ?
98 ok_button_text_ : cancel_button_text_;
101 bool DownloadFeedbackDialogView::OnButtonClicked(bool accepted) {
102 profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled,
104 DialogStatusData* data =
105 static_cast<DialogStatusData*>(profile_->GetUserData(kDialogStatusKey));
107 data->set_currently_shown(false);
109 UMA_HISTOGRAM_BOOLEAN("Download.FeedbackDialogEnabled", accepted);
111 callback_.Run(accepted);
115 bool DownloadFeedbackDialogView::Cancel() {
116 return OnButtonClicked(false);
119 bool DownloadFeedbackDialogView::Accept() {
120 return OnButtonClicked(true);
123 ui::ModalType DownloadFeedbackDialogView::GetModalType() const {
124 return ui::MODAL_TYPE_WINDOW;
127 base::string16 DownloadFeedbackDialogView::GetWindowTitle() const {
131 void DownloadFeedbackDialogView::DeleteDelegate() {
135 views::Widget* DownloadFeedbackDialogView::GetWidget() {
136 return explanation_box_view_->GetWidget();
139 const views::Widget* DownloadFeedbackDialogView::GetWidget() const {
140 return explanation_box_view_->GetWidget();
143 views::View* DownloadFeedbackDialogView::GetContentsView() {
144 return explanation_box_view_;
147 views::View* DownloadFeedbackDialogView::CreateExtraView() {
151 void DownloadFeedbackDialogView::LinkClicked(
152 views::Link* source, int event_flags) {
153 WindowOpenDisposition disposition =
154 ui::DispositionFromEventFlags(event_flags);
155 content::OpenURLParams params(
156 GURL(l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL)),
158 disposition == CURRENT_TAB ? NEW_FOREGROUND_TAB : disposition,
159 ui::PAGE_TRANSITION_LINK, false);
160 navigator_->OpenURL(params);