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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/session_sync_service_factory.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "build/build_config.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/data_type_store_service_factory.h"
#include "chrome/browser/sync/device_info_sync_service_factory.h"
#include "chrome/browser/sync/glue/sync_start_util.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router.h"
#include "chrome/browser/sync/sessions/sync_sessions_web_contents_router_factory.h"
#include "chrome/browser/ui/sync/browser_synced_window_delegates_getter.h"
#include "chrome/common/channel_info.h"
#include "chrome/common/url_constants.h"
#include "components/dom_distiller/core/url_constants.h"
#include "components/history/core/browser/history_service.h"
#include "components/sync/model/data_type_store_service.h"
#include "components/sync_device_info/device_info_sync_service.h"
#include "components/sync_device_info/device_info_tracker.h"
#include "components/sync_sessions/session_sync_prefs.h"
#include "components/sync_sessions/session_sync_service_impl.h"
#include "components/sync_sessions/sync_sessions_client.h"
#include "content/public/common/url_utils.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/sync/glue/synced_window_delegates_getter_android.h"
#endif // BUILDFLAG(IS_ANDROID)
namespace {
bool ShouldSyncURLImpl(const GURL& url) {
return url.is_valid() && !content::HasWebUIScheme(url) &&
!url.SchemeIs(chrome::kChromeNativeScheme) && !url.SchemeIsFile() &&
!url.SchemeIs(dom_distiller::kDomDistillerScheme);
}
// Chrome implementation of SyncSessionsClient.
class SyncSessionsClientImpl final : public sync_sessions::SyncSessionsClient {
public:
explicit SyncSessionsClientImpl(Profile* profile)
: profile_(profile), session_sync_prefs_(profile->GetPrefs()) {
window_delegates_getter_ =
#if BUILDFLAG(IS_ANDROID)
// Android doesn't have multi-profile support, so no need to pass the
// profile in.
std::make_unique<browser_sync::SyncedWindowDelegatesGetterAndroid>();
#else // BUILDFLAG(IS_ANDROID)
std::make_unique<browser_sync::BrowserSyncedWindowDelegatesGetter>(
profile);
#endif // BUILDFLAG(IS_ANDROID)
}
SyncSessionsClientImpl(const SyncSessionsClientImpl&) = delete;
SyncSessionsClientImpl& operator=(const SyncSessionsClientImpl&) = delete;
~SyncSessionsClientImpl() override = default;
// SyncSessionsClient implementation.
sync_sessions::SessionSyncPrefs* GetSessionSyncPrefs() override {
return &session_sync_prefs_;
}
syncer::RepeatingDataTypeStoreFactory GetStoreFactory() override {
return DataTypeStoreServiceFactory::GetForProfile(profile_)
->GetStoreFactory();
}
void ClearAllOnDemandFavicons() override {
history::HistoryService* history_service =
HistoryServiceFactory::GetForProfile(
profile_, ServiceAccessType::EXPLICIT_ACCESS);
if (!history_service) {
return;
}
history_service->ClearAllOnDemandFavicons();
}
bool ShouldSyncURL(const GURL& url) const override {
return ShouldSyncURLImpl(url);
}
bool IsRecentLocalCacheGuid(const std::string& cache_guid) const override {
return DeviceInfoSyncServiceFactory::GetForProfile(profile_)
->GetDeviceInfoTracker()
->IsRecentLocalCacheGuid(cache_guid);
}
sync_sessions::SyncedWindowDelegatesGetter* GetSyncedWindowDelegatesGetter()
override {
return window_delegates_getter_.get();
}
sync_sessions::LocalSessionEventRouter* GetLocalSessionEventRouter()
override {
syncer::SyncableService::StartSyncFlare flare(
sync_start_util::GetFlareForSyncableService(profile_->GetPath()));
sync_sessions::SyncSessionsWebContentsRouter* router =
sync_sessions::SyncSessionsWebContentsRouterFactory::GetForProfile(
profile_);
// TODO(mastiz): Injecting a start flare as a side effect of what seems to
// be a getter is error-prone. In fact, we seem to call this function very
// early for the USS implementation.
router->InjectStartSyncFlare(flare);
return router;
}
base::WeakPtr<SyncSessionsClient> AsWeakPtr() override {
return weak_ptr_factory_.GetWeakPtr();
}
private:
const raw_ptr<Profile> profile_;
std::unique_ptr<sync_sessions::SyncedWindowDelegatesGetter>
window_delegates_getter_;
sync_sessions::SessionSyncPrefs session_sync_prefs_;
base::WeakPtrFactory<SyncSessionsClientImpl> weak_ptr_factory_{this};
};
} // namespace
// static
sync_sessions::SessionSyncService* SessionSyncServiceFactory::GetForProfile(
Profile* profile) {
return static_cast<sync_sessions::SessionSyncService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
// static
SessionSyncServiceFactory* SessionSyncServiceFactory::GetInstance() {
static base::NoDestructor<SessionSyncServiceFactory> instance;
return instance.get();
}
// static - exposed for testing and metrics.
bool SessionSyncServiceFactory::ShouldSyncURLForTestingAndMetrics(
const GURL& url) {
return ShouldSyncURLImpl(url);
}
SessionSyncServiceFactory::SessionSyncServiceFactory()
: ProfileKeyedServiceFactory(
"SessionSyncService",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/40257657): Check if this service is needed in
// Guest mode.
.WithGuest(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/41488885): Check if this service is needed for
// Ash Internals.
.WithAshInternals(ProfileSelection::kOriginalOnly)
.Build()) {
DependsOn(DataTypeStoreServiceFactory::GetInstance());
DependsOn(DeviceInfoSyncServiceFactory::GetInstance());
DependsOn(FaviconServiceFactory::GetInstance());
DependsOn(HistoryServiceFactory::GetInstance());
DependsOn(sync_sessions::SyncSessionsWebContentsRouterFactory::GetInstance());
}
SessionSyncServiceFactory::~SessionSyncServiceFactory() = default;
std::unique_ptr<KeyedService>
SessionSyncServiceFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
Profile* profile = Profile::FromBrowserContext(context);
return std::make_unique<sync_sessions::SessionSyncServiceImpl>(
chrome::GetChannel(), std::make_unique<SyncSessionsClientImpl>(profile));
}
|