1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_DEFAULT_BROWSER_DEFAULT_BROWSER_MANAGER_H_
#define CHROME_BROWSER_DEFAULT_BROWSER_DEFAULT_BROWSER_MANAGER_H_
#include <memory>
#include <string>
#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "build/buildflag.h"
#include "chrome/browser/default_browser/default_browser_controller.h"
#include "ui/base/unowned_user_data/scoped_unowned_user_data.h"
#include "url/gurl.h"
class BrowserProcess;
namespace default_browser {
class DefaultBrowserMonitor;
class DefaultBrowserNotificationHandler;
using DefaultBrowserCheckCompletionCallback =
base::OnceCallback<void(DefaultBrowserState)>;
using DefaultBrowserChangedCallback =
base::RepeatingCallback<void(DefaultBrowserState)>;
// DefaultBrowserManager is the long-lived central coordinator and the public
// API for the default browser framework. It is responsible for selecting the
// correct setter and creating a controller, and provide general APIs for
// default-browser utilities.
class DefaultBrowserManager {
public:
DECLARE_USER_DATA(DefaultBrowserManager);
// Delegate for performing shell-dependent operations.
class ShellDelegate {
public:
virtual ~ShellDelegate() = 0;
// Asynchronously checks whether the browser is the default.
virtual void StartCheckIsDefault(
shell_integration::DefaultWebClientWorkerCallback callback) = 0;
#if BUILDFLAG(IS_WIN)
// Asynchronously fetches the program ID of the default client for the
// given `scheme`.
virtual void StartCheckDefaultClientProgId(
const GURL& scheme,
base::OnceCallback<void(const std::u16string&)> callback) = 0;
#endif // BUILDFLAG(IS_WIN)
};
explicit DefaultBrowserManager(BrowserProcess* browser_process,
std::unique_ptr<ShellDelegate> shell_delegate);
~DefaultBrowserManager();
DefaultBrowserManager(const DefaultBrowserManager&) = delete;
DefaultBrowserManager& operator=(const DefaultBrowserManager&) = delete;
static DefaultBrowserManager* From(BrowserProcess* browser_process);
static std::unique_ptr<ShellDelegate> CreateDefaultDelegate();
// Selects an appropriate setter, and create and returns a unique pointer to a
// controller instance.
static std::unique_ptr<DefaultBrowserController> CreateControllerFor(
DefaultBrowserEntrypointType ui_entrypoint);
// Utility method to check the current default browser state asynchronously.
void GetDefaultBrowserState(DefaultBrowserCheckCompletionCallback callback);
// Registers a callback that will be invoked on the manager thread whenever
// the system's default browser for HTTP/HTTPS protocols changes. The returned
// subscription object MUST be kept in scope for as long as the caller wishes
// to receive notifications. Destroying the subscription object will
// unregister the callback.
//
// For now, only Windows platform will notify when a change occur.
base::CallbackListSubscription RegisterDefaultBrowserChanged(
DefaultBrowserChangedCallback callback);
private:
void OnDefaultBrowserCheckResult(
default_browser::DefaultBrowserCheckCompletionCallback callback,
default_browser::DefaultBrowserState default_state);
// Performs additional validations on the default browser check's result to
// detect potentially incorrect results.
void PerformDefaultBrowserCheckValidations(
default_browser::DefaultBrowserState default_state);
// Called by the DefaultBrowserMonitor when a system-level change is detected.
// Triggers a re-verification to get the latest browser default state.
void OnMonitorDetectedChange();
// Callback for when the async state check is completed.
void NotifyObservers(DefaultBrowserState state);
// Delegate for handling shell operations, such as checking and setting
// default browser.
const std::unique_ptr<ShellDelegate> shell_delegate_;
// The platform default browser change monitor that handles the low-level
// logic for detecting when the system's default browser has changed.
std::unique_ptr<DefaultBrowserMonitor> monitor_;
// The handler responsible for showing system notifications.
std::unique_ptr<DefaultBrowserNotificationHandler> notification_handler_;
// List of high-level observers (Notification, UI handlers, etc.)
base::RepeatingCallbackList<void(DefaultBrowserState)> observers_;
// The subscription to signals from the low-level `monitor_`.
base::CallbackListSubscription monitor_subscription_;
ui::ScopedUnownedUserData<DefaultBrowserManager> scoped_unowned_user_data_;
};
} // namespace default_browser
#endif // CHROME_BROWSER_DEFAULT_BROWSER_DEFAULT_BROWSER_MANAGER_H_
|