| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/external_protocol_dialog.h" | 5 #include "chrome/browser/ui/views/external_protocol_dialog.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 | 35 |
| 36 // static | 36 // static |
| 37 void ExternalProtocolHandler::RunExternalProtocolDialog( | 37 void ExternalProtocolHandler::RunExternalProtocolDialog( |
| 38 const GURL& url, int render_process_host_id, int routing_id) { | 38 const GURL& url, int render_process_host_id, int routing_id) { |
| 39 std::wstring command = | 39 std::wstring command = |
| 40 ExternalProtocolDialog::GetApplicationForProtocol(url); | 40 ExternalProtocolDialog::GetApplicationForProtocol(url); |
| 41 if (command.empty()) { | 41 if (command.empty()) { |
| 42 // ShellExecute won't do anything. Don't bother warning the user. | 42 // ShellExecute won't do anything. Don't bother warning the user. |
| 43 return; | 43 return; |
| 44 } | 44 } |
| 45 WebContents* web_contents = tab_util::GetWebContentsByID( | |
| 46 render_process_host_id, routing_id); | |
| 47 DCHECK(web_contents); | |
| 48 // Windowing system takes ownership. | 45 // Windowing system takes ownership. |
| 49 new ExternalProtocolDialog(web_contents, url, command); | 46 new ExternalProtocolDialog(url, render_process_host_id, routing_id, command); |
| 50 } | 47 } |
| 51 | 48 |
| 52 /////////////////////////////////////////////////////////////////////////////// | 49 /////////////////////////////////////////////////////////////////////////////// |
| 53 // ExternalProtocolDialog | 50 // ExternalProtocolDialog |
| 54 | 51 |
| 55 ExternalProtocolDialog::~ExternalProtocolDialog() { | 52 ExternalProtocolDialog::~ExternalProtocolDialog() { |
| 56 } | 53 } |
| 57 | 54 |
| 58 ////////////////////////////////////////////////////////////////////////////// | 55 ////////////////////////////////////////////////////////////////////////////// |
| 59 // ExternalProtocolDialog, views::DialogDelegate implementation: | 56 // ExternalProtocolDialog, views::DialogDelegate implementation: |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 // users start accepting these dialogs too quickly, we should worry about | 94 // users start accepting these dialogs too quickly, we should worry about |
| 98 // clickjacking. | 95 // clickjacking. |
| 99 UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url", | 96 UMA_HISTOGRAM_LONG_TIMES("clickjacking.launch_url", |
| 100 base::TimeTicks::Now() - creation_time_); | 97 base::TimeTicks::Now() - creation_time_); |
| 101 | 98 |
| 102 if (message_box_view_->IsCheckBoxSelected()) { | 99 if (message_box_view_->IsCheckBoxSelected()) { |
| 103 ExternalProtocolHandler::SetBlockState( | 100 ExternalProtocolHandler::SetBlockState( |
| 104 url_.scheme(), ExternalProtocolHandler::DONT_BLOCK); | 101 url_.scheme(), ExternalProtocolHandler::DONT_BLOCK); |
| 105 } | 102 } |
| 106 | 103 |
| 107 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(url_); | 104 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck( |
| 105 url_, render_process_host_id_, routing_id_); |
| 108 // Returning true closes the dialog. | 106 // Returning true closes the dialog. |
| 109 return true; | 107 return true; |
| 110 } | 108 } |
| 111 | 109 |
| 112 views::View* ExternalProtocolDialog::GetContentsView() { | 110 views::View* ExternalProtocolDialog::GetContentsView() { |
| 113 return message_box_view_; | 111 return message_box_view_; |
| 114 } | 112 } |
| 115 | 113 |
| 116 views::Widget* ExternalProtocolDialog::GetWidget() { | 114 views::Widget* ExternalProtocolDialog::GetWidget() { |
| 117 return message_box_view_->GetWidget(); | 115 return message_box_view_->GetWidget(); |
| 118 } | 116 } |
| 119 | 117 |
| 120 const views::Widget* ExternalProtocolDialog::GetWidget() const { | 118 const views::Widget* ExternalProtocolDialog::GetWidget() const { |
| 121 return message_box_view_->GetWidget(); | 119 return message_box_view_->GetWidget(); |
| 122 } | 120 } |
| 123 | 121 |
| 124 /////////////////////////////////////////////////////////////////////////////// | 122 /////////////////////////////////////////////////////////////////////////////// |
| 125 // ExternalProtocolDialog, private: | 123 // ExternalProtocolDialog, private: |
| 126 | 124 |
| 127 ExternalProtocolDialog::ExternalProtocolDialog(WebContents* web_contents, | 125 ExternalProtocolDialog::ExternalProtocolDialog(const GURL& url, |
| 128 const GURL& url, | 126 int render_process_host_id, |
| 127 int routing_id, |
| 129 const std::wstring& command) | 128 const std::wstring& command) |
| 130 : web_contents_(web_contents), | 129 : url_(url), |
| 131 url_(url), | 130 render_process_host_id_(render_process_host_id), |
| 131 routing_id_(routing_id), |
| 132 creation_time_(base::TimeTicks::Now()) { | 132 creation_time_(base::TimeTicks::Now()) { |
| 133 const int kMaxUrlWithoutSchemeSize = 256; | 133 const int kMaxUrlWithoutSchemeSize = 256; |
| 134 const int kMaxCommandSize = 256; | 134 const int kMaxCommandSize = 256; |
| 135 string16 elided_url_without_scheme; | 135 string16 elided_url_without_scheme; |
| 136 string16 elided_command; | 136 string16 elided_command; |
| 137 gfx::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), | 137 gfx::ElideString(ASCIIToUTF16(url.possibly_invalid_spec()), |
| 138 kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); | 138 kMaxUrlWithoutSchemeSize, &elided_url_without_scheme); |
| 139 gfx::ElideString(WideToUTF16Hack(command), kMaxCommandSize, &elided_command); | 139 gfx::ElideString(WideToUTF16Hack(command), kMaxCommandSize, &elided_command); |
| 140 | 140 |
| 141 string16 message_text = l10n_util::GetStringFUTF16( | 141 string16 message_text = l10n_util::GetStringFUTF16( |
| 142 IDS_EXTERNAL_PROTOCOL_INFORMATION, | 142 IDS_EXTERNAL_PROTOCOL_INFORMATION, |
| 143 ASCIIToUTF16(url.scheme() + ":"), | 143 ASCIIToUTF16(url.scheme() + ":"), |
| 144 elided_url_without_scheme) + ASCIIToUTF16("\n\n"); | 144 elided_url_without_scheme) + ASCIIToUTF16("\n\n"); |
| 145 | 145 |
| 146 message_text += l10n_util::GetStringFUTF16( | 146 message_text += l10n_util::GetStringFUTF16( |
| 147 IDS_EXTERNAL_PROTOCOL_APPLICATION_TO_LAUNCH, | 147 IDS_EXTERNAL_PROTOCOL_APPLICATION_TO_LAUNCH, |
| 148 elided_command) + ASCIIToUTF16("\n\n"); | 148 elided_command) + ASCIIToUTF16("\n\n"); |
| 149 | 149 |
| 150 message_text += l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_WARNING); | 150 message_text += l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_WARNING); |
| 151 | 151 |
| 152 views::MessageBoxView::InitParams params(message_text); | 152 views::MessageBoxView::InitParams params(message_text); |
| 153 params.message_width = kMessageWidth; | 153 params.message_width = kMessageWidth; |
| 154 message_box_view_ = new views::MessageBoxView(params); | 154 message_box_view_ = new views::MessageBoxView(params); |
| 155 message_box_view_->SetCheckBoxLabel( | 155 message_box_view_->SetCheckBoxLabel( |
| 156 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)); | 156 l10n_util::GetStringUTF16(IDS_EXTERNAL_PROTOCOL_CHECKBOX_TEXT)); |
| 157 | 157 |
| 158 // Dialog is top level if we don't have a web_contents associated with us. | 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_); |
| 159 gfx::NativeWindow parent_window = NULL; | 161 gfx::NativeWindow parent_window = NULL; |
| 160 if (web_contents_) | 162 if (web_contents) |
| 161 parent_window = web_contents_->GetView()->GetTopLevelNativeWindow(); | 163 parent_window = web_contents->GetView()->GetTopLevelNativeWindow(); |
| 162 CreateBrowserModalDialogViews(this, parent_window)->Show(); | 164 CreateBrowserModalDialogViews(this, parent_window)->Show(); |
| 163 } | 165 } |
| 164 | 166 |
| 165 // static | 167 // static |
| 166 std::wstring ExternalProtocolDialog::GetApplicationForProtocol( | 168 std::wstring ExternalProtocolDialog::GetApplicationForProtocol( |
| 167 const GURL& url) { | 169 const GURL& url) { |
| 168 // We shouldn't be accessing the registry from the UI thread, since it can go | 170 // We shouldn't be accessing the registry from the UI thread, since it can go |
| 169 // to disk. http://crbug.com/61996 | 171 // to disk. http://crbug.com/61996 |
| 170 base::ThreadRestrictions::ScopedAllowIO allow_io; | 172 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 171 | 173 |
| 172 std::wstring url_spec = ASCIIToWide(url.possibly_invalid_spec()); | 174 std::wstring url_spec = ASCIIToWide(url.possibly_invalid_spec()); |
| 173 std::wstring cmd_key_path = | 175 std::wstring cmd_key_path = |
| 174 ASCIIToWide(url.scheme() + "\\shell\\open\\command"); | 176 ASCIIToWide(url.scheme() + "\\shell\\open\\command"); |
| 175 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT, cmd_key_path.c_str(), KEY_READ); | 177 base::win::RegKey cmd_key(HKEY_CLASSES_ROOT, cmd_key_path.c_str(), KEY_READ); |
| 176 size_t split_offset = url_spec.find(L':'); | 178 size_t split_offset = url_spec.find(L':'); |
| 177 if (split_offset == std::wstring::npos) | 179 if (split_offset == std::wstring::npos) |
| 178 return std::wstring(); | 180 return std::wstring(); |
| 179 std::wstring parameters = url_spec.substr(split_offset + 1, | 181 std::wstring parameters = url_spec.substr(split_offset + 1, |
| 180 url_spec.length() - 1); | 182 url_spec.length() - 1); |
| 181 std::wstring application_to_launch; | 183 std::wstring application_to_launch; |
| 182 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) { | 184 if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) { |
| 183 ReplaceSubstringsAfterOffset(&application_to_launch, 0, L"%1", parameters); | 185 ReplaceSubstringsAfterOffset(&application_to_launch, 0, L"%1", parameters); |
| 184 return application_to_launch; | 186 return application_to_launch; |
| 185 } else { | 187 } else { |
| 186 return std::wstring(); | 188 return std::wstring(); |
| 187 } | 189 } |
| 188 } | 190 } |
| OLD | NEW |