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
|
// Copyright 2019 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/ash/multidevice_setup/multidevice_setup_service_factory.h"
#include <memory>
#include <utility>
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "chrome/browser/ash/android_sms/android_sms_app_manager.h"
#include "chrome/browser/ash/android_sms/android_sms_pairing_state_tracker_impl.h"
#include "chrome/browser/ash/android_sms/android_sms_service_factory.h"
#include "chrome/browser/ash/device_sync/device_sync_client_factory.h"
#include "chrome/browser/ash/multidevice_setup/auth_token_validator_factory.h"
#include "chrome/browser/ash/multidevice_setup/auth_token_validator_impl.h"
#include "chrome/browser/ash/multidevice_setup/oobe_completion_tracker_factory.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "chromeos/ash/services/multidevice_setup/multidevice_setup_service.h"
#include "chromeos/ash/services/multidevice_setup/public/cpp/prefs.h"
#include "chromeos/ash/services/multidevice_setup/public/mojom/multidevice_setup.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/user_manager/user_manager.h"
namespace ash {
namespace multidevice_setup {
namespace {
// Class that wraps MultiDeviceSetupService in a KeyedService.
class MultiDeviceSetupServiceHolder : public KeyedService {
public:
explicit MultiDeviceSetupServiceHolder(content::BrowserContext* context)
: profile_(Profile::FromBrowserContext(context)) {
const user_manager::User* user =
ProfileHelper::Get()->GetUserByProfile(profile_);
const user_manager::User* primary_user =
user_manager::UserManager::Get()->GetPrimaryUser();
DCHECK(user);
DCHECK(primary_user);
bool is_secondary_user =
user->GetAccountId() != primary_user->GetAccountId();
android_sms::AndroidSmsService* android_sms_service =
android_sms::AndroidSmsServiceFactory::GetForBrowserContext(context);
multidevice_setup_service_ = std::make_unique<MultiDeviceSetupService>(
profile_->GetPrefs(),
device_sync::DeviceSyncClientFactory::GetForProfile(profile_),
AuthTokenValidatorFactory::GetForProfile(profile_),
OobeCompletionTrackerFactory::GetForProfile(profile_),
android_sms_service ? android_sms_service->android_sms_app_manager()
: nullptr,
android_sms_service
? android_sms_service->android_sms_pairing_state_tracker()
: nullptr,
is_secondary_user);
}
MultiDeviceSetupService* multidevice_setup_service() {
return multidevice_setup_service_.get();
}
private:
// KeyedService:
void Shutdown() override { multidevice_setup_service_.reset(); }
const raw_ptr<Profile> profile_;
std::unique_ptr<MultiDeviceSetupService> multidevice_setup_service_;
};
} // namespace
// static
MultiDeviceSetupService* MultiDeviceSetupServiceFactory::GetForProfile(
Profile* profile) {
if (!profile) {
PA_LOG(WARNING) << "Missing profile. Unable to return "
"MultiDeviceSetupService, returning nullptr instead";
return nullptr;
}
MultiDeviceSetupServiceHolder* holder =
static_cast<MultiDeviceSetupServiceHolder*>(
MultiDeviceSetupServiceFactory::GetInstance()
->GetServiceForBrowserContext(profile, true));
if (!holder) {
PA_LOG(WARNING)
<< "Missing MultiDeviceSetupServiceHolder. Unable to return "
"MultiDeviceSetupService, returning nullptr instead.";
return nullptr;
}
return holder->multidevice_setup_service();
}
// static
MultiDeviceSetupServiceFactory* MultiDeviceSetupServiceFactory::GetInstance() {
static base::NoDestructor<MultiDeviceSetupServiceFactory> factory;
return factory.get();
}
MultiDeviceSetupServiceFactory::MultiDeviceSetupServiceFactory()
: ProfileKeyedServiceFactory(
"MultiDeviceSetupService",
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(device_sync::DeviceSyncClientFactory::GetInstance());
DependsOn(AuthTokenValidatorFactory::GetInstance());
DependsOn(OobeCompletionTrackerFactory::GetInstance());
DependsOn(android_sms::AndroidSmsServiceFactory::GetInstance());
}
MultiDeviceSetupServiceFactory::~MultiDeviceSetupServiceFactory() = default;
std::unique_ptr<KeyedService>
MultiDeviceSetupServiceFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
if (!multidevice_setup::AreAnyMultiDeviceFeaturesAllowed(
Profile::FromBrowserContext(context)->GetPrefs())) {
PA_LOG(WARNING)
<< "No Multidevice Features allowed. Unable to return "
"MultiDeviceSetupServiceHolder, returning nullptr instead.";
return nullptr;
}
return std::make_unique<MultiDeviceSetupServiceHolder>(context);
}
} // namespace multidevice_setup
} // namespace ash
|