blob: 3382a41ac26a1a204ebc0488049aacf566c46251 [file]
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_
#define CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_
#include <string>
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/optional.h"
#include "base/strings/string16.h"
#include "chrome/browser/profiles/profile.h"
#include "google_apis/gaia/core_account_id.h"
class TokensLoadedCallbackRunner;
// Extracts an account from an existing profile and moves it to a new profile.
class DiceSignedInProfileCreator {
public:
// Empty user data, attached to the profile if this is a guest profile and a
// signin token was transferred.
class GuestSigninTokenTransferredUserData
: public base::SupportsUserData::Data {
public:
GuestSigninTokenTransferredUserData() = default;
static void Set(Profile* profile) {
profile->SetUserData(
kGuestSigninTokenTransferredUserDataKey,
std::make_unique<GuestSigninTokenTransferredUserData>());
}
static bool Get(Profile* profile) {
return profile->GetUserData(kGuestSigninTokenTransferredUserDataKey);
}
private:
DISALLOW_COPY_AND_ASSIGN(GuestSigninTokenTransferredUserData);
};
// Creates a new profile or uses Guest profile if |use_guest_profile|, and
// moves the account from source_profile to it.
// The callback is called with the new profile or nullptr in case of failure.
// The callback is never called synchronously.
// If |local_profile_name| is not empty, it will be set as local name for the
// new profile.
// If |icon_index| is nullopt, a random icon will be selected.
DiceSignedInProfileCreator(Profile* source_profile,
CoreAccountId account_id,
const base::string16& local_profile_name,
base::Optional<size_t> icon_index,
bool use_guest_profile,
base::OnceCallback<void(Profile*)> callback);
// Uses this version when the profile already exists at `target_profile_path`
// but may not be loaded in memory. The profile is loaded if necessary, and
// the account is moved.
DiceSignedInProfileCreator(Profile* source_profile,
CoreAccountId account_id,
const base::FilePath& target_profile_path,
base::OnceCallback<void(Profile*)> callback);
~DiceSignedInProfileCreator();
DiceSignedInProfileCreator(const DiceSignedInProfileCreator&) = delete;
DiceSignedInProfileCreator& operator=(const DiceSignedInProfileCreator&) =
delete;
// Key for GuestSigninTokenTransferredUserDataKey.
static const void* const kGuestSigninTokenTransferredUserDataKey;
private:
// Callback invoked once a profile is created, so we can transfer the
// credentials.
void OnNewProfileCreated(Profile* new_profile, Profile::CreateStatus status);
// Called when the profile is initialized.
void OnNewProfileInitialized(Profile* new_profile);
// Callback invoked once the token service is ready for the new profile.
void OnNewProfileTokensLoaded(Profile* new_profile);
Profile* const source_profile_;
const CoreAccountId account_id_;
base::OnceCallback<void(Profile*)> callback_;
std::unique_ptr<TokensLoadedCallbackRunner> tokens_loaded_callback_runner_;
base::WeakPtrFactory<DiceSignedInProfileCreator> weak_pointer_factory_{this};
};
#endif // CHROME_BROWSER_SIGNIN_DICE_SIGNED_IN_PROFILE_CREATOR_H_