OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
14 #include "chrome/browser/browser_process_impl.h" | 14 #include "chrome/browser/browser_process_impl.h" |
15 #include "chrome/browser/platform_util.h" | 15 #include "chrome/browser/platform_util.h" |
16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
17 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 17 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
18 #include "chrome/browser/shell_integration.h" | |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
19 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
20 #include "net/base/escape.h" | 21 #include "net/base/escape.h" |
21 | 22 |
22 // Whether we accept requests for launching external protocols. This is set to | 23 // Whether we accept requests for launching external protocols. This is set to |
23 // false every time an external protocol is requested, and set back to true on | 24 // false every time an external protocol is requested, and set back to true on |
24 // each user gesture. This variable should only be accessed from the UI thread. | 25 // each user gesture. This variable should only be accessed from the UI thread. |
25 static bool g_accept_requests = true; | 26 static bool g_accept_requests = true; |
26 | 27 |
27 // static | 28 // static |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 | 119 |
119 if (state == UNKNOWN) { | 120 if (state == UNKNOWN) { |
120 update_excluded_schemas->Remove(scheme, NULL); | 121 update_excluded_schemas->Remove(scheme, NULL); |
121 } else { | 122 } else { |
122 update_excluded_schemas->SetBoolean(scheme, | 123 update_excluded_schemas->SetBoolean(scheme, |
123 state == BLOCK ? true : false); | 124 state == BLOCK ? true : false); |
124 } | 125 } |
125 } | 126 } |
126 } | 127 } |
127 | 128 |
129 namespace { | |
tony
2011/08/30 17:51:26
Nit: I would move this to the top of the file (abo
| |
130 | |
131 // When we are about to launch a URL with the default OS level application, | |
132 // we check if that external application will be us. If it is we just ignore | |
133 // the request. | |
134 class ExternalDefaultProtocolObserver | |
135 : public ShellIntegration::DefaultWebClientObserver { | |
136 public: | |
137 ExternalDefaultProtocolObserver(const GURL& escaped_url, | |
138 int render_process_host_id, | |
139 int tab_contents_id, | |
140 bool prompt_user) | |
141 : escaped_url_(escaped_url), | |
142 render_process_host_id_(render_process_host_id), | |
143 tab_contents_id_(tab_contents_id), | |
144 prompt_user_(prompt_user) {} | |
145 | |
146 virtual void SetDefaultWebClientUIState( | |
147 ShellIntegration::DefaultWebClientUIState state) OVERRIDE { | |
148 | |
tony
2011/08/30 17:51:26
Can we DCHECK that we're on the right thread here?
| |
149 // If we are still working out if we're the default, or we've found | |
150 // out we definately are the default, we end here. | |
151 if (state == ShellIntegration::STATE_PROCESSING || | |
152 state == ShellIntegration::STATE_IS_DEFAULT) { | |
153 return; | |
154 } | |
155 | |
156 // If we get here, either we are not the default or we cannot work out | |
157 // what the default is, so we proceed. | |
158 if (prompt_user_) { | |
159 // Ask the user if they want to allow the protocol. This will call | |
160 // LaunchUrlWithoutSecurityCheck if the user decides to accept the | |
161 // protocol. | |
162 ExternalProtocolHandler::RunExternalProtocolDialog( | |
163 escaped_url_, render_process_host_id_, tab_contents_id_); | |
164 return; | |
165 } | |
166 | |
167 ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(escaped_url_); | |
168 } | |
169 | |
170 virtual bool IsOwnedByWorker() OVERRIDE { return true; } | |
171 | |
172 private: | |
173 GURL escaped_url_; | |
174 int render_process_host_id_; | |
175 int tab_contents_id_; | |
176 bool prompt_user_; | |
177 }; | |
178 | |
179 } // namespace | |
180 | |
128 // static | 181 // static |
129 void ExternalProtocolHandler::LaunchUrl(const GURL& url, | 182 void ExternalProtocolHandler::LaunchUrl(const GURL& url, |
130 int render_process_host_id, | 183 int render_process_host_id, |
131 int tab_contents_id) { | 184 int tab_contents_id) { |
132 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); | 185 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); |
133 | 186 |
134 // Escape the input scheme to be sure that the command does not | 187 // Escape the input scheme to be sure that the command does not |
135 // have parameters unexpected by the external program. | 188 // have parameters unexpected by the external program. |
136 std::string escaped_url_string = EscapeExternalHandlerValue(url.spec()); | 189 std::string escaped_url_string = EscapeExternalHandlerValue(url.spec()); |
137 GURL escaped_url(escaped_url_string); | 190 GURL escaped_url(escaped_url_string); |
138 BlockState block_state = GetBlockState(escaped_url.scheme()); | 191 BlockState block_state = GetBlockState(escaped_url.scheme()); |
139 if (block_state == BLOCK) | 192 if (block_state == BLOCK) |
140 return; | 193 return; |
141 | 194 |
142 g_accept_requests = false; | 195 g_accept_requests = false; |
143 | 196 |
144 if (block_state == UNKNOWN) { | 197 // The worker creates tasks with references to itself and puts them into |
145 // Ask the user if they want to allow the protocol. This will call | 198 // message loops. When no tasks are left it will delete the observer and |
146 // LaunchUrlWithoutSecurityCheck if the user decides to accept the protocol. | 199 // eventually be deleted itself. |
147 RunExternalProtocolDialog(escaped_url, | 200 ExternalDefaultProtocolObserver* observer; |
148 render_process_host_id, | 201 observer = new ExternalDefaultProtocolObserver(url, render_process_host_id, |
tony
2011/08/30 17:51:26
Nit: Maybe merge this with the previous line.
| |
149 tab_contents_id); | 202 tab_contents_id, block_state == UNKNOWN); |
150 return; | 203 scoped_refptr<ShellIntegration::DefaultProtocolClientWorker> worker; |
151 } | 204 worker = new ShellIntegration::DefaultProtocolClientWorker(observer, |
tony
2011/08/30 17:51:26
Nit: Ditto
| |
152 | 205 escaped_url.scheme()); |
153 LaunchUrlWithoutSecurityCheck(escaped_url); | 206 // Start the check process running. This will send tasks to the FILE thread |
207 // and when the answer is known will send the result back to the observer on | |
208 // the UI thread. | |
209 worker->StartCheckIsDefault(); | |
154 } | 210 } |
155 | 211 |
156 // static | 212 // static |
157 void ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(const GURL& url) { | 213 void ExternalProtocolHandler::LaunchUrlWithoutSecurityCheck(const GURL& url) { |
158 #if defined(OS_MACOSX) | 214 #if defined(OS_MACOSX) |
159 // This must run on the UI thread on OS X. | 215 // This must run on the UI thread on OS X. |
160 platform_util::OpenExternal(url); | 216 platform_util::OpenExternal(url); |
161 #else | 217 #else |
162 // Otherwise put this work on the file thread. On Windows ShellExecute may | 218 // Otherwise put this work on the file thread. On Windows ShellExecute may |
163 // block for a significant amount of time, and it shouldn't hurt on Linux. | 219 // block for a significant amount of time, and it shouldn't hurt on Linux. |
(...skipping 10 matching lines...) Expand all Loading... | |
174 // static | 230 // static |
175 void ExternalProtocolHandler::RegisterPrefs(PrefService* prefs) { | 231 void ExternalProtocolHandler::RegisterPrefs(PrefService* prefs) { |
176 prefs->RegisterDictionaryPref(prefs::kExcludedSchemes); | 232 prefs->RegisterDictionaryPref(prefs::kExcludedSchemes); |
177 } | 233 } |
178 | 234 |
179 // static | 235 // static |
180 void ExternalProtocolHandler::PermitLaunchUrl() { | 236 void ExternalProtocolHandler::PermitLaunchUrl() { |
181 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); | 237 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type()); |
182 g_accept_requests = true; | 238 g_accept_requests = true; |
183 } | 239 } |
OLD | NEW |