| 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/external_protocol/external_protocol_handler.h" | 5 #include "chrome/browser/external_protocol/external_protocol_handler.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/prefs/scoped_user_pref_update.h" | 14 #include "base/prefs/scoped_user_pref_update.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/platform_util.h" | 19 #include "chrome/browser/platform_util.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/tab_contents/tab_util.h" | 21 #include "chrome/browser/tab_contents/tab_util.h" |
| 22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/web_contents.h" | 24 #include "content/public/browser/web_contents.h" |
| 25 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
| 26 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 27 | 27 |
| 28 using content::BrowserThread; | 28 using content::BrowserThread; |
| 29 | 29 |
| 30 // Whether we accept requests for launching external protocols. This is set to | |
| 31 // false every time an external protocol is requested, and set back to true on | |
| 32 // each user gesture. This variable should only be accessed from the UI thread. | |
| 33 static bool g_accept_requests = true; | |
| 34 | |
| 35 namespace { | 30 namespace { |
| 36 | 31 |
| 37 // Functions enabling unit testing. Using a NULL delegate will use the default | 32 // Functions enabling unit testing. Using a NULL delegate will use the default |
| 38 // behavior; if a delegate is provided it will be used instead. | 33 // behavior; if a delegate is provided it will be used instead. |
| 39 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | 34 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
| 40 ShellIntegration::DefaultWebClientObserver* observer, | 35 ShellIntegration::DefaultWebClientObserver* observer, |
| 41 const std::string& protocol, | 36 const std::string& protocol, |
| 42 ExternalProtocolHandler::Delegate* delegate) { | 37 ExternalProtocolHandler::Delegate* delegate) { |
| 43 if (!delegate) | 38 if (!delegate) |
| 44 return new ShellIntegration::DefaultProtocolClientWorker(observer, | 39 return new ShellIntegration::DefaultProtocolClientWorker(observer, |
| 45 protocol); | 40 protocol); |
| 46 | 41 |
| 47 return delegate->CreateShellWorker(observer, protocol); | 42 return delegate->CreateShellWorker(observer, protocol); |
| 48 } | 43 } |
| 49 | 44 |
| 50 ExternalProtocolHandler::BlockState GetBlockStateWithDelegate( | 45 ExternalProtocolHandler::BlockState GetBlockStateWithDelegate( |
| 51 const std::string& scheme, | 46 const std::string& scheme, |
| 52 ExternalProtocolHandler::Delegate* delegate) { | 47 ExternalProtocolHandler::Delegate* delegate, |
| 48 bool initiated_by_user_gesture) { |
| 53 if (!delegate) | 49 if (!delegate) |
| 54 return ExternalProtocolHandler::GetBlockState(scheme); | 50 return ExternalProtocolHandler::GetBlockState(scheme, |
| 51 initiated_by_user_gesture); |
| 55 | 52 |
| 56 return delegate->GetBlockState(scheme); | 53 return delegate->GetBlockState(scheme, initiated_by_user_gesture); |
| 57 } | 54 } |
| 58 | 55 |
| 59 void RunExternalProtocolDialogWithDelegate( | 56 void RunExternalProtocolDialogWithDelegate( |
| 60 const GURL& url, | 57 const GURL& url, |
| 61 int render_process_host_id, | 58 int render_process_host_id, |
| 62 int routing_id, | 59 int routing_id, |
| 63 ExternalProtocolHandler::Delegate* delegate) { | 60 ExternalProtocolHandler::Delegate* delegate) { |
| 64 if (!delegate) { | 61 if (!delegate) { |
| 65 ExternalProtocolHandler::RunExternalProtocolDialog(url, | 62 ExternalProtocolHandler::RunExternalProtocolDialog(url, |
| 66 render_process_host_id, | 63 render_process_host_id, |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 193 |
| 197 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) { | 194 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) { |
| 198 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) { | 195 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) { |
| 199 win_pref->SetBoolean(allowed_schemes[i], false); | 196 win_pref->SetBoolean(allowed_schemes[i], false); |
| 200 } | 197 } |
| 201 } | 198 } |
| 202 } | 199 } |
| 203 | 200 |
| 204 // static | 201 // static |
| 205 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState( | 202 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState( |
| 206 const std::string& scheme) { | 203 const std::string& scheme, |
| 207 // If we are being carpet bombed, block the request. | 204 bool initiated_by_user_gesture) { |
| 208 if (!g_accept_requests) | 205 if (!initiated_by_user_gesture) |
| 209 return BLOCK; | 206 return BLOCK; |
| 210 | 207 |
| 211 if (scheme.length() == 1) { | 208 if (scheme.length() == 1) { |
| 212 // We have a URL that looks something like: | 209 // We have a URL that looks something like: |
| 213 // C:/WINDOWS/system32/notepad.exe | 210 // C:/WINDOWS/system32/notepad.exe |
| 214 // ShellExecuting this URL will cause the specified program to be executed. | 211 // ShellExecuting this URL will cause the specified program to be executed. |
| 215 return BLOCK; | 212 return BLOCK; |
| 216 } | 213 } |
| 217 | 214 |
| 218 // Check the stored prefs. | 215 // Check the stored prefs. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 245 | 242 |
| 246 if (state == UNKNOWN) { | 243 if (state == UNKNOWN) { |
| 247 update_excluded_schemas->Remove(scheme, NULL); | 244 update_excluded_schemas->Remove(scheme, NULL); |
| 248 } else { | 245 } else { |
| 249 update_excluded_schemas->SetBoolean(scheme, (state == BLOCK)); | 246 update_excluded_schemas->SetBoolean(scheme, (state == BLOCK)); |
| 250 } | 247 } |
| 251 } | 248 } |
| 252 } | 249 } |
| 253 | 250 |
| 254 // static | 251 // static |
| 255 void ExternalProtocolHandler::LaunchUrlWithDelegate(const GURL& url, | 252 void ExternalProtocolHandler::LaunchUrlWithDelegate( |
| 256 int render_process_host_id, | 253 const GURL& url, |
| 257 int tab_contents_id, | 254 int render_process_host_id, |
| 258 Delegate* delegate) { | 255 int tab_contents_id, |
| 256 Delegate* delegate, |
| 257 bool initiated_by_user_gesture) { |
| 259 DCHECK(base::MessageLoopForUI::IsCurrent()); | 258 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 260 | 259 |
| 261 // Escape the input scheme to be sure that the command does not | 260 // Escape the input scheme to be sure that the command does not |
| 262 // have parameters unexpected by the external program. | 261 // have parameters unexpected by the external program. |
| 263 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec()); | 262 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec()); |
| 264 GURL escaped_url(escaped_url_string); | 263 GURL escaped_url(escaped_url_string); |
| 265 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(), | 264 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(), |
| 266 delegate); | 265 delegate, |
| 266 initiated_by_user_gesture); |
| 267 if (block_state == BLOCK) { | 267 if (block_state == BLOCK) { |
| 268 if (delegate) | 268 if (delegate) |
| 269 delegate->BlockRequest(); | 269 delegate->BlockRequest(); |
| 270 return; | 270 return; |
| 271 } | 271 } |
| 272 | 272 |
| 273 g_accept_requests = false; | |
| 274 | |
| 275 // The worker creates tasks with references to itself and puts them into | 273 // The worker creates tasks with references to itself and puts them into |
| 276 // message loops. When no tasks are left it will delete the observer and | 274 // message loops. When no tasks are left it will delete the observer and |
| 277 // eventually be deleted itself. | 275 // eventually be deleted itself. |
| 278 ShellIntegration::DefaultWebClientObserver* observer = | 276 ShellIntegration::DefaultWebClientObserver* observer = |
| 279 new ExternalDefaultProtocolObserver(url, | 277 new ExternalDefaultProtocolObserver(url, |
| 280 render_process_host_id, | 278 render_process_host_id, |
| 281 tab_contents_id, | 279 tab_contents_id, |
| 282 block_state == UNKNOWN, | 280 block_state == UNKNOWN, |
| 283 delegate); | 281 delegate); |
| 284 scoped_refptr<ShellIntegration::DefaultProtocolClientWorker> worker = | 282 scoped_refptr<ShellIntegration::DefaultProtocolClientWorker> worker = |
| (...skipping 16 matching lines...) Expand all Loading... |
| 301 return; | 299 return; |
| 302 | 300 |
| 303 platform_util::OpenExternal( | 301 platform_util::OpenExternal( |
| 304 Profile::FromBrowserContext(web_contents->GetBrowserContext()), url); | 302 Profile::FromBrowserContext(web_contents->GetBrowserContext()), url); |
| 305 } | 303 } |
| 306 | 304 |
| 307 // static | 305 // static |
| 308 void ExternalProtocolHandler::RegisterPrefs(PrefRegistrySimple* registry) { | 306 void ExternalProtocolHandler::RegisterPrefs(PrefRegistrySimple* registry) { |
| 309 registry->RegisterDictionaryPref(prefs::kExcludedSchemes); | 307 registry->RegisterDictionaryPref(prefs::kExcludedSchemes); |
| 310 } | 308 } |
| 311 | |
| 312 // static | |
| 313 void ExternalProtocolHandler::PermitLaunchUrl() { | |
| 314 DCHECK(base::MessageLoopForUI::IsCurrent()); | |
| 315 g_accept_requests = true; | |
| 316 } | |
| OLD | NEW |