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/shell_integration.h" | 5 #include "chrome/browser/shell_integration.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "base/timer/timer.h" |
14 #include "chrome/browser/policy/policy_path_parser.h" | 15 #include "chrome/browser/policy/policy_path_parser.h" |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
17 #include "components/version_info/version_info.h" | 18 #include "components/version_info/version_info.h" |
18 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
19 | 20 |
20 #if defined(OS_CHROMEOS) | 21 #if defined(OS_CHROMEOS) |
21 #include "chromeos/chromeos_switches.h" | 22 #include "chromeos/chromeos_switches.h" |
22 #endif | 23 #endif |
23 | 24 |
24 #if !defined(OS_WIN) | 25 #if !defined(OS_WIN) |
25 #include "chrome/common/channel_info.h" | 26 #include "chrome/common/channel_info.h" |
26 #include "chrome/grit/chromium_strings.h" | 27 #include "chrome/grit/chromium_strings.h" |
27 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
28 #endif | 29 #endif |
29 | 30 |
30 using content::BrowserThread; | 31 using content::BrowserThread; |
31 | 32 |
32 // static | 33 // static |
33 ShellIntegration::DefaultWebClientSetPermission | 34 ShellIntegration::DefaultWebClientSetPermission |
34 ShellIntegration::CanSetAsDefaultProtocolClient() { | 35 ShellIntegration::CanSetAsDefaultProtocolClient() { |
35 // Allowed as long as the browser can become the operating system default | 36 // Allowed as long as the browser can become the operating system default |
36 // browser. | 37 // browser. |
37 return CanSetAsDefaultBrowser(); | 38 DefaultWebClientSetPermission permission = CanSetAsDefaultBrowser(); |
| 39 |
| 40 // Set as default asynchronous is only supported for default web browser. |
| 41 return (permission == SET_DEFAULT_ASYNCHRONOUS) ? SET_DEFAULT_INTERACTIVE |
| 42 : permission; |
38 } | 43 } |
39 | 44 |
40 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL; | 45 static const struct ShellIntegration::AppModeInfo* gAppModeInfo = NULL; |
41 | 46 |
42 // static | 47 // static |
43 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) { | 48 void ShellIntegration::SetAppModeInfo(const struct AppModeInfo* info) { |
44 gAppModeInfo = info; | 49 gAppModeInfo = info; |
45 } | 50 } |
46 | 51 |
47 // static | 52 // static |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 if (chrome::GetChannel() == version_info::Channel::CANARY) | 122 if (chrome::GetChannel() == version_info::Channel::CANARY) |
118 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY); | 123 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME_CANARY); |
119 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME); | 124 return l10n_util::GetStringUTF16(IDS_APP_SHORTCUTS_SUBDIR_NAME); |
120 } | 125 } |
121 | 126 |
122 // static | 127 // static |
123 bool ShellIntegration::SetAsDefaultBrowserInteractive() { | 128 bool ShellIntegration::SetAsDefaultBrowserInteractive() { |
124 return false; | 129 return false; |
125 } | 130 } |
126 | 131 |
| 132 #if !defined(OS_WIN) |
| 133 // static |
| 134 bool ShellIntegration::IsSetAsDefaultAsynchronous() { |
| 135 return false; |
| 136 } |
| 137 |
127 // static | 138 // static |
| 139 void ShellIntegration::SetAsDefaultBrowserAsynchronous() { |
| 140 NOTREACHED(); |
| 141 } |
| 142 #endif // !defined(OS_WIN) |
| 143 |
| 144 // static |
128 bool ShellIntegration::SetAsDefaultProtocolClientInteractive( | 145 bool ShellIntegration::SetAsDefaultProtocolClientInteractive( |
129 const std::string& protocol) { | 146 const std::string& protocol) { |
130 return false; | 147 return false; |
131 } | 148 } |
132 | 149 |
133 // static | 150 // static |
134 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() { | 151 bool ShellIntegration::IsElevationNeededForSettingDefaultProtocolClient() { |
135 return false; | 152 return false; |
136 } | 153 } |
137 | 154 |
138 #endif // !defined(OS_WIN) | 155 #endif // !defined(OS_WIN) |
139 | 156 |
140 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() { | 157 bool ShellIntegration::DefaultWebClientObserver::IsOwnedByWorker() { |
141 return false; | 158 return false; |
142 } | 159 } |
143 | 160 |
144 bool ShellIntegration::DefaultWebClientObserver:: | 161 bool ShellIntegration::DefaultWebClientObserver:: |
145 IsInteractiveSetDefaultPermitted() { | 162 IsInteractiveSetDefaultPermitted() { |
146 return false; | 163 return false; |
147 } | 164 } |
148 | 165 |
149 /////////////////////////////////////////////////////////////////////////////// | 166 /////////////////////////////////////////////////////////////////////////////// |
150 // ShellIntegration::DefaultWebClientWorker | 167 // ShellIntegration::DefaultWebClientWorker |
151 // | 168 // |
152 | 169 |
153 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker( | 170 ShellIntegration::DefaultWebClientWorker::DefaultWebClientWorker( |
154 DefaultWebClientObserver* observer) | 171 DefaultWebClientObserver* observer) |
155 : observer_(observer) { | 172 : observer_(observer) {} |
156 } | |
157 | 173 |
158 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() { | 174 void ShellIntegration::DefaultWebClientWorker::StartCheckIsDefault() { |
159 if (observer_) { | 175 if (observer_) { |
160 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); | 176 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); |
161 BrowserThread::PostTask( | 177 BrowserThread::PostTask( |
162 BrowserThread::FILE, FROM_HERE, | 178 BrowserThread::FILE, FROM_HERE, |
163 base::Bind( | 179 base::Bind(&DefaultWebClientWorker::CheckIsDefault, this)); |
164 &DefaultWebClientWorker::ExecuteCheckIsDefault, this)); | |
165 } | 180 } |
166 } | 181 } |
167 | 182 |
168 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() { | 183 void ShellIntegration::DefaultWebClientWorker::StartSetAsDefault() { |
| 184 // Cancel the already running process if another start is requested. |
| 185 if (set_as_default_in_progress_) |
| 186 OnSetAsDefaultAttemptComplete(false); |
| 187 |
| 188 set_as_default_in_progress_ = true; |
169 bool interactive_permitted = false; | 189 bool interactive_permitted = false; |
170 if (observer_) { | 190 if (observer_) { |
171 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); | 191 observer_->SetDefaultWebClientUIState(STATE_PROCESSING); |
172 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted(); | 192 interactive_permitted = observer_->IsInteractiveSetDefaultPermitted(); |
| 193 |
| 194 InitializeSetAsDefault(); |
173 } | 195 } |
174 BrowserThread::PostTask( | 196 |
175 BrowserThread::FILE, FROM_HERE, | 197 if (set_as_default_in_progress_) |
176 base::Bind(&DefaultWebClientWorker::ExecuteSetAsDefault, this, | 198 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
177 interactive_permitted)); | 199 base::Bind(&DefaultWebClientWorker::SetAsDefault, |
| 200 this, interactive_permitted)); |
178 } | 201 } |
179 | 202 |
180 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() { | 203 void ShellIntegration::DefaultWebClientWorker::ObserverDestroyed() { |
181 // Our associated view has gone away, so we shouldn't call back to it if | 204 // Our associated view has gone away, so we shouldn't call back to it if |
182 // our worker thread returns after the view is dead. | 205 // our worker thread returns after the view is dead. |
183 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 206 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
184 observer_ = NULL; | 207 observer_ = NULL; |
| 208 // If an attempt to set the default browser is already in progress, its result |
| 209 // won't be posted to any observers. Manually invoke |
| 210 // OnSetAsDefaultAttemptComplete() to ensure we free the default browser |
| 211 // callback and timer. |
| 212 if (set_as_default_in_progress_) |
| 213 OnSetAsDefaultAttemptComplete(false); |
185 } | 214 } |
186 | 215 |
187 /////////////////////////////////////////////////////////////////////////////// | 216 /////////////////////////////////////////////////////////////////////////////// |
188 // DefaultWebClientWorker, private: | 217 // DefaultWebClientWorker, private: |
189 | 218 |
190 void ShellIntegration::DefaultWebClientWorker::ExecuteCheckIsDefault() { | 219 ShellIntegration::DefaultWebClientWorker::~DefaultWebClientWorker() {} |
191 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | |
192 DefaultWebClientState state = CheckIsDefault(); | |
193 BrowserThread::PostTask( | |
194 BrowserThread::UI, FROM_HERE, | |
195 base::Bind( | |
196 &DefaultWebClientWorker::CompleteCheckIsDefault, this, state)); | |
197 } | |
198 | 220 |
199 void ShellIntegration::DefaultWebClientWorker::CompleteCheckIsDefault( | 221 void ShellIntegration::DefaultWebClientWorker::OnCheckIsDefaultComplete( |
200 DefaultWebClientState state) { | 222 DefaultWebClientState state) { |
201 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 223 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
202 UpdateUI(state); | 224 UpdateUI(state); |
203 // The worker has finished everything it needs to do, so free the observer | 225 // The worker has finished everything it needs to do, so free the observer |
204 // if we own it. | 226 // if we own it. |
205 if (observer_ && observer_->IsOwnedByWorker()) { | 227 if (observer_ && observer_->IsOwnedByWorker()) { |
206 delete observer_; | 228 delete observer_; |
207 observer_ = NULL; | 229 observer_ = NULL; |
208 } | 230 } |
209 } | 231 } |
210 | 232 |
211 void ShellIntegration::DefaultWebClientWorker::ExecuteSetAsDefault( | 233 void ShellIntegration::DefaultWebClientWorker::OnSetAsDefaultAttemptComplete( |
212 bool interactive_permitted) { | 234 bool succeeded) { |
213 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 235 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 236 // Hold on to a reference because if this was called via the default browser |
| 237 // callback in StartupBrowserCreator, clearing the callback in |
| 238 // FinalizeSetAsDefault would otherwise remove the last reference and delete |
| 239 // us in the middle of this function. |
| 240 scoped_refptr<DefaultWebClientWorker> scoped_ref(this); |
214 | 241 |
215 bool result = SetAsDefault(interactive_permitted); | 242 if (set_as_default_in_progress_) { |
216 BrowserThread::PostTask( | 243 set_as_default_in_progress_ = false; |
217 BrowserThread::UI, FROM_HERE, | 244 |
218 base::Bind(&DefaultWebClientWorker::CompleteSetAsDefault, this, result)); | 245 FinalizeSetAsDefault(succeeded); |
| 246 if (observer_) |
| 247 observer_->OnSetAsDefaultConcluded(succeeded); |
| 248 |
| 249 // Start the default browser check which will notify the observer as to |
| 250 // whether Chrome is really the default browser. This is needed because |
| 251 // detecting that the process was successful is not 100% sure. |
| 252 // For example, on Windows 10+, the user might have unchecked the "Always |
| 253 // use this app" checkbox which can't be detected. |
| 254 StartCheckIsDefault(); |
| 255 } |
219 } | 256 } |
220 | 257 |
221 void ShellIntegration::DefaultWebClientWorker::CompleteSetAsDefault( | 258 void ShellIntegration::DefaultWebClientWorker::InitializeSetAsDefault() {} |
222 bool succeeded) { | 259 |
223 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 260 void ShellIntegration::DefaultWebClientWorker::FinalizeSetAsDefault( |
224 // First tell the observer what the SetAsDefault call has returned. | 261 bool succeeded) {} |
225 if (observer_) | |
226 observer_->OnSetAsDefaultConcluded(succeeded); | |
227 // Set as default completed, check again to make sure it stuck... | |
228 StartCheckIsDefault(); | |
229 } | |
230 | 262 |
231 void ShellIntegration::DefaultWebClientWorker::UpdateUI( | 263 void ShellIntegration::DefaultWebClientWorker::UpdateUI( |
232 DefaultWebClientState state) { | 264 DefaultWebClientState state) { |
233 if (observer_) { | 265 if (observer_) { |
234 switch (state) { | 266 switch (state) { |
235 case NOT_DEFAULT: | 267 case NOT_DEFAULT: |
236 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT); | 268 observer_->SetDefaultWebClientUIState(STATE_NOT_DEFAULT); |
237 break; | 269 break; |
238 case IS_DEFAULT: | 270 case IS_DEFAULT: |
239 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT); | 271 observer_->SetDefaultWebClientUIState(STATE_IS_DEFAULT); |
240 break; | 272 break; |
241 case UNKNOWN_DEFAULT: | 273 case UNKNOWN_DEFAULT: |
242 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN); | 274 observer_->SetDefaultWebClientUIState(STATE_UNKNOWN); |
243 break; | 275 break; |
244 default: | 276 default: |
245 break; | 277 break; |
246 } | 278 } |
247 } | 279 } |
248 } | 280 } |
249 | 281 |
250 /////////////////////////////////////////////////////////////////////////////// | 282 /////////////////////////////////////////////////////////////////////////////// |
251 // ShellIntegration::DefaultBrowserWorker | 283 // ShellIntegration::DefaultBrowserWorker |
252 // | 284 // |
253 | 285 |
254 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker( | 286 ShellIntegration::DefaultBrowserWorker::DefaultBrowserWorker( |
255 DefaultWebClientObserver* observer) | 287 DefaultWebClientObserver* observer) |
256 : DefaultWebClientWorker(observer) { | 288 : DefaultWebClientWorker(observer) { |
257 } | 289 } |
258 | 290 |
| 291 ShellIntegration::DefaultBrowserWorker::~DefaultBrowserWorker() { |
| 292 if (set_as_default_in_progress_) |
| 293 OnSetAsDefaultAttemptComplete(false); |
| 294 } |
| 295 |
259 /////////////////////////////////////////////////////////////////////////////// | 296 /////////////////////////////////////////////////////////////////////////////// |
260 // DefaultBrowserWorker, private: | 297 // DefaultBrowserWorker, private: |
261 | 298 |
262 ShellIntegration::DefaultWebClientState | 299 void ShellIntegration::DefaultBrowserWorker::CheckIsDefault() { |
263 ShellIntegration::DefaultBrowserWorker::CheckIsDefault() { | 300 DefaultWebClientState state = ShellIntegration::GetDefaultBrowser(); |
264 return ShellIntegration::GetDefaultBrowser(); | 301 BrowserThread::PostTask( |
| 302 BrowserThread::UI, FROM_HERE, |
| 303 base::Bind(&DefaultBrowserWorker::OnCheckIsDefaultComplete, this, state)); |
265 } | 304 } |
266 | 305 |
267 bool ShellIntegration::DefaultBrowserWorker::SetAsDefault( | 306 void ShellIntegration::DefaultBrowserWorker::SetAsDefault( |
268 bool interactive_permitted) { | 307 bool interactive_permitted) { |
269 bool result = false; | 308 bool result = false; |
270 switch (ShellIntegration::CanSetAsDefaultBrowser()) { | 309 switch (ShellIntegration::CanSetAsDefaultBrowser()) { |
| 310 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED: |
| 311 NOTREACHED(); |
| 312 break; |
271 case ShellIntegration::SET_DEFAULT_UNATTENDED: | 313 case ShellIntegration::SET_DEFAULT_UNATTENDED: |
272 result = ShellIntegration::SetAsDefaultBrowser(); | 314 result = ShellIntegration::SetAsDefaultBrowser(); |
273 break; | 315 break; |
274 case ShellIntegration::SET_DEFAULT_INTERACTIVE: | 316 case ShellIntegration::SET_DEFAULT_INTERACTIVE: |
275 if (interactive_permitted) | 317 if (interactive_permitted) |
276 result = ShellIntegration::SetAsDefaultBrowserInteractive(); | 318 result = ShellIntegration::SetAsDefaultBrowserInteractive(); |
277 break; | 319 break; |
278 default: | 320 case ShellIntegration::SET_DEFAULT_ASYNCHRONOUS: |
279 NOTREACHED(); | 321 if (!interactive_permitted) |
| 322 break; |
| 323 // This function will cause OnSetAsDefaultAttemptComplete() to be called |
| 324 // asynchronously via a filter established in InitializeSetAsDefault(). |
| 325 ShellIntegration::SetAsDefaultBrowserAsynchronous(); |
| 326 return; |
280 } | 327 } |
281 | 328 BrowserThread::PostTask( |
282 return result; | 329 BrowserThread::UI, FROM_HERE, |
| 330 base::Bind(&DefaultBrowserWorker::OnSetAsDefaultAttemptComplete, this, |
| 331 result)); |
283 } | 332 } |
284 | 333 |
285 /////////////////////////////////////////////////////////////////////////////// | 334 /////////////////////////////////////////////////////////////////////////////// |
286 // ShellIntegration::DefaultProtocolClientWorker | 335 // ShellIntegration::DefaultProtocolClientWorker |
287 // | 336 // |
288 | 337 |
289 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker( | 338 ShellIntegration::DefaultProtocolClientWorker::DefaultProtocolClientWorker( |
290 DefaultWebClientObserver* observer, const std::string& protocol) | 339 DefaultWebClientObserver* observer, const std::string& protocol) |
291 : DefaultWebClientWorker(observer), | 340 : DefaultWebClientWorker(observer), |
292 protocol_(protocol) { | 341 protocol_(protocol) { |
293 } | 342 } |
294 | 343 |
295 /////////////////////////////////////////////////////////////////////////////// | 344 /////////////////////////////////////////////////////////////////////////////// |
296 // DefaultProtocolClientWorker, private: | 345 // DefaultProtocolClientWorker, private: |
297 | 346 |
298 ShellIntegration::DefaultWebClientState | 347 ShellIntegration::DefaultProtocolClientWorker::~DefaultProtocolClientWorker() {} |
299 ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() { | 348 |
300 return ShellIntegration::IsDefaultProtocolClient(protocol_); | 349 void ShellIntegration::DefaultProtocolClientWorker::CheckIsDefault() { |
| 350 DefaultWebClientState state = |
| 351 ShellIntegration::IsDefaultProtocolClient(protocol_); |
| 352 BrowserThread::PostTask( |
| 353 BrowserThread::UI, FROM_HERE, |
| 354 base::Bind(&DefaultProtocolClientWorker::OnCheckIsDefaultComplete, this, |
| 355 state)); |
301 } | 356 } |
302 | 357 |
303 bool ShellIntegration::DefaultProtocolClientWorker::SetAsDefault( | 358 void ShellIntegration::DefaultProtocolClientWorker::SetAsDefault( |
304 bool interactive_permitted) { | 359 bool interactive_permitted) { |
305 bool result = false; | 360 bool result = false; |
306 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) { | 361 switch (ShellIntegration::CanSetAsDefaultProtocolClient()) { |
307 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED: | 362 case ShellIntegration::SET_DEFAULT_NOT_ALLOWED: |
308 result = false; | 363 // Not allowed, do nothing. |
309 break; | 364 break; |
310 case ShellIntegration::SET_DEFAULT_UNATTENDED: | 365 case ShellIntegration::SET_DEFAULT_UNATTENDED: |
311 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_); | 366 result = ShellIntegration::SetAsDefaultProtocolClient(protocol_); |
312 break; | 367 break; |
313 case ShellIntegration::SET_DEFAULT_INTERACTIVE: | 368 case ShellIntegration::SET_DEFAULT_INTERACTIVE: |
314 if (interactive_permitted) { | 369 if (interactive_permitted) { |
315 result = ShellIntegration::SetAsDefaultProtocolClientInteractive( | 370 result = |
316 protocol_); | 371 ShellIntegration::SetAsDefaultProtocolClientInteractive(protocol_); |
317 } | 372 } |
318 break; | 373 break; |
| 374 case ShellIntegration::SET_DEFAULT_ASYNCHRONOUS: |
| 375 NOTREACHED(); |
| 376 break; |
319 } | 377 } |
320 | 378 BrowserThread::PostTask( |
321 return result; | 379 BrowserThread::UI, FROM_HERE, |
| 380 base::Bind(&DefaultProtocolClientWorker::OnSetAsDefaultAttemptComplete, |
| 381 this, result)); |
322 } | 382 } |
OLD | NEW |