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.
5 #include "chrome/browser/external_protocol/external_protocol_handler.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/pref_registry_simple.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/strings/string_util.h"
16 #include "base/threading/thread.h"
17 #include "build/build_config.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/platform_util.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/tab_contents/tab_util.h"
22 #include "chrome/common/pref_names.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/web_contents.h"
25 #include "net/base/escape.h"
28 using content::BrowserThread
;
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;
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.
39 ShellIntegration::DefaultProtocolClientWorker
* CreateShellWorker(
40 ShellIntegration::DefaultWebClientObserver
* observer
,
41 const std::string
& protocol
,
42 ExternalProtocolHandler::Delegate
* delegate
) {
44 return new ShellIntegration::DefaultProtocolClientWorker(observer
,
47 return delegate
->CreateShellWorker(observer
, protocol
);
50 ExternalProtocolHandler::BlockState
GetBlockStateWithDelegate(
51 const std::string
& scheme
,
52 ExternalProtocolHandler::Delegate
* delegate
) {
54 return ExternalProtocolHandler::GetBlockState(scheme
);
56 return delegate
->GetBlockState(scheme
);
59 void RunExternalProtocolDialogWithDelegate(
61 int render_process_host_id
,
63 ui::PageTransition page_transition
,
64 bool has_user_gesture
,
65 ExternalProtocolHandler::Delegate
* delegate
) {
67 ExternalProtocolHandler::RunExternalProtocolDialog(
68 url
, render_process_host_id
, routing_id
, page_transition
,
71 delegate
->RunExternalProtocolDialog(
72 url
, render_process_host_id
, routing_id
, page_transition
,
77 void LaunchUrlWithoutSecurityCheckWithDelegate(
79 int render_process_host_id
,
81 ExternalProtocolHandler::Delegate
* delegate
) {
83 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(
84 url
, render_process_host_id
, tab_contents_id
);
86 delegate
->LaunchUrlWithoutSecurityCheck(url
);
90 // When we are about to launch a URL with the default OS level application,
91 // we check if that external application will be us. If it is we just ignore
93 class ExternalDefaultProtocolObserver
94 : public ShellIntegration::DefaultWebClientObserver
{
96 ExternalDefaultProtocolObserver(const GURL
& escaped_url
,
97 int render_process_host_id
,
100 ui::PageTransition page_transition
,
101 bool has_user_gesture
,
102 ExternalProtocolHandler::Delegate
* delegate
)
103 : delegate_(delegate
),
104 escaped_url_(escaped_url
),
105 render_process_host_id_(render_process_host_id
),
106 tab_contents_id_(tab_contents_id
),
107 prompt_user_(prompt_user
),
108 page_transition_(page_transition
),
109 has_user_gesture_(has_user_gesture
) {}
111 void SetDefaultWebClientUIState(
112 ShellIntegration::DefaultWebClientUIState state
) override
{
113 DCHECK(base::MessageLoopForUI::IsCurrent());
115 // If we are still working out if we're the default, or we've found
116 // out we definately are the default, we end here.
117 if (state
== ShellIntegration::STATE_PROCESSING
) {
122 delegate_
->FinishedProcessingCheck();
124 if (state
== ShellIntegration::STATE_IS_DEFAULT
) {
126 delegate_
->BlockRequest();
130 // If we get here, either we are not the default or we cannot work out
131 // what the default is, so we proceed.
133 // Ask the user if they want to allow the protocol. This will call
134 // LaunchUrlWithoutSecurityCheck if the user decides to accept the
136 RunExternalProtocolDialogWithDelegate(
137 escaped_url_
, render_process_host_id_
, tab_contents_id_
,
138 page_transition_
, has_user_gesture_
, delegate_
);
142 LaunchUrlWithoutSecurityCheckWithDelegate(
143 escaped_url_
, render_process_host_id_
, tab_contents_id_
, delegate_
);
146 bool IsOwnedByWorker() override
{ return true; }
149 ExternalProtocolHandler::Delegate
* delegate_
;
150 const GURL escaped_url_
;
151 const int render_process_host_id_
;
152 const int tab_contents_id_
;
153 const bool prompt_user_
;
154 const ui::PageTransition page_transition_
;
155 const bool has_user_gesture_
;
161 void ExternalProtocolHandler::PrepopulateDictionary(
162 base::DictionaryValue
* win_pref
) {
163 static bool is_warm
= false;
168 static const char* const denied_schemes
[] = {
173 // ShellExecuting file:///C:/WINDOWS/system32/notepad.exe will simply
174 // execute the file specified! Hopefully we won't see any "file" schemes
175 // because we think of file:// URLs as handled URLs, but better to be safe
176 // than to let an attacker format the user's hard drive.
184 // view-source is a special case in chrome. When it comes through an
185 // iframe or a redirect, it looks like an external protocol, but we don't
186 // want to shellexecute it.
191 static const char* const allowed_schemes
[] = {
201 for (size_t i
= 0; i
< arraysize(denied_schemes
); ++i
) {
202 if (!win_pref
->GetBoolean(denied_schemes
[i
], &should_block
)) {
203 win_pref
->SetBoolean(denied_schemes
[i
], true);
207 for (size_t i
= 0; i
< arraysize(allowed_schemes
); ++i
) {
208 if (!win_pref
->GetBoolean(allowed_schemes
[i
], &should_block
)) {
209 win_pref
->SetBoolean(allowed_schemes
[i
], false);
215 ExternalProtocolHandler::BlockState
ExternalProtocolHandler::GetBlockState(
216 const std::string
& scheme
) {
217 // If we are being carpet bombed, block the request.
218 if (!g_accept_requests
)
221 if (scheme
.length() == 1) {
222 // We have a URL that looks something like:
223 // C:/WINDOWS/system32/notepad.exe
224 // ShellExecuting this URL will cause the specified program to be executed.
228 // Check the stored prefs.
229 // TODO(pkasting): This kind of thing should go in the preferences on the
230 // profile, not in the local state. http://crbug.com/457254
231 PrefService
* pref
= g_browser_process
->local_state();
232 if (pref
) { // May be NULL during testing.
233 DictionaryPrefUpdate
update_excluded_schemas(pref
, prefs::kExcludedSchemes
);
235 // Warm up the dictionary if needed.
236 PrepopulateDictionary(update_excluded_schemas
.Get());
239 if (update_excluded_schemas
->GetBoolean(scheme
, &should_block
))
240 return should_block
? BLOCK
: DONT_BLOCK
;
247 void ExternalProtocolHandler::SetBlockState(const std::string
& scheme
,
249 // Set in the stored prefs.
250 // TODO(pkasting): This kind of thing should go in the preferences on the
251 // profile, not in the local state. http://crbug.com/457254
252 PrefService
* pref
= g_browser_process
->local_state();
253 if (pref
) { // May be NULL during testing.
254 DictionaryPrefUpdate
update_excluded_schemas(pref
, prefs::kExcludedSchemes
);
256 if (state
== UNKNOWN
) {
257 update_excluded_schemas
->Remove(scheme
, NULL
);
259 update_excluded_schemas
->SetBoolean(scheme
, (state
== BLOCK
));
265 void ExternalProtocolHandler::LaunchUrlWithDelegate(
267 int render_process_host_id
,
269 ui::PageTransition page_transition
,
270 bool has_user_gesture
,
271 Delegate
* delegate
) {
272 DCHECK(base::MessageLoopForUI::IsCurrent());
274 // Escape the input scheme to be sure that the command does not
275 // have parameters unexpected by the external program.
276 std::string escaped_url_string
= net::EscapeExternalHandlerValue(url
.spec());
277 GURL
escaped_url(escaped_url_string
);
278 BlockState block_state
=
279 GetBlockStateWithDelegate(escaped_url
.scheme(), delegate
);
280 if (block_state
== BLOCK
) {
282 delegate
->BlockRequest();
286 g_accept_requests
= false;
288 // The worker creates tasks with references to itself and puts them into
289 // message loops. When no tasks are left it will delete the observer and
290 // eventually be deleted itself.
291 ShellIntegration::DefaultWebClientObserver
* observer
=
292 new ExternalDefaultProtocolObserver(url
,
293 render_process_host_id
,
295 block_state
== UNKNOWN
,
299 scoped_refptr
<ShellIntegration::DefaultProtocolClientWorker
> worker
=
300 CreateShellWorker(observer
, escaped_url
.scheme(), delegate
);
302 // Start the check process running. This will send tasks to the FILE thread
303 // and when the answer is known will send the result back to the observer on
305 worker
->StartCheckIsDefault();
309 void ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(
311 int render_process_host_id
,
312 int tab_contents_id
) {
313 content::WebContents
* web_contents
= tab_util::GetWebContentsByID(
314 render_process_host_id
, tab_contents_id
);
318 platform_util::OpenExternal(
319 Profile::FromBrowserContext(web_contents
->GetBrowserContext()), url
);
323 void ExternalProtocolHandler::RegisterPrefs(PrefRegistrySimple
* registry
) {
324 registry
->RegisterDictionaryPref(prefs::kExcludedSchemes
);
328 void ExternalProtocolHandler::PermitLaunchUrl() {
329 DCHECK(base::MessageLoopForUI::IsCurrent());
330 g_accept_requests
= true;