File: preinstalled_web_apps.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 (267 lines) | stat: -rw-r--r-- 10,233 bytes parent folder | download | duplicates (4)
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
// Copyright 2020 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/web_applications/preinstalled_web_apps/preinstalled_web_apps.h"

#include <optional>
#include <string>
#include <utility>

#include "base/check_op.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "build/buildflag.h"
#include "chrome/browser/web_applications/preinstalled_app_install_features.h"
#include "chrome/common/chrome_switches.h"

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#include "chrome/browser/web_applications/preinstalled_web_apps/gmail.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_chat.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_docs.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_drive.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_sheets.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_slides.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/youtube.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/web_app_id_constants.h"
#include "base/feature_list.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/calculator.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/gemini.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_calendar.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/google_meet.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/messages_dogfood.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/notebook_lm.h"
#include "chrome/common/extensions/extension_constants.h"
#include "extensions/common/constants.h"
#include "google_apis/gaia/gaia_auth_util.h"
#endif  // BUILDFLAG(IS_CHROMEOS)

#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

namespace web_app {
namespace {

std::vector<ExternalInstallOptions>* g_preinstalled_app_data_for_testing =
    nullptr;

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)

#if !BUILDFLAG(IS_CHROMEOS)
BASE_FEATURE(kChatPreinstalledWebApp,
             "ChatPreinstalledWebApp",
             base::FEATURE_ENABLED_BY_DEFAULT);

BASE_FEATURE_PARAM(bool,
                   kOnlyForNewUsers,
                   &kChatPreinstalledWebApp,
                   "only_for_new_users",
                   false);
#endif

#if BUILDFLAG(IS_CHROMEOS)
bool IsGoogleInternalAccount() {
  Profile* profile = ProfileManager::GetActiveUserProfile();
  if (!profile)
    return false;
  return gaia::IsGoogleInternalAccountEmail(profile->GetProfileUserName());
}
#endif  // BUILDFLAG(IS_CHROMEOS)


std::vector<ExternalInstallOptions> GetChromeBrandedApps(
    Profile& profile,
    const std::optional<DeviceInfo>& device_info) {
  bool is_standalone_tabbed =
      IsPreinstalledDocsSheetsSlidesDriveStandaloneTabbed(profile);
  // TODO(crbug.com/40705277): Replace these C++ configs with JSON configs like
  // those seen in: chrome/test/data/web_app_default_apps/good_json
  // This requires:
  // - Mimicking the directory packaging used by
  //   chrome/browser/resources/default_apps.
  // - Hooking up a second JSON config load to PreinstalledWebAppManager.
  // - Validating everything works on all OSs (Mac bundles things differently).
  // - Ensure that these resources are correctly installed by our Chrome
  //   installers on every desktop platform.
  std::vector<ExternalInstallOptions> apps = {
      GetConfigForGmail(),
      GetConfigForGoogleDocs(is_standalone_tabbed),
      GetConfigForGoogleDrive(/*is_standalone=*/is_standalone_tabbed),
      GetConfigForGoogleSheets(is_standalone_tabbed),
      GetConfigForGoogleSlides(is_standalone_tabbed),
      GetConfigForYouTube(),
#if BUILDFLAG(IS_CHROMEOS)
      GetConfigForCalculator(),
      GetConfigForGemini(device_info),
      GetConfigForNotebookLm(),
      GetConfigForGoogleCalendar(),
      GetConfigForGoogleChat(/*is_standalone=*/true,
                             /*only_for_new_users=*/false),
      GetConfigForGoogleMeet(),
#endif  // BUILDFLAG(IS_CHROMEOS)
  };

#if !BUILDFLAG(IS_CHROMEOS)
  if (base::FeatureList::IsEnabled(kChatPreinstalledWebApp)) {
    apps.insert(apps.end(), GetConfigForGoogleChat(
                                /*is_standalone=*/false,
                                /*only_for_new_users=*/kOnlyForNewUsers.Get()));
  }
#endif  // !BUILDFLAG(IS_CHROMEOS)

  return apps;
}
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)

}  // namespace

DeviceInfo::DeviceInfo() = default;

DeviceInfo::DeviceInfo(const DeviceInfo&) = default;

DeviceInfo::DeviceInfo(DeviceInfo&&) = default;

DeviceInfo& DeviceInfo::operator=(const DeviceInfo&) = default;

DeviceInfo& DeviceInfo::operator=(DeviceInfo&&) = default;

DeviceInfo::~DeviceInfo() = default;

PreinstallUrlAllowList& GetPreinstallUrlAllowListForTesting() {
  static base::NoDestructor<PreinstallUrlAllowList> preinstall_url_allow_list;
  return *preinstall_url_allow_list;
}

ScopedPreinstallUrlAllowList SetPreinstallUrlAllowListForTesting(
    PreinstallUrlAllowList preinstall_url_allow_list) {
  return {&GetPreinstallUrlAllowListForTesting(),
          std::move(preinstall_url_allow_list)};
}

bool PreinstalledWebAppsDisabled() {
  return base::CommandLine::ForCurrentProcess()->HasSwitch(
             ::switches::kDisableDefaultApps) &&
         !GetPreinstallUrlAllowListForTesting().has_value();
}

std::vector<ExternalInstallOptions> GetPreinstalledWebApps(
    Profile& profile,
    const std::optional<DeviceInfo>& device_info) {
  if (g_preinstalled_app_data_for_testing)
    return *g_preinstalled_app_data_for_testing;

  if (PreinstalledWebAppsDisabled())
    return {};

#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if BUILDFLAG(IS_CHROMEOS)
  // TODO(crbug.com/40854011): replace with config in admin console.
  if (IsGoogleInternalAccount()) {
    std::vector<ExternalInstallOptions> apps =
        GetChromeBrandedApps(profile, device_info);
    apps.push_back(GetConfigForMessagesDogfood());
    return apps;
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  return GetChromeBrandedApps(profile, device_info);
#else
  return {};
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING)
}

ScopedTestingPreinstalledAppData::ScopedTestingPreinstalledAppData() {
  DCHECK_EQ(nullptr, g_preinstalled_app_data_for_testing);
  g_preinstalled_app_data_for_testing = &apps;
}

ScopedTestingPreinstalledAppData::~ScopedTestingPreinstalledAppData() {
  DCHECK_EQ(&apps, g_preinstalled_app_data_for_testing);
  g_preinstalled_app_data_for_testing = nullptr;
}

PreinstalledWebAppMigration::PreinstalledWebAppMigration() = default;
PreinstalledWebAppMigration::PreinstalledWebAppMigration(
    PreinstalledWebAppMigration&&) noexcept = default;
PreinstalledWebAppMigration::~PreinstalledWebAppMigration() = default;

std::vector<PreinstalledWebAppMigration> GetPreinstalledWebAppMigrations(
    Profile& profile) {
  std::vector<PreinstalledWebAppMigration> migrations;
  for (const ExternalInstallOptions& options :
       GetPreinstalledWebApps(profile)) {
    if (!options.expected_app_id)
      continue;

    // All entries in the default web app migration had only one Chrome app to
    // replace.
    if (options.uninstall_and_replace.size() != 1)
      continue;

    if (options.gate_on_feature &&
        !IsPreinstalledAppInstallFeatureEnabled(*options.gate_on_feature)) {
      continue;
    }

    PreinstalledWebAppMigration migration;
    migration.install_url = options.install_url;
    migration.expected_web_app_id = *options.expected_app_id;
    migration.old_chrome_app_id = options.uninstall_and_replace[0];
    migrations.push_back(std::move(migration));
  }

#if BUILDFLAG(GOOGLE_CHROME_BRANDING) && BUILDFLAG(IS_CHROMEOS)
  if (!g_preinstalled_app_data_for_testing && !PreinstalledWebAppsDisabled()) {
    // Manually hard coded entries from
    // https://chrome-internal.googlesource.com/chromeos/overlays/chromeos-overlay/+/main/chromeos-base/chromeos-default-apps/files/web_apps
    // for any json configs that include a uninstall_and_replace field.
    // This is a temporary measure while the default web app duplication
    // issue is cleaned up.
    // TODO(crbug.com/40818306): Clean up once no longer needed.
    // Default installed GSuite web apps.
    {
      PreinstalledWebAppMigration keep_migration;
      keep_migration.install_url =
          GURL("https://keep.google.com/installwebapp?usp=chrome_default");
      keep_migration.expected_web_app_id = ash::kGoogleKeepAppId;
      keep_migration.old_chrome_app_id = extension_misc::kGoogleKeepAppId;
      migrations.push_back(std::move(keep_migration));
    }

    // Default installed non-GSuite web apps.
    {
      PreinstalledWebAppMigration books_migration;
      books_migration.install_url =
          GURL("https://play.google.com/books/installwebapp?usp=chromedefault");
      books_migration.expected_web_app_id = ash::kPlayBooksAppId;
      books_migration.old_chrome_app_id = extension_misc::kGooglePlayBooksAppId;
      migrations.push_back(std::move(books_migration));

      PreinstalledWebAppMigration maps_migration;
      maps_migration.install_url =
          GURL("https://www.google.com/maps/preview/pwa/ttinstall.html");
      maps_migration.expected_web_app_id = ash::kGoogleMapsAppId;
      maps_migration.old_chrome_app_id = extension_misc::kGoogleMapsAppId;
      migrations.push_back(std::move(maps_migration));

      PreinstalledWebAppMigration movies_migration;
      movies_migration.install_url = GURL(
          "https://play.google.com/store/movies/"
          "installwebapp?usp=chrome_default");
      movies_migration.expected_web_app_id = ash::kGoogleMoviesAppId;
      movies_migration.old_chrome_app_id =
          extension_misc::kGooglePlayMoviesAppId;
      migrations.push_back(std::move(movies_migration));
    }
  }
#endif  // BUILDFLAG(GOOGLE_CHROME_BRANDING) && BUILDFLAG(IS_CHROMEOS)
  return migrations;
}

}  // namespace web_app