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