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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chromeos/ash/components/growth/campaigns_model.h"
#include <memory>
#include <optional>
#include "ash/constants/ash_features.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/webui/grit/ash_mall_cros_app_resources.h"
#include "ash/webui/grit/ash_personalization_app_resources.h"
#include "ash/webui/grit/ash_print_management_resources.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "base/time/time.h"
#include "base/version.h"
#include "build/branding_buildflags.h"
#include "build/buildflag.h"
#include "chromeos/ash/components/growth/action_performer.h"
#include "chromeos/ash/components/growth/campaigns_logger.h"
#include "chromeos/ash/components/growth/growth_metrics.h"
#include "chromeos/ash/components/scalable_iph/buildflags.h"
#include "chromeos/ash/grit/ash_resources.h"
#include "chromeos/ui/vector_icons/vector_icons.h"
#include "ui/base/models/image_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/chromeos/styles/cros_tokens_color_mappings.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/vector_icon_types.h"
namespace growth {
namespace {
inline constexpr char kTargetings[] = "targetings";
inline constexpr char kId[] = "id";
inline constexpr char kGroupId[] = "groupId";
inline constexpr char kStudyId[] = "studyId";
inline constexpr char kShouldRegisterTrialWithTriggerEventName[] =
"registerTrialWithTriggerEventName";
// Targetings.
// Demo Mode targeting paths.
inline constexpr char kDemoModeTargeting[] = "demoMode";
inline constexpr char kDemoModeStoreIds[] = "storeIds";
inline constexpr char kDemoModeRetailers[] = "retailers";
inline constexpr char kDemoModeCountries[] = "countries";
inline constexpr char kDemoModeFeatureAware[] =
"capability.isFeatureAwareDevice";
inline constexpr char kDemoModeCloudGaming[] = "capability.isCloudGamingDevice";
inline constexpr char kMinDemoModeAppVersion[] = "appVersion.min";
inline constexpr char kMaxDemoModeAppVersion[] = "appVersion.max";
// Device Targeting paths.
inline constexpr char kDeviceTargeting[] = "device";
inline constexpr char kApplicationLocales[] = "locales";
inline constexpr char kBoards[] = "boards";
inline constexpr char kUserLocales[] = "userLocales";
inline constexpr char kIncludedCountries[] = "includedCountries";
inline constexpr char kExcludedCountries[] = "excludedCountries";
inline constexpr char kMinMilestone[] = "milestone.min";
inline constexpr char kMaxMilestone[] = "milestone.max";
inline constexpr char kMinVersion[] = "version.min";
inline constexpr char kMaxVersion[] = "version.max";
inline constexpr char kFeatureAware[] = "isFeatureAwareDevice";
inline constexpr char kRegisteredTime[] = "registeredTime";
inline constexpr char kDeviceAgeInHours[] = "deviceAgeInHours";
inline constexpr char kChannels[] = "channels";
// Session Targeting paths.
inline constexpr char kSessionTargeting[] = "session";
// Experiment Tag Targeting paths.
inline constexpr char kPredefinedFeatureIndex[] = "predefinedFeatureIndex";
inline constexpr char kOneOffExpFeatureIndex[] = "oneOffExpFeatureIndex";
inline constexpr char kExperimentTargetings[] = "experimentTags";
// User Targeting paths.
inline constexpr char kMinorUser[] = "isMinorUser";
inline constexpr char kOwner[] = "isOwner";
// Events Targeting paths.
inline constexpr char kEventsTargetings[] = "events";
inline constexpr char kImpressionCap[] = "impressionCap";
inline constexpr char kDismissalCap[] = "dismissalCap";
inline constexpr char kGroupImpressionCap[] = "groupImpressionCap";
inline constexpr char kGroupDismissalCap[] = "groupDismissalCap";
inline constexpr char kEventsConditions[] = "conditions";
inline constexpr int kImpressionCapDefaultValue = 3;
inline constexpr int kDismissalCapDefaultValue = 1;
// Runtime Targeting paths.
inline constexpr char kRuntimeTargeting[] = "runtime";
// Trigger Targeting paths.
// Path `triggers` was used in M126 and has been deprecated since M127.
// Path `triggersList` was added for M127.
inline constexpr char kTriggerTargetings[] = "triggerList";
inline constexpr char kTriggerType[] = "triggerType";
inline constexpr char kTriggerEvents[] = "triggerEvents";
// User Preference Targeting path.
inline constexpr char kUserPrefTargetings[] = "userPrefs";
// Shelf Hotseat Targeting path.
inline constexpr char kHotseatAppIcon[] = "hotseat.appIcon";
// Scheduling Targeting paths.
inline constexpr char kSchedulingTargetings[] = "schedulings";
inline constexpr char kTimeWindowStart[] = "start";
inline constexpr char kTimeWindowEnd[] = "end";
// Number Range Targeting paths.
inline constexpr char kNumberRangeStart[] = "start";
inline constexpr char kNumberRangeEnd[] = "end";
// String List Targeting paths.
inline constexpr char kStringListIncludes[] = "includes";
inline constexpr char kStringListExcludes[] = "excludes";
// Opened App Targeting paths.
inline constexpr char kAppsOpenedTargetings[] = "appsOpened";
inline constexpr char kAppId[] = "appId";
// Active URL regexes path.
inline constexpr char kActiveUrlRegexes[] = "activeUrlRegexes";
// Payloads
inline constexpr char kPayloadPathTemplate[] = "payload.%s";
inline constexpr char kDemoModePayloadPath[] = "demoModeApp";
inline constexpr char kNudgePayloadPath[] = "nudge";
inline constexpr char kNotificationPayloadPath[] = "notification";
inline constexpr char kOobePerkDiscoveryPayloadPath[] = "oobePerkDiscovery";
inline constexpr char kDemoModeSignInExperiencePath[] =
"demoModeSignInExperience";
// Actions
inline constexpr char kActionTypePath[] = "type";
inline constexpr char kActionParamsPath[] = "params";
// Anchor paths.
inline constexpr char kActiveAppWindowAnchorType[] =
"activeAppWindowAnchorType";
inline constexpr char kShelfAppButtonId[] = "shelfAppButtonId";
// Image Model.
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
inline constexpr char kImage[] = "image";
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
inline constexpr int kIconSize = 60;
inline constexpr char kVectorIcon[] = "vectorIcon";
// Vector Icon
inline constexpr char kBuiltInVectorIcon[] = "builtInVectorIcon";
// Each feature will be used in one finch study.
// These features are reusable if a feature is not currently used.
// Entries should not be ordered as feature is selected by index defined in the
// campaign.
inline const base::Feature* kPredefinedFeaturesForExperimentTagTargeting[] = {
&ash::features::kGrowthCampaignsExperiment1,
&ash::features::kGrowthCampaignsExperiment2,
&ash::features::kGrowthCampaignsExperiment3,
&ash::features::kGrowthCampaignsExperiment4,
&ash::features::kGrowthCampaignsExperiment5,
&ash::features::kGrowthCampaignsExperiment6,
&ash::features::kGrowthCampaignsExperiment7,
&ash::features::kGrowthCampaignsExperiment8,
&ash::features::kGrowthCampaignsExperiment9,
&ash::features::kGrowthCampaignsExperiment10,
&ash::features::kGrowthCampaignsExperiment11,
&ash::features::kGrowthCampaignsExperiment12,
&ash::features::kGrowthCampaignsExperiment13,
&ash::features::kGrowthCampaignsExperiment14,
&ash::features::kGrowthCampaignsExperiment15,
&ash::features::kGrowthCampaignsExperiment16,
&ash::features::kGrowthCampaignsExperiment17,
&ash::features::kGrowthCampaignsExperiment18,
&ash::features::kGrowthCampaignsExperiment19,
&ash::features::kGrowthCampaignsExperiment20,
};
// List of one-off feature flags used for delivering finch params for
// study/groups that refer to more than one feature flags.
// Each feature will be used in one finch study.
// These features are not reusable. It is tied to the finch config of a
// particular experiment.
// Entries should not be ordered as feature is selected by index defined in the
// campaign.
const base::Feature* kOneOffFeaturesForExperimentTagTargeting[] = {
&ash::features::kGrowthCampaignsExperimentG1Nudge,
&ash::features::kGrowthCampaignsExperimentFileAppGamgee,
};
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
// Image
inline constexpr char kBuiltInImage[] = "builtInImage";
inline constexpr gfx::Size kBubbleIconSizeDip = gfx::Size(kIconSize, kIconSize);
std::optional<int> GetBuiltInImageResourceId(
const std::optional<BuiltInImage>& image_model_type) {
if (!image_model_type) {
return std::nullopt;
}
switch (image_model_type.value()) {
case BuiltInImage::kGeminiApp:
return IDR_GROWTH_FRAMEWORK_GEMINI_APP_PNG;
case BuiltInImage::kG1:
return IDR_GROWTH_FRAMEWORK_G1_PNG;
case BuiltInImage::kSparkRebuy:
return IDR_GROWTH_FRAMEWORK_SPARK_REBUY_PNG;
case BuiltInImage::kSpark1PApp:
return IDR_GROWTH_FRAMEWORK_SPARK_1P_APP_PNG;
case BuiltInImage::kSparkV2:
return IDR_GROWTH_FRAMEWORK_SPARK_V2_PNG;
case BuiltInImage::kG1Notification:
return IDR_GROWTH_FRAMEWORK_G1_NOTIFICATION_PNG;
case BuiltInImage::kMall:
return IDR_GROWTH_FRAMEWORK_MALL_PNG;
#if BUILDFLAG(ENABLE_CROS_SCALABLE_IPH)
case BuiltInImage::kPrintJobsIcon:
return IDR_ASH_PRINT_MANAGEMENT_PRINT_MANAGEMENT_192_PNG;
case BuiltInImage::kGoogleDocsIcon:
return IDR_SCALABLE_IPH_GOOGLE_DOCS_ICON_120_PNG;
case BuiltInImage::kYouTubeIcon:
return IDR_SCALABLE_IPH_YOUTUBE_ICON_120_PNG;
case BuiltInImage::kPlayStoreIcon:
return IDR_SCALABLE_IPH_GOOGLE_PLAY_ICON_120_PNG;
#else
// Sclable Iph images are included only if ash-build and Chrome branded.
// Returns a fall-back image for the other case.
case BuiltInImage::kPrintJobsIcon:
case BuiltInImage::kGoogleDocsIcon:
case BuiltInImage::kYouTubeIcon:
case BuiltInImage::kPlayStoreIcon:
return IDR_PRODUCT_LOGO_128;
#endif // BUILDFLAG(ENABLE_CROS_SCALABLE_IPH)
case BuiltInImage::kRNotification:
return IDR_GROWTH_FRAMEWORK_R_NOTIFICATION_PNG;
case growth::BuiltInImage::kMallAppIcon:
return IDR_ASH_MALL_CROS_APP_IMAGES_MALL_ICON_192_PNG;
case growth::BuiltInImage::kPersonalizationIcon:
return IDR_ASH_PERSONALIZATION_APP_HUB_ICON_192_PNG;
}
return std::nullopt;
}
std::optional<BuiltInImage> GetBuiltInImageType(
const base::Value::Dict* image_dict) {
auto built_in_image_value = image_dict->FindInt(kBuiltInImage);
if (!built_in_image_value) {
return std::nullopt;
}
auto built_in_image = built_in_image_value.value();
if (built_in_image < 0 ||
built_in_image > static_cast<int>(BuiltInImage::kMaxValue)) {
return std::nullopt;
}
return static_cast<BuiltInImage>(built_in_image);
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
std::optional<BuiltInVectorIcon> GetBuiltInVectorIconType(
const base::Value::Dict* vector_icon_dict) {
auto built_in_vector_icon_value =
vector_icon_dict->FindInt(kBuiltInVectorIcon);
if (!built_in_vector_icon_value) {
return std::nullopt;
}
auto icon = built_in_vector_icon_value.value();
if (icon < 0 || icon > static_cast<int>(BuiltInVectorIcon::kMaxValue)) {
return std::nullopt;
}
return static_cast<BuiltInVectorIcon>(icon);
}
std::optional<base::Version> StringToVersion(const std::string* version_value) {
if (!version_value) {
return std::nullopt;
}
const auto version = base::Version(*version_value);
if (!version.IsValid()) {
return std::nullopt;
}
return std::move(version);
}
const base::Feature* SelectFeatureByIndex(const base::Feature* features[],
int size,
int index) {
if (index < 0 || index >= size) {
RecordCampaignsManagerError(CampaignsManagerError::kFeatureIndexOutOfRange);
return nullptr;
}
return features[index];
}
} // namespace
Trigger::Trigger(TriggerType type) : type(type) {}
Trigger::~Trigger() = default;
Campaigns* GetMutableCampaignsBySlot(CampaignsPerSlot* campaigns_per_slot,
Slot slot) {
if (!campaigns_per_slot) {
return nullptr;
}
return campaigns_per_slot->FindList(
base::NumberToString(static_cast<int>(slot)));
}
const Campaigns* GetCampaignsBySlot(const CampaignsPerSlot* campaigns_per_slot,
Slot slot) {
if (!campaigns_per_slot) {
return nullptr;
}
return campaigns_per_slot->FindList(
base::NumberToString(static_cast<int>(slot)));
}
const Targetings* GetTargetings(const Campaign* campaign) {
return campaign->FindList(kTargetings);
}
// Return the payload for the given `slot`. Payload could be nullptr for running
// A/A testing. When payload is nullptr, fallback to the default behavior.
const Payload* GetPayloadBySlot(const Campaign* campaign, Slot slot) {
switch (slot) {
case Slot::kDemoModeApp:
return campaign->FindDictByDottedPath(
base::StringPrintf(kPayloadPathTemplate, kDemoModePayloadPath));
case Slot::kNudge:
return campaign->FindDictByDottedPath(
base::StringPrintf(kPayloadPathTemplate, kNudgePayloadPath));
case Slot::kNotification:
return campaign->FindDictByDottedPath(
base::StringPrintf(kPayloadPathTemplate, kNotificationPayloadPath));
case Slot::kOobePerkDiscovery:
return campaign->FindDictByDottedPath(base::StringPrintf(
kPayloadPathTemplate, kOobePerkDiscoveryPayloadPath));
case Slot::kDemoModeSignInExperience:
return campaign->FindDictByDottedPath(base::StringPrintf(
kPayloadPathTemplate, kDemoModeSignInExperiencePath));
case Slot::kDemoModeFreePlayApps:
case Slot::kDryRun:
NOTREACHED();
}
return nullptr;
}
std::optional<int> GetCampaignId(const Campaign* campaign) {
return campaign->FindInt(kId);
}
std::optional<int> GetCampaignGroupId(const Campaign* campaign) {
return campaign->FindInt(kGroupId);
}
std::optional<int> GetStudyId(const Campaign* campaign) {
return campaign->FindInt(kStudyId);
}
std::optional<bool> ShouldRegisterTrialWithTriggerEventName(
const Campaign* campaign) {
return campaign->FindBool(kShouldRegisterTrialWithTriggerEventName);
}
// Targeting Base.
TargetingBase::TargetingBase(const Targeting* targeting_dict,
const char* targeting_path)
: targeting_(targeting_dict), targeting_path_(targeting_path) {}
TargetingBase::~TargetingBase() = default;
bool TargetingBase::IsValid() const {
return !!targeting_->FindDict(targeting_path_);
}
const base::Value::List* TargetingBase::GetListCriteria(
const char* path_suffix) const {
return targeting_->FindListByDottedPath(GetCriteriaPath(path_suffix));
}
const std::optional<bool> TargetingBase::GetBoolCriteria(
const char* path_suffix) const {
return targeting_->FindBoolByDottedPath(GetCriteriaPath(path_suffix));
}
const std::optional<int> TargetingBase::GetIntCriteria(
const char* path_suffix) const {
return targeting_->FindIntByDottedPath(GetCriteriaPath(path_suffix));
}
const std::string* TargetingBase::GetStringCriteria(
const char* path_suffix) const {
return targeting_->FindStringByDottedPath(GetCriteriaPath(path_suffix));
}
const base::Value::Dict* TargetingBase::GetDictCriteria(
const char* path_suffix) const {
return targeting_->FindDictByDottedPath(GetCriteriaPath(path_suffix));
}
const std::string TargetingBase::GetCriteriaPath(
const char* path_suffix) const {
return base::StringPrintf("%s.%s", targeting_path_, path_suffix);
}
// Demo Mode Targeting.
DemoModeTargeting::DemoModeTargeting(const Targeting* targeting_dict)
: TargetingBase(targeting_dict, kDemoModeTargeting) {}
DemoModeTargeting::~DemoModeTargeting() = default;
const base::Value::List* DemoModeTargeting::GetStoreIds() const {
return GetListCriteria(kDemoModeStoreIds);
}
const base::Value::List* DemoModeTargeting::GetRetailers() const {
return GetListCriteria(kDemoModeRetailers);
}
const base::Value::List* DemoModeTargeting::GetCountries() const {
return GetListCriteria(kDemoModeCountries);
}
const std::optional<base::Version> DemoModeTargeting::GetAppMinVersion() const {
return StringToVersion(GetStringCriteria(kMinDemoModeAppVersion));
}
const std::optional<base::Version> DemoModeTargeting::GetAppMaxVersion() const {
return StringToVersion(GetStringCriteria(kMaxDemoModeAppVersion));
}
const std::optional<bool> DemoModeTargeting::TargetCloudGamingDevice() const {
return GetBoolCriteria(kDemoModeCloudGaming);
}
const std::optional<bool> DemoModeTargeting::TargetFeatureAwareDevice() const {
return GetBoolCriteria(kDemoModeFeatureAware);
}
// Device Targeting.
DeviceTargeting::DeviceTargeting(const Targeting* targeting_dict)
: TargetingBase(targeting_dict, kDeviceTargeting) {}
DeviceTargeting::~DeviceTargeting() = default;
const std::unique_ptr<StringListTargeting> DeviceTargeting::GetBoards() const {
auto* string_list_dict = GetDictCriteria(kBoards);
if (!string_list_dict) {
return nullptr;
}
return std::make_unique<StringListTargeting>(string_list_dict);
}
const base::Value::List* DeviceTargeting::GetLocales() const {
return GetListCriteria(kApplicationLocales);
}
const base::Value::List* DeviceTargeting::GetUserLocales() const {
return GetListCriteria(kUserLocales);
}
const base::Value::List* DeviceTargeting::GetIncludedCountries() const {
return GetListCriteria(kIncludedCountries);
}
const base::Value::List* DeviceTargeting::GetExcludedCountries() const {
return GetListCriteria(kExcludedCountries);
}
const std::optional<int> DeviceTargeting::GetMinMilestone() const {
return GetIntCriteria(kMinMilestone);
}
const std::optional<int> DeviceTargeting::GetMaxMilestone() const {
return GetIntCriteria(kMaxMilestone);
}
const std::optional<base::Version> DeviceTargeting::GetMinVersion() const {
return StringToVersion(GetStringCriteria(kMinVersion));
}
const std::optional<base::Version> DeviceTargeting::GetMaxVersion() const {
return StringToVersion(GetStringCriteria(kMaxVersion));
}
const std::optional<bool> DeviceTargeting::GetFeatureAwareDevice() const {
return GetBoolCriteria(kFeatureAware);
}
std::unique_ptr<TimeWindowTargeting> DeviceTargeting::GetRegisteredTime()
const {
auto* registered_time_dict = GetDictCriteria(kRegisteredTime);
if (!registered_time_dict) {
return nullptr;
}
return std::make_unique<TimeWindowTargeting>(registered_time_dict);
}
const std::unique_ptr<NumberRangeTargeting> DeviceTargeting::GetDeviceAge()
const {
auto* number_range_dict = GetDictCriteria(kDeviceAgeInHours);
if (!number_range_dict) {
return nullptr;
}
return std::make_unique<NumberRangeTargeting>(number_range_dict);
}
const std::unique_ptr<StringListTargeting> DeviceTargeting::GetChannels()
const {
auto* string_list_dict = GetDictCriteria(kChannels);
if (!string_list_dict) {
return nullptr;
}
return std::make_unique<StringListTargeting>(string_list_dict);
}
// Apps Targeting.
AppTargeting::AppTargeting(const base::Value::Dict* app_dict)
: app_dict_(app_dict) {}
AppTargeting::~AppTargeting() = default;
const std::string* AppTargeting::GetAppId() const {
return app_dict_->FindString(kAppId);
}
// Events Targeting.
EventsTargeting::EventsTargeting(const base::Value::Dict* config_dict)
: config_dict_(config_dict) {}
EventsTargeting::~EventsTargeting() = default;
int EventsTargeting::GetImpressionCap() const {
auto cap = config_dict_->FindInt(kImpressionCap);
return cap.value_or(kImpressionCapDefaultValue);
}
int EventsTargeting::GetDismissalCap() const {
auto cap = config_dict_->FindInt(kDismissalCap);
return cap.value_or(kDismissalCapDefaultValue);
}
std::optional<int> EventsTargeting::GetGroupImpressionCap() const {
return config_dict_->FindInt(kGroupImpressionCap);
}
std::optional<int> EventsTargeting::GetGroupDismissalCap() const {
return config_dict_->FindInt(kGroupDismissalCap);
}
const base::Value::List* EventsTargeting::GetEventsConditions() const {
return config_dict_->FindList(kEventsConditions);
}
// Trigger Targeting.
TriggerTargeting::TriggerTargeting(const base::Value::Dict* trigger_dict)
: trigger_dict_(trigger_dict) {}
TriggerTargeting::~TriggerTargeting() = default;
std::optional<int> TriggerTargeting::GetTriggerType() const {
return trigger_dict_->FindInt(kTriggerType);
}
const base::Value::List* TriggerTargeting::GetTriggerEvents() const {
return trigger_dict_->FindList(kTriggerEvents);
}
// Time window Targeting.
TimeWindowTargeting::TimeWindowTargeting(
const base::Value::Dict* time_window_dict)
: time_window_dict_(time_window_dict) {}
TimeWindowTargeting::~TimeWindowTargeting() = default;
const base::Time TimeWindowTargeting::GetStartTime() const {
auto start = time_window_dict_->FindDouble(kTimeWindowStart);
if (start.has_value()) {
return base::Time::FromSecondsSinceUnixEpoch(start.value());
}
return base::Time::Min();
}
const base::Time TimeWindowTargeting::GetEndTime() const {
auto end = time_window_dict_->FindDouble(kTimeWindowEnd);
if (end.has_value()) {
return base::Time::FromSecondsSinceUnixEpoch(end.value());
}
return base::Time::Max();
}
// Number Range Targeting.
NumberRangeTargeting::NumberRangeTargeting(
const base::Value::Dict* number_range_dict)
: number_range_dict_(number_range_dict) {}
NumberRangeTargeting::~NumberRangeTargeting() = default;
const std::optional<int> NumberRangeTargeting::GetStart() const {
return number_range_dict_->FindInt(kNumberRangeStart);
}
const std::optional<int> NumberRangeTargeting::GetEnd() const {
return number_range_dict_->FindInt(kNumberRangeEnd);
}
// String List Targeting.
StringListTargeting::StringListTargeting(
const base::Value::Dict* string_list_dict)
: string_list_dict_(string_list_dict) {}
StringListTargeting::~StringListTargeting() = default;
const base::Value::List* StringListTargeting::GetIncludes() const {
return string_list_dict_->FindList(kStringListIncludes);
}
const base::Value::List* StringListTargeting::GetExcludes() const {
return string_list_dict_->FindList(kStringListExcludes);
}
// Session Targeting.
SessionTargeting::SessionTargeting(const Targeting* targeting_dict)
: TargetingBase(targeting_dict, kSessionTargeting) {}
SessionTargeting::~SessionTargeting() = default;
std::optional<const base::Feature*> SessionTargeting::GetFeature() const {
const auto one_off_feature_index = GetIntCriteria(kOneOffExpFeatureIndex);
if (one_off_feature_index) {
return SelectFeatureByIndex(
kOneOffFeaturesForExperimentTagTargeting,
static_cast<int>(std::size(kOneOffFeaturesForExperimentTagTargeting)),
one_off_feature_index.value());
}
const auto predefined_feature_index = GetIntCriteria(kPredefinedFeatureIndex);
if (predefined_feature_index) {
return SelectFeatureByIndex(
kPredefinedFeaturesForExperimentTagTargeting,
static_cast<int>(
std::size(kPredefinedFeaturesForExperimentTagTargeting)),
predefined_feature_index.value());
}
return std::nullopt;
}
const base::Value::List* SessionTargeting::GetExperimentTags() const {
return GetListCriteria(kExperimentTargetings);
}
std::optional<bool> SessionTargeting::GetMinorUser() const {
return GetBoolCriteria(kMinorUser);
}
std::optional<bool> SessionTargeting::GetIsOwner() const {
return GetBoolCriteria(kOwner);
}
// Runtime Targeting.
RuntimeTargeting::RuntimeTargeting(const Targeting* targeting_dict)
: TargetingBase(targeting_dict, kRuntimeTargeting) {}
RuntimeTargeting::~RuntimeTargeting() = default;
const std::vector<std::unique_ptr<TimeWindowTargeting>>
RuntimeTargeting::GetSchedulings() const {
std::vector<std::unique_ptr<TimeWindowTargeting>> schedulings;
auto* scheduling_list = GetListCriteria(kSchedulingTargetings);
if (!scheduling_list) {
return schedulings;
}
for (auto& scheduling_dict : *scheduling_list) {
if (!scheduling_dict.is_dict()) {
// Ignore invalid scheduling.
RecordCampaignsManagerError(CampaignsManagerError::kInvalidScheduling);
continue;
}
schedulings.push_back(
std::make_unique<TimeWindowTargeting>(&scheduling_dict.GetDict()));
}
return schedulings;
}
const std::vector<std::unique_ptr<AppTargeting>>
RuntimeTargeting::GetAppsOpened() const {
std::vector<std::unique_ptr<AppTargeting>> app_targetings;
auto* app_targeting_dicts = GetListCriteria(kAppsOpenedTargetings);
if (!app_targeting_dicts) {
return app_targetings;
}
for (auto& app_targeting_dict : *app_targeting_dicts) {
if (!app_targeting_dict.is_dict()) {
RecordCampaignsManagerError(CampaignsManagerError::kInvalidAppTargeting);
continue;
}
app_targetings.push_back(
std::make_unique<AppTargeting>(&app_targeting_dict.GetDict()));
}
return app_targetings;
}
const std::vector<std::string> RuntimeTargeting::GetActiveUrlRegexes() const {
std::vector<std::string> active_urls_regexs;
const auto* active_url_regexes_value = GetListCriteria(kActiveUrlRegexes);
if (!active_url_regexes_value) {
return active_urls_regexs;
}
for (const auto& active_url_regex_value : *active_url_regexes_value) {
if (!active_url_regex_value.is_string()) {
RecordCampaignsManagerError(CampaignsManagerError::kInvalidUrlRegrex);
CAMPAIGNS_LOG(ERROR) << "Invalid active url regex: "
<< active_url_regex_value.DebugString();
continue;
}
active_urls_regexs.push_back(active_url_regex_value.GetString());
}
return active_urls_regexs;
}
std::unique_ptr<EventsTargeting> RuntimeTargeting::GetEventsTargeting() const {
auto* config = GetDictCriteria(kEventsTargetings);
if (!config) {
return nullptr;
}
return std::make_unique<EventsTargeting>(config);
}
const std::vector<std::unique_ptr<TriggerTargeting>>
RuntimeTargeting::GetTriggers() const {
std::vector<std::unique_ptr<TriggerTargeting>> triggers;
auto* triggers_list = GetListCriteria(kTriggerTargetings);
if (!triggers_list) {
return triggers;
}
for (const auto& trigger : *triggers_list) {
if (!trigger.is_dict()) {
// Ignore invalid trigger.
RecordCampaignsManagerError(CampaignsManagerError::kInvalidTrigger);
continue;
}
triggers.push_back(std::make_unique<TriggerTargeting>(&trigger.GetDict()));
}
return triggers;
}
const base::Value::List* RuntimeTargeting::GetUserPrefTargetings() const {
return GetListCriteria(kUserPrefTargetings);
}
std::unique_ptr<AppTargeting> RuntimeTargeting::GetHotseatAppIcon() const {
auto* app = GetDictCriteria(kHotseatAppIcon);
if (!app) {
return nullptr;
}
return std::make_unique<AppTargeting>(app);
}
// Action.
Action::Action(const base::Value::Dict* action_dict)
: action_dict_(action_dict) {}
Action::~Action() = default;
std::optional<growth::ActionType> Action::GetActionType() const {
auto action_type_value = action_dict_->FindInt(kActionTypePath);
if (!action_type_value) {
CAMPAIGNS_LOG(ERROR) << "Missing action type.";
RecordCampaignsManagerError(CampaignsManagerError::kMissingActionType);
return std::nullopt;
}
auto action_type = action_type_value.value();
if (action_type < 0 ||
action_type > static_cast<int>(growth::ActionType::kMaxValue)) {
CAMPAIGNS_LOG(ERROR) << "Unrecognized action type.";
// TODO: b/330931877 - Record an error.
return std::nullopt;
}
return static_cast<growth::ActionType>(action_type);
}
const base::Value::Dict* Action::GetParams() const {
return action_dict_->FindDict(kActionParamsPath);
}
// Anchor.
Anchor::Anchor(const Targeting* anchor_dict) : anchor_dict_(anchor_dict) {}
Anchor::~Anchor() = default;
const std::optional<WindowAnchorType> Anchor::GetActiveAppWindowAnchorType()
const {
if (!anchor_dict_) {
// No valid anchor dict.
return std::nullopt;
}
const auto anchor_type_value =
anchor_dict_->FindInt(kActiveAppWindowAnchorType);
if (!anchor_type_value) {
// Invalid anchor type.
CAMPAIGNS_LOG(ERROR) << "Invalid anchor type";
RecordCampaignsManagerError(CampaignsManagerError::kInvalidAnchorType);
return std::nullopt;
}
auto anchor_type = anchor_type_value.value();
if (anchor_type < 0 ||
anchor_type > static_cast<int>(WindowAnchorType::kMaxValue)) {
return std::nullopt;
}
return static_cast<WindowAnchorType>(anchor_type);
}
const std::string* Anchor::GetShelfAppButtonId() const {
if (!anchor_dict_) {
// No valid anchor dict.
return nullptr;
}
return anchor_dict_->FindString(kShelfAppButtonId);
}
// Image.
Image::Image(const base::Value::Dict* image_dict) : image_dict_(image_dict) {}
Image::~Image() = default;
const gfx::Image* Image::GetImage() const {
if (!image_dict_) {
return nullptr;
}
// TODO: b/329113710- Handle other image sources.
return GetBuiltInImage();
}
const gfx::Image* Image::GetBuiltInImage() const {
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const auto image_id =
GetBuiltInImageResourceId(GetBuiltInImageType(image_dict_));
if (image_id) {
return &ui::ResourceBundle::GetSharedInstance().GetImageNamed(
image_id.value());
}
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
RecordCampaignsManagerError(CampaignsManagerError::kUnrecognizedBuiltInImage);
CAMPAIGNS_LOG(ERROR) << "Unrecognized built in image.";
return nullptr;
}
// Vector Icon.
VectorIcon::VectorIcon(const base::Value::Dict* vector_icon_dict)
: vector_icon_dict_(vector_icon_dict) {}
VectorIcon::~VectorIcon() = default;
const gfx::VectorIcon* VectorIcon::GetVectorIcon() const {
if (!vector_icon_dict_) {
return nullptr;
}
// TODO:b/329113710 - Handle other vector icon sources.
return GetBuiltInVectorIcon();
}
const gfx::VectorIcon* VectorIcon::GetBuiltInVectorIcon() const {
const auto icon = GetBuiltInVectorIconType(vector_icon_dict_);
if (!icon) {
RecordCampaignsManagerError(
CampaignsManagerError::kMissingBuiltInVectorIcon);
CAMPAIGNS_LOG(ERROR) << "Missing built in vector icon.";
return nullptr;
}
switch (icon.value()) {
case BuiltInVectorIcon::kRedeem:
return &chromeos::kRedeemIcon;
case BuiltInVectorIcon::kHelpApp:
return &ash::kNotificationHelpAppIcon;
}
RecordCampaignsManagerError(CampaignsManagerError::kMissingBuiltInVectorIcon);
CAMPAIGNS_LOG(ERROR) << "Unrecognized built in vector icon.";
}
// Image Model.
ImageModel::ImageModel(const base::Value::Dict* image_model_dict)
: image_model_dict_(image_model_dict) {}
ImageModel::~ImageModel() = default;
const std::optional<ui::ImageModel> ImageModel::GetImageModel() const {
if (!image_model_dict_) {
return std::nullopt;
}
// TODO(b/329113710): Handle other image sources.
return GetBuiltInImageModel();
}
const std::optional<ui::ImageModel> ImageModel::GetBuiltInImageModel() const {
const auto* vector_icon =
VectorIcon(image_model_dict_->FindDict(kVectorIcon)).GetVectorIcon();
if (vector_icon) {
// Returns vector icon.
return ui::ImageModel::FromVectorIcon(
*vector_icon, cros_tokens::kCrosSysOnSurface, kIconSize);
}
#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
const auto* image = Image(image_model_dict_->FindDict(kImage)).GetImage();
if (image) {
const gfx::ImageSkia* imageSkia = image->ToImageSkia();
gfx::ImageSkia resized_image = gfx::ImageSkiaOperations::CreateResizedImage(
*imageSkia, skia::ImageOperations::RESIZE_BEST, kBubbleIconSizeDip);
resized_image.EnsureRepsForSupportedScales();
return ui::ImageModel::FromImageSkia(resized_image);
}
// TODO: b/340895798 - update the error type naming and description.
RecordCampaignsManagerError(CampaignsManagerError::kUnrecognizedBuiltInIcon);
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
CAMPAIGNS_LOG(ERROR) << "Unrecognized built in image model.";
return std::nullopt;
}
} // namespace growth
|