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/prefs/pref_service.h"
8 #include "base/supports_user_data.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/views/constrained_window_views.h"
11 #include "grit/generated_resources.h"
12 #include "ui/base/l10n/l10n_util.h"
13 #include "ui/views/controls/message_box_view.h"
14 #include "ui/views/widget/widget.h"
18 const void* kDialogStatusKey
= &kDialogStatusKey
;
20 class DialogStatusData
: public base::SupportsUserData::Data
{
22 DialogStatusData() : currently_shown_(false) {}
23 virtual ~DialogStatusData() {}
24 bool currently_shown() const { return currently_shown_
; }
25 void set_currently_shown(bool shown
) { currently_shown_
= shown
; }
27 bool currently_shown_
;
33 void DownloadFeedbackDialogView::Show(
34 gfx::NativeWindow parent_window
,
36 const base::Callback
<void(DownloadReportingStatus
)>& callback
) {
37 // This dialog should only be shown if it hasn't been shown before.
38 DCHECK(profile
->GetPrefs()->GetInteger(
39 prefs::kSafeBrowsingDownloadReportingEnabled
) == kDialogNotYetShown
);
41 // Only one dialog should be shown at a time, so check to see if another one
42 // is open. If another one is open, treat this parallel call as if reporting
43 // is disabled (to be conservative).
44 DialogStatusData
* data
=
45 static_cast<DialogStatusData
*>(profile
->GetUserData(kDialogStatusKey
));
47 data
= new DialogStatusData();
48 profile
->SetUserData(kDialogStatusKey
, data
);
50 if (data
->currently_shown() == false) {
51 data
->set_currently_shown(true);
52 DownloadFeedbackDialogView
* window
=
53 new DownloadFeedbackDialogView(profile
, callback
);
54 CreateBrowserModalDialogViews(window
, parent_window
)->Show();
56 callback
.Run(kDownloadReportingDisabled
);
60 void DownloadFeedbackDialogView::ReleaseDialogStatusHold() {
61 DialogStatusData
* data
=
62 static_cast<DialogStatusData
*>(profile_
->GetUserData(kDialogStatusKey
));
64 data
->set_currently_shown(false);
67 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
69 const base::Callback
<void(DownloadReportingStatus
)>& callback
)
72 explanation_box_view_(new views::MessageBoxView(
73 views::MessageBoxView::InitParams(l10n_util::GetStringUTF16(
74 IDS_FEEDBACK_SERVICE_DIALOG_EXPLANATION
)))),
75 title_text_(l10n_util::GetStringUTF16(IDS_FEEDBACK_SERVICE_DIALOG_TITLE
)),
76 ok_button_text_(l10n_util::GetStringUTF16(
77 IDS_FEEDBACK_SERVICE_DIALOG_OK_BUTTON_LABEL
)),
78 cancel_button_text_(l10n_util::GetStringUTF16(
79 IDS_FEEDBACK_SERVICE_DIALOG_CANCEL_BUTTON_LABEL
)) {
82 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
84 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
85 return ui::DIALOG_BUTTON_CANCEL
;
88 base::string16
DownloadFeedbackDialogView::GetDialogButtonLabel(
89 ui::DialogButton button
) const {
90 return (button
== ui::DIALOG_BUTTON_OK
) ?
91 ok_button_text_
: cancel_button_text_
;
94 bool DownloadFeedbackDialogView::Cancel() {
95 profile_
->GetPrefs()->SetInteger(
96 prefs::kSafeBrowsingDownloadReportingEnabled
, kDownloadReportingDisabled
);
97 ReleaseDialogStatusHold();
98 callback_
.Run(kDownloadReportingDisabled
);
102 bool DownloadFeedbackDialogView::Accept() {
103 profile_
->GetPrefs()->SetInteger(
104 prefs::kSafeBrowsingDownloadReportingEnabled
, kDownloadReportingEnabled
);
105 ReleaseDialogStatusHold();
106 callback_
.Run(kDownloadReportingEnabled
);
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_
;