Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / external_protocol_dialog.cc
blob7bed86d1ebf5572fa388654685879f917d74a690
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/gfx/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 new ExternalProtocolDialog(web_contents, url);
41 ///////////////////////////////////////////////////////////////////////////////
42 // ExternalProtocolDialog
44 ExternalProtocolDialog::~ExternalProtocolDialog() {
47 //////////////////////////////////////////////////////////////////////////////
48 // ExternalProtocolDialog, views::DialogDelegate implementation:
50 int ExternalProtocolDialog::GetDialogButtons() const {
51 return ui::DIALOG_BUTTON_OK;
54 base::string16 ExternalProtocolDialog::GetDialogButtonLabel(
55 ui::DialogButton button) const {
56 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT);
59 base::string16 ExternalProtocolDialog::GetWindowTitle() const {
60 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE);
63 void ExternalProtocolDialog::DeleteDelegate() {
64 delete this;
67 bool ExternalProtocolDialog::Accept() {
68 if (message_box_view_->IsCheckBoxSelected()) {
69 ExternalProtocolHandler::SetBlockState(
70 scheme_, ExternalProtocolHandler::DONT_BLOCK);
72 // Returning true closes the dialog.
73 return true;
76 views::View* ExternalProtocolDialog::GetContentsView() {
77 return message_box_view_;
80 const views::Widget* ExternalProtocolDialog::GetWidget() const {
81 return message_box_view_->GetWidget();
84 views::Widget* ExternalProtocolDialog::GetWidget() {
85 return message_box_view_->GetWidget();
88 ///////////////////////////////////////////////////////////////////////////////
89 // ExternalProtocolDialog, private:
91 ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents,
92 const GURL& url)
93 : creation_time_(base::TimeTicks::Now()),
94 scheme_(url.scheme()) {
95 const int kMaxUrlWithoutSchemeSize = 256;
96 base::string16 elided_url_without_scheme;
97 gfx::ElideString(base::ASCIIToUTF16(url.possibly_invalid_spec()),
98 kMaxUrlWithoutSchemeSize, &elided_url_without_scheme);
100 views::MessageBoxView::InitParams params(
101 l10n_util::GetStringFUTF16(IDS_EXTERNAL_PROTOCOL_INFORMATION,
102 base::ASCIIToUTF16(url.scheme() + ":"),
103 elided_url_without_scheme) + base::ASCIIToUTF16("\n\n"));
104 params.message_width = kMessageWidth;
105 message_box_view_ = new views::MessageBoxView(params);
106 message_box_view_->SetCheckBoxLabel(
107 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT));
109 gfx::NativeWindow parent_window;
110 if (web_contents) {
111 parent_window = web_contents->GetView()->GetTopLevelNativeWindow();
112 } else {
113 // Dialog is top level if we don't have a web_contents associated with us.
114 parent_window = NULL;
116 views::DialogDelegate::CreateDialogWidget(this, NULL, parent_window)->Show();