snooping_protection_notification_blocker_internal.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_NOTIFICATION_BLOCKER_INTERNAL_H_
  5. #define ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_NOTIFICATION_BLOCKER_INTERNAL_H_
  6. // Logic exposed only for testing on which clients of the notification blocker
  7. // cannot rely.
  8. #include <string>
  9. #include "ash/strings/grit/ash_strings.h"
  10. #include "components/account_id/account_id.h"
  11. #include "components/services/app_service/public/cpp/app_registry_cache.h"
  12. #include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"
  13. #include "ui/base/l10n/l10n_util.h"
  14. #include "ui/message_center/public/cpp/notification_types.h"
  15. #include "ui/message_center/public/cpp/notifier_id.h"
  16. namespace ash {
  17. namespace hps_internal {
  18. // Returns a human-readable title for the given notification source. Improper
  19. // nouns are always returned lower-case and must be subsequently capitalized if
  20. // necessary.
  21. //
  22. // Takes a template argument to allow injection of a duck-typed fake app
  23. // registry cache.
  24. template <typename AppRegistryCacheWrapperType>
  25. std::u16string GetNotifierTitle(const message_center::NotifierId& id,
  26. const AccountId& account_id) {
  27. std::u16string title = l10n_util::GetStringUTF16(
  28. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_SYSTEM_TITLE_LOWER);
  29. // Assign default title based on notifier type.
  30. switch (id.type) {
  31. case message_center::NotifierType::APPLICATION:
  32. title = l10n_util::GetStringUTF16(
  33. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_APP_TITLE_LOWER);
  34. break;
  35. case message_center::NotifierType::ARC_APPLICATION:
  36. title = l10n_util::GetStringUTF16(
  37. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_ARC_TITLE);
  38. break;
  39. case message_center::NotifierType::CROSTINI_APPLICATION:
  40. title = l10n_util::GetStringUTF16(
  41. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_CROSTINI_TITLE);
  42. break;
  43. case message_center::NotifierType::WEB_PAGE:
  44. title = id.title.value_or(l10n_util::GetStringUTF16(
  45. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_WEB_TITLE_LOWER));
  46. break;
  47. case message_center::NotifierType::SYSTEM_COMPONENT:
  48. // Handled by initial value.
  49. break;
  50. case message_center::NotifierType::PHONE_HUB:
  51. title = l10n_util::GetStringUTF16(
  52. IDS_ASH_SMART_PRIVACY_SNOOPING_NOTIFICATION_PHONE_HUB_TITLE);
  53. break;
  54. }
  55. // If we can access a more-specific app title, assign it here.
  56. if (id.type == message_center::NotifierType::APPLICATION ||
  57. id.type == message_center::NotifierType::ARC_APPLICATION ||
  58. id.type == message_center::NotifierType::CROSTINI_APPLICATION) {
  59. // Access the registry of human-readable app names.
  60. auto* app_cache =
  61. AppRegistryCacheWrapperType::Get().GetAppRegistryCache(account_id);
  62. if (!app_cache) {
  63. LOG(ERROR) << "Couldn't find app registry cache for user";
  64. return title;
  65. }
  66. const bool found =
  67. app_cache->ForOneApp(id.id, [&](const apps::AppUpdate& update) {
  68. const std::string& short_name = update.ShortName();
  69. title = std::u16string(short_name.begin(), short_name.end());
  70. });
  71. if (!found)
  72. LOG(WARNING) << "No matching notifier found for ID " << id.id;
  73. }
  74. return title;
  75. }
  76. std::u16string ASH_EXPORT
  77. GetTitlesBlockedMessage(const std::vector<std::u16string>& titles);
  78. } // namespace hps_internal
  79. } // namespace ash
  80. #endif // ASH_SYSTEM_HUMAN_PRESENCE_SNOOPING_PROTECTION_NOTIFICATION_BLOCKER_INTERNAL_H_