Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / download / download_feedback_dialog_view.cc
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.
4
5 #include "chrome/browser/ui/views/download/download_feedback_dialog_view.h"
6
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"
16
17 namespace {
18
19 const void* kDialogStatusKey = &kDialogStatusKey;
20
21 class DialogStatusData : public base::SupportsUserData::Data {
22  public:
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; }
27  private:
28   bool currently_shown_;
29 };
30
31 }  // namespace
32
33 // static
34 void DownloadFeedbackDialogView::Show(
35     gfx::NativeWindow parent_window,
36     Profile* profile,
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));
41
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));
47   if (data == NULL) {
48     data = new DialogStatusData();
49     profile->SetUserData(kDialogStatusKey, data);
50   }
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();
56   } else {
57     callback.Run(false);
58   }
59 }
60
61 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
62     Profile* profile,
63     const UserDecisionCallback& callback)
64     : profile_(profile),
65       callback_(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)) {
74 }
75
76 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
77
78 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
79   return ui::DIALOG_BUTTON_CANCEL;
80 }
81
82 base::string16 DownloadFeedbackDialogView::GetDialogButtonLabel(
83     ui::DialogButton button) const {
84   return (button == ui::DIALOG_BUTTON_OK) ?
85       ok_button_text_ : cancel_button_text_;
86 }
87
88 bool DownloadFeedbackDialogView::OnButtonClicked(bool accepted) {
89   profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingDownloadFeedbackEnabled,
90                                    accepted);
91   DialogStatusData* data =
92      static_cast<DialogStatusData*>(profile_->GetUserData(kDialogStatusKey));
93   DCHECK(data);
94   data->set_currently_shown(false);
95
96   UMA_HISTOGRAM_BOOLEAN("Download.FeedbackDialogEnabled", accepted);
97
98   callback_.Run(accepted);
99   return true;
100 }
101
102 bool DownloadFeedbackDialogView::Cancel() {
103   return OnButtonClicked(false);
104 }
105
106 bool DownloadFeedbackDialogView::Accept() {
107   return OnButtonClicked(true);
108 }
109
110 ui::ModalType DownloadFeedbackDialogView::GetModalType() const {
111   return ui::MODAL_TYPE_WINDOW;
112 }
113
114 base::string16 DownloadFeedbackDialogView::GetWindowTitle() const {
115   return title_text_;
116 }
117
118 void DownloadFeedbackDialogView::DeleteDelegate() {
119   delete this;
120 }
121
122 views::Widget* DownloadFeedbackDialogView::GetWidget() {
123   return explanation_box_view_->GetWidget();
124 }
125
126 const views::Widget* DownloadFeedbackDialogView::GetWidget() const {
127   return explanation_box_view_->GetWidget();
128 }
129
130 views::View* DownloadFeedbackDialogView::GetContentsView() {
131   return explanation_box_view_;
132 }