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/chromeos/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/grit/chromium_strings.h"
13 #include "chrome/grit/generated_resources.h"
14 #include "content/public/browser/web_contents.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/gfx/text_elider.h"
17 #include "ui/views/controls/message_box_view.h"
18 #include "ui/views/widget/widget.h"
21 using content::WebContents
;
25 const int kMessageWidth
= 400;
29 ///////////////////////////////////////////////////////////////////////////////
30 // ExternalProtocolHandler
33 void ExternalProtocolHandler::RunExternalProtocolDialog(
34 const GURL
& url
, int render_process_host_id
, int routing_id
) {
35 WebContents
* web_contents
= tab_util::GetWebContentsByID(
36 render_process_host_id
, routing_id
);
37 new ExternalProtocolDialog(web_contents
, url
);
40 ///////////////////////////////////////////////////////////////////////////////
41 // ExternalProtocolDialog
43 ExternalProtocolDialog::~ExternalProtocolDialog() {
46 //////////////////////////////////////////////////////////////////////////////
47 // ExternalProtocolDialog, views::DialogDelegate implementation:
49 int ExternalProtocolDialog::GetDialogButtons() const {
50 return ui::DIALOG_BUTTON_OK
;
53 base::string16
ExternalProtocolDialog::GetDialogButtonLabel(
54 ui::DialogButton button
) const {
55 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT
);
58 base::string16
ExternalProtocolDialog::GetWindowTitle() const {
59 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE
);
62 void ExternalProtocolDialog::DeleteDelegate() {
66 bool ExternalProtocolDialog::Accept() {
67 if (message_box_view_
->IsCheckBoxSelected()) {
68 ExternalProtocolHandler::SetBlockState(
69 scheme_
, ExternalProtocolHandler::DONT_BLOCK
);
71 // Returning true closes the dialog.
75 views::View
* ExternalProtocolDialog::GetContentsView() {
76 return message_box_view_
;
79 const views::Widget
* ExternalProtocolDialog::GetWidget() const {
80 return message_box_view_
->GetWidget();
83 views::Widget
* ExternalProtocolDialog::GetWidget() {
84 return message_box_view_
->GetWidget();
87 ///////////////////////////////////////////////////////////////////////////////
88 // ExternalProtocolDialog, private:
90 ExternalProtocolDialog::ExternalProtocolDialog(WebContents
* web_contents
,
92 : creation_time_(base::TimeTicks::Now()),
93 scheme_(url
.scheme()) {
94 const int kMaxUrlWithoutSchemeSize
= 256;
95 base::string16 elided_url_without_scheme
;
96 gfx::ElideString(base::ASCIIToUTF16(url
.possibly_invalid_spec()),
97 kMaxUrlWithoutSchemeSize
, &elided_url_without_scheme
);
99 views::MessageBoxView::InitParams
params(
100 l10n_util::GetStringFUTF16(IDS_EXTERNAL_PROTOCOL_INFORMATION
,
101 base::ASCIIToUTF16(url
.scheme() + ":"),
102 elided_url_without_scheme
) + base::ASCIIToUTF16("\n\n"));
103 params
.message_width
= kMessageWidth
;
104 message_box_view_
= new views::MessageBoxView(params
);
105 message_box_view_
->SetCheckBoxLabel(
106 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT
));
108 gfx::NativeWindow parent_window
;
110 parent_window
= web_contents
->GetTopLevelNativeWindow();
112 // Dialog is top level if we don't have a web_contents associated with us.
113 parent_window
= NULL
;
115 views::DialogDelegate::CreateDialogWidget(this, NULL
, parent_window
)->Show();