| 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 | 30 // User gesture counter. This is increased when a user gesture is available, and |
| 31 // false every time an external protocol is requested, and set back to true on | 31 // decreased when the gesture is consumed or goes out of scope. This variable |
| 32 // each user gesture. This variable should only be accessed from the UI thread. | 32 // should only be accessed from the UI thread. |
| 33 static bool g_accept_requests = true; | 33 static size_t g_consumable_user_gestures = 0; |
| 34 | 34 |
| 35 namespace { | 35 namespace { |
| 36 | 36 |
| 37 // Functions enabling unit testing. Using a NULL delegate will use the default | 37 // Functions enabling unit testing. Using a NULL delegate will use the default |
| 38 // behavior; if a delegate is provided it will be used instead. | 38 // behavior; if a delegate is provided it will be used instead. |
| 39 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | 39 ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( |
| 40 ShellIntegration::DefaultWebClientObserver* observer, | 40 ShellIntegration::DefaultWebClientObserver* observer, |
| 41 const std::string& protocol, | 41 const std::string& protocol, |
| 42 ExternalProtocolHandler::Delegate* delegate) { | 42 ExternalProtocolHandler::Delegate* delegate) { |
| 43 if (!delegate) | 43 if (!delegate) |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) { | 197 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) { |
| 198 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) { | 198 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) { |
| 199 win_pref->SetBoolean(allowed_schemes[i], false); | 199 win_pref->SetBoolean(allowed_schemes[i], false); |
| 200 } | 200 } |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 // static | 204 // static |
| 205 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState( | 205 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState( |
| 206 const std::string& scheme) { | 206 const std::string& scheme) { |
| 207 // If we are being carpet bombed, block the request. | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 208 if (!g_accept_requests) | 208 if (!g_consumable_user_gestures) |
| 209 return BLOCK; | 209 return BLOCK; |
| 210 | 210 |
| 211 if (scheme.length() == 1) { | 211 if (scheme.length() == 1) { |
| 212 // We have a URL that looks something like: | 212 // We have a URL that looks something like: |
| 213 // C:/WINDOWS/system32/notepad.exe | 213 // C:/WINDOWS/system32/notepad.exe |
| 214 // ShellExecuting this URL will cause the specified program to be executed. | 214 // ShellExecuting this URL will cause the specified program to be executed. |
| 215 return BLOCK; | 215 return BLOCK; |
| 216 } | 216 } |
| 217 | 217 |
| 218 // Check the stored prefs. | 218 // Check the stored prefs. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec()); | 263 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec()); |
| 264 GURL escaped_url(escaped_url_string); | 264 GURL escaped_url(escaped_url_string); |
| 265 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(), | 265 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(), |
| 266 delegate); | 266 delegate); |
| 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 17 matching lines...) Expand all Loading... |
| 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 | 309 |
| 312 // static | 310 ExternalProtocolHandler::ScopedUserGesture::ScopedUserGesture() { |
| 313 void ExternalProtocolHandler::PermitLaunchUrl() { | 311 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 314 DCHECK(base::MessageLoopForUI::IsCurrent()); | 312 g_consumable_user_gestures++; |
| 315 g_accept_requests = true; | |
| 316 } | 313 } |
| 314 |
| 315 ExternalProtocolHandler::ScopedUserGesture::~ScopedUserGesture() { |
| 316 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 317 g_consumable_user_gestures--; |
| 318 } |
| OLD | NEW |