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
|
// Copyright 2020 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/reading_list/reading_list_model_factory.h"
#include <memory>
#include <utility>
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/no_destructor.h"
#include "base/task/deferred_sequenced_task_runner.h"
#include "base/time/default_clock.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/data_type_store_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/reading_list/core/dual_reading_list_model.h"
#include "components/reading_list/core/reading_list_model_impl.h"
#include "components/reading_list/core/reading_list_model_storage_impl.h"
#include "components/reading_list/core/reading_list_pref_names.h"
#include "components/sync/base/features.h"
#include "components/sync/model/data_type_store_service.h"
#include "components/sync/model/wipe_model_upon_sync_disabled_behavior.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
namespace {
std::unique_ptr<KeyedService> BuildReadingListModel(
content::BrowserContext* context) {
Profile* const profile = Profile::FromBrowserContext(context);
syncer::OnceDataTypeStoreFactory store_factory =
DataTypeStoreServiceFactory::GetForProfile(profile)->GetStoreFactory();
auto local_storage =
std::make_unique<ReadingListModelStorageImpl>(std::move(store_factory));
auto reading_list_model_for_local_storage =
std::make_unique<ReadingListModelImpl>(
std::move(local_storage), syncer::StorageType::kUnspecified,
syncer::WipeModelUponSyncDisabledBehavior::kNever,
base::DefaultClock::GetInstance());
syncer::OnceDataTypeStoreFactory store_factory_for_account_storage =
DataTypeStoreServiceFactory::GetForProfile(profile)
->GetStoreFactoryForAccountStorage();
auto account_storage = std::make_unique<ReadingListModelStorageImpl>(
std::move(store_factory_for_account_storage));
auto reading_list_model_for_account_storage =
std::make_unique<ReadingListModelImpl>(
std::move(account_storage), syncer::StorageType::kAccount,
syncer::WipeModelUponSyncDisabledBehavior::kAlways,
base::DefaultClock::GetInstance());
return std::make_unique<reading_list::DualReadingListModel>(
/*local_or_syncable_model=*/std::move(
reading_list_model_for_local_storage),
/*account_model=*/std::move(reading_list_model_for_account_storage));
}
} // namespace
// static
ReadingListModel* ReadingListModelFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<ReadingListModel*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
reading_list::DualReadingListModel*
ReadingListModelFactory::GetAsDualReadingListForBrowserContext(
content::BrowserContext* context) {
return static_cast<reading_list::DualReadingListModel*>(
GetInstance()->GetServiceForBrowserContext(context, true));
}
// static
bool ReadingListModelFactory::HasModel(content::BrowserContext* context) {
return GetInstance()->GetServiceForBrowserContext(
context, /*create=*/false) != nullptr;
}
// static
ReadingListModelFactory* ReadingListModelFactory::GetInstance() {
static base::NoDestructor<ReadingListModelFactory> instance;
return instance.get();
}
// static
BrowserContextKeyedServiceFactory::TestingFactory
ReadingListModelFactory::GetDefaultFactoryForTesting() {
return base::BindRepeating(&BuildReadingListModel);
}
ReadingListModelFactory::ReadingListModelFactory()
: ProfileKeyedServiceFactory(
"ReadingListModel",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kRedirectedToOriginal)
// TODO(crbug.com/40257657): Check if this service is needed in
// Guest mode.
.WithGuest(ProfileSelection::kRedirectedToOriginal)
// TODO(crbug.com/41488885): Check if this service is needed for
// Ash Internals.
.WithAshInternals(ProfileSelection::kRedirectedToOriginal)
.Build()) {
DependsOn(DataTypeStoreServiceFactory::GetInstance());
}
ReadingListModelFactory::~ReadingListModelFactory() = default;
std::unique_ptr<KeyedService>
ReadingListModelFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
return BuildReadingListModel(context);
}
void ReadingListModelFactory::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
#if !BUILDFLAG(IS_ANDROID)
registry->RegisterBooleanPref(
reading_list::prefs::kReadingListDesktopFirstUseExperienceShown, false,
PrefRegistry::NO_REGISTRATION_FLAGS);
#endif // !BUILDFLAG(IS_ANDROID)
}
bool ReadingListModelFactory::ServiceIsNULLWhileTesting() const {
return true;
}
|