external_protocol_dialog.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
5#include "chrome/browser/ui/views/external_protocol_dialog.h"
6
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/browser/ui/views/constrained_window_views.h"
14#include "content/public/browser/web_contents.h"
15#include "content/public/browser/web_contents_view.h"
16#include "grit/chromium_strings.h"
17#include "grit/generated_resources.h"
18#include "ui/base/l10n/l10n_util.h"
19#include "ui/gfx/text_elider.h"
20#include "ui/views/controls/message_box_view.h"
21#include "ui/views/widget/widget.h"
22
23using content::WebContents;
24
25namespace {
26
27const int kMessageWidth = 400;
28
29}  // namespace
30
31///////////////////////////////////////////////////////////////////////////////
32// ExternalProtocolHandler
33
34// static
35void ExternalProtocolHandler::RunExternalProtocolDialog(
36    const GURL& url, int render_process_host_id, int routing_id) {
37  scoped_ptr<ExternalProtocolDialogDelegate> delegate(
38      new ExternalProtocolDialogDelegate(url,
39                                         render_process_host_id,
40                                         routing_id));
41  if (delegate->program_name().empty()) {
42    // ShellExecute won't do anything. Don't bother warning the user.
43    return;
44  }
45
46  // Windowing system takes ownership.
47  new ExternalProtocolDialog(delegate.PassAs<const ProtocolDialogDelegate>(),
48                             render_process_host_id,
49                             routing_id);
50}
51
52///////////////////////////////////////////////////////////////////////////////
53// ExternalProtocolDialog
54
55ExternalProtocolDialog::~ExternalProtocolDialog() {
56}
57
58//////////////////////////////////////////////////////////////////////////////
59// ExternalProtocolDialog, views::DialogDelegate implementation:
60
61int ExternalProtocolDialog::GetDefaultDialogButton() const {
62  return ui::DIALOG_BUTTON_CANCEL;
63}
64
65base::string16 ExternalProtocolDialog::GetDialogButtonLabel(
66    ui::DialogButton button) const {
67  if (button == ui::DIALOG_BUTTON_OK)
68    return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT);
69  else
70    return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CANCEL_BUTTON_TEXT);
71}
72
73base::string16 ExternalProtocolDialog::GetWindowTitle() const {
74  return delegate_->GetTitleText();
75}
76
77void ExternalProtocolDialog::DeleteDelegate() {
78  delete this;
79}
80
81bool ExternalProtocolDialog::Cancel() {
82  // We also get called back here if the user closes the dialog or presses
83  // escape. In these cases it would be preferable to ignore the state of the
84  // check box but MessageBox doesn't distinguish this from pressing the cancel
85  // button.
86  delegate_->DoCancel(delegate_->url(),
87                      message_box_view_->IsCheckBoxSelected());
88
89  // Returning true closes the dialog.
90  return true;
91}
92
93bool ExternalProtocolDialog::Accept() {
94  // We record how long it takes the user to accept an external protocol.  If
95  // users start accepting these dialogs too quickly, we should worry about
96  // clickjacking.
97  UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url",
98                           base::TimeTicks::Now() - creation_time_);
99
100  delegate_->DoAccept(delegate_->url(),
101                      message_box_view_->IsCheckBoxSelected());
102
103  // Returning true closes the dialog.
104  return true;
105}
106
107views::View* ExternalProtocolDialog::GetContentsView() {
108  return message_box_view_;
109}
110
111views::Widget* ExternalProtocolDialog::GetWidget() {
112  return message_box_view_->GetWidget();
113}
114
115const views::Widget* ExternalProtocolDialog::GetWidget() const {
116  return message_box_view_->GetWidget();
117}
118
119///////////////////////////////////////////////////////////////////////////////
120// ExternalProtocolDialog, private:
121
122ExternalProtocolDialog::ExternalProtocolDialog(
123    scoped_ptr<const ProtocolDialogDelegate> delegate,
124    int render_process_host_id,
125    int routing_id)
126    : delegate_(delegate.Pass()),
127      render_process_host_id_(render_process_host_id),
128      routing_id_(routing_id),
129      creation_time_(base::TimeTicks::Now()) {
130  views::MessageBoxView::InitParams params(delegate_->GetMessageText());
131  params.message_width = kMessageWidth;
132  message_box_view_ = new views::MessageBoxView(params);
133  message_box_view_->SetCheckBoxLabel(delegate_->GetCheckboxText());
134
135  // Dialog is top level if we don't have a web_contents associated with us.
136  WebContents* web_contents = tab_util::GetWebContentsByID(
137      render_process_host_id_, routing_id_);
138  gfx::NativeWindow parent_window = NULL;
139  if (web_contents)
140    parent_window = web_contents->GetView()->GetTopLevelNativeWindow();
141  CreateBrowserModalDialogViews(this, parent_window)->Show();
142}
143