File: app_access_notifier.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; 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,811; 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 (338 lines) | stat: -rw-r--r-- 12,458 bytes parent folder | download | duplicates (5)
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
// 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 "chrome/browser/ui/ash/app_access/app_access_notifier.h"

#include <optional>
#include <string>
#include <vector>

#include "app_access_notifier.h"
#include "ash/constants/ash_features.h"
#include "ash/system/privacy/privacy_indicators_controller.h"
#include "ash/system/privacy_hub/camera_privacy_switch_controller.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/trace_event/trace_event.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "components/account_id/account_id.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache_wrapper.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/session_manager/core/session_manager.h"
#include "components/session_manager/session_manager_types.h"

namespace {

apps::AppCapabilityAccessCache* GetAppCapabilityAccessCache(
    AccountId account_id) {
  return apps::AppCapabilityAccessCacheWrapper::Get()
      .GetAppCapabilityAccessCache(account_id);
}

std::optional<std::u16string> MapAppIdToShortName(
    std::string app_id,
    apps::AppCapabilityAccessCache* capability_cache,
    apps::AppRegistryCache* registry_cache,
    const std::set<std::string>& apps_accessing_sensor) {
  DCHECK(capability_cache);
  DCHECK(registry_cache);

  for (const std::string& app : apps_accessing_sensor) {
    std::optional<std::u16string> name;
    registry_cache->ForOneApp(app,
                              [&app_id, &name](const apps::AppUpdate& update) {
                                if (update.AppId() == app_id) {
                                  name = base::UTF8ToUTF16(update.ShortName());
                                }
                              });
    if (name.has_value()) {
      return name;
    }
  }

  return std::nullopt;
}

// A helper to send `ash::CameraPrivacySwitchController` a notification when an
// application starts or stops using the camera. `application_added` is true
// when the application starts using the camera and false when the application
// stops using the camera.
void SendActiveCameraApplicationsChangedNotification(bool application_added) {
  auto* camera_controller = ash::CameraPrivacySwitchController::Get();
  CHECK(camera_controller);
  camera_controller->ActiveApplicationsChanged(application_added);
}

}  // namespace

AppAccessNotifier::AppAccessNotifier() {
  // These checks are needed for testing, where SessionManager and/or
  // UserManager may not exist.
  session_manager::SessionManager* sm = session_manager::SessionManager::Get();
  if (sm) {
    session_manager_observation_.Observe(sm);
  }
  user_manager::UserManager* um = user_manager::UserManager::Get();
  if (um) {
    user_session_state_observation_.Observe(um);
  }

  CheckActiveUserChanged();
}

AppAccessNotifier::~AppAccessNotifier() = default;

// Returns names of apps accessing camera.
std::vector<std::u16string> AppAccessNotifier::GetAppsAccessingCamera() {
  return GetAppsAccessingSensor(
      &camera_using_app_ids_[active_user_account_id_],
      base::BindOnce([](apps::AppCapabilityAccessCache& cache) {
        return cache.GetAppsAccessingCamera();
      }));
}
// Returns names of apps accessing microphone.
std::vector<std::u16string> AppAccessNotifier::GetAppsAccessingMicrophone() {
  return GetAppsAccessingSensor(
      &mic_using_app_ids_[active_user_account_id_],
      base::BindOnce([](apps::AppCapabilityAccessCache& cache) {
        return cache.GetAppsAccessingMicrophone();
      }));
}

std::vector<std::u16string> AppAccessNotifier::GetAppsAccessingSensor(
    const MruAppIdList* app_id_list,
    base::OnceCallback<std::set<std::string>(apps::AppCapabilityAccessCache&)>
        app_getter) {
  apps::AppRegistryCache* reg_cache = GetActiveUserAppRegistryCache();

  apps::AppCapabilityAccessCache* cap_cache =
      GetActiveUserAppCapabilityAccessCache();

  // A reg_cache and/or cap_cache of value nullptr is possible if we have no
  // active user, e.g. the login screen, so we test and return  empty list in
  // that case instead of using DCHECK().
  if (!reg_cache || !cap_cache || app_id_list->empty()) {
    return {};
  }

  const std::set<std::string>& apps_accessing_sensor =
      std::move(app_getter).Run(*cap_cache);

  std::vector<std::u16string> app_names;
  for (const auto& app_id : *app_id_list) {
    std::optional<std::u16string> app_name = MapAppIdToShortName(
        app_id, cap_cache, reg_cache, apps_accessing_sensor);
    if (app_name.has_value()) {
      app_names.push_back(app_name.value());
    }
  }
  return app_names;
}

bool AppAccessNotifier::MapContainsAppId(const MruAppIdMap& id_map,
                                         const std::string& app_id) {
  auto it = id_map.find(active_user_account_id_);
  if (it == id_map.end()) {
    return false;
  }
  return base::Contains(it->second, app_id);
}

void AppAccessNotifier::OnCapabilityAccessUpdate(
    const apps::CapabilityAccessUpdate& update) {
  auto app_id = update.AppId();

  const bool is_camera_used = update.Camera().value_or(false);
  const bool is_microphone_used = update.Microphone().value_or(false);

  // TODO(b/261444378): Avoid calculating the booleans and use update.*Changed()
  const bool was_using_camera_already =
      MapContainsAppId(camera_using_app_ids_, app_id);
  const bool was_using_microphone_already =
      MapContainsAppId(mic_using_app_ids_, app_id);

  if (is_camera_used && !was_using_camera_already) {
    // App with id `app_id` started using camera.
    camera_using_app_ids_[active_user_account_id_].push_front(update.AppId());
    SendActiveCameraApplicationsChangedNotification(/*application_added=*/true);
  } else if (!is_camera_used && was_using_camera_already) {
    // App with id `app_id` stopped using camera.
    std::erase(camera_using_app_ids_[active_user_account_id_], update.AppId());
    SendActiveCameraApplicationsChangedNotification(
        /*application_added=*/false);
  }

  if (is_microphone_used && !was_using_microphone_already) {
    // App with id `app_id` started using microphone.
    mic_using_app_ids_[active_user_account_id_].push_front(update.AppId());
  } else if (!is_microphone_used && was_using_microphone_already) {
    // App with id `app_id` stopped using microphone.
    std::erase(mic_using_app_ids_[active_user_account_id_], update.AppId());
  }

  // Privacy indicators is only enabled when Video Conference is disabled.
  if (!ash::features::IsVideoConferenceEnabled()) {
    auto* registry_cache = GetActiveUserAppRegistryCache();
    if (!registry_cache) {
      return;
    }

    auto app_type = registry_cache->GetAppType(app_id);
    std::optional<base::RepeatingClosure> launch_settings_callback;
    if (app_type == apps::AppType::kSystemWeb) {
      // We don't have the capability to launch privacy settings for system web
      // app, so we will disable the settings button for this type of app.
      launch_settings_callback = std::nullopt;
    } else {
      launch_settings_callback =
          base::BindRepeating(&AppAccessNotifier::LaunchAppSettings, app_id);
    }

    ash::PrivacyIndicatorsController::Get()->UpdatePrivacyIndicators(
        app_id, /*app_name=*/GetAppShortNameFromAppId(app_id), is_camera_used,
        is_microphone_used, /*delegate=*/
        base::MakeRefCounted<ash::PrivacyIndicatorsNotificationDelegate>(
            launch_settings_callback),
        ash::PrivacyIndicatorsSource::kApps);

    base::UmaHistogramEnumeration("Ash.PrivacyIndicators.AppAccessUpdate.Type",
                                  registry_cache->GetAppType(app_id));
  }
}

void AppAccessNotifier::OnAppCapabilityAccessCacheWillBeDestroyed(
    apps::AppCapabilityAccessCache* cache) {
  app_capability_access_cache_observation_.Reset();
}

//
// A couple of notes on why we have OnSessionStateChanged() and
// ActiveUserChanged(), i.e. why we observe both SessionManager and UserManager.
//
// The critical logic here is based on knowing when an app starts or stops
// attempting to use the microphone, and for this we observe the active user's
// AppCapabilityAccessCache.  When the active user's AppCapabilityAccessCache
// changes, we need to stop observing any AppCapabilityAccessCache we were
// previously observing and start observing the currently active one.  This is
// the job of CheckActiveUserChanged().
//

void AppAccessNotifier::OnSessionStateChanged() {
  TRACE_EVENT0("ui", "AppAccessNotifier::OnSessionStateChanged");
  session_manager::SessionState state =
      session_manager::SessionManager::Get()->session_state();
  if (state == session_manager::SessionState::ACTIVE) {
    CheckActiveUserChanged();
    session_manager_observation_.Reset();
  }
}

void AppAccessNotifier::ActiveUserChanged(user_manager::User* active_user) {
  CheckActiveUserChanged();
}

// static
std::optional<std::u16string> AppAccessNotifier::GetAppShortNameFromAppId(
    std::string app_id) {
  std::optional<std::u16string> name;
  auto* registry_cache = GetActiveUserAppRegistryCache();
  if (!registry_cache) {
    return name;
  }

  registry_cache->ForEachApp([&app_id, &name](const apps::AppUpdate& update) {
    if (update.AppId() == app_id) {
      name = base::UTF8ToUTF16(update.ShortName());
    }
  });
  return name;
}

// static
void AppAccessNotifier::LaunchAppSettings(const std::string& app_id) {
  Profile* profile = ProfileManager::GetActiveUserProfile();
  if (!profile ||
      !apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return;
  }

  auto* registry_cache = GetActiveUserAppRegistryCache();
  if (!registry_cache) {
    return;
  }

  auto app_type = registry_cache->GetAppType(app_id);

  // We don't have the capability to launch privacy settings for system web
  // app, so settings button is disabled for this type of app.
  DCHECK(app_type != apps::AppType::kSystemWeb);

  if (app_type == apps::AppType::kWeb) {
    chrome::ShowAppManagementPage(profile, app_id,
                                  ash::settings::AppManagementEntryPoint::
                                      kPrivacyIndicatorsNotificationSettings);
  } else {
    apps::AppServiceProxyFactory::GetForProfile(profile)->OpenNativeSettings(
        app_id);
  }

  base::UmaHistogramEnumeration("Ash.PrivacyIndicators.LaunchSettings",
                                registry_cache->GetAppType(app_id));
}

AccountId AppAccessNotifier::GetActiveUserAccountId() {
  auto* manager = user_manager::UserManager::Get();
  const user_manager::User* active_user = manager->GetActiveUser();
  if (!active_user) {
    return EmptyAccountId();
  }

  return active_user->GetAccountId();
}

void AppAccessNotifier::CheckActiveUserChanged() {
  AccountId id = GetActiveUserAccountId();
  if (id == EmptyAccountId() || id == active_user_account_id_) {
    return;
  }

  if (active_user_account_id_ != EmptyAccountId()) {
    app_capability_access_cache_observation_.Reset();
    active_user_account_id_ = EmptyAccountId();
  }

  apps::AppCapabilityAccessCache* cap_cache = GetAppCapabilityAccessCache(id);
  if (cap_cache) {
    app_capability_access_cache_observation_.Observe(cap_cache);
    active_user_account_id_ = id;
  }
}

// static
apps::AppRegistryCache* AppAccessNotifier::GetActiveUserAppRegistryCache() {
  Profile* profile = ProfileManager::GetActiveUserProfile();
  if (!profile ||
      !apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return nullptr;
  }

  apps::AppServiceProxy* proxy =
      apps::AppServiceProxyFactory::GetForProfile(profile);
  return &proxy->AppRegistryCache();
}

apps::AppCapabilityAccessCache*
AppAccessNotifier::GetActiveUserAppCapabilityAccessCache() {
  return apps::AppCapabilityAccessCacheWrapper::Get()
      .GetAppCapabilityAccessCache(GetActiveUserAccountId());
}