Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / chromeos / external_protocol_dialog.cc
blob724cd540a33cfa3d9bec45c8c5963364991c8bd8
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 "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_view.h"
14 #include "grit/chromium_strings.h"
15 #include "grit/generated_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/text/text_elider.h"
18 #include "ui/views/controls/message_box_view.h"
19 #include "ui/views/widget/widget.h"
20 #include "url/gurl.h"
22 using content::WebContents;
24 namespace {
26 const int kMessageWidth = 400;
28 } // namespace
30 ///////////////////////////////////////////////////////////////////////////////
31 // ExternalProtocolHandler
33 // static
34 void ExternalProtocolHandler::RunExternalProtocolDialog(
35 const GURL& url, int render_process_host_id, int routing_id) {
36 WebContents* web_contents = tab_util::GetWebContentsByID(
37 render_process_host_id, routing_id);
38 DCHECK(web_contents);
39 new ExternalProtocolDialog(web_contents, url);
42 ///////////////////////////////////////////////////////////////////////////////
43 // ExternalProtocolDialog
45 ExternalProtocolDialog::~ExternalProtocolDialog() {
48 //////////////////////////////////////////////////////////////////////////////
49 // ExternalProtocolDialog, views::DialogDelegate implementation:
51 int ExternalProtocolDialog::GetDialogButtons() const {
52 return ui::DIALOG_BUTTON_OK;
55 string16 ExternalProtocolDialog::GetDialogButtonLabel(
56 ui::DialogButton button) const {
57 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT);
60 string16 ExternalProtocolDialog::GetWindowTitle() const {
61 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE);
64 void ExternalProtocolDialog::DeleteDelegate() {
65 delete this;
68 bool ExternalProtocolDialog::Accept() {
69 if (message_box_view_->IsCheckBoxSelected()) {
70 ExternalProtocolHandler::SetBlockState(
71 scheme_, ExternalProtocolHandler::DONT_BLOCK);
73 // Returning true closes the dialog.
74 return true;
77 views::View* ExternalProtocolDialog::GetContentsView() {
78 return message_box_view_;
81 const views::Widget* ExternalProtocolDialog::GetWidget() const {
82 return message_box_view_->GetWidget();
85 views::Widget* ExternalProtocolDialog::GetWidget() {
86 return message_box_view_->GetWidget();
89 ///////////////////////////////////////////////////////////////////////////////
90 // ExternalProtocolDialog, private:
92 ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents,
93 const GURL& url)
94 : creation_time_(base::TimeTicks::Now()),
95 scheme_(url.scheme()) {
96 const int kMaxUrlWithoutSchemeSize = 256;
97 string16 elided_url_without_scheme;
98 ui::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()),
99 kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
101 views::MessageBoxView::InitParams params(
102 l10n_util::GetStringFUTF16(IDS_EXTERNAL_PROTOCOL_INFORMATION,
103 ASCIIToUTF16(url.scheme() + ":"),
104 elided_url_without_scheme) + ASCIIToUTF16("\n\n"));
105 params.message_width = kMessageWidth;
106 message_box_view_ = new views::MessageBoxView(params);
107 message_box_view_->SetCheckBoxLabel(
108 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT));
110 gfx::NativeWindow parent_window;
111 if (web_contents) {
112 parent_window = web_contents->GetView()->GetTopLevelNativeWindow();
113 } else {
114 // Dialog is top level if we don't have a web_contents associated with us.
115 parent_window = NULL;
117 views::DialogDelegate::CreateDialogWidget(this, NULL, parent_window)->Show();