Implement new dangerous download reporting dialog for UNCOMMON_DOWNLOAD, in Views
[chromium-blink-merge.git] / chrome / browser / ui / views / download / download_feedback_dialog_view.cc
blob00774ad9b4c2aea926d6ab5f409c4f204f39b83a
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"
16 namespace {
18 const void* kDialogStatusKey = &kDialogStatusKey;
20 class DialogStatusData : public base::SupportsUserData::Data {
21 public:
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; }
26 private:
27 bool currently_shown_;
30 } // namespace
32 // static
33 void DownloadFeedbackDialogView::Show(
34 gfx::NativeWindow parent_window,
35 Profile* profile,
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));
46 if (data == NULL) {
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();
55 } else {
56 callback.Run(kDownloadReportingDisabled);
60 void DownloadFeedbackDialogView::ReleaseDialogStatusHold() {
61 DialogStatusData* data =
62 static_cast<DialogStatusData*>(profile_->GetUserData(kDialogStatusKey));
63 DCHECK(data);
64 data->set_currently_shown(false);
67 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
68 Profile* profile,
69 const base::Callback<void(DownloadReportingStatus)>& callback)
70 : profile_(profile),
71 callback_(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);
99 return true;
102 bool DownloadFeedbackDialogView::Accept() {
103 profile_->GetPrefs()->SetInteger(
104 prefs::kSafeBrowsingDownloadReportingEnabled, kDownloadReportingEnabled);
105 ReleaseDialogStatusHold();
106 callback_.Run(kDownloadReportingEnabled);
107 return true;
110 ui::ModalType DownloadFeedbackDialogView::GetModalType() const {
111 return ui::MODAL_TYPE_WINDOW;
114 base::string16 DownloadFeedbackDialogView::GetWindowTitle() const {
115 return title_text_;
118 void DownloadFeedbackDialogView::DeleteDelegate() {
119 delete this;
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_;