external_protocol_dialog.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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/chromeos/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 "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" 21 22using content::WebContents; 23 24namespace { 25 26const int kMessageWidth = 400; 27 28} // namespace 29 30/////////////////////////////////////////////////////////////////////////////// 31// ExternalProtocolHandler 32 33// static 34void 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); 40} 41 42/////////////////////////////////////////////////////////////////////////////// 43// ExternalProtocolDialog 44 45ExternalProtocolDialog::~ExternalProtocolDialog() { 46} 47 48////////////////////////////////////////////////////////////////////////////// 49// ExternalProtocolDialog, views::DialogDelegate implementation: 50 51int ExternalProtocolDialog::GetDialogButtons() const { 52 return ui::DIALOG_BUTTON_OK; 53} 54 55string16 ExternalProtocolDialog::GetDialogButtonLabel( 56 ui::DialogButton button) const { 57 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT); 58} 59 60string16 ExternalProtocolDialog::GetWindowTitle() const { 61 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE); 62} 63 64void ExternalProtocolDialog::DeleteDelegate() { 65 delete this; 66} 67 68bool ExternalProtocolDialog::Accept() { 69 if (message_box_view_->IsCheckBoxSelected()) { 70 ExternalProtocolHandler::SetBlockState( 71 scheme_, ExternalProtocolHandler::DONT_BLOCK); 72 } 73 // Returning true closes the dialog. 74 return true; 75} 76 77views::View* ExternalProtocolDialog::GetContentsView() { 78 return message_box_view_; 79} 80 81const views::Widget* ExternalProtocolDialog::GetWidget() const { 82 return message_box_view_->GetWidget(); 83} 84 85views::Widget* ExternalProtocolDialog::GetWidget() { 86 return message_box_view_->GetWidget(); 87} 88 89/////////////////////////////////////////////////////////////////////////////// 90// ExternalProtocolDialog, private: 91 92ExternalProtocolDialog::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); 100 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)); 109 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; 116 } 117 views::DialogDelegate::CreateDialogWidget(this, NULL, parent_window)->Show(); 118} 119