#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) {
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);
}
}
bool IsShoppingListEligible(AccountChecker* account_checker) {
if (!commerce::IsRegionLockedFeatureEnabled(kShoppingList,
account_checker->GetCountry(),
account_checker->GetLocale())) {
return false;
}
if (!account_checker->GetPrefs() ||
!IsShoppingListAllowedForEnterprise(account_checker->GetPrefs())) {
return false;
}
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, 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,
account_checker->GetCountry(),
account_checker->GetLocale());
}
bool IsPriceAnnotationsEnabled(AccountChecker* account_checker) {
return account_checker &&
commerce::IsRegionLockedFeatureEnabled(kPriceAnnotations,
account_checker->GetCountry(),
account_checker->GetLocale());
}
bool IsProductSpecificationsAllowedForEnterprise(PrefService* prefs) {
return prefs->GetInteger(optimization_guide::prefs::
kProductSpecificationsEnterprisePolicyAllowed) <
2 ||
!prefs->IsManagedPreference(
optimization_guide::prefs::
kProductSpecificationsEnterprisePolicyAllowed);
}
bool IsProductSpecificationsQualityLoggingAllowed(PrefService* prefs) {
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) {
return base::FeatureList::IsEnabled(kProductSpecifications);
}
bool CanManageProductSpecificationsSets(
AccountChecker* account_checker,
ProductSpecificationsService* product_spec_service) {
if (!account_checker) {
return false;
}
if (!IsSyncingProductSpecifications(account_checker) ||
!CanLoadProductSpecificationsFullPageUi(account_checker)) {
return false;
}
if (!CanFetchProductSpecificationsData(account_checker)) {
int entity_count =
product_spec_service
? product_spec_service->GetAllProductSpecifications().size()
: 0;
if (entity_count == 0) {
return false;
}
}
return true;
}
bool CanFetchProductSpecificationsData(AccountChecker* account_checker) {
return CanFetchProductSpecificationsData(account_checker,
false);
}
bool IsProductSpecificationsSettingVisible(AccountChecker* account_checker) {
return CanFetchProductSpecificationsData(account_checker,
true);
}
bool IsDiscountInfoApiEnabled(AccountChecker* account_checker) {
return account_checker &&
commerce::IsRegionLockedFeatureEnabled(kEnableDiscountInfoApi,
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,
account_checker->GetCountry(),
account_checker->GetLocale());
}
bool IsShoppingPageTypesApiEnabled(AccountChecker* account_checker) {
return account_checker &&
commerce::IsRegionLockedFeatureEnabled(kShoppingPageTypes,
account_checker->GetCountry(),
account_checker->GetLocale());
}
bool IsDiscountAutofillEnabled(AccountChecker* account_checker) {
return account_checker && account_checker->IsSignedIn() &&
account_checker->IsAnonymizedUrlDataCollectionEnabled() &&
commerce::IsRegionLockedFeatureEnabled(kDiscountAutofill,
account_checker->GetCountry(),
account_checker->GetLocale());
}
}