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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/enterprise/browser/promotion/promotion_eligibility_checker.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/signin/public/identity_manager/access_token_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "google_apis/gaia/gaia_constants.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace {
constexpr char kOauthConsumerName[] = "promotion_eligibility_checker";
constexpr char kPolicyPromotionBannerLocale[] = "en-US";
} // namespace
namespace enterprise_promotion {
PromotionEligibilityChecker::PromotionEligibilityChecker(
const std::string& profile_id,
policy::CloudPolicyClient* client,
signin::IdentityManager* identity_manager,
std::string locale,
bool dismissed_banner_pref)
: identity_manager_(std::move(identity_manager)) {
if (dismissed_banner_pref || locale != kPolicyPromotionBannerLocale) {
return;
}
if (!client || !client->is_registered()) {
return;
}
promotion_query_client_ = std::make_unique<policy::CloudPolicyClient>(
profile_id, client->service(), client->GetURLLoaderFactory(),
policy::CloudPolicyClient::DeviceDMTokenCallback());
promotion_query_client_->SetupRegistration(
client->dm_token(), client->client_id(), client->user_affiliation_ids());
}
PromotionEligibilityChecker::~PromotionEligibilityChecker() = default;
void PromotionEligibilityChecker::MaybeCheckPromotionEligibility(
const CoreAccountId account_id,
PromotionEligibilityChecker::PromotionEligibilityCallback callback) {
DCHECK(!access_token_fetcher_);
// The caller must supply a username.
DCHECK(callback);
callback_ = std::move(callback);
if (account_id.empty() ||
!identity_manager_->HasAccountWithRefreshToken(account_id) ||
!promotion_query_client_ || !promotion_query_client_->is_registered()) {
std::move(callback_).Run(
enterprise_management::GetUserEligiblePromotionsResponse());
return;
}
signin::ScopeSet scopes;
scopes.insert(GaiaConstants::kDeviceManagementServiceOAuth);
scopes.insert(GaiaConstants::kGoogleUserInfoEmail);
access_token_fetcher_ = identity_manager_->CreateAccessTokenFetcherForAccount(
account_id, kOauthConsumerName, scopes,
base::BindOnce(&PromotionEligibilityChecker::OnAuthTokenFetched,
weak_factory_.GetWeakPtr()),
signin::AccessTokenFetcher::Mode::kWaitUntilRefreshTokenAvailable);
}
void PromotionEligibilityChecker::OnAuthTokenFetched(
GoogleServiceAuthError error,
signin::AccessTokenInfo token_info) {
if (error.state() != GoogleServiceAuthError::NONE) {
std::move(callback_).Run(
enterprise_management::GetUserEligiblePromotionsResponse());
return;
}
oauth_token_ = token_info.token;
if (oauth_token_.empty()) {
std::move(callback_).Run(
enterprise_management::GetUserEligiblePromotionsResponse());
return;
}
CheckPromotionEligibilityWithAuthToken(oauth_token_);
}
void PromotionEligibilityChecker::CheckPromotionEligibilityWithAuthToken(
std::string oauth_token) {
promotion_query_client_->SetOAuthTokenAsAdditionalAuth(oauth_token);
promotion_query_client_->DeterminePromotionEligibility(std::move(callback_));
}
void PromotionEligibilityChecker::SetCloudPolicyClientForTesting(
std::unique_ptr<policy::CloudPolicyClient> testing_client) {
promotion_query_client_ = std::move(testing_client);
}
} // namespace enterprise_promotion
|