external_protocol_dialog.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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/ui/views/external_protocol_dialog.h" 6 7#include "base/metrics/histogram.h" 8#include "base/string_util.h" 9#include "base/threading/thread.h" 10#include "base/threading/thread_restrictions.h" 11#include "base/utf_string_conversions.h" 12#include "base/win/registry.h" 13#include "chrome/browser/external_protocol/external_protocol_handler.h" 14#include "chrome/browser/tab_contents/tab_util.h" 15#include "content/public/browser/web_contents.h" 16#include "content/public/browser/web_contents_view.h" 17#include "grit/chromium_strings.h" 18#include "grit/generated_resources.h" 19#include "ui/base/l10n/l10n_util.h" 20#include "ui/base/text/text_elider.h" 21#include "ui/views/controls/message_box_view.h" 22#include "ui/views/widget/widget.h" 23 24using content::WebContents; 25 26namespace { 27 28const int kMessageWidth = 400; 29 30} // namespace 31 32/////////////////////////////////////////////////////////////////////////////// 33// ExternalProtocolHandler 34 35// static 36void ExternalProtocolHandler::RunExternalProtocolDialog( 37 const GURL& url, int render_process_host_id, int routing_id) { 38 std::wstring command = 39 ExternalProtocolDialog::GetApplicationForProtocol(url); 40 if (command.empty()) { 41 // ShellExecute won't do anything. Don't bother warning the user. 42 return; 43 } 44 WebContents* web_contents = tab_util::GetWebContentsByID( 45 render_process_host_id, routing_id); 46 DCHECK(web_contents); 47 // Windowing system takes ownership. 48 new ExternalProtocolDialog(web_contents, url, command); 49} 50 51/////////////////////////////////////////////////////////////////////////////// 52// ExternalProtocolDialog 53 54ExternalProtocolDialog::~ExternalProtocolDialog() { 55} 56 57////////////////////////////////////////////////////////////////////////////// 58// ExternalProtocolDialog, views::DialogDelegate implementation: 59 60int ExternalProtocolDialog::GetDefaultDialogButton() const { 61 return ui::DIALOG_BUTTON_CANCEL; 62} 63 64string16 ExternalProtocolDialog::GetDialogButtonLabel( 65 ui::DialogButton button) const { 66 if (button == ui::DIALOG_BUTTON_OK) 67 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT); 68 else 69 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CANCEL_BUTTON_TEXT); 70} 71 72string16 ExternalProtocolDialog::GetWindowTitle() const { 73 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE); 74} 75 76void ExternalProtocolDialog::DeleteDelegate() { 77 delete this; 78} 79 80bool ExternalProtocolDialog::Cancel() { 81 // We also get called back here if the user closes the dialog or presses 82 // escape. In these cases it would be preferable to ignore the state of the 83 // check box but MessageBox doesn't distinguish this from pressing the cancel 84 // button. 85 if (message_box_view_->IsCheckBoxSelected()) { 86 ExternalProtocolHandler::SetBlockState( 87 url_.scheme(), ExternalProtocolHandler::BLOCK); 88 } 89 90 // Returning true closes the dialog. 91 return true; 92} 93 94bool ExternalProtocolDialog::Accept() { 95 // We record how long it takes the user to accept an external protocol. If 96 // users start accepting these dialogs too quickly, we should worry about 97 // clickjacking. 98 UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url", 99 base::TimeTicks::Now() - creation_time_); 100 101 if (message_box_view_->IsCheckBoxSelected()) { 102 ExternalProtocolHandler::SetBlockState( 103 url_.scheme(), ExternalProtocolHandler::DONT_BLOCK); 104 } 105 106 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(url_); 107 // Returning true closes the dialog. 108 return true; 109} 110 111views::View* ExternalProtocolDialog::GetContentsView() { 112 return message_box_view_; 113} 114 115views::Widget* ExternalProtocolDialog::GetWidget() { 116 return message_box_view_->GetWidget(); 117} 118 119const views::Widget* ExternalProtocolDialog::GetWidget() const { 120 return message_box_view_->GetWidget(); 121} 122 123/////////////////////////////////////////////////////////////////////////////// 124// ExternalProtocolDialog, private: 125 126ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents, 127 const GURL& url, 128 const std::wstring& command) 129 : web_contents_(web_contents), 130 url_(url), 131 creation_time_(base::TimeTicks::Now()) { 132 const int kMaxUrlWithoutSchemeSize = 256; 133 const int kMaxCommandSize = 256; 134 string16 elided_url_without_scheme; 135 string16 elided_command; 136 ui::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), 137 kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); 138 ui::ElideString(WideToUTF16Hack(command), kMaxCommandSize, &elided_command); 139 140 string16 message_text = l10n_util::GetStringFUTF16( 141 IDS_EXTERNAL_PROTOCOL_INFORMATION, 142 ASCIIToUTF16(url.scheme() + ":"), 143 elided_url_without_scheme) + ASCIIToUTF16("\n\n"); 144 145 message_text += l10n_util::GetStringFUTF16( 146 IDS_EXTERNAL_PROTOCOL_APPLICATION_TO_LAUNCH, 147 elided_command) + ASCIIToUTF16("\n\n"); 148 149 message_text += l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_WARNING); 150 151 views::MessageBoxView::InitParams params(message_text); 152 params.message_width = kMessageWidth; 153 message_box_view_ = new views::MessageBoxView(params); 154 message_box_view_->SetCheckBoxLabel( 155 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)); 156 157 // Dialog is top level if we don't have a web_contents associated with us. 158 HWND root_hwnd = NULL; 159 if (web_contents_) { 160 root_hwnd = GetAncestor(web_contents_->GetView()->GetContentNativeView(), 161 GA_ROOT); 162 } 163 164 views::DialogDelegate::CreateDialogWidget(this, NULL, root_hwnd)->Show(); 165} 166 167// static 168std::wstring ExternalProtocolDialog::GetApplicationForProtocol( 169 const GURL& url) { 170 // We shouldn't be accessing the registry from the UI thread, since it can go 171 // to disk. http://crbug.com/61996 172 base::ThreadRestrictions::ScopedAllowIO allow_io; 173 174 std::wstring url_spec = ASCIIToWide(url.possibly_invalid_spec()); 175 std::wstring cmd_key_path = 176 ASCIIToWide(url.scheme() + "\\shell\\open\\command"); 177 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT, cmd_key_path.c_str(), KEY_READ); 178 size_t split_offset = url_spec.find(L':'); 179 if (split_offset == std::wstring::npos) 180 return std::wstring(); 181 std::wstring parameters = url_spec.substr(split_offset + 1, 182 url_spec.length() - 1); 183 std::wstring application_to_launch; 184 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) { 185 ReplaceSubstringsAfterOffset(&application_to_launch, 0, L"%1", parameters); 186 return application_to_launch; 187 } else { 188 return std::wstring(); 189 } 190} 191