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/profiles/profile.h"
11 #include "chrome/browser/ui/views/constrained_window_views.h"
12 #include "grit/generated_resources.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/views/controls/message_box_view.h"
15 #include "ui/views/widget/widget.h"
19 const void* kDialogStatusKey
= &kDialogStatusKey
;
21 class DialogStatusData
: public base::SupportsUserData::Data
{
23 DialogStatusData() : currently_shown_(false) {}
24 virtual ~DialogStatusData() {}
25 bool currently_shown() const { return currently_shown_
; }
26 void set_currently_shown(bool shown
) { currently_shown_
= shown
; }
28 bool currently_shown_
;
34 void DownloadFeedbackDialogView::Show(
35 gfx::NativeWindow parent_window
,
37 const UserDecisionCallback
& callback
) {
38 // This dialog should only be shown if it hasn't been shown before.
39 DCHECK(!profile
->GetPrefs()->HasPrefPath(
40 prefs::kSafeBrowsingDownloadFeedbackEnabled
));
42 // Only one dialog should be shown at a time, so check to see if another one
43 // is open. If another one is open, treat this parallel call as if reporting
44 // is disabled (to be conservative).
45 DialogStatusData
* data
=
46 static_cast<DialogStatusData
*>(profile
->GetUserData(kDialogStatusKey
));
48 data
= new DialogStatusData();
49 profile
->SetUserData(kDialogStatusKey
, data
);
51 if (data
->currently_shown() == false) {
52 data
->set_currently_shown(true);
53 DownloadFeedbackDialogView
* window
=
54 new DownloadFeedbackDialogView(profile
, callback
);
55 CreateBrowserModalDialogViews(window
, parent_window
)->Show();
61 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
63 const UserDecisionCallback
& callback
)
66 explanation_box_view_(new views::MessageBoxView(
67 views::MessageBoxView::InitParams(l10n_util::GetStringUTF16(
68 IDS_FEEDBACK_SERVICE_DIALOG_EXPLANATION
)))),
69 title_text_(l10n_util::GetStringUTF16(IDS_FEEDBACK_SERVICE_DIALOG_TITLE
)),
70 ok_button_text_(l10n_util::GetStringUTF16(
71 IDS_FEEDBACK_SERVICE_DIALOG_OK_BUTTON_LABEL
)),
72 cancel_button_text_(l10n_util::GetStringUTF16(
73 IDS_FEEDBACK_SERVICE_DIALOG_CANCEL_BUTTON_LABEL
)) {
76 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
78 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
79 return ui::DIALOG_BUTTON_CANCEL
;
82 base::string16
DownloadFeedbackDialogView::GetDialogButtonLabel(
83 ui::DialogButton button
) const {
84 return (button
== ui::DIALOG_BUTTON_OK
) ?
85 ok_button_text_
: cancel_button_text_
;
88 bool DownloadFeedbackDialogView::OnButtonClicked(bool accepted
) {
89 profile_
->GetPrefs()->SetBoolean(prefs::kSafeBrowsingDownloadFeedbackEnabled
,
91 DialogStatusData
* data
=
92 static_cast<DialogStatusData
*>(profile_
->GetUserData(kDialogStatusKey
));
94 data
->set_currently_shown(false);
96 UMA_HISTOGRAM_BOOLEAN("Download.FeedbackDialogEnabled", accepted
);
98 callback_
.Run(accepted
);
102 bool DownloadFeedbackDialogView::Cancel() {
103 return OnButtonClicked(false);
106 bool DownloadFeedbackDialogView::Accept() {
107 return OnButtonClicked(true);
110 ui::ModalType
DownloadFeedbackDialogView::GetModalType() const {
111 return ui::MODAL_TYPE_WINDOW
;
114 base::string16
DownloadFeedbackDialogView::GetWindowTitle() const {
118 void DownloadFeedbackDialogView::DeleteDelegate() {
122 views::Widget
* DownloadFeedbackDialogView::GetWidget() {
123 return explanation_box_view_
->GetWidget();
126 const views::Widget
* DownloadFeedbackDialogView::GetWidget() const {
127 return explanation_box_view_
->GetWidget();
130 views::View
* DownloadFeedbackDialogView::GetContentsView() {
131 return explanation_box_view_
;