Chromium Code Reviews| 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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 }; | 194 }; |
| 195 | 195 |
| 196 class DefaultWebClientObserver { | 196 class DefaultWebClientObserver { |
| 197 public: | 197 public: |
| 198 virtual ~DefaultWebClientObserver() {} | 198 virtual ~DefaultWebClientObserver() {} |
| 199 // Updates the UI state to reflect the current default browser state. | 199 // Updates the UI state to reflect the current default browser state. |
| 200 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state) = 0; | 200 virtual void SetDefaultWebClientUIState(DefaultWebClientUIState state) = 0; |
| 201 // Called to notify the UI of the immediate result of invoking | 201 // Called to notify the UI of the immediate result of invoking |
| 202 // SetAsDefault. | 202 // SetAsDefault. |
| 203 virtual void OnSetAsDefaultConcluded(bool succeeded) {} | 203 virtual void OnSetAsDefaultConcluded(bool succeeded) {} |
| 204 // Observer classes that return true to OwnedByWorker are automatically | |
| 205 // freed by the worker when they are no longer needed. False by default. | |
| 206 virtual bool IsOwnedByWorker(); | |
| 207 // An observer can permit or decline set-as-default operation if it | |
| 208 // requires triggering user interaction. By default not allowed. | |
| 209 virtual bool IsInteractiveSetDefaultPermitted(); | |
| 210 }; | 204 }; |
| 211 | 205 |
| 212 // Helper objects that handle checking if Chrome is the default browser | 206 // Helper objects that handle checking if Chrome is the default browser |
| 213 // or application for a url protocol on Windows and Linux, and also setting | 207 // or application for a url protocol on Windows and Linux, and also setting |
| 214 // it as the default. These operations are performed asynchronously on the | 208 // it as the default. These operations are performed asynchronously on the |
| 215 // file thread since registry access (on Windows) or the preference database | 209 // file thread since registry access (on Windows) or the preference database |
| 216 // (on Linux) are involved and this can be slow. | 210 // (on Linux) are involved and this can be slow. |
| 211 // By default, the worker will present the user with an interactive flow if | |
| 212 // required by the platform. This can be suppressed via | |
| 213 // set_interactive_permitted(), in which case an attempt to set Chrome as | |
| 214 // the default handler will silently fail on such platforms. | |
| 217 class DefaultWebClientWorker | 215 class DefaultWebClientWorker |
| 218 : public base::RefCountedThreadSafe<DefaultWebClientWorker> { | 216 : public base::RefCountedThreadSafe<DefaultWebClientWorker> { |
| 219 public: | 217 public: |
| 220 explicit DefaultWebClientWorker(DefaultWebClientObserver* observer); | 218 // Constructor. The worker will post updates to |observer|. If |
| 219 // |delete_observer| is true, the worker owns the observer and it will be | |
| 220 // freed in the destructor. | |
|
Nico
2016/02/09 03:01:31
I find "optionally owned" code is usually hard to
Peter Kasting
2016/02/09 06:04:22
I agree.
In the limit, two constructors (raw poin
Patrick Monette
2016/02/09 17:15:05
Yeah I also agree and I'm working on fixing this i
| |
| 221 DefaultWebClientWorker(DefaultWebClientObserver* observer, | |
| 222 bool delete_observer); | |
| 223 | |
| 224 // Controls whether the worker can use user interaction to set the default | |
| 225 // web client. If false, the set-as-default operation will fail on OS where | |
| 226 // it is required. | |
| 227 void set_interactive_permitted(bool interactive_permitted) { | |
| 228 interactive_permitted_ = interactive_permitted; | |
| 229 } | |
| 221 | 230 |
| 222 // Checks to see if Chrome is the default web client application. The result | 231 // Checks to see if Chrome is the default web client application. The result |
| 223 // will be passed back to the observer via the SetDefaultWebClientUIState | 232 // will be passed back to the observer via the SetDefaultWebClientUIState |
| 224 // function. If there is no observer, this function does not do anything. | 233 // function. If there is no observer, this function does not do anything. |
| 225 void StartCheckIsDefault(); | 234 void StartCheckIsDefault(); |
| 226 | 235 |
| 227 // Sets Chrome as the default web client application. If there is an | 236 // Sets Chrome as the default web client application. If there is an |
| 228 // observer, once the operation has completed the new default will be | 237 // observer, once the operation has completed the new default will be |
| 229 // queried and the current status reported via SetDefaultWebClientUIState. | 238 // queried and the current status reported via SetDefaultWebClientUIState. |
| 230 void StartSetAsDefault(); | 239 void StartSetAsDefault(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 // is default process. | 283 // is default process. |
| 275 // It is safe to call this multiple times. Only the first call is processed | 284 // It is safe to call this multiple times. Only the first call is processed |
| 276 // after StartSetAsDefault() is invoked. | 285 // after StartSetAsDefault() is invoked. |
| 277 void OnSetAsDefaultAttemptComplete(AttemptResult result); | 286 void OnSetAsDefaultAttemptComplete(AttemptResult result); |
| 278 | 287 |
| 279 // Returns true if FinalizeSetAsDefault() will be called. | 288 // Returns true if FinalizeSetAsDefault() will be called. |
| 280 bool set_as_default_initialized() const { | 289 bool set_as_default_initialized() const { |
| 281 return set_as_default_initialized_; | 290 return set_as_default_initialized_; |
| 282 } | 291 } |
| 283 | 292 |
| 293 // When false, the operation to set as default will fail for interactive | |
| 294 // flows. | |
| 295 bool interactive_permitted_ = true; | |
| 296 | |
| 284 // Flag that indicates if the set-as-default operation is in progess to | 297 // Flag that indicates if the set-as-default operation is in progess to |
| 285 // prevent multiple notifications to the observer. | 298 // prevent multiple notifications to the observer. |
| 286 bool set_as_default_in_progress_ = false; | 299 bool set_as_default_in_progress_ = false; |
| 287 | 300 |
| 288 private: | 301 private: |
| 289 // Checks whether Chrome is the default web client. Always called on the | 302 // Checks whether Chrome is the default web client. Always called on the |
| 290 // FILE thread. Subclasses are responsible for calling | 303 // FILE thread. Subclasses are responsible for calling |
| 291 // OnCheckIsDefaultComplete() on the UI thread. | 304 // OnCheckIsDefaultComplete() on the UI thread. |
| 292 virtual void CheckIsDefault() = 0; | 305 virtual void CheckIsDefault() = 0; |
| 293 | 306 |
| 294 // Sets Chrome as the default web client. Always called on the FILE thread. | 307 // Sets Chrome as the default web client. Always called on the FILE thread. |
| 295 // |interactive_permitted| will make SetAsDefault() fail if it requires | 308 // Subclasses are responsible for calling OnSetAsDefaultAttemptComplete() on |
| 296 // interaction with the user. Subclasses are responsible for calling | 309 // the UI thread. |
| 297 // OnSetAsDefaultAttemptComplete() on the UI thread. | 310 virtual void SetAsDefault() = 0; |
| 298 virtual void SetAsDefault(bool interactive_permitted) = 0; | |
| 299 | 311 |
| 300 // Returns the prefix used for metrics to differentiate UMA metrics for | 312 // Returns the prefix used for metrics to differentiate UMA metrics for |
| 301 // setting the default browser and setting the default protocol client. | 313 // setting the default browser and setting the default protocol client. |
| 302 virtual const char* GetHistogramPrefix() = 0; | 314 virtual const char* GetHistogramPrefix() = 0; |
| 303 | 315 |
| 304 // Invoked on the UI thread prior to starting a set-as-default operation. | 316 // Invoked on the UI thread prior to starting a set-as-default operation. |
| 305 // Returns true if the initialization succeeded and a subsequent call to | 317 // Returns true if the initialization succeeded and a subsequent call to |
| 306 // FinalizeSetAsDefault() is required. | 318 // FinalizeSetAsDefault() is required. |
| 307 virtual bool InitializeSetAsDefault(); | 319 virtual bool InitializeSetAsDefault(); |
| 308 | 320 |
| 309 // Invoked on the UI thread following a set-as-default operation. | 321 // Invoked on the UI thread following a set-as-default operation. |
| 310 virtual void FinalizeSetAsDefault(); | 322 virtual void FinalizeSetAsDefault(); |
| 311 | 323 |
| 312 // Reports the result and duration for one set-as-default attempt. | 324 // Reports the result and duration for one set-as-default attempt. |
| 313 void ReportAttemptResult(AttemptResult result); | 325 void ReportAttemptResult(AttemptResult result); |
| 314 | 326 |
| 315 // Updates the UI in our associated view with the current default web | 327 // Updates the UI in our associated view with the current default web |
| 316 // client state. | 328 // client state. |
| 317 void UpdateUI(DefaultWebClientState state); | 329 void UpdateUI(DefaultWebClientState state); |
| 318 | 330 |
| 319 // Returns true if the duration of an attempt to set the default web client | 331 // Returns true if the duration of an attempt to set the default web client |
| 320 // should be reported to UMA for |result|. | 332 // should be reported to UMA for |result|. |
| 321 static bool ShouldReportDurationForResult(AttemptResult result); | 333 static bool ShouldReportDurationForResult(AttemptResult result); |
| 322 | 334 |
| 323 // Returns a string based on |result|. This is used for UMA reports. | 335 // Returns a string based on |result|. This is used for UMA reports. |
| 324 static const char* AttemptResultToString(AttemptResult result); | 336 static const char* AttemptResultToString(AttemptResult result); |
| 325 | 337 |
| 326 DefaultWebClientObserver* observer_; | 338 DefaultWebClientObserver* observer_; |
| 327 | 339 |
| 340 // Indicates if the the observer will be automatically freed by the worker. | |
| 341 bool delete_observer_; | |
| 342 | |
| 328 // Flag that indicates the return value of InitializeSetAsDefault(). If | 343 // Flag that indicates the return value of InitializeSetAsDefault(). If |
| 329 // true, FinalizeSetAsDefault() will be called to clear what was | 344 // true, FinalizeSetAsDefault() will be called to clear what was |
| 330 // initialized. | 345 // initialized. |
| 331 bool set_as_default_initialized_ = false; | 346 bool set_as_default_initialized_ = false; |
| 332 | 347 |
| 333 // Records the time it takes to set the default browser. | 348 // Records the time it takes to set the default browser. |
| 334 base::TimeTicks start_time_; | 349 base::TimeTicks start_time_; |
| 335 | 350 |
| 336 // Wait until Chrome has been confirmed as the default browser before | 351 // Wait until Chrome has been confirmed as the default browser before |
| 337 // reporting a successful attempt. | 352 // reporting a successful attempt. |
| 338 bool check_default_should_report_success_ = false; | 353 bool check_default_should_report_success_ = false; |
| 339 | 354 |
| 340 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); | 355 DISALLOW_COPY_AND_ASSIGN(DefaultWebClientWorker); |
| 341 }; | 356 }; |
| 342 | 357 |
| 343 // Worker for checking and setting the default browser. | 358 // Worker for checking and setting the default browser. |
| 344 class DefaultBrowserWorker : public DefaultWebClientWorker { | 359 class DefaultBrowserWorker : public DefaultWebClientWorker { |
| 345 public: | 360 public: |
| 346 explicit DefaultBrowserWorker(DefaultWebClientObserver* observer); | 361 // Constructor. The worker will post updates to |observer|. If |
| 362 // |delete_observer| is true, the worker owns the observer and it will be | |
| 363 // freed in the destructor. | |
| 364 DefaultBrowserWorker(DefaultWebClientObserver* observer, | |
| 365 bool delete_observer); | |
| 347 | 366 |
| 348 private: | 367 private: |
| 349 ~DefaultBrowserWorker() override; | 368 ~DefaultBrowserWorker() override; |
| 350 | 369 |
| 351 // Check if Chrome is the default browser. | 370 // Check if Chrome is the default browser. |
| 352 void CheckIsDefault() override; | 371 void CheckIsDefault() override; |
| 353 | 372 |
| 354 // Set Chrome as the default browser. | 373 // Set Chrome as the default browser. |
| 355 void SetAsDefault(bool interactive_permitted) override; | 374 void SetAsDefault() override; |
| 356 | 375 |
| 357 // Returns the histogram prefix for DefaultBrowserWorker. | 376 // Returns the histogram prefix for DefaultBrowserWorker. |
| 358 const char* GetHistogramPrefix() override; | 377 const char* GetHistogramPrefix() override; |
| 359 | 378 |
| 360 #if defined(OS_WIN) | 379 #if defined(OS_WIN) |
| 361 // On Windows 10+, adds the default browser callback and starts the timer | 380 // On Windows 10+, adds the default browser callback and starts the timer |
| 362 // that determines if the operation was successful or not. | 381 // that determines if the operation was successful or not. |
| 363 bool InitializeSetAsDefault() override; | 382 bool InitializeSetAsDefault() override; |
| 364 | 383 |
| 365 // On Windows 10+, removes the default browser callback and stops the timer. | 384 // On Windows 10+, removes the default browser callback and stops the timer. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 376 | 395 |
| 377 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); | 396 DISALLOW_COPY_AND_ASSIGN(DefaultBrowserWorker); |
| 378 }; | 397 }; |
| 379 | 398 |
| 380 // Worker for checking and setting the default client application | 399 // Worker for checking and setting the default client application |
| 381 // for a given protocol. A different worker instance is needed for each | 400 // for a given protocol. A different worker instance is needed for each |
| 382 // protocol you are interested in, so to check or set the default for | 401 // protocol you are interested in, so to check or set the default for |
| 383 // multiple protocols you should use multiple worker objects. | 402 // multiple protocols you should use multiple worker objects. |
| 384 class DefaultProtocolClientWorker : public DefaultWebClientWorker { | 403 class DefaultProtocolClientWorker : public DefaultWebClientWorker { |
| 385 public: | 404 public: |
| 405 // Constructor. The worker will post updates to |observer|. If | |
| 406 // |delete_observer| is true, the worker owns the observer and it will be | |
| 407 // freed in the destructor. | |
| 386 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, | 408 DefaultProtocolClientWorker(DefaultWebClientObserver* observer, |
| 387 const std::string& protocol); | 409 const std::string& protocol, |
| 410 bool delete_observer); | |
| 388 | 411 |
| 389 const std::string& protocol() const { return protocol_; } | 412 const std::string& protocol() const { return protocol_; } |
| 390 | 413 |
| 391 protected: | 414 protected: |
| 392 ~DefaultProtocolClientWorker() override; | 415 ~DefaultProtocolClientWorker() override; |
| 393 | 416 |
| 394 private: | 417 private: |
| 395 // Check is Chrome is the default handler for this protocol. | 418 // Check is Chrome is the default handler for this protocol. |
| 396 void CheckIsDefault() override; | 419 void CheckIsDefault() override; |
| 397 | 420 |
| 398 // Set Chrome as the default handler for this protocol. | 421 // Set Chrome as the default handler for this protocol. |
| 399 void SetAsDefault(bool interactive_permitted) override; | 422 void SetAsDefault() override; |
| 400 | 423 |
| 401 // Returns the histogram prefix for DefaultProtocolClientWorker. | 424 // Returns the histogram prefix for DefaultProtocolClientWorker. |
| 402 const char* GetHistogramPrefix() override; | 425 const char* GetHistogramPrefix() override; |
| 403 | 426 |
| 404 std::string protocol_; | 427 std::string protocol_; |
| 405 | 428 |
| 406 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); | 429 DISALLOW_COPY_AND_ASSIGN(DefaultProtocolClientWorker); |
| 407 }; | 430 }; |
| 408 }; | 431 }; |
| 409 | 432 |
| 410 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ | 433 #endif // CHROME_BROWSER_SHELL_INTEGRATION_H_ |
| OLD | NEW |