Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / external_protocol_dialog.cc
blob0eb50a153212309ffe23dc185a33db6d78262077
1 // Copyright (c) 2012 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/external_protocol_dialog.h"
7 #include "base/metrics/histogram.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/external_protocol/external_protocol_handler.h"
11 #include "chrome/browser/tab_contents/tab_util.h"
12 #include "chrome/browser/ui/external_protocol_dialog_delegate.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "components/constrained_window/constrained_window_views.h"
15 #include "content/public/browser/web_contents.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/gfx/text_elider.h"
18 #include "ui/views/controls/message_box_view.h"
19 #include "ui/views/widget/widget.h"
21 using content::WebContents;
23 namespace {
25 const int kMessageWidth = 400;
27 } // namespace
29 ///////////////////////////////////////////////////////////////////////////////
30 // ExternalProtocolHandler
32 // static
33 void ExternalProtocolHandler::RunExternalProtocolDialog(
34 const GURL& url, int render_process_host_id, int routing_id,
35 ui::PageTransition page_transition, bool has_user_gesture) {
36 scoped_ptr<ExternalProtocolDialogDelegate> delegate(
37 new ExternalProtocolDialogDelegate(url,
38 render_process_host_id,
39 routing_id));
40 if (delegate->program_name().empty()) {
41 // ShellExecute won't do anything. Don't bother warning the user.
42 return;
45 // Windowing system takes ownership.
46 new ExternalProtocolDialog(
47 delegate.Pass(), render_process_host_id, routing_id);
50 ///////////////////////////////////////////////////////////////////////////////
51 // ExternalProtocolDialog
53 ExternalProtocolDialog::~ExternalProtocolDialog() {
56 //////////////////////////////////////////////////////////////////////////////
57 // ExternalProtocolDialog, views::DialogDelegate implementation:
59 int ExternalProtocolDialog::GetDefaultDialogButton() const {
60 return ui::DIALOG_BUTTON_CANCEL;
63 base::string16 ExternalProtocolDialog::GetDialogButtonLabel(
64 ui::DialogButton button) const {
65 if (button == ui::DIALOG_BUTTON_OK)
66 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT);
67 else
68 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CANCEL_BUTTON_TEXT);
71 base::string16 ExternalProtocolDialog::GetWindowTitle() const {
72 return delegate_->GetTitleText();
75 void ExternalProtocolDialog::DeleteDelegate() {
76 delete this;
79 bool ExternalProtocolDialog::Cancel() {
80 // We also get called back here if the user closes the dialog or presses
81 // escape. In these cases it would be preferable to ignore the state of the
82 // check box but MessageBox doesn't distinguish this from pressing the cancel
83 // button.
84 delegate_->DoCancel(delegate_->url(),
85 message_box_view_->IsCheckBoxSelected());
87 // Returning true closes the dialog.
88 return true;
91 bool ExternalProtocolDialog::Accept() {
92 // We record how long it takes the user to accept an external protocol. If
93 // users start accepting these dialogs too quickly, we should worry about
94 // clickjacking.
95 UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url",
96 base::TimeTicks::Now() - creation_time_);
98 delegate_->DoAccept(delegate_->url(),
99 message_box_view_->IsCheckBoxSelected());
101 // Returning true closes the dialog.
102 return true;
105 views::View* ExternalProtocolDialog::GetContentsView() {
106 return message_box_view_;
109 views::Widget* ExternalProtocolDialog::GetWidget() {
110 return message_box_view_->GetWidget();
113 const views::Widget* ExternalProtocolDialog::GetWidget() const {
114 return message_box_view_->GetWidget();
117 ui::ModalType ExternalProtocolDialog::GetModalType() const {
118 return ui::MODAL_TYPE_CHILD;
121 ///////////////////////////////////////////////////////////////////////////////
122 // ExternalProtocolDialog, private:
124 ExternalProtocolDialog::ExternalProtocolDialog(
125 scoped_ptr<const ProtocolDialogDelegate> delegate,
126 int render_process_host_id,
127 int routing_id)
128 : delegate_(delegate.Pass()),
129 render_process_host_id_(render_process_host_id),
130 routing_id_(routing_id),
131 creation_time_(base::TimeTicks::Now()) {
132 views::MessageBoxView::InitParams params(delegate_->GetMessageText());
133 params.message_width = kMessageWidth;
134 message_box_view_ = new views::MessageBoxView(params);
135 message_box_view_->SetCheckBoxLabel(delegate_->GetCheckboxText());
137 WebContents* web_contents = tab_util::GetWebContentsByID(
138 render_process_host_id_, routing_id_);
139 // Only launch the dialog if there is a web contents associated with the
140 // request.
141 if (web_contents)
142 constrained_window::ShowWebModalDialogViews(this, web_contents);