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 "grit/chromium_strings.h" 14#include "grit/generated_resources.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" 19#include "url/gurl.h" 20 21using content::WebContents; 22 23namespace { 24 25const int kMessageWidth = 400; 26 27} // namespace 28 29/////////////////////////////////////////////////////////////////////////////// 30// ExternalProtocolHandler 31 32// static 33void 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); 38} 39 40/////////////////////////////////////////////////////////////////////////////// 41// ExternalProtocolDialog 42 43ExternalProtocolDialog::~ExternalProtocolDialog() { 44} 45 46////////////////////////////////////////////////////////////////////////////// 47// ExternalProtocolDialog, views::DialogDelegate implementation: 48 49int ExternalProtocolDialog::GetDialogButtons() const { 50 return ui::DIALOG_BUTTON_OK; 51} 52 53base::string16 ExternalProtocolDialog::GetDialogButtonLabel( 54 ui::DialogButton button) const { 55 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT); 56} 57 58base::string16 ExternalProtocolDialog::GetWindowTitle() const { 59 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE); 60} 61 62void ExternalProtocolDialog::DeleteDelegate() { 63 delete this; 64} 65 66bool ExternalProtocolDialog::Accept() { 67 if (message_box_view_->IsCheckBoxSelected()) { 68 ExternalProtocolHandler::SetBlockState( 69 scheme_, ExternalProtocolHandler::DONT_BLOCK); 70 } 71 // Returning true closes the dialog. 72 return true; 73} 74 75views::View* ExternalProtocolDialog::GetContentsView() { 76 return message_box_view_; 77} 78 79const views::Widget* ExternalProtocolDialog::GetWidget() const { 80 return message_box_view_->GetWidget(); 81} 82 83views::Widget* ExternalProtocolDialog::GetWidget() { 84 return message_box_view_->GetWidget(); 85} 86 87/////////////////////////////////////////////////////////////////////////////// 88// ExternalProtocolDialog, private: 89 90ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents, 91 const GURL& url) 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); 98 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)); 107 108 gfx::NativeWindow parent_window; 109 if (web_contents) { 110 parent_window = web_contents->GetTopLevelNativeWindow(); 111 } else { 112 // Dialog is top level if we don't have a web_contents associated with us. 113 parent_window = NULL; 114 } 115 views::DialogDelegate::CreateDialogWidget(this, NULL, parent_window)->Show(); 116} 117