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 #ifndef CHROME_BROWSER_SHELL_INTEGRATION_H_ | 5 #ifndef CHROME_BROWSER_SHELL_INTEGRATION_H_ |
6 #define CHROME_BROWSER_SHELL_INTEGRATION_H_ | 6 #define CHROME_BROWSER_SHELL_INTEGRATION_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/time/time.h" |
14 #include "ui/gfx/image/image_family.h" | 15 #include "ui/gfx/image/image_family.h" |
15 #include "url/gurl.h" | 16 #include "url/gurl.h" |
16 | 17 |
17 namespace base { | 18 namespace base { |
18 class CommandLine; | 19 class CommandLine; |
| 20 class OneShotTimer; |
19 } | 21 } |
20 | 22 |
21 class ShellIntegration { | 23 class ShellIntegration { |
22 public: | 24 public: |
23 // Sets Chrome as the default browser (only for the current user). Returns | 25 // Sets Chrome as the default browser (only for the current user). Returns |
24 // false if this operation fails. | 26 // false if this operation fails. |
25 static bool SetAsDefaultBrowser(); | 27 static bool SetAsDefaultBrowser(); |
26 | 28 |
27 // Initiates an OS shell flow which (if followed by the user) should set | 29 // Initiates an OS shell flow which (if followed by the user) should set |
28 // Chrome as the default browser. Returns false if the flow cannot be | 30 // Chrome as the default browser. Returns false if the flow cannot be |
29 // initialized, if it is not supported (introduced for Windows 8) or if the | 31 // initialized, if it is not supported (introduced for Windows 8) or if the |
30 // user cancels the operation. This is a blocking call and requires a FILE | 32 // user cancels the operation. This is a blocking call and requires a FILE |
31 // thread. If Chrome is already default browser, no interactive dialog will be | 33 // thread. If Chrome is already default browser, no interactive dialog will be |
32 // shown and this method returns true. | 34 // shown and this method returns true. |
33 static bool SetAsDefaultBrowserInteractive(); | 35 static bool SetAsDefaultBrowserInteractive(); |
34 | 36 |
| 37 // Returns true if setting the default browser is an asynchronous operation. |
| 38 // In practice, this is only true on Windows 10+. |
| 39 static bool IsSetAsDefaultAsynchronous(); |
| 40 |
| 41 // Prompts the user to select the default browser by trying to open the help |
| 42 // page that explains how to set Chrome as the default browser. Only call this |
| 43 // if IsSetAsDefaultAsynchronous() is true. |
| 44 // |
| 45 // This call provides no indication as to whether or not the operation |
| 46 // succeeded. Consider using DefaultBrowserWorker which will invoke its |
| 47 // observer as appropriate. |
| 48 static void SetAsDefaultBrowserAsynchronous(); |
| 49 |
35 // Sets Chrome as the default client application for the given protocol | 50 // Sets Chrome as the default client application for the given protocol |
36 // (only for the current user). Returns false if this operation fails. | 51 // (only for the current user). Returns false if this operation fails. |
37 static bool SetAsDefaultProtocolClient(const std::string& protocol); | 52 static bool SetAsDefaultProtocolClient(const std::string& protocol); |
38 | 53 |
39 // Initiates an OS shell flow which (if followed by the user) should set | 54 // Initiates an OS shell flow which (if followed by the user) should set |
40 // Chrome as the default handler for |protocol|. Returns false if the flow | 55 // Chrome as the default handler for |protocol|. Returns false if the flow |
41 // cannot be initialized, if it is not supported (introduced for Windows 8) | 56 // cannot be initialized, if it is not supported (introduced for Windows 8) |
42 // or if the user cancels the operation. This is a blocking call and requires | 57 // or if the user cancels the operation. This is a blocking call and requires |
43 // a FILE thread. If Chrome is already default for |protocol|, no interactive | 58 // a FILE thread. If Chrome is already default for |protocol|, no interactive |
44 // dialog will be shown and this method returns true. | 59 // dialog will be shown and this method returns true. |
45 static bool SetAsDefaultProtocolClientInteractive( | 60 static bool SetAsDefaultProtocolClientInteractive( |
46 const std::string& protocol); | 61 const std::string& protocol); |
47 | 62 |
48 // In Windows 8 a browser can be made default-in-metro only in an interactive | 63 // Windows 8 and Windows 10 introduced different ways to set the default |
49 // flow. We will distinguish between two types of permissions here to avoid | 64 // browser. |
50 // forcing the user into UI interaction when this should not be done. | |
51 enum DefaultWebClientSetPermission { | 65 enum DefaultWebClientSetPermission { |
| 66 // The browser distribution is not permitted to be made default. |
52 SET_DEFAULT_NOT_ALLOWED, | 67 SET_DEFAULT_NOT_ALLOWED, |
| 68 // This is the most common case where no special permission or interaction |
| 69 // is required to set the default browser. |
53 SET_DEFAULT_UNATTENDED, | 70 SET_DEFAULT_UNATTENDED, |
| 71 // On Windows 8, a browser can be made default only in an interactive flow. |
54 SET_DEFAULT_INTERACTIVE, | 72 SET_DEFAULT_INTERACTIVE, |
| 73 // On Windows 10+, the set default browser flow is both interactive and |
| 74 // asynchronous. |
| 75 SET_DEFAULT_ASYNCHRONOUS, |
55 }; | 76 }; |
56 | 77 |
57 // Returns requirements for making the running browser the user's default. | 78 // Returns requirements for making the running browser the user's default. |
58 static DefaultWebClientSetPermission CanSetAsDefaultBrowser(); | 79 static DefaultWebClientSetPermission CanSetAsDefaultBrowser(); |
59 | 80 |
60 // Returns requirements for making the running browser the user's default | 81 // Returns requirements for making the running browser the user's default |
61 // client application for specific protocols. | 82 // client application for specific protocols. |
62 static DefaultWebClientSetPermission CanSetAsDefaultProtocolClient(); | 83 static DefaultWebClientSetPermission CanSetAsDefaultProtocolClient(); |
63 | 84 |
64 // Returns true if making the running browser the default client for any | 85 // Returns true if making the running browser the default client for any |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 // observer, once the operation has completed the new default will be | 238 // observer, once the operation has completed the new default will be |
218 // queried and the current status reported via SetDefaultWebClientUIState. | 239 // queried and the current status reported via SetDefaultWebClientUIState. |
219 void StartSetAsDefault(); | 240 void StartSetAsDefault(); |
220 | 241 |
221 // Called to notify the worker that the view is gone. | 242 // Called to notify the worker that the view is gone. |
222 void ObserverDestroyed(); | 243 void ObserverDestroyed(); |
223 | 244 |
224 protected: | 245 protected: |
225 friend class base::RefCountedThreadSafe<DefaultWebClientWorker>; | 246 friend class base::RefCountedThreadSafe<DefaultWebClientWorker>; |
226 | 247 |
227 virtual ~DefaultWebClientWorker() {} | 248 virtual ~DefaultWebClientWorker(); |
| 249 |
| 250 // Communicates the result to the observer. In contrast to |
| 251 // OnSetAsDefaultAttemptComplete(), this should not be called multiple |
| 252 // times. |
| 253 void OnCheckIsDefaultComplete(DefaultWebClientState state); |
| 254 |
| 255 // Called when the set as default operation is completed. This then invokes |
| 256 // FinalizeSetAsDefault() and, if an observer is present, starts the check |
| 257 // is default process. |succeeded| is true if the actual call to a |
| 258 // set-default function was successful. |
| 259 // It is safe to call this multiple times. Only the first call is processed |
| 260 // after StartSetAsDefault() is invoked. |
| 261 void OnSetAsDefaultAttemptComplete(bool succeeded); |
| 262 |
| 263 // Flag that indicates if the set-as-default operation is in progess to |
| 264 // prevent multiple notifications to the observer. |
| 265 bool set_as_default_in_progress_ = false; |
228 | 266 |
229 private: | 267 private: |
230 // Function that performs the check. | 268 // Checks whether Chrome is the default web client. Always called on the |
231 virtual DefaultWebClientState CheckIsDefault() = 0; | 269 // FILE thread. Subclasses are responsible for calling |
| 270 // OnCheckIsDefaultComplete() on the UI thread. |
| 271 virtual void CheckIsDefault() = 0; |
232 | 272 |
233 // Function that sets Chrome as the default web client. Returns false if | 273 // Sets Chrome as the default web client. Always called on the FILE thread. |
234 // the operation fails or has been cancelled by the user. | 274 // |interactive_permitted| will make SetAsDefault() fail if it requires |
235 virtual bool SetAsDefault(bool interactive_permitted) = 0; | 275 // interaction with the user. Subclasses are responsible for calling |
| 276 // OnSetAsDefaultAttemptComplete() on the UI thread. |
| 277 virtual void SetAsDefault(bool interactive_permitted) = 0; |
236 | 278 |
237 // Function that handles performing the check on the file thread. This | 279 // Invoked on the UI thread prior to starting a set-as-default operation. |
238 // function is posted as a task onto the file thread, where it performs | 280 virtual void InitializeSetAsDefault(); |
239 // the check. When the check has finished the CompleteCheckIsDefault | |
240 // function is posted to the UI thread, where the result is sent back to | |
241 // the observer. | |
242 void ExecuteCheckIsDefault(); | |
243 | 281 |
244 // Function that handles setting Chrome as the default web client on the | 282 // Invoked on the UI thread following a set-as-default operation. |
245 // file thread. This function is posted as a task onto the file thread. | 283 virtual void FinalizeSetAsDefault(bool succeeded); |
246 // Once it is finished the CompleteSetAsDefault function is posted to the | |
247 // UI thread which will check the status of Chrome as the default, and | |
248 // send this to the observer. | |
249 // |interactive_permitted| indicates if the routine is allowed to carry on | |
250 // in context where user interaction is required (CanSetAsDefault* | |
251 // returns SET_DEFAULT_INTERACTIVE). | |
252 void ExecuteSetAsDefault(bool interactive_permitted); | |
253 | |
254 // Communicate results to the observer. This function is posted as a task | |
255 // onto the UI thread by the ExecuteCheckIsDefault function running in the | |
256 // file thread. | |
257 void CompleteCheckIsDefault(DefaultWebClientState state); | |
258 | |
259 // When the action to set Chrome as the default has completed this function | |
260 // is run. It is posted as a task back onto the UI thread by the | |
261 // ExecuteSetAsDefault function running in the file thread. This function | |
262 // will the start the check process, which, if an observer is present, | |
263 // reports to it the new status. | |
264 // |succeeded| is true if the actual call to a set-default function (from | |
265 // ExecuteSetAsDefault) was successful. | |
266 void CompleteSetAsDefault(bool succeeded); | |
267 | 284 |
268 // Updates the UI in our associated view with the current default web | 285 // Updates the UI in our associated view with the current default web |
269 // client state. | 286 // client state. |
270 void UpdateUI(DefaultWebClientState state); | 287 void UpdateUI(DefaultWebClientState state); |
271 | 288 |
272 DefaultWebClientObserver* observer_; | 289 DefaultWebClientObserver* observer_; |
273 | 290 |
274 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); | 291 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); |
275 }; | 292 }; |
276 | 293 |
277 // Worker for checking and setting the default browser. | 294 // Worker for checking and setting the default browser. |
278 class DefaultBrowserWorker : public DefaultWebClientWorker { | 295 class DefaultBrowserWorker : public DefaultWebClientWorker { |
279 public: | 296 public: |
280 explicit DefaultBrowserWorker(DefaultWebClientObserver* observer); | 297 explicit DefaultBrowserWorker(DefaultWebClientObserver* observer); |
281 | 298 |
282 private: | 299 private: |
283 ~DefaultBrowserWorker() override {} | 300 ~DefaultBrowserWorker() override; |
284 | 301 |
285 // Check if Chrome is the default browser. | 302 // Check if Chrome is the default browser. |
286 DefaultWebClientState CheckIsDefault() override; | 303 void CheckIsDefault() override; |
287 | 304 |
288 // Set Chrome as the default browser. | 305 // Set Chrome as the default browser. |
289 bool SetAsDefault(bool interactive_permitted) override; | 306 void SetAsDefault(bool interactive_permitted) override; |
| 307 |
| 308 #if defined(OS_WIN) |
| 309 // On Windows 10+, adds the default browser callback and starts the timer |
| 310 // that determines if the operation was successful or not. |
| 311 void InitializeSetAsDefault() override; |
| 312 |
| 313 // On Windows 10+, removes the default browser callback and stops the timer. |
| 314 void FinalizeSetAsDefault(bool succeeded) override; |
| 315 |
| 316 // Used to determine if setting the default browser was unsuccesful. |
| 317 scoped_ptr<base::OneShotTimer> async_timer_; |
| 318 |
| 319 // Records the time it takes to set the default browser asynchronously. |
| 320 base::TimeTicks start_time_; |
| 321 #endif // !defined(OS_WIN) |
290 | 322 |
291 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); | 323 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); |
292 }; | 324 }; |
293 | 325 |
294 // Worker for checking and setting the default client application | 326 // Worker for checking and setting the default client application |
295 // for a given protocol. A different worker instance is needed for each | 327 // for a given protocol. A different worker instance is needed for each |
296 // protocol you are interested in, so to check or set the default for | 328 // protocol you are interested in, so to check or set the default for |
297 // multiple protocols you should use multiple worker objects. | 329 // multiple protocols you should use multiple worker objects. |
298 class DefaultProtocolClientWorker : public DefaultWebClientWorker { | 330 class DefaultProtocolClientWorker : public DefaultWebClientWorker { |
299 public: | 331 public: |
300 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, | 332 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, |
301 const std::string& protocol); | 333 const std::string& protocol); |
302 | 334 |
303 const std::string& protocol() const { return protocol_; } | 335 const std::string& protocol() const { return protocol_; } |
304 | 336 |
305 protected: | 337 protected: |
306 ~DefaultProtocolClientWorker() override {} | 338 ~DefaultProtocolClientWorker() override; |
307 | 339 |
308 private: | 340 private: |
309 // Check is Chrome is the default handler for this protocol. | 341 // Check is Chrome is the default handler for this protocol. |
310 DefaultWebClientState CheckIsDefault() override; | 342 void CheckIsDefault() override; |
311 | 343 |
312 // Set Chrome as the default handler for this protocol. | 344 // Set Chrome as the default handler for this protocol. |
313 bool SetAsDefault(bool interactive_permitted) override; | 345 void SetAsDefault(bool interactive_permitted) override; |
314 | 346 |
315 std::string protocol_; | 347 std::string protocol_; |
316 | 348 |
317 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); | 349 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); |
318 }; | 350 }; |
319 }; | 351 }; |
320 | 352 |
321 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ | 353 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ |
OLD | NEW |