File: app_list_badge_controller.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (137 lines) | stat: -rw-r--r-- 4,532 bytes parent folder | download | duplicates (7)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/app_list/app_list_badge_controller.h"

#include <memory>
#include <string>

#include "ash/app_list/app_list_model_provider.h"
#include "ash/app_list/model/app_list_item.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "base/functional/bind.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache_wrapper.h"

namespace ash {

AppListBadgeController::AppListBadgeController() {
  Shell::Get()->session_controller()->AddObserver(this);

  auto* model_provider = AppListModelProvider::Get();
  DCHECK(model_provider);
  model_provider->AddObserver(this);
  SetActiveModel(model_provider->model());
}

AppListBadgeController::~AppListBadgeController() = default;

void AppListBadgeController::Shutdown() {
  AppListModelProvider::Get()->RemoveObserver(this);
  Shell::Get()->session_controller()->RemoveObserver(this);
  model_observation_.Reset();
}

void AppListBadgeController::OnActiveAppListModelsChanged(
    AppListModel* model,
    SearchModel* search_model) {
  SetActiveModel(model);
}

void AppListBadgeController::OnAppListItemAdded(AppListItem* item) {
  if (cache_ && notification_badging_pref_enabled_.value_or(false)) {
    // Update the notification badge indicator for the newly added app list
    // item.
    cache_->ForOneApp(item->id(), [item](const apps::AppUpdate& update) {
      item->UpdateNotificationBadge(update.HasBadge().value_or(false));
    });
  }
}

void AppListBadgeController::OnActiveUserPrefServiceChanged(
    PrefService* pref_service) {
  pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
  pref_change_registrar_->Init(pref_service);

  pref_change_registrar_->Add(
      prefs::kAppNotificationBadgingEnabled,
      base::BindRepeating(&AppListBadgeController::UpdateAppNotificationBadging,
                          base::Unretained(this)));

  // Observe AppRegistryCache for the current active account to get
  // notification updates.
  AccountId account_id =
      Shell::Get()->session_controller()->GetActiveAccountId();
  cache_ = apps::AppRegistryCacheWrapper::Get().GetAppRegistryCache(account_id);

  app_registry_cache_observer_.Reset();
  if (cache_) {
    app_registry_cache_observer_.Observe(cache_);
  }

  // Resetting the recorded pref forces the next call to
  // UpdateAppNotificationBadging() to update notification badging for every
  // app item.
  notification_badging_pref_enabled_.reset();
  UpdateAppNotificationBadging();
}

void AppListBadgeController::OnAppUpdate(const apps::AppUpdate& update) {
  if (update.HasBadgeChanged() &&
      notification_badging_pref_enabled_.value_or(false)) {
    UpdateItemNotificationBadge(update.AppId(),
                                update.HasBadge().value_or(false));
  }
}

void AppListBadgeController::OnAppRegistryCacheWillBeDestroyed(
    apps::AppRegistryCache* cache) {
  app_registry_cache_observer_.Reset();
}

void AppListBadgeController::UpdateItemNotificationBadge(
    const std::string& app_id,
    bool has_badge) {
  if (!model_)
    return;
  AppListItem* item = model_->FindItem(app_id);
  if (!item)
    return;
  item->UpdateNotificationBadge(has_badge);
}

void AppListBadgeController::UpdateAppNotificationBadging() {
  bool new_badging_enabled = pref_change_registrar_
                                 ? pref_change_registrar_->prefs()->GetBoolean(
                                       prefs::kAppNotificationBadgingEnabled)
                                 : false;

  if (notification_badging_pref_enabled_.has_value() &&
      notification_badging_pref_enabled_.value() == new_badging_enabled) {
    return;
  }
  notification_badging_pref_enabled_ = new_badging_enabled;

  if (cache_) {
    cache_->ForEachApp([this](const apps::AppUpdate& update) {
      // Set the app notification badge hidden when the pref is disabled.
      bool has_badge = notification_badging_pref_enabled_.value() &&
                       (update.HasBadge().value_or(false));
      UpdateItemNotificationBadge(update.AppId(), has_badge);
    });
  }
}

void AppListBadgeController::SetActiveModel(AppListModel* model) {
  model_ = model;
  model_observation_.Reset();

  if (model_)
    model_observation_.Observe(model_.get());
}

}  // namespace ash