Update To 11.40.268.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/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"
21
22 using content::OpenURLParams;
23
24 namespace {
25
26 const void* kDialogStatusKey = &kDialogStatusKey;
27
28 class DialogStatusData : public base::SupportsUserData::Data {
29  public:
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; }
34  private:
35   bool currently_shown_;
36 };
37
38 }  // namespace
39
40 // static
41 void DownloadFeedbackDialogView::Show(
42     gfx::NativeWindow parent_window,
43     Profile* profile,
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));
49
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));
55   if (data == NULL) {
56     data = new DialogStatusData();
57     profile->SetUserData(kDialogStatusKey, data);
58   }
59   if (data->currently_shown() == false) {
60     data->set_currently_shown(true);
61     DownloadFeedbackDialogView* window =
62         new DownloadFeedbackDialogView(profile, navigator, callback);
63     CreateBrowserModalDialogViews(window, parent_window)->Show();
64   } else {
65     callback.Run(false);
66   }
67 }
68
69 DownloadFeedbackDialogView::DownloadFeedbackDialogView(
70     Profile* profile,
71     content::PageNavigator* navigator,
72     const UserDecisionCallback& callback)
73     : profile_(profile),
74       navigator_(navigator),
75       callback_(callback),
76       explanation_box_view_(new views::MessageBoxView(
77           views::MessageBoxView::InitParams(l10n_util::GetStringUTF16(
78               IDS_FEEDBACK_SERVICE_DIALOG_EXPLANATION)))),
79       link_view_(new views::Link(l10n_util::GetStringUTF16(
80           IDS_SAFE_BROWSING_PRIVACY_POLICY_PAGE))),
81       title_text_(l10n_util::GetStringUTF16(IDS_FEEDBACK_SERVICE_DIALOG_TITLE)),
82       ok_button_text_(l10n_util::GetStringUTF16(
83           IDS_FEEDBACK_SERVICE_DIALOG_OK_BUTTON_LABEL)),
84       cancel_button_text_(l10n_util::GetStringUTF16(
85           IDS_FEEDBACK_SERVICE_DIALOG_CANCEL_BUTTON_LABEL)) {
86   link_view_->set_listener(this);
87 }
88
89 DownloadFeedbackDialogView::~DownloadFeedbackDialogView() {}
90
91 int DownloadFeedbackDialogView::GetDefaultDialogButton() const {
92   return ui::DIALOG_BUTTON_CANCEL;
93 }
94
95 base::string16 DownloadFeedbackDialogView::GetDialogButtonLabel(
96     ui::DialogButton button) const {
97   return (button == ui::DIALOG_BUTTON_OK) ?
98       ok_button_text_ : cancel_button_text_;
99 }
100
101 bool DownloadFeedbackDialogView::OnButtonClicked(bool accepted) {
102   profile_->GetPrefs()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled,
103                                    accepted);
104   DialogStatusData* data =
105      static_cast<DialogStatusData*>(profile_->GetUserData(kDialogStatusKey));
106   DCHECK(data);
107   data->set_currently_shown(false);
108
109   UMA_HISTOGRAM_BOOLEAN("Download.FeedbackDialogEnabled", accepted);
110
111   callback_.Run(accepted);
112   return true;
113 }
114
115 bool DownloadFeedbackDialogView::Cancel() {
116   return OnButtonClicked(false);
117 }
118
119 bool DownloadFeedbackDialogView::Accept() {
120   return OnButtonClicked(true);
121 }
122
123 ui::ModalType DownloadFeedbackDialogView::GetModalType() const {
124   return ui::MODAL_TYPE_WINDOW;
125 }
126
127 base::string16 DownloadFeedbackDialogView::GetWindowTitle() const {
128   return title_text_;
129 }
130
131 void DownloadFeedbackDialogView::DeleteDelegate() {
132   delete this;
133 }
134
135 views::Widget* DownloadFeedbackDialogView::GetWidget() {
136   return explanation_box_view_->GetWidget();
137 }
138
139 const views::Widget* DownloadFeedbackDialogView::GetWidget() const {
140   return explanation_box_view_->GetWidget();
141 }
142
143 views::View* DownloadFeedbackDialogView::GetContentsView() {
144   return explanation_box_view_;
145 }
146
147 views::View* DownloadFeedbackDialogView::CreateExtraView() {
148   return link_view_;
149 }
150
151 void DownloadFeedbackDialogView::LinkClicked(
152     views::Link* source, int event_flags) {
153   WindowOpenDisposition disposition =
154       ui::DispositionFromEventFlags(event_flags);
155   content::OpenURLParams params(
156       GURL(l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL)),
157       content::Referrer(),
158       disposition == CURRENT_TAB ? NEW_FOREGROUND_TAB : disposition,
159       ui::PAGE_TRANSITION_LINK, false);
160   navigator_->OpenURL(params);
161 }