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 25 using content::WebContents; 26 27 namespace { 28 29 const int kMessageWidth = 400; 30 31 } // namespace 32 33 /////////////////////////////////////////////////////////////////////////////// 34 // ExternalProtocolHandler 35 36 // static 37 void 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 // Windowing system takes ownership. 46 new ExternalProtocolDialog(url, render_process_host_id, routing_id, command); 47 } 48 49 /////////////////////////////////////////////////////////////////////////////// 50 // ExternalProtocolDialog 51 52 ExternalProtocolDialog::~ExternalProtocolDialog() { 53 } 54 55 ////////////////////////////////////////////////////////////////////////////// 56 // ExternalProtocolDialog, views::DialogDelegate implementation: 57 58 int ExternalProtocolDialog::GetDefaultDialogButton() const { 59 return ui::DIALOG_BUTTON_CANCEL; 60 } 61 62 base::string16 ExternalProtocolDialog::GetDialogButtonLabel( 63 ui::DialogButton button) const { 64 if (button == ui::DIALOG_BUTTON_OK) 65 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_OK_BUTTON_TEXT); 66 else 67 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CANCEL_BUTTON_TEXT); 68 } 69 70 base::string16 ExternalProtocolDialog::GetWindowTitle() const { 71 return l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_TITLE); 72 } 73 74 void ExternalProtocolDialog::DeleteDelegate() { 75 delete this; 76 } 77 78 bool ExternalProtocolDialog::Cancel() { 79 // We also get called back here if the user closes the dialog or presses 80 // escape. In these cases it would be preferable to ignore the state of the 81 // check box but MessageBox doesn't distinguish this from pressing the cancel 82 // button. 83 if (message_box_view_->IsCheckBoxSelected()) { 84 ExternalProtocolHandler::SetBlockState( 85 url_.scheme(), ExternalProtocolHandler::BLOCK); 86 } 87 88 // Returning true closes the dialog. 89 return true; 90 } 91 92 bool ExternalProtocolDialog::Accept() { 93 // We record how long it takes the user to accept an external protocol. If 94 // users start accepting these dialogs too quickly, we should worry about 95 // clickjacking. 96 UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url", 97 base::TimeTicks::Now() - creation_time_); 98 99 if (message_box_view_->IsCheckBoxSelected()) { 100 ExternalProtocolHandler::SetBlockState( 101 url_.scheme(), ExternalProtocolHandler::DONT_BLOCK); 102 } 103 104 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck( 105 url_, render_process_host_id_, routing_id_); 106 // Returning true closes the dialog. 107 return true; 108 } 109 110 views::View* ExternalProtocolDialog::GetContentsView() { 111 return message_box_view_; 112 } 113 114 views::Widget* ExternalProtocolDialog::GetWidget() { 115 return message_box_view_->GetWidget(); 116 } 117 118 const views::Widget* ExternalProtocolDialog::GetWidget() const { 119 return message_box_view_->GetWidget(); 120 } 121 122 /////////////////////////////////////////////////////////////////////////////// 123 // ExternalProtocolDialog, private: 124 125 ExternalProtocolDialog::ExternalProtocolDialog(const GURL& url, 126 int render_process_host_id, 127 int routing_id, 128 const std::wstring& command) 129 : url_(url), 130 render_process_host_id_(render_process_host_id), 131 routing_id_(routing_id), 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 WebContents* web_contents = tab_util::GetWebContentsByID( 160 render_process_host_id_, routing_id_); 161 gfx::NativeWindow parent_window = NULL; 162 if (web_contents) 163 parent_window = web_contents->GetView()->GetTopLevelNativeWindow(); 164 CreateBrowserModalDialogViews(this, parent_window)->Show(); 165 } 166 167 // static 168 std::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