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 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
|
// 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.
#include "chromeos/ash/components/scalable_iph/scalable_iph.h"
#include <memory>
#include <string_view>
#include <vector>
#include "ash/constants/ash_features.h"
#include "base/check.h"
#include "base/check_is_test.h"
#include "base/containers/enum_set.h"
#include "base/containers/fixed_flat_map.h"
#include "base/containers/fixed_flat_set.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/field_trial_params.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "chromeos/ash/components/scalable_iph/config.h"
#include "chromeos/ash/components/scalable_iph/iph_session.h"
#include "chromeos/ash/components/scalable_iph/logger.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph_constants.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph_delegate.h"
#include "components/feature_engagement/public/feature_constants.h"
namespace scalable_iph {
namespace {
using NotificationParams =
::scalable_iph::ScalableIphDelegate::NotificationParams;
using BubbleParams = ::scalable_iph::ScalableIphDelegate::BubbleParams;
using BubbleIcon = ::scalable_iph::ScalableIphDelegate::BubbleIcon;
constexpr char kFunctionCallAfterKeyedServiceShutdown[] =
"Function call after keyed service shutdown.";
// A set of ScalableIph events which can trigger an IPH.
constexpr auto kIphTriggeringEvents =
base::MakeFixedFlatSet<ScalableIph::Event>(
{ScalableIph::Event::kFiveMinTick, ScalableIph::Event::kUnlocked});
bool force_enable_iph_feature_for_testing = false;
std::string GetHelpAppIphEventName(ActionType action_type) {
switch (action_type) {
case ActionType::kOpenChrome:
return kEventNameHelpAppActionTypeOpenChrome;
case ActionType::kOpenLauncher:
return kEventNameHelpAppActionTypeOpenLauncher;
case ActionType::kOpenPersonalizationApp:
return kEventNameHelpAppActionTypeOpenPersonalizationApp;
case ActionType::kOpenPlayStore:
return kEventNameHelpAppActionTypeOpenPlayStore;
case ActionType::kOpenGoogleDocs:
return kEventNameHelpAppActionTypeOpenGoogleDocs;
case ActionType::kOpenGooglePhotos:
return kEventNameHelpAppActionTypeOpenGooglePhotos;
case ActionType::kOpenSettingsPrinter:
return kEventNameHelpAppActionTypeOpenSettingsPrinter;
case ActionType::kOpenPhoneHub:
return kEventNameHelpAppActionTypeOpenPhoneHub;
case ActionType::kOpenYouTube:
return kEventNameHelpAppActionTypeOpenYouTube;
case ActionType::kOpenFileManager:
return kEventNameHelpAppActionTypeOpenFileManager;
case ActionType::kInvalid:
default:
return "";
}
}
// The list of IPH features `SclableIph` supports. `ScalableIph` checks trigger
// conditions of all events listed in this list when it receives an `Event`.
const std::vector<raw_ptr<const base::Feature, VectorExperimental>>&
GetFeatureListConstant() {
static const base::NoDestructor<
std::vector<raw_ptr<const base::Feature, VectorExperimental>>>
feature_list({
// This must be sorted from One to Ten. A config expects that IPHs are
// evaluated in this priority.
// Timer based.
&feature_engagement::kIPHScalableIphTimerBasedOneFeature,
&feature_engagement::kIPHScalableIphTimerBasedTwoFeature,
&feature_engagement::kIPHScalableIphTimerBasedThreeFeature,
&feature_engagement::kIPHScalableIphTimerBasedFourFeature,
&feature_engagement::kIPHScalableIphTimerBasedFiveFeature,
&feature_engagement::kIPHScalableIphTimerBasedSixFeature,
&feature_engagement::kIPHScalableIphTimerBasedSevenFeature,
&feature_engagement::kIPHScalableIphTimerBasedEightFeature,
&feature_engagement::kIPHScalableIphTimerBasedNineFeature,
&feature_engagement::kIPHScalableIphTimerBasedTenFeature,
// Unlocked based.
&feature_engagement::kIPHScalableIphUnlockedBasedOneFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedTwoFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedThreeFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedFourFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedFiveFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedSixFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedSevenFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedEightFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedNineFeature,
&feature_engagement::kIPHScalableIphUnlockedBasedTenFeature,
// Help App based.
&feature_engagement::kIPHScalableIphHelpAppBasedNudgeFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedOneFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedTwoFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedThreeFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedFourFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedFiveFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedSixFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedSevenFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedEightFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedNineFeature,
&feature_engagement::kIPHScalableIphHelpAppBasedTenFeature,
// Gaming.
&feature_engagement::kIPHScalableIphGamingFeature,
});
return *feature_list;
}
constexpr auto kActionTypesMap =
// Key will be set in server side config.
base::MakeFixedFlatMap<std::string_view, ActionType>({
{kActionTypeOpenChrome, ActionType::kOpenChrome},
{kActionTypeOpenLauncher, ActionType::kOpenLauncher},
{kActionTypeOpenPersonalizationApp,
ActionType::kOpenPersonalizationApp},
{kActionTypeOpenPlayStore, ActionType::kOpenPlayStore},
{kActionTypeOpenGoogleDocs, ActionType::kOpenGoogleDocs},
{kActionTypeOpenGooglePhotos, ActionType::kOpenGooglePhotos},
{kActionTypeOpenSettingsPrinter, ActionType::kOpenSettingsPrinter},
{kActionTypeOpenPhoneHub, ActionType::kOpenPhoneHub},
{kActionTypeOpenYouTube, ActionType::kOpenYouTube},
{kActionTypeOpenFileManager, ActionType::kOpenFileManager},
{kActionTypeOpenHelpAppPerks, ActionType::kOpenHelpAppPerks},
{kActionTypeOpenChromebookPerksWeb,
ActionType::kOpenChromebookPerksWeb},
{kActionTypeOpenChromebookPerksGfnPriority2022,
ActionType::kOpenChromebookPerksGfnPriority2022},
{kActionTypeOpenChromebookPerksMinecraft2023,
ActionType::kOpenChromebookPerksMinecraft2023},
{kActionTypeOpenChromebookPerksMinecraftRealms2023,
ActionType::kOpenChromebookPerksMinecraftRealms2023},
});
constexpr auto kBubbleIconsMap =
// Key will be set in server side config.
base::MakeFixedFlatMap<std::string_view, BubbleIcon>({
{kBubbleIconChromeIcon, BubbleIcon::kChromeIcon},
{kBubbleIconPlayStoreIcon, BubbleIcon::kPlayStoreIcon},
{kBubbleIconGoogleDocsIcon, BubbleIcon::kGoogleDocsIcon},
{kBubbleIconGooglePhotosIcon, BubbleIcon::kGooglePhotosIcon},
{kBubbleIconPrintJobsIcon, BubbleIcon::kPrintJobsIcon},
{kBubbleIconYouTubeIcon, BubbleIcon::kYouTubeIcon},
});
constexpr auto kAppListItemActivationEventsMap =
base::MakeFixedFlatMap<std::string_view, ScalableIph::Event>({
{kWebAppGoogleDocsAppId,
ScalableIph::Event::kAppListItemActivationGoogleDocs},
{kWebAppYouTubeAppId,
ScalableIph::Event::kAppListItemActivationYouTube},
{kWebAppGooglePhotosAppId,
ScalableIph::Event::kAppListItemActivationGooglePhotosWeb},
{kAndroidAppGooglePlayStoreAppId,
ScalableIph::Event::kAppListItemActivationGooglePlayStore},
{kAndroidAppGooglePhotosAppId,
ScalableIph::Event::kAppListItemActivationGooglePhotosAndroid},
});
constexpr auto kShelfItemActivationEventsMap =
base::MakeFixedFlatMap<std::string_view, ScalableIph::Event>({
{kWebAppGoogleDocsAppId,
ScalableIph::Event::kShelfItemActivationGoogleDocs},
{kWebAppYouTubeAppId, ScalableIph::Event::kShelfItemActivationYouTube},
{kWebAppGooglePhotosAppId,
ScalableIph::Event::kShelfItemActivationGooglePhotosWeb},
{kAndroidGooglePhotosAppId,
ScalableIph::Event::kShelfItemActivationGooglePhotosAndroid},
});
constexpr base::TimeDelta kTimeTickEventInterval = base::Minutes(5);
std::string GetEventName(ScalableIph::Event event) {
// Use switch statement as you can get a compiler error if you forget to add a
// conversion.
switch (event) {
case ScalableIph::Event::kFiveMinTick:
return kEventNameFiveMinTick;
case ScalableIph::Event::kUnlocked:
return kEventNameUnlocked;
case ScalableIph::Event::kAppListShown:
return kEventNameAppListShown;
case ScalableIph::Event::kAppListItemActivationYouTube:
return kEventNameAppListItemActivationYouTube;
case ScalableIph::Event::kAppListItemActivationGoogleDocs:
return kEventNameAppListItemActivationGoogleDocs;
case ScalableIph::Event::kAppListItemActivationGooglePhotosWeb:
return kEventNameAppListItemActivationGooglePhotosWeb;
case ScalableIph::Event::kOpenPersonalizationApp:
return kEventNameOpenPersonalizationApp;
case ScalableIph::Event::kShelfItemActivationYouTube:
return kEventNameShelfItemActivationYouTube;
case ScalableIph::Event::kShelfItemActivationGoogleDocs:
return kEventNameShelfItemActivationGoogleDocs;
case ScalableIph::Event::kShelfItemActivationGooglePhotosWeb:
return kEventNameShelfItemActivationGooglePhotosWeb;
case ScalableIph::Event::kShelfItemActivationGooglePhotosAndroid:
return kEventNameShelfItemActivationGooglePhotosAndroid;
case ScalableIph::Event::kShelfItemActivationGooglePlay:
return kEventNameShelfItemActivationGooglePlay;
case ScalableIph::Event::kAppListItemActivationGooglePlayStore:
return kEventNameAppListItemActivationGooglePlayStore;
case ScalableIph::Event::kAppListItemActivationGooglePhotosAndroid:
return kEventNameAppListItemActivationGooglePhotosAndroid;
case ScalableIph::Event::kPrintJobCreated:
return kEventNamePrintJobCreated;
case ScalableIph::Event::kGameWindowOpened:
return kEventNameGameWindowOpened;
}
}
std::string GetParamValue(const base::Feature& feature,
const std::string& param_name) {
std::unique_ptr<Config> config = GetConfig(feature);
if (config && config->params.contains(param_name)) {
return config->params.at(param_name);
}
std::string fully_qualified_param_name =
base::StrCat({feature.name, "_", param_name});
std::string value = base::GetFieldTrialParamValueByFeature(
feature, fully_qualified_param_name);
// Non-fully-qualified name field must always be empty.
DCHECK(base::GetFieldTrialParamValueByFeature(feature, param_name).empty())
<< param_name
<< " is specified in a non-fully-qualified way. It should be specified "
"as "
<< fully_qualified_param_name
<< ". It's often the case in Scalable Iph to enable multiple features at "
"once. To avoid an unexpected fall-back behavior, non-fully-qualified "
"name is not accepted. Parameter names of custom fields must be "
"specified in a fully qualified way: [Feature Name]_[Parameter Name]";
return value;
}
void LogParamValueParseError(Logger* logger,
const base::Location& location,
const std::string& feature_name,
const std::string& param_name) {
logger->Log(
location,
base::StringPrintf(
"%s does not have a valid %s param value. Stop parsing the config.",
feature_name.c_str(), param_name.c_str()));
}
UiType ParseUiType(Logger* logger, const base::Feature& feature) {
std::string ui_type = GetParamValue(feature, kCustomUiTypeParamName);
if (ui_type != kCustomUiTypeValueNotification &&
ui_type != kCustomUiTypeValueBubble &&
ui_type != kCustomUiTypeValueNone) {
SCALABLE_IPH_LOG(logger) << ui_type << " is not a valid UI type.";
}
if (ui_type == kCustomUiTypeValueNotification) {
return UiType::kNotification;
}
if (ui_type == kCustomUiTypeValueBubble) {
return UiType::kBubble;
}
return UiType::kNone;
}
UiType GetUiType(Logger* logger, const base::Feature& feature) {
std::unique_ptr<Config> config = GetConfig(feature);
if (config) {
return config->ui_type;
}
return ParseUiType(logger, feature);
}
ActionType ParseActionType(const std::string& action_type_string) {
auto it = kActionTypesMap.find(action_type_string);
if (it == kActionTypesMap.end()) {
// If the server side config action type cannot be parsed, will return the
// kInvalid as the parsed result.
return ActionType::kInvalid;
}
return it->second;
}
std::string ParseActionEventName(const std::string& event_used_param) {
// The `event_used_param` is in this format:
// `name:ScalableIphTimerBasedOneEventUsed;comparator:any;window:365;storage:365`.
auto key_values = base::SplitString(
event_used_param, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
if (key_values.size() != 4) {
return "";
}
auto name_value = base::SplitString(key_values[0], ":", base::TRIM_WHITESPACE,
base::SPLIT_WANT_NONEMPTY);
if (name_value.size() != 2) {
return "";
}
if (name_value[0] != "name") {
return "";
}
return name_value[1];
}
ScalableIphDelegate::NotificationIcon GetNotificationIcon(
const std::string& icon) {
if (icon == kCustomNotificationIconValueRedeem) {
return ScalableIphDelegate::NotificationIcon::kRedeem;
}
return ScalableIphDelegate::NotificationIcon::kDefault;
}
ScalableIphDelegate::NotificationSummaryText GetNotificationSummaryText(
const std::string& summary_text) {
if (summary_text == kCustomNotificationSummaryTextValueNone) {
return ScalableIphDelegate::NotificationSummaryText::kNone;
}
return ScalableIphDelegate::NotificationSummaryText::kWelcomeTips;
}
std::unique_ptr<NotificationParams> ParseNotificationParams(
Logger* logger,
const base::Feature& feature) {
std::unique_ptr<NotificationParams> param =
std::make_unique<NotificationParams>();
param->notification_id =
GetParamValue(feature, kCustomNotificationIdParamName);
if (param->notification_id.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomNotificationIdParamName);
return nullptr;
}
param->title = GetParamValue(feature, kCustomNotificationTitleParamName);
if (param->title.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomNotificationTitleParamName);
return nullptr;
}
// Notification body text is an optional field. This can take an empty string.
param->text = GetParamValue(feature, kCustomNotificationBodyTextParamName);
param->button.text =
GetParamValue(feature, kCustomNotificationButtonTextParamName);
if (param->button.text.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomNotificationButtonTextParamName);
return nullptr;
}
std::string action_type =
GetParamValue(feature, kCustomButtonActionTypeParamName);
if (action_type.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionTypeParamName);
return nullptr;
}
param->button.action.action_type = ParseActionType(action_type);
if (param->button.action.action_type == ActionType::kInvalid) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionTypeParamName);
return nullptr;
}
std::string event_used =
GetParamValue(feature, kCustomButtonActionEventParamName);
if (event_used.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionEventParamName);
return nullptr;
}
param->button.action.iph_event_name = ParseActionEventName(event_used);
if (param->button.action.iph_event_name.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionEventParamName);
return nullptr;
}
std::string image_type =
GetParamValue(feature, kCustomNotificationImageTypeParamName);
param->image_type = ScalableIphDelegate::NotificationImageType::kNoImage;
if (image_type == kCustomNotificationImageTypeValueWallpaper) {
param->image_type = ScalableIphDelegate::NotificationImageType::kWallpaper;
} else if (image_type == kCustomNotificationImageTypeValueMinecraft) {
param->image_type = ScalableIphDelegate::NotificationImageType::kMinecraft;
}
std::string icon = GetParamValue(feature, kCustomNotificationIconParamName);
if (!icon.empty()) {
param->icon = GetNotificationIcon(icon);
}
SCALABLE_IPH_LOG(logger) << kCustomNotificationIconParamName
<< " is specified as " << icon << ". " << param->icon
<< " is set.";
std::string summary_text =
GetParamValue(feature, kCustomNotificationSummaryTextParamName);
if (!summary_text.empty()) {
param->summary_text = GetNotificationSummaryText(summary_text);
}
SCALABLE_IPH_LOG(logger) << kCustomNotificationSummaryTextParamName
<< " is specified as " << summary_text << ". "
<< param->summary_text << " is set.";
std::string source =
GetParamValue(feature, kCustomNotificationSourceTextParamName);
if (!source.empty()) {
param->source = source;
} else {
param->source = kCustomNotificationSourceTextValueDefault;
}
SCALABLE_IPH_LOG(logger) << kCustomNotificationSourceTextParamName
<< " is specified as " << source << ". "
<< param->source << " is set.";
return param;
}
std::unique_ptr<NotificationParams> GetNotificationParams(
Logger* logger,
const base::Feature& feature) {
std::unique_ptr<Config> config = GetConfig(feature);
if (config) {
return std::move(config->notification_params);
}
return ParseNotificationParams(logger, feature);
}
BubbleIcon ParseBubbleIcon(const std::string& icon_string) {
auto it = kBubbleIconsMap.find(icon_string);
if (it == kBubbleIconsMap.end()) {
// If the server side config bubble icon cannot be parsed, will return the
// kNoIcon as the parsed result.
return BubbleIcon::kNoIcon;
}
return it->second;
}
std::unique_ptr<BubbleParams> ParseBubbleParams(Logger* logger,
const base::Feature& feature) {
std::unique_ptr<BubbleParams> param = std::make_unique<BubbleParams>();
param->bubble_id = GetParamValue(feature, kCustomBubbleIdParamName);
if (param->bubble_id.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomBubbleIdParamName);
return nullptr;
}
// Title of bubble could be empty.
param->title = GetParamValue(feature, kCustomBubbleTitleParamName);
param->text = GetParamValue(feature, kCustomBubbleTextParamName);
if (param->text.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomBubbleTextParamName);
return nullptr;
}
// Button and action:
// Some nudge may not have a button and action.
param->button.text = GetParamValue(feature, kCustomBubbleButtonTextParamName);
if (!param->button.text.empty()) {
std::string action_type =
GetParamValue(feature, kCustomButtonActionTypeParamName);
if (action_type.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionTypeParamName);
return nullptr;
}
param->button.action.action_type = ParseActionType(action_type);
if (param->button.action.action_type == ActionType::kInvalid) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionTypeParamName);
return nullptr;
}
std::string event_used =
GetParamValue(feature, kCustomButtonActionEventParamName);
if (event_used.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionEventParamName);
return nullptr;
}
param->button.action.iph_event_name = ParseActionEventName(event_used);
if (param->button.action.iph_event_name.empty()) {
LogParamValueParseError(logger, FROM_HERE, feature.name,
kCustomButtonActionEventParamName);
return nullptr;
}
}
auto icon_string = GetParamValue(feature, kCustomBubbleIconParamName);
param->icon = ParseBubbleIcon(icon_string);
param->anchor_view_app_id =
GetParamValue(feature, kCustomBubbleAnchorViewAppIdParamName);
return param;
}
std::unique_ptr<BubbleParams> GetBubbleParams(Logger* logger,
const base::Feature& feature) {
std::unique_ptr<Config> config = GetConfig(feature);
if (config) {
return std::move(config->bubble_params);
}
return ParseBubbleParams(logger, feature);
}
bool ValidateVersionNumber(const base::Feature& feature) {
std::unique_ptr<Config> config = GetConfig(feature);
if (config) {
return config->version_number == kCurrentVersionNumber;
}
std::string version_number_value =
GetParamValue(feature, kCustomParamsVersionNumberParamName);
if (version_number_value.empty()) {
return false;
}
int version_number = 0;
if (!base::StringToInt(version_number_value, &version_number)) {
return false;
}
return version_number == kCurrentVersionNumber;
}
// `ScalableIphDelegate::SessionState` can take four states:
// `kUnknownInitialValue`, `kActive`, `kLocked`, `kOther`. We care two cases:
//
// 1. Session start
// For session start, we observe `kUnknownInitialValue` -[any intermediate
// states]-> `kActive` as unlock event. To allow [any intermediate states] in
// the middle, we won't advance internal `session_state_` during the phase. In
// production, there is `session_manager::SessionState::LOGGED_IN_NOT_ACTIVE`,
// which will be observed as: `kUnknownInitialValue` -> `kOther` -> `kActive`.
//
// 2. Unlock event
// For unlock event, we observe `kLocked` -> `kActive` as unlock event.
//
// This method returns `TransitionSet`, which is a set of enums. `GetTransition`
// does not maintain its state. But it expect the caller to manage it. The set
// specifies expected operations for the caller: advancing its internal state,
// handling unlock transition.
ScalableIph::TransitionSet GetTransition(ScalableIphDelegate::SessionState from,
ScalableIphDelegate::SessionState to) {
if (from == to) {
// Note that `OnSessionStateChanged` can be called more than once with the
// same `session_state` as `session_manager::SessionState` does not map to
// `ScalableIphDelegate::SessionState` with a 1:1 mapping, e.g.
// `ScalableIphDelegate::SessionState::kOther` is mapped to several states
// of `session_manager::SessionState`.
return {};
}
if (to == ScalableIphDelegate::SessionState::kUnknownInitialValue) {
// There should be no transition to `kUnknownInitialValue`. Ignore those
// transitions.
return {};
}
switch (from) {
case ScalableIphDelegate::SessionState::kUnknownInitialValue:
if (to == ScalableIphDelegate::SessionState::kActive) {
return {ScalableIph::SessionStateTransition::kAdvanceState,
ScalableIph::SessionStateTransition::kUnlock};
}
return {};
case ScalableIphDelegate::SessionState::kOther:
return {ScalableIph::SessionStateTransition::kAdvanceState};
case ScalableIphDelegate::SessionState::kLocked:
return to == ScalableIphDelegate::SessionState::kActive
? ScalableIph::TransitionSet(
{ScalableIph::SessionStateTransition::kAdvanceState,
ScalableIph::SessionStateTransition::kUnlock})
: ScalableIph::TransitionSet(
{ScalableIph::SessionStateTransition::kAdvanceState});
case ScalableIphDelegate::SessionState::kActive:
return {ScalableIph::SessionStateTransition::kAdvanceState};
}
}
} // namespace
// static
bool ScalableIph::IsAnyIphFeatureEnabled() {
if (force_enable_iph_feature_for_testing) {
return true;
}
const std::vector<raw_ptr<const base::Feature, VectorExperimental>>&
feature_list = GetFeatureListConstant();
for (auto feature : feature_list) {
if (base::FeatureList::IsEnabled(*feature)) {
return true;
}
}
return false;
}
// static
void ScalableIph::ForceEnableIphFeatureForTesting() {
CHECK_IS_TEST();
CHECK(!force_enable_iph_feature_for_testing)
<< "Iph feature is already force enabled";
force_enable_iph_feature_for_testing = true;
}
ScalableIph::ScalableIph(feature_engagement::Tracker* tracker,
std::unique_ptr<ScalableIphDelegate> delegate,
std::unique_ptr<Logger> logger)
: tracker_(tracker),
delegate_(std::move(delegate)),
logger_(std::move(logger)) {
CHECK(tracker_);
CHECK(delegate_);
CHECK(logger_);
delegate_observation_.Observe(delegate_.get());
EnsureTimerStarted();
online_ = delegate_->IsOnline();
SCALABLE_IPH_LOG(GetLogger()) << "Initialize: Online: " << online_;
tracker_->AddOnInitializedCallback(
base::BindOnce(&ScalableIph::CheckTriggerConditionsOnInitSuccess,
weak_ptr_factory_.GetWeakPtr()));
}
ScalableIph::~ScalableIph() = default;
void ScalableIph::Shutdown() {
timer_.Stop();
tracker_ = nullptr;
delegate_observation_.Reset();
delegate_.reset();
}
void ScalableIph::OnConnectionChanged(bool online) {
if (online_ == online) {
return;
}
online_ = online;
SCALABLE_IPH_LOG(GetLogger())
<< "Connection status changed. Online: " << online;
tracker_->AddOnInitializedCallback(
base::BindOnce(&ScalableIph::CheckTriggerConditionsOnInitSuccess,
weak_ptr_factory_.GetWeakPtr()));
}
void ScalableIph::OnSessionStateChanged(
ScalableIphDelegate::SessionState new_session_state) {
TransitionSet transition_set =
GetTransition(session_state_, new_session_state);
if (transition_set.empty()) {
SCALABLE_IPH_LOG(GetLogger())
<< "Uninterested session state transition observed from "
<< session_state_ << " to " << new_session_state
<< ". No state update made. No unlock event observed.";
}
// State transition must happen before `RecordEvent` as any code after
// `RecordEvent` can read the latest state.
if (transition_set.Has(SessionStateTransition::kAdvanceState)) {
SCALABLE_IPH_LOG(GetLogger())
<< "Session state changed from " << session_state_ << " to "
<< new_session_state;
session_state_ = new_session_state;
}
if (transition_set.Has(SessionStateTransition::kUnlock)) {
SCALABLE_IPH_LOG(GetLogger()) << "Transition is recognized as unlock event";
// Recording `kUnlocked` can trigger condition checks.
RecordEvent(Event::kUnlocked);
}
}
void ScalableIph::OnSuspendDoneWithoutLockScreen() {
if (session_state_ == ScalableIphDelegate::SessionState::kLocked) {
SCALABLE_IPH_LOG(GetLogger())
<< "Unexpected ScalableIph::OnSuspendDoneWithoutLockScreen call";
DCHECK(false) << "OnSuspendDoneWithoutLockScreen should never be called "
"with a lock screen";
}
SCALABLE_IPH_LOG(GetLogger())
<< "Recording kUnlocked because of OnSuspendDoneWithoutLockScreen";
RecordEvent(Event::kUnlocked);
}
void ScalableIph::OnAppListVisibilityChanged(bool shown) {
SCALABLE_IPH_LOG(GetLogger())
<< "App list visibility changed. Shown: " << shown;
if (shown) {
RecordEvent(Event::kAppListShown);
}
}
void ScalableIph::OnHasSavedPrintersChanged(bool has_saved_printers) {
DCHECK_NE(has_saved_printers_, has_saved_printers);
has_saved_printers_ = has_saved_printers;
SCALABLE_IPH_LOG(GetLogger())
<< "Has saved printers status changed. Has saved printers: "
<< has_saved_printers;
if (!has_saved_printers_closure_for_testing_.is_null()) {
has_saved_printers_closure_for_testing_.Run();
has_saved_printers_closure_for_testing_.Reset();
}
}
void ScalableIph::OnPhoneHubOnboardingEligibleChanged(
bool phonehub_onboarding_eligible) {
DCHECK_NE(phonehub_onboarding_eligible_, phonehub_onboarding_eligible);
SCALABLE_IPH_LOG(GetLogger())
<< "Phonehub onboarding eligible state has "
"changed: Phone hub onboarding eligible: from: "
<< phonehub_onboarding_eligible_
<< " to: " << phonehub_onboarding_eligible;
phonehub_onboarding_eligible_ = phonehub_onboarding_eligible;
}
void ScalableIph::PerformActionForIphSession(ActionType action_type) {
SCALABLE_IPH_LOG(GetLogger())
<< "Performing an action for an iph session. Action type:" << action_type;
PerformAction(action_type);
}
void ScalableIph::MaybeRecordAppListItemActivation(const std::string& id) {
auto it = kAppListItemActivationEventsMap.find(id);
if (it == kAppListItemActivationEventsMap.end()) {
SCALABLE_IPH_LOG(GetLogger())
<< "Observed an app list item activation. But not recording an app "
"list item activation as it's not listed in the map.";
return;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Recording an app list item activation as event: " << it->second;
// Record an event via `RecordEvent` instead of directly notifying an event to
// `tracker_` as `RecordEvent` can do common tasks, e.g. Making sure that a
// `tracker_` is initialized, etc.
RecordEvent(it->second);
}
void ScalableIph::MaybeRecordShelfItemActivationById(const std::string& id) {
auto it = kShelfItemActivationEventsMap.find(id);
if (it == kShelfItemActivationEventsMap.end()) {
SCALABLE_IPH_LOG(GetLogger())
<< "Observed a shelf item activation. But not recording a shelf item "
"activation as it's not listed in the map.";
return;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Recording a shelf item activation as event: " << it->second;
RecordEvent(it->second);
}
void ScalableIph::OverrideFeatureListForTesting(
const std::vector<raw_ptr<const base::Feature, VectorExperimental>>
feature_list) {
CHECK(feature_list_for_testing_.size() == 0)
<< "It's NOT allowed to override feature list twice for testing";
CHECK(feature_list.size() > 0) << "An empty list is NOT allowed to set.";
feature_list_for_testing_ = feature_list;
}
void ScalableIph::OverrideTaskRunnerForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner) {
CHECK(timer_.IsRunning())
<< "Timer is expected to be always running until Shutdown";
timer_.Stop();
timer_.SetTaskRunner(task_runner);
EnsureTimerStarted();
}
const std::vector<raw_ptr<const base::Feature, VectorExperimental>>&
ScalableIph::GetFeatureListConstantForTesting() {
CHECK_IS_TEST();
return GetFeatureListConstant();
}
// static:
ScalableIph::TransitionSet ScalableIph::GetTransitionForTesting(
ScalableIphDelegate::SessionState from,
ScalableIphDelegate::SessionState to) {
CHECK_IS_TEST();
return GetTransition(from, to);
}
bool ScalableIph::CheckTriggerEventForTesting(
const base::Feature& feature,
const std::optional<ScalableIph::Event>& trigger_event) {
CHECK_IS_TEST();
return CheckTriggerEvent(feature, trigger_event);
}
bool ScalableIph::ShouldPinHelpAppToShelf() {
return ash::features::AreHelpAppWelcomeTipsEnabled();
}
void ScalableIph::PerformActionForHelpApp(ActionType action_type) {
SCALABLE_IPH_LOG(GetLogger())
<< "Perform action for help app. Action type: " << action_type;
std::string iph_event_name = GetHelpAppIphEventName(action_type);
// ActionType enum is defined on the client side. We can use CHECK as this is
// a client side constraint.
CHECK(!iph_event_name.empty()) << "Unable to resolve the IPH event name to "
"an action type for the help app";
tracker_->NotifyEvent(iph_event_name);
PerformAction(action_type);
}
void ScalableIph::PerformAction(ActionType action_type) {
delegate_->PerformActionForScalableIph(action_type);
}
void ScalableIph::SetHasSavedPrintersChangedClosureForTesting(
base::RepeatingClosure has_saved_printers_closure) {
CHECK(has_saved_printers_closure_for_testing_.is_null());
has_saved_printers_closure_for_testing_ =
std::move(has_saved_printers_closure);
}
void ScalableIph::RecordEvent(ScalableIph::Event event) {
SCALABLE_IPH_LOG(GetLogger()) << "Record event. Event: " << event;
if (!tracker_) {
DCHECK(false) << kFunctionCallAfterKeyedServiceShutdown;
return;
}
// `AddOnInitializedCallback` immediately calls the callback if it's already
// initialized.
tracker_->AddOnInitializedCallback(
base::BindOnce(&ScalableIph::RecordEventInternal,
weak_ptr_factory_.GetWeakPtr(), event));
}
Logger* ScalableIph::GetLogger() {
return logger_.get();
}
void ScalableIph::EnsureTimerStarted() {
timer_.Start(FROM_HERE, kTimeTickEventInterval,
base::BindRepeating(&ScalableIph::RecordTimeTickEvent,
weak_ptr_factory_.GetWeakPtr()));
}
void ScalableIph::RecordTimeTickEvent() {
// Do not record timer event outside of an active session, e.g. OOBE, lock
// screen.
if (session_state_ != ScalableIphDelegate::SessionState::kActive) {
SCALABLE_IPH_LOG(GetLogger())
<< "Observed time tick event. But not recording it as session state is "
"not Active. Current session state is: "
<< session_state_;
return;
}
SCALABLE_IPH_LOG(GetLogger()) << "Record time tick event.";
RecordEvent(Event::kFiveMinTick);
}
void ScalableIph::RecordEventInternal(ScalableIph::Event event,
bool init_success) {
if (!tracker_) {
DCHECK(false) << kFunctionCallAfterKeyedServiceShutdown;
return;
}
if (!init_success) {
SCALABLE_IPH_LOG(GetLogger())
<< "Failed to initialize feature_engagement::Tracker";
DCHECK(false) << "Failed to initialize feature_engagement::Tracker.";
return;
}
if (session_state_ != ScalableIphDelegate::SessionState::kActive) {
SCALABLE_IPH_LOG(GetLogger())
<< "No event is expected to be recorded outside of an active session.";
return;
}
const std::string event_name = GetEventName(event);
SCALABLE_IPH_LOG(GetLogger()) << "Recording event as " << event_name;
tracker_->NotifyEvent(event_name);
if (kIphTriggeringEvents.contains(event)) {
SCALABLE_IPH_LOG(GetLogger()) << event
<< " is a condition check triggering event. "
"Running trigger conditions check.";
CheckTriggerConditions(event);
}
}
void ScalableIph::CheckTriggerConditionsOnInitSuccess(bool init_success) {
if (!init_success) {
SCALABLE_IPH_LOG(GetLogger())
<< "Failed to initialize feature_engagement::Tracker.";
return;
}
CheckTriggerConditions(std::nullopt);
}
void ScalableIph::CheckTriggerConditions(
const std::optional<ScalableIph::Event>& trigger_event) {
// Make sure that `tracker_` is initialized. `tracker_` should not cause crash
// even if we call `ShouldTriggerHelpUI` before initialization. But it returns
// false. It can become a difficult to notice/debug bug if we accidentally
// introduce a code path where we call it before initialization.
DCHECK(tracker_->IsInitialized());
if (session_state_ != ScalableIphDelegate::SessionState::kActive) {
SCALABLE_IPH_LOG(GetLogger()) << "Session state is not Active. No trigger "
"condition check. Session state is "
<< session_state_;
return;
}
SCALABLE_IPH_LOG(GetLogger()) << "Running trigger conditions check.";
for (const base::Feature* feature : GetFeatureList()) {
SCALABLE_IPH_LOG(GetLogger()) << "Checking: " << feature->name;
if (!base::FeatureList::IsEnabled(*feature)) {
SCALABLE_IPH_LOG(GetLogger())
<< feature->name << " is not enabled. Skipping condition check.";
continue;
}
if (!ValidateVersionNumber(*feature)) {
SCALABLE_IPH_LOG(GetLogger())
<< "Version number does not match with the current version "
"number. Skipping a config: "
<< feature->name;
continue;
}
if (!CheckCustomConditions(*feature, trigger_event)) {
SCALABLE_IPH_LOG(GetLogger())
<< "Custom conditions are not satisfied for " << feature->name;
continue;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Custom conditions are satisfied for " << feature->name;
if (!tracker_->ShouldTriggerHelpUI(*feature)) {
SCALABLE_IPH_LOG(GetLogger())
<< "Trigger conditions in feature_engagement::Tracker are not "
"satisfied for "
<< feature->name;
continue;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Trigger conditions in feature_engagement::Tracker are satisfied "
"for "
<< feature->name;
UiType ui_type = GetUiType(GetLogger(), *feature);
switch (ui_type) {
case UiType::kNotification: {
std::unique_ptr<NotificationParams> notification_params =
GetNotificationParams(GetLogger(), *feature);
if (!notification_params) {
SCALABLE_IPH_LOG(GetLogger())
<< "Failed to parse notification params for " << feature->name
<< ". Skipping the config.";
continue;
}
SCALABLE_IPH_LOG(GetLogger()) << "Triggering a notification.";
if (delegate_->ShowNotification(
*notification_params.get(),
std::make_unique<IphSession>(*feature, tracker_, this))) {
SCALABLE_IPH_LOG(GetLogger())
<< "Requested the UI framework to show a notification. Request "
"status: success. -> Do not check other trigger conditions to "
"avoid triggering multiple IPHs at the same time.";
return;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Requested the UI framework to show a notification. Request "
"status: failure. -> Keep checking other trigger conditions as "
"this IPH should not be shown.";
continue;
}
case UiType::kBubble: {
std::unique_ptr<BubbleParams> bubble_params =
GetBubbleParams(GetLogger(), *feature);
if (!bubble_params) {
SCALABLE_IPH_LOG(GetLogger())
<< "Failed to parse bubble params for " << feature->name
<< ". Skipping the config.";
continue;
}
SCALABLE_IPH_LOG(GetLogger()) << "Triggering a bubble.";
if (delegate_->ShowBubble(
*bubble_params.get(),
std::make_unique<IphSession>(*feature, tracker_, this))) {
SCALABLE_IPH_LOG(GetLogger())
<< "Requested the UI framework to show a bubble. Request status: "
"success. -> Do not check other trigger conditions to avoid "
"triggering multiple IPHs at the same time.";
return;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Requested the UI framework to show a bubble. Request status: "
"failure. -> Keep checking other trigger conditions as this IPH "
"should not be shown.";
continue;
}
case UiType::kNone:
SCALABLE_IPH_LOG(GetLogger())
<< "Condition gets satisfied. But specified ui type is None.";
break;
}
}
}
bool ScalableIph::CheckCustomConditions(
const base::Feature& feature,
const std::optional<ScalableIph::Event>& trigger_event) {
SCALABLE_IPH_LOG(GetLogger())
<< "Checking custom conditions for " << feature.name;
return CheckTriggerEvent(feature, trigger_event) &&
CheckNetworkConnection(feature) && CheckClientAge(feature) &&
CheckHasSavedPrinters(feature) &&
CheckPhoneHubOnboardingEligible(feature);
}
bool ScalableIph::CheckTriggerEvent(
const base::Feature& feature,
const std::optional<ScalableIph::Event>& trigger_event) {
SCALABLE_IPH_LOG(GetLogger())
<< "Checking trigger event condition for " << feature.name;
std::string trigger_event_condition =
GetParamValue(feature, kCustomConditionTriggerEventParamName);
if (trigger_event_condition.empty()) {
SCALABLE_IPH_LOG(GetLogger()) << "No trigger event condition specified.";
return true;
}
if (!trigger_event.has_value()) {
SCALABLE_IPH_LOG(GetLogger())
<< "This condition check is NOT triggered by an event. But trigger "
"event condition is specified. Condition unsatisfied.";
return false;
}
std::string trigger_event_name = GetEventName(trigger_event.value());
const bool result = trigger_event_condition == trigger_event_name;
SCALABLE_IPH_LOG(GetLogger())
<< "Specified trigger event name is " << trigger_event_condition
<< ". This condition check is triggered by " << trigger_event.value()
<< ". Compared trigger event name is " << trigger_event_name
<< ". Result: " << result;
return result;
}
bool ScalableIph::CheckNetworkConnection(const base::Feature& feature) {
SCALABLE_IPH_LOG(GetLogger())
<< "Checking network condition for " << feature.name;
std::string connection_condition =
GetParamValue(feature, kCustomConditionNetworkConnectionParamName);
if (connection_condition.empty()) {
SCALABLE_IPH_LOG(GetLogger()) << "No network condition specified.";
return true;
}
// If an invalid value is provided, does not satisfy a condition for a
// fail-safe behavior.
if (connection_condition != kCustomConditionNetworkConnectionOnline) {
SCALABLE_IPH_LOG(GetLogger())
<< "Only " << kCustomConditionNetworkConnectionOnline
<< " is the valid value for network connection condition";
return false;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Expecting online. Current status is: Online: " << online_;
return online_;
}
bool ScalableIph::CheckClientAge(const base::Feature& feature) {
SCALABLE_IPH_LOG(GetLogger()) << "Checking client age for " << feature.name;
std::string client_age_condition =
GetParamValue(feature, kCustomConditionClientAgeInDaysParamName);
if (client_age_condition.empty()) {
SCALABLE_IPH_LOG(GetLogger()) << "No client age condition specified.";
return true;
}
// Use `SCALABLE_IPH_LOG`s for logging instead of `DCHECK(false)` as we want
// to test those fail-safe behaviors in browser_tests.
int max_client_age = 0;
if (!base::StringToInt(client_age_condition, &max_client_age)) {
SCALABLE_IPH_LOG(GetLogger())
<< "Failed to parse client age condition. It must be an integer.";
return false;
}
if (max_client_age < 0) {
SCALABLE_IPH_LOG(GetLogger())
<< "Client age condition must be a positive integer value.";
return false;
}
int client_age = delegate_->ClientAgeInDays();
if (client_age < 0) {
SCALABLE_IPH_LOG(GetLogger())
<< "Client age is a negative number. This can happen if a "
"user changes time zone, etc. Condition is not satisfied "
"for a fail safe behavior.";
return false;
}
const bool result = client_age <= max_client_age;
SCALABLE_IPH_LOG(GetLogger())
<< "Current client age is " << client_age
<< ". Specified max client age is " << max_client_age
<< " (inclusive). Condition satisfied is: " << result;
return result;
}
bool ScalableIph::CheckHasSavedPrinters(const base::Feature& feature) {
SCALABLE_IPH_LOG(GetLogger())
<< "Checking has saved printers condition for " << feature.name;
std::string has_saved_printers_condition =
GetParamValue(feature, kCustomConditionHasSavedPrintersParamName);
if (has_saved_printers_condition.empty()) {
SCALABLE_IPH_LOG(GetLogger())
<< "No has saved printers condition specified.";
return true;
}
if (has_saved_printers_condition !=
kCustomConditionHasSavedPrintersValueTrue &&
has_saved_printers_condition !=
kCustomConditionHasSavedPrintersValueFalse) {
SCALABLE_IPH_LOG(GetLogger())
<< "Invalid value provided for "
<< kCustomConditionHasSavedPrintersParamName
<< ". This condition is not satisfied for a fail-safe behavior.";
return false;
}
const bool expected_value =
has_saved_printers_condition == kCustomConditionHasSavedPrintersValueTrue;
const bool result = has_saved_printers_ == expected_value;
SCALABLE_IPH_LOG(GetLogger())
<< "Expected value is " << expected_value
<< ". Current has saved printers value is " << has_saved_printers_
<< ". Result is " << result;
return result;
}
bool ScalableIph::CheckPhoneHubOnboardingEligible(
const base::Feature& feature) {
SCALABLE_IPH_LOG(GetLogger())
<< "Checking phone hub onboarding eligible for " << feature.name;
std::string phonehub_onboarding_eligible_value = GetParamValue(
feature, kCustomConditionPhoneHubOnboardingEligibleParamName);
if (phonehub_onboarding_eligible_value.empty()) {
SCALABLE_IPH_LOG(GetLogger())
<< "No phone hub onboarding eligible condition specified.";
return true;
}
if (phonehub_onboarding_eligible_value !=
kCustomConditionPhoneHubOnboardingEligibleValueTrue) {
SCALABLE_IPH_LOG(GetLogger())
<< "Only " << kCustomConditionPhoneHubOnboardingEligibleValueTrue
<< " is a valid value for "
<< kCustomConditionPhoneHubOnboardingEligibleParamName
<< ". Provided value: " << phonehub_onboarding_eligible_value
<< ". Condition not satisfied for a fail-safe behavior.";
return false;
}
SCALABLE_IPH_LOG(GetLogger())
<< "Expected value is "
<< kCustomConditionPhoneHubOnboardingEligibleValueTrue
<< ". Current phone hub onboarding eligible value is "
<< phonehub_onboarding_eligible_ << ". Result is "
<< phonehub_onboarding_eligible_;
return phonehub_onboarding_eligible_;
}
const std::vector<raw_ptr<const base::Feature, VectorExperimental>>&
ScalableIph::GetFeatureList() const {
if (!feature_list_for_testing_.empty()) {
return feature_list_for_testing_;
}
return GetFeatureListConstant();
}
std::ostream& operator<<(std::ostream& out, ScalableIph::Event event) {
return out << GetEventName(event);
}
} // namespace scalable_iph
|