File: chrome_shelf_controller_util.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (256 lines) | stat: -rw-r--r-- 9,631 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 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/shelf/chrome_shelf_controller_util.h"

#include <algorithm>

#include "ash/constants/ash_features.h"
#include "ash/constants/web_app_id_constants.h"
#include "ash/public/cpp/shelf_item_delegate.h"
#include "ash/public/cpp/shelf_model.h"
#include "base/containers/contains.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/apps/app_service/policy_util.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_registry_cache.h"
#include "chrome/browser/ash/app_list/app_list_controller_delegate.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/app_list/extension_app_utils.h"
#include "chrome/browser/ash/eche_app/app_id.h"
#include "chrome/browser/ash/login/demo_mode/demo_session.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_helper.h"
#include "chrome/browser/ui/ash/shelf/arc_app_shelf_id.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_item_factory.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_prefs.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/web_applications/policy/web_app_policy_manager.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/pref_names.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph.h"
#include "chromeos/ash/components/scalable_iph/scalable_iph_factory.h"
#include "components/prefs/pref_service.h"
#include "components/webapps/common/web_app_id.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"

const extensions::Extension* GetExtensionForAppID(const std::string& app_id,
                                                  Profile* profile) {
  return extensions::ExtensionRegistry::Get(profile)->GetExtensionById(
      app_id, extensions::ExtensionRegistry::EVERYTHING);
}

AppListControllerDelegate::Pinnable GetPinnableForAppID(
    const std::string& app_id,
    Profile* profile) {
  // These file manager apps have a shelf presence, but can only be launched
  // when provided a filename to open. Likewise, the feedback extension, the
  // Eche application need context when launching. Pinning these creates an
  // item that does nothing.
  const char* kNoPinAppIds[] = {
      ash::eche_app::kEcheAppId,
  };
  if (base::Contains(kNoPinAppIds, app_id)) {
    return AppListControllerDelegate::NO_PIN;
  }

  const std::optional<std::vector<std::string>> policy_ids =
      apps_util::GetPolicyIdsFromAppId(profile, app_id);

  if (!policy_ids || policy_ids->empty()) {
    return AppListControllerDelegate::PIN_EDITABLE;
  }

  if (ash::DemoSession::Get() &&
      std::ranges::none_of(*policy_ids, [](const auto& policy_id) {
        return ash::DemoSession::Get()->ShouldShowAppInShelf(policy_id);
      })) {
    return AppListControllerDelegate::PIN_EDITABLE;
  }

  const base::Value::List& policy_apps =
      profile->GetPrefs()->GetList(prefs::kPolicyPinnedLauncherApps);

  for (const base::Value& policy_dict_entry : policy_apps) {
    if (!policy_dict_entry.is_dict()) {
      return AppListControllerDelegate::PIN_EDITABLE;
    }

    const std::string* policy_entry = policy_dict_entry.GetDict().FindString(
        ChromeShelfPrefs::kPinnedAppsPrefAppIDKey);
    if (!policy_entry) {
      return AppListControllerDelegate::PIN_EDITABLE;
    }

    if (base::Contains(*policy_ids,
                       apps_util::TransformRawPolicyId(*policy_entry))) {
      return AppListControllerDelegate::PIN_FIXED;
    }
  }

  return AppListControllerDelegate::PIN_EDITABLE;
}

bool IsAppHiddenFromShelf(Profile* profile, const std::string& app_id) {
  if (!apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile)) {
    return false;
  }

  bool hidden = false;
  apps::AppServiceProxyFactory::GetForProfile(profile)
      ->AppRegistryCache()
      .ForOneApp(app_id, [&hidden](const apps::AppUpdate& update) {
        hidden = !update.ShowInShelf().value_or(true);
      });

  return hidden;
}

bool IsPromiseAppReadyToShowInShelf(Profile* profile,
                                    const std::string& promise_package_id) {
  if (!ash::features::ArePromiseIconsEnabled()) {
    return false;
  }
  CHECK(apps::AppServiceProxyFactory::IsAppServiceAvailableForProfile(profile));
  const apps::PromiseApp* promise_app =
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->PromiseAppRegistryCache()
          ->GetPromiseAppForStringPackageId(promise_package_id);
  return promise_app && promise_app->should_show.value_or(false);
}

bool IsAppPinEditable(apps::AppType app_type,
                      const std::string& app_id,
                      Profile* profile) {
  if (ash::features::ArePromiseIconsEnabled() &&
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->PromiseAppRegistryCache()
          ->GetPromiseAppForStringPackageId(app_id)) {
    return true;
  }

  if (IsAppHiddenFromShelf(profile, app_id)) {
    return false;
  }

  switch (app_type) {
    case apps::AppType::kArc: {
      const arc::ArcAppShelfId& arc_shelf_id =
          arc::ArcAppShelfId::FromString(app_id);
      DCHECK(arc_shelf_id.valid());
      const ArcAppListPrefs* arc_list_prefs = ArcAppListPrefs::Get(profile);
      DCHECK(arc_list_prefs);
      std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
          arc_list_prefs->GetApp(arc_shelf_id.app_id());
      if (!arc_shelf_id.has_shelf_group_id() && app_info &&
          app_info->launchable) {
        return true;
      }
      return false;
    }
    case apps::AppType::kPluginVm: {
      bool show_in_launcher = false;
      apps::AppServiceProxyFactory::GetForProfile(profile)
          ->AppRegistryCache()
          .ForOneApp(
              app_id, [&show_in_launcher](const apps::AppUpdate& update) {
                show_in_launcher = update.ShowInLauncher().value_or(false);
              });
      return show_in_launcher;
    }
    case apps::AppType::kCrostini:
    case apps::AppType::kBorealis:
    case apps::AppType::kChromeApp:
    case apps::AppType::kWeb:
    case apps::AppType::kSystemWeb:
      return true;
    case apps::AppType::kUnknown:
      // Type kUnknown is used for "unregistered" Crostini apps, which do not
      // have a .desktop file and can only be closed, not pinned.
      return false;
    case apps::AppType::kRemote:
    case apps::AppType::kExtension:
      NOTREACHED() << "Type " << (int)app_type
                   << " should not appear in shelf.";
    case apps::AppType::kBruschetta:
      return true;
  }
}

bool IsBrowserRepresentedInBrowserList(Browser* browser,
                                       const ash::ShelfModel* model) {
  // Only Ash desktop browser windows for the active user are represented.
  if (!browser ||
      !multi_user_util::IsProfileFromActiveUser(browser->profile())) {
    return false;
  }

  if (browser->is_type_app() || browser->is_type_app_popup()) {
    // V1 App popup windows may have their own item.
    ash::ShelfID id(web_app::GetAppIdFromApplicationName(browser->app_name()));
    if (model->ItemByID(id)) {
      return false;
    }
  }

  return true;
}

void PinAppWithIDToShelf(const std::string& app_id) {
  auto* shelf_controller = ChromeShelfController::instance();
  auto* shelf_model = shelf_controller->shelf_model();
  if (shelf_model->ItemIndexByAppID(app_id) >= 0) {
    shelf_model->PinExistingItemWithID(app_id);
  } else {
    shelf_model->AddAndPinAppWithFactoryConstructedDelegate(app_id);
  }
}

void UnpinAppWithIDFromShelf(const std::string& app_id) {
  auto* shelf_controller = ChromeShelfController::instance();
  shelf_controller->shelf_model()->UnpinAppWithID(app_id);
}

apps::LaunchSource ShelfLaunchSourceToAppsLaunchSource(
    ash::ShelfLaunchSource source) {
  switch (source) {
    case ash::LAUNCH_FROM_UNKNOWN:
      return apps::LaunchSource::kUnknown;
    case ash::LAUNCH_FROM_INTERNAL:
      return apps::LaunchSource::kFromChromeInternal;
    case ash::LAUNCH_FROM_APP_LIST:
      return apps::LaunchSource::kFromAppListGrid;
    case ash::LAUNCH_FROM_APP_LIST_SEARCH:
      return apps::LaunchSource::kFromAppListQuery;
    case ash::LAUNCH_FROM_APP_LIST_RECOMMENDATION:
      return apps::LaunchSource::kFromAppListRecommendation;
    case ash::LAUNCH_FROM_SHELF:
      return apps::LaunchSource::kFromShelf;
  }
}

void MaybeRecordAppLaunchForScalableIph(const std::string& app_id,
                                        Profile* profile,
                                        ash::ShelfLaunchSource source) {
  // Launches from app list is covered in `AppListClientImpl::ActivateItem`.
  if (source != ash::ShelfLaunchSource::LAUNCH_FROM_SHELF) {
    return;
  }

  scalable_iph::ScalableIph* scalable_iph =
      ScalableIphFactory::GetForBrowserContext(profile);
  if (!scalable_iph) {
    return;
  }

  scalable_iph->MaybeRecordShelfItemActivationById(app_id);
}