File: feature_utils.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (202 lines) | stat: -rw-r--r-- 8,186 bytes parent folder | download | duplicates (4)
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright 2024 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/commerce/core/feature_utils.h"

#include "base/feature_list.h"
#include "components/commerce/core/account_checker.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/pref_names.h"
#include "components/commerce/core/product_specifications/product_specifications_service.h"
#include "components/optimization_guide/core/feature_registry/feature_registration.h"
#include "components/optimization_guide/core/optimization_guide_features.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/user_selectable_type.h"

namespace commerce {
namespace {
bool CanFetchProductSpecificationsData(AccountChecker* account_checker,
                                       bool skip_enterprise_check) {
  // msbb, enterprise, parental controls, sync type, and model execution
  // features.
  return account_checker &&
         (skip_enterprise_check || IsProductSpecificationsAllowedForEnterprise(
                                       account_checker->GetPrefs())) &&
         account_checker->IsSignedIn() &&
         account_checker->IsAnonymizedUrlDataCollectionEnabled() &&
         !account_checker->IsSubjectToParentalControls() &&
         account_checker->CanUseModelExecutionFeatures() &&
         IsSyncingProductSpecifications(account_checker) &&
         CanLoadProductSpecificationsFullPageUi(account_checker);
}
}  // namespace

bool IsShoppingListEligible(AccountChecker* account_checker) {
  if (!commerce::IsRegionLockedFeatureEnabled(
          kShoppingList, kShoppingListRegionLaunched,
          account_checker->GetCountry(), account_checker->GetLocale())) {
    return false;
  }

  if (!account_checker->GetPrefs() ||
      !IsShoppingListAllowedForEnterprise(account_checker->GetPrefs())) {
    return false;
  }

  // Make sure the user allows subscriptions to be made and that we can fetch
  // store data.
  if (!account_checker || !account_checker->IsSignedIn() ||
      !account_checker->IsSyncTypeEnabled(
          syncer::UserSelectableType::kBookmarks) ||
      !account_checker->IsAnonymizedUrlDataCollectionEnabled() ||
      account_checker->IsSubjectToParentalControls()) {
    return false;
  }

  return true;
}

bool IsPriceInsightsApiEnabled(AccountChecker* account_checker) {
  return account_checker &&
         commerce::IsRegionLockedFeatureEnabled(
             kPriceInsights, kPriceInsightsRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsPriceInsightsEligible(AccountChecker* account_checker) {
  return IsPriceInsightsApiEnabled(account_checker) &&
         account_checker->IsAnonymizedUrlDataCollectionEnabled();
}

bool IsSubscriptionsApiEnabled(AccountChecker* account_checker) {
  return IsRegionLockedFeatureEnabled(
      kSubscriptionsApi, kSubscriptionsApiRegionLaunched,
      account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsPriceAnnotationsEnabled(AccountChecker* account_checker) {
  return account_checker &&
         commerce::IsRegionLockedFeatureEnabled(
             kPriceAnnotations, kPriceAnnotationsRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsProductSpecificationsAllowedForEnterprise(PrefService* prefs) {
  // 0 is fully enabled, 1 is enabled without logging, 2 is totally disabled.
  return prefs->GetInteger(optimization_guide::prefs::
                               kProductSpecificationsEnterprisePolicyAllowed) <
             2 ||
         !prefs->IsManagedPreference(
             optimization_guide::prefs::
                 kProductSpecificationsEnterprisePolicyAllowed);
}

bool IsProductSpecificationsQualityLoggingAllowed(PrefService* prefs) {
  // Explicitly check that the enterprise setting is 0. We check the managed
  // state to ensure the policy is correctly defined (all enterprise prefs are
  // managed).
  return prefs->GetInteger(optimization_guide::prefs::
                               kProductSpecificationsEnterprisePolicyAllowed) ==
             0 ||
         !prefs->IsManagedPreference(
             optimization_guide::prefs::
                 kProductSpecificationsEnterprisePolicyAllowed);
}

bool IsSyncingProductSpecifications(AccountChecker* account_checker) {
  return account_checker &&
         account_checker->IsSyncTypeEnabled(
             syncer::UserSelectableType::kProductComparison) &&
         account_checker->IsSyncAvailable();
}

bool CanLoadProductSpecificationsFullPageUi(AccountChecker* account_checker) {
  // TODO(352761768): Reintroduce the "region launched" version of the flag
  //                  with a supplementary kill switch flag so that it's
  //                  possible turn the whole feature off using one flag
  //                  while also supporting our "staggered" rollout.
  return base::FeatureList::IsEnabled(kProductSpecifications);
}

bool CanManageProductSpecificationsSets(
    AccountChecker* account_checker,
    ProductSpecificationsService* product_spec_service) {
  if (!account_checker) {
    return false;
  }

  // If we can't read sync entities, there's nothing to manage. Similarly,
  // being able to load the full-page is required so clicking on the items
  // works correctly.
  if (!IsSyncingProductSpecifications(account_checker) ||
      !CanLoadProductSpecificationsFullPageUi(account_checker)) {
    return false;
  }

  // We allow management of entities if they exist even if the feature is
  // otherwise inaccessible (assuming the flags are on).
  if (!CanFetchProductSpecificationsData(account_checker)) {
    int entity_count =
        product_spec_service
            ? product_spec_service->GetAllProductSpecifications().size()
            : 0;

    if (entity_count == 0) {
      // If there are no entities, we can only manage them if the full page
      // UI is enabled.
      return false;
    }
  }

  return true;
}

bool CanFetchProductSpecificationsData(AccountChecker* account_checker) {
  return CanFetchProductSpecificationsData(account_checker,
                                           /*skip_enterprise_check=*/false);
}

bool IsProductSpecificationsSettingVisible(AccountChecker* account_checker) {
  DCHECK(base::FeatureList::IsEnabled(
      optimization_guide::features::kAiSettingsPageEnterpriseDisabledUi));
  return CanFetchProductSpecificationsData(account_checker,
                                           /*skip_enterprise_check=*/true);
}

bool IsDiscountInfoApiEnabled(AccountChecker* account_checker) {
  return account_checker &&
         commerce::IsRegionLockedFeatureEnabled(
             kEnableDiscountInfoApi, kEnableDiscountInfoApiRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsDiscountEligibleToShowOnNavigation(AccountChecker* account_checker) {
  return IsDiscountInfoApiEnabled(account_checker) &&
         account_checker->IsSignedIn() &&
         account_checker->IsAnonymizedUrlDataCollectionEnabled();
}

bool IsMerchantViewerEnabled(AccountChecker* account_checker) {
  return account_checker &&
         commerce::IsRegionLockedFeatureEnabled(
             kCommerceMerchantViewer, kCommerceMerchantViewerRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsShoppingPageTypesApiEnabled(AccountChecker* account_checker) {
  return account_checker &&
         commerce::IsRegionLockedFeatureEnabled(
             kShoppingPageTypes, kShoppingPageTypesRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}

bool IsDiscountAutofillEnabled(AccountChecker* account_checker) {
  return account_checker && account_checker->IsSignedIn() &&
         account_checker->IsAnonymizedUrlDataCollectionEnabled() &&
         commerce::IsRegionLockedFeatureEnabled(
             kDiscountAutofill, kDiscountAutofillRegionLaunched,
             account_checker->GetCountry(), account_checker->GetLocale());
}
}  // namespace commerce