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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
// Copyright 2024 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_SYNC_CHROME_SYNC_CONTROLLER_BUILDER_H_
#define CHROME_BROWSER_SYNC_CHROME_SYNC_CONTROLLER_BUILDER_H_
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/spellcheck_buildflags.h"
#include "extensions/buildflags/buildflags.h"
class Profile;
class SecurityEventRecorder;
namespace syncer {
class DataTypeController;
class DataTypeStoreService;
class SyncService;
} // namespace syncer
namespace webapk {
class WebApkSyncService;
} // namespace webapk
#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
class ExtensionSyncService;
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
class ThemeService;
namespace web_app {
class WebAppProvider;
} // namespace web_app
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
#if BUILDFLAG(ENABLE_SPELLCHECK)
class SpellcheckService;
#endif // BUILDFLAG(ENABLE_SPELLCHECK)
#if BUILDFLAG(IS_CHROMEOS)
namespace app_list {
class AppListSyncableService;
} // namespace app_list
namespace ash {
class SyncedPrintersManager;
namespace floating_sso {
class FloatingSsoService;
} // namespace floating_sso
namespace printing::oauth2 {
class AuthorizationZonesManager;
} // namespace printing::oauth2
namespace sync_wifi {
class WifiConfigurationSyncService;
} // namespace sync_wifi
} // namespace ash
namespace arc {
class ArcPackageSyncableService;
} // namespace arc
namespace desks_storage {
class DeskSyncService;
} // namespace desks_storage
namespace sync_preferences {
class PrefServiceSyncable;
} // namespace sync_preferences
#endif // BUILDFLAG(IS_CHROMEOS)
// Class responsible for instantiating sync controllers (DataTypeController)
// for datatypes / features under chrome/.
//
// NOTE: prefer adding new types to browser_sync::CommonControllerBuilder if the
// type is available in components/, even if it's not enabled on all embedders
// or platforms.
//
// Users of this class need to inject dependencies by invoking all setters (more
// on this below) and finally invoke `Build()` to instantiate controllers.
class ChromeSyncControllerBuilder {
public:
ChromeSyncControllerBuilder();
~ChromeSyncControllerBuilder();
// Setters to inject dependencies. Each of these setters must be invoked
// before invoking `Build()`. In some cases it is allowed to inject nullptr.
void SetDataTypeStoreService(
syncer::DataTypeStoreService* data_type_store_service);
void SetSecurityEventRecorder(SecurityEventRecorder* security_event_recorder);
#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
void SetExtensionSyncService(ExtensionSyncService* extension_sync_service);
void SetExtensionSystemProfile(Profile* profile);
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
void SetThemeService(ThemeService* theme_service);
void SetWebAppProvider(web_app::WebAppProvider* web_app_provider);
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
#if BUILDFLAG(ENABLE_SPELLCHECK)
void SetSpellcheckService(SpellcheckService* spellcheck_service);
#endif // BUILDFLAG(ENABLE_SPELLCHECK)
#if BUILDFLAG(IS_ANDROID)
void SetWebApkSyncService(webapk::WebApkSyncService* web_apk_sync_service);
#endif // BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
void SetAppListSyncableService(
app_list::AppListSyncableService* app_list_syncable_service);
void SetAuthorizationZonesManager(
ash::printing::oauth2::AuthorizationZonesManager*
authorization_zones_manager);
void SetArcPackageSyncableService(
arc::ArcPackageSyncableService* arc_package_syncable_service,
Profile* arc_package_profile);
void SetDeskSyncService(desks_storage::DeskSyncService* desk_sync_service);
void SetFloatingSsoService(
ash::floating_sso::FloatingSsoService* floating_sso_service);
void SetOsPrefServiceSyncable(
sync_preferences::PrefServiceSyncable* os_pref_service_syncable);
void SetPrefService(PrefService* pref_service);
void SetSyncedPrintersManager(
ash::SyncedPrintersManager* synced_printer_manager);
void SetWifiConfigurationSyncService(
ash::sync_wifi::WifiConfigurationSyncService*
wifi_configuration_sync_service);
#endif // BUILDFLAG(IS_CHROMEOS)
// Actually builds the controllers. All setters above must have been called
// beforehand (null may or may not be allowed).
std::vector<std::unique_ptr<syncer::DataTypeController>> Build(
syncer::SyncService* sync_service);
private:
// Minimalistic fork of std::optional that enforces via CHECK that it has a
// value when accessing it.
template <typename Ptr>
class SafeOptional {
public:
SafeOptional() = default;
~SafeOptional() = default;
void Set(Ptr ptr) {
CHECK(!ptr_.has_value());
ptr_.emplace(std::move(ptr));
}
// Set() must have been called before.
Ptr value() const {
CHECK(ptr_.has_value());
return ptr_.value();
}
private:
std::optional<Ptr> ptr_;
};
// For all above, nullopt indicates the corresponding setter wasn't invoked.
// nullptr indicates the setter was invoked with nullptr.
SafeOptional<raw_ptr<syncer::DataTypeStoreService>> data_type_store_service_;
SafeOptional<raw_ptr<SecurityEventRecorder>> security_event_recorder_;
#if BUILDFLAG(ENABLE_EXTENSIONS_CORE)
SafeOptional<raw_ptr<ExtensionSyncService>> extension_sync_service_;
// This Profile instance has nothing special and is just the profile being
// exercised by the factory. A more tailored name is used simply to limit its
// usage beyond extensions.
SafeOptional<raw_ptr<Profile>> extension_system_profile_;
#endif
#if BUILDFLAG(ENABLE_EXTENSIONS)
SafeOptional<raw_ptr<ThemeService>> theme_service_;
SafeOptional<raw_ptr<web_app::WebAppProvider>> web_app_provider_;
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
#if BUILDFLAG(ENABLE_SPELLCHECK)
SafeOptional<raw_ptr<SpellcheckService>> spellcheck_service_;
#endif // BUILDFLAG(ENABLE_SPELLCHECK)
#if BUILDFLAG(IS_ANDROID)
SafeOptional<raw_ptr<webapk::WebApkSyncService>> web_apk_sync_service_;
#endif // BUILDFLAG(IS_ANDROID)
#if BUILDFLAG(IS_CHROMEOS)
SafeOptional<raw_ptr<app_list::AppListSyncableService>>
app_list_syncable_service_;
SafeOptional<raw_ptr<ash::printing::oauth2::AuthorizationZonesManager>>
authorization_zones_manager_;
SafeOptional<raw_ptr<arc::ArcPackageSyncableService>>
arc_package_syncable_service_;
// This Profile instance has nothing special and is just the profile being
// exercised by the factory. A more tailored name is used simply to limit its
// usage beyond ARC.
SafeOptional<raw_ptr<Profile>> arc_package_profile_;
SafeOptional<raw_ptr<desks_storage::DeskSyncService>> desk_sync_service_;
SafeOptional<raw_ptr<ash::floating_sso::FloatingSsoService>>
floating_sso_service_;
SafeOptional<raw_ptr<sync_preferences::PrefServiceSyncable>>
os_pref_service_syncable_;
SafeOptional<raw_ptr<PrefService>> pref_service_;
SafeOptional<raw_ptr<ash::SyncedPrintersManager>> synced_printer_manager_;
SafeOptional<raw_ptr<ash::sync_wifi::WifiConfigurationSyncService>>
wifi_configuration_sync_service_;
#endif // BUILDFLAG(IS_CHROMEOS)
};
#endif // CHROME_BROWSER_SYNC_CHROME_SYNC_CONTROLLER_BUILDER_H_
|