app_list_badge_controller.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2021 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_APP_LIST_APP_LIST_BADGE_CONTROLLER_H_
  5. #define ASH_APP_LIST_APP_LIST_BADGE_CONTROLLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "ash/app_list/app_list_model_provider.h"
  9. #include "ash/app_list/model/app_list_model.h"
  10. #include "ash/app_list/model/app_list_model_observer.h"
  11. #include "ash/ash_export.h"
  12. #include "ash/public/cpp/session/session_observer.h"
  13. #include "base/scoped_observation.h"
  14. #include "components/services/app_service/public/cpp/app_registry_cache.h"
  15. #include "third_party/abseil-cpp/absl/types/optional.h"
  16. class PrefChangeRegistrar;
  17. namespace ash {
  18. class AppListModel;
  19. // Handles badges on app list items (e.g. notification badges).
  20. class ASH_EXPORT AppListBadgeController
  21. : public AppListModelProvider::Observer,
  22. public AppListModelObserver,
  23. public SessionObserver,
  24. public apps::AppRegistryCache::Observer {
  25. public:
  26. AppListBadgeController();
  27. AppListBadgeController(const AppListBadgeController&) = delete;
  28. AppListBadgeController& operator=(const AppListBadgeController&) = delete;
  29. ~AppListBadgeController() override;
  30. void Shutdown();
  31. // AppListModelProvider::Observer:
  32. void OnActiveAppListModelsChanged(AppListModel* model,
  33. SearchModel* search_model) override;
  34. // AppListModelObserver:
  35. void OnAppListItemAdded(AppListItem* item) override;
  36. // SessionObserver:
  37. void OnActiveUserPrefServiceChanged(PrefService* pref_service) override;
  38. // apps::AppRegistryCache::Observer:
  39. void OnAppUpdate(const apps::AppUpdate& update) override;
  40. void OnAppRegistryCacheWillBeDestroyed(
  41. apps::AppRegistryCache* cache) override;
  42. private:
  43. // Updates whether a notification badge is shown for the AppListItemView
  44. // corresponding with the |app_id|.
  45. void UpdateItemNotificationBadge(const std::string& app_id, bool has_badge);
  46. // Checks the notification badging pref and then updates whether a
  47. // notification badge is shown for each AppListItem.
  48. void UpdateAppNotificationBadging();
  49. // Sets the active AppListModel and observes it for changes.
  50. void SetActiveModel(AppListModel* model);
  51. AppListModel* model_ = nullptr;
  52. // Observed to update notification badging on app list items. Also used to get
  53. // initial notification badge information when app list items are added.
  54. apps::AppRegistryCache* cache_ = nullptr;
  55. // Observes user profile prefs for the app list.
  56. std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
  57. // Whether the pref for notification badging is enabled.
  58. absl::optional<bool> notification_badging_pref_enabled_;
  59. base::ScopedObservation<AppListModel, AppListModelObserver>
  60. model_observation_{this};
  61. };
  62. } // namespace ash
  63. #endif // ASH_APP_LIST_APP_LIST_BADGE_CONTROLLER_H_