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