File: promotion_eligibility_checker.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (108 lines) | stat: -rw-r--r-- 3,782 bytes parent folder | download | duplicates (5)
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