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 constrained_window::CreateBrowserModalDialogViews(
64 window
, parent_window
)->Show();
70 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
72 content::PageNavigator
* navigator
,
73 const UserDecisionCallback
& callback
)
75 navigator_(navigator
),
77 explanation_box_view_(new views::MessageBoxView(
78 views::MessageBoxView::InitParams(l10n_util::GetStringUTF16(
79 IDS_FEEDBACK_SERVICE_DIALOG_EXPLANATION
)))),
80 link_view_(new views::Link(l10n_util::GetStringUTF16(
81 IDS_SAFE_BROWSING_PRIVACY_POLICY_PAGE
))),
82 title_text_(l10n_util::GetStringUTF16(IDS_FEEDBACK_SERVICE_DIALOG_TITLE
)),
83 ok_button_text_(l10n_util::GetStringUTF16(
84 IDS_FEEDBACK_SERVICE_DIALOG_OK_BUTTON_LABEL
)),
85 cancel_button_text_(l10n_util::GetStringUTF16(
86 IDS_FEEDBACK_SERVICE_DIALOG_CANCEL_BUTTON_LABEL
)) {
87 link_view_
->set_listener(this);
90 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
92 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
93 return ui::DIALOG_BUTTON_CANCEL
;
96 base::string16
DownloadFeedbackDialogView::GetDialogButtonLabel(
97 ui::DialogButton button
) const {
98 return (button
== ui::DIALOG_BUTTON_OK
) ?
99 ok_button_text_
: cancel_button_text_
;
102 bool DownloadFeedbackDialogView::OnButtonClicked(bool accepted
) {
103 profile_
->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled
,
105 DialogStatusData
* data
=
106 static_cast<DialogStatusData
*>(profile_
->GetUserData(kDialogStatusKey
));
108 data
->set_currently_shown(false);
110 UMA_HISTOGRAM_BOOLEAN("Download.FeedbackDialogEnabled", accepted
);
112 callback_
.Run(accepted
);
116 bool DownloadFeedbackDialogView::Cancel() {
117 return OnButtonClicked(false);
120 bool DownloadFeedbackDialogView::Accept() {
121 return OnButtonClicked(true);
124 ui::ModalType
DownloadFeedbackDialogView::GetModalType() const {
125 return ui::MODAL_TYPE_WINDOW
;
128 base::string16
DownloadFeedbackDialogView::GetWindowTitle() const {
132 void DownloadFeedbackDialogView::DeleteDelegate() {
136 views::Widget
* DownloadFeedbackDialogView::GetWidget() {
137 return explanation_box_view_
->GetWidget();
140 const views::Widget
* DownloadFeedbackDialogView::GetWidget() const {
141 return explanation_box_view_
->GetWidget();
144 views::View
* DownloadFeedbackDialogView::GetContentsView() {
145 return explanation_box_view_
;
148 views::View
* DownloadFeedbackDialogView::CreateExtraView() {
152 void DownloadFeedbackDialogView::LinkClicked(
153 views::Link
* source
, int event_flags
) {
154 WindowOpenDisposition disposition
=
155 ui::DispositionFromEventFlags(event_flags
);
156 content::OpenURLParams
params(
157 GURL(l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL
)),
159 disposition
== CURRENT_TAB
? NEW_FOREGROUND_TAB
: disposition
,
160 ui::PAGE_TRANSITION_LINK
, false);
161 navigator_
->OpenURL(params
);