Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4)

Side by Side Diff: chrome/browser/external_protocol/external_protocol_handler.cc

Issue 131783012: Fix the handling of user gestures for external protocol handler dialogs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix build Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 user_gesture) {
53 if (!delegate) 49 if (!delegate)
54 return ExternalProtocolHandler::GetBlockState(scheme); 50 return ExternalProtocolHandler::GetBlockState(scheme, user_gesture);
55 51
56 return delegate->GetBlockState(scheme); 52 return delegate->GetBlockState(scheme, user_gesture);
57 } 53 }
58 54
59 void RunExternalProtocolDialogWithDelegate( 55 void RunExternalProtocolDialogWithDelegate(
60 const GURL& url, 56 const GURL& url,
61 int render_process_host_id, 57 int render_process_host_id,
62 int routing_id, 58 int routing_id,
63 ExternalProtocolHandler::Delegate* delegate) { 59 ExternalProtocolHandler::Delegate* delegate) {
64 if (!delegate) { 60 if (!delegate) {
65 ExternalProtocolHandler::RunExternalProtocolDialog(url, 61 ExternalProtocolHandler::RunExternalProtocolDialog(url,
66 render_process_host_id, 62 render_process_host_id,
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 192
197 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) { 193 for (size_t i = 0; i < arraysize(allowed_schemes); ++i) {
198 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) { 194 if (!win_pref->GetBoolean(allowed_schemes[i], &should_block)) {
199 win_pref->SetBoolean(allowed_schemes[i], false); 195 win_pref->SetBoolean(allowed_schemes[i], false);
200 } 196 }
201 } 197 }
202 } 198 }
203 199
204 // static 200 // static
205 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState( 201 ExternalProtocolHandler::BlockState ExternalProtocolHandler::GetBlockState(
206 const std::string& scheme) { 202 const std::string& scheme,
207 // If we are being carpet bombed, block the request. 203 bool user_gesture) {
208 if (!g_accept_requests) 204
205 if (!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 29 matching lines...) Expand all
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(const GURL& url,
256 int render_process_host_id, 253 int render_process_host_id,
257 int tab_contents_id, 254 int tab_contents_id,
258 Delegate* delegate) { 255 Delegate* delegate,
256 bool user_gesture) {
259 DCHECK(base::MessageLoopForUI::IsCurrent()); 257 DCHECK(base::MessageLoopForUI::IsCurrent());
260 258
261 // Escape the input scheme to be sure that the command does not 259 // Escape the input scheme to be sure that the command does not
262 // have parameters unexpected by the external program. 260 // have parameters unexpected by the external program.
263 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec()); 261 std::string escaped_url_string = net::EscapeExternalHandlerValue(url.spec());
264 GURL escaped_url(escaped_url_string); 262 GURL escaped_url(escaped_url_string);
265 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(), 263 BlockState block_state = GetBlockStateWithDelegate(escaped_url.scheme(),
266 delegate); 264 delegate,
265 user_gesture);
267 if (block_state == BLOCK) { 266 if (block_state == BLOCK) {
268 if (delegate) 267 if (delegate)
269 delegate->BlockRequest(); 268 delegate->BlockRequest();
270 return; 269 return;
271 } 270 }
272 271
273 g_accept_requests = false;
274
275 // The worker creates tasks with references to itself and puts them into 272 // 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 273 // message loops. When no tasks are left it will delete the observer and
277 // eventually be deleted itself. 274 // eventually be deleted itself.
278 ShellIntegration::DefaultWebClientObserver* observer = 275 ShellIntegration::DefaultWebClientObserver* observer =
279 new ExternalDefaultProtocolObserver(url, 276 new ExternalDefaultProtocolObserver(url,
280 render_process_host_id, 277 render_process_host_id,
281 tab_contents_id, 278 tab_contents_id,
282 block_state == UNKNOWN, 279 block_state == UNKNOWN,
283 delegate); 280 delegate);
284 scoped_refptr<ShellIntegration::DefaultProtocolClientWorker> worker = 281 scoped_refptr<ShellIntegration::DefaultProtocolClientWorker> worker =
(...skipping 16 matching lines...) Expand all
301 return; 298 return;
302 299
303 platform_util::OpenExternal( 300 platform_util::OpenExternal(
304 Profile::FromBrowserContext(web_contents->GetBrowserContext()), url); 301 Profile::FromBrowserContext(web_contents->GetBrowserContext()), url);
305 } 302 }
306 303
307 // static 304 // static
308 void ExternalProtocolHandler::RegisterPrefs(PrefRegistrySimple* registry) { 305 void ExternalProtocolHandler::RegisterPrefs(PrefRegistrySimple* registry) {
309 registry->RegisterDictionaryPref(prefs::kExcludedSchemes); 306 registry->RegisterDictionaryPref(prefs::kExcludedSchemes);
310 } 307 }
311
312 // static
313 void ExternalProtocolHandler::PermitLaunchUrl() {
314 DCHECK(base::MessageLoopForUI::IsCurrent());
315 g_accept_requests = true;
316 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698