File: shortcut_sub_manager.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 (360 lines) | stat: -rw-r--r-- 14,406 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright 2022 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/os_integration/shortcut_sub_manager.h"

#include <string>
#include <utility>

#include "base/containers/flat_map.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/os_integration/os_integration_test_override.h"
#include "chrome/browser/web_applications/os_integration/web_app_shortcut.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/web_app_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/time.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/web_applications/os_integration/mac/app_shim_registry.h"
#endif

namespace web_app {
namespace {

// Result of shortcuts creation process.
// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class CreationResult {
  kSuccess = 0,
  kFailToCreateShortcut = 1,
  kMaxValue = kFailToCreateShortcut
};

}  // namespace

ShortcutSubManager::ShortcutSubManager(Profile& profile,
                                       WebAppProvider& provider)
    : profile_(profile), provider_(provider) {}

ShortcutSubManager::~ShortcutSubManager() = default;

void ShortcutSubManager::Configure(
    const webapps::AppId& app_id,
    proto::os_state::WebAppOsIntegration& desired_state,
    base::OnceClosure configure_done) {
  DCHECK(!desired_state.has_shortcut());

  desired_state.clear_shortcut();

  if (provider_->registrar_unsafe().GetInstallState(app_id) !=
      proto::INSTALLED_WITH_OS_INTEGRATION) {
    std::move(configure_done).Run();
    return;
  }

  auto* shortcut = desired_state.mutable_shortcut();
  shortcut->set_title(provider_->registrar_unsafe().GetAppShortName(app_id));
  shortcut->set_description(
      provider_->registrar_unsafe().GetAppDescription(app_id));
  provider_->icon_manager().ReadIconsLastUpdateTime(
      app_id, base::BindOnce(&ShortcutSubManager::StoreIconDataFromDisk,
                             weak_ptr_factory_.GetWeakPtr(), shortcut)
                  .Then(std::move(configure_done)));
}

void ShortcutSubManager::Execute(
    const webapps::AppId& app_id,
    const std::optional<SynchronizeOsOptions>& synchronize_options,
    const proto::os_state::WebAppOsIntegration& desired_state,
    const proto::os_state::WebAppOsIntegration& current_state,
    base::OnceClosure callback) {
  base::FilePath shortcut_data_dir = GetOsIntegrationResourcesDirectoryForApp(
      profile_->GetPath(), app_id,
      provider_->registrar_unsafe().GetAppStartUrl(app_id));

  const WebApp* app = provider_->registrar_unsafe().GetAppById(app_id);
  DCHECK(app);

  const bool force_update_shortcuts =
      synchronize_options.has_value() &&
      synchronize_options.value().force_update_shortcuts;

  bool force_create_shortcuts =
      synchronize_options.has_value() &&
      synchronize_options.value().force_create_shortcuts;

  // First, handle the case where both current & desired state don't have
  // shortcuts, which should be a no-op.
  if (!desired_state.has_shortcut() && !current_state.has_shortcut()) {
    std::move(callback).Run();
    return;
  }

  CHECK_OS_INTEGRATION_ALLOWED();

#if BUILDFLAG(IS_MAC)
  // On Mac, sometimes the AppShimRegistry and the `current_state` get out of
  // sync. If so, force the shortcut creation.
  force_create_shortcuts |= current_state.has_shortcut() &&
                            !AppShimRegistry::Get()->IsAppInstalledInProfile(
                                app_id, profile_->GetPath());
#endif

  // Second, handle shortcut creation if either one of the following conditions
  // match:
  // 1. current_state is empty but desired_state has shortcut information.
  // 2. desired_state has value and force_create_shortcuts is set in the
  // synchronize_options. This is necessary for use-cases where the user might
  // have deleted shortcuts manually but the current_state has not been updated
  // to show that.
  if ((desired_state.has_shortcut() && !current_state.has_shortcut()) ||
      (desired_state.has_shortcut() && force_create_shortcuts)) {
#if BUILDFLAG(IS_MAC)
    AppShimRegistry::Get()->OnAppInstalledForProfile(app_id,
                                                     profile_->GetPath());
#endif

    std::unique_ptr<ShortcutInfo> desired_shortcut_info =
        BuildShortcutInfoWithoutFavicon(
            app_id, provider_->registrar_unsafe().GetAppStartUrl(app_id),
            profile_->GetPath(),
            profile_->GetPrefs()->GetString(prefs::kProfileName),
            desired_state);
    PopulateFaviconForShortcutInfo(
        app, provider_->icon_manager(), std::move(desired_shortcut_info),
        base::BindOnce(&ShortcutSubManager::CreateShortcut,
                       weak_ptr_factory_.GetWeakPtr(), app_id,
                       synchronize_options, std::move(callback)));
    return;
  }

  // Third, handle shortcut removal.
  if (!desired_state.has_shortcut() && current_state.has_shortcut()) {
    std::unique_ptr<ShortcutInfo> current_shortcut_info =
        BuildShortcutInfoWithoutFavicon(
            app_id, provider_->registrar_unsafe().GetAppStartUrl(app_id),
            profile_->GetPath(),
            profile_->GetPrefs()->GetString(prefs::kProfileName),
            current_state);

    internals::ScheduleDeletePlatformShortcuts(
        shortcut_data_dir, std::move(current_shortcut_info),
        base::BindOnce(&ShortcutSubManager::OnShortcutsDeleted,
                       weak_ptr_factory_.GetWeakPtr(), app_id,
                       std::move(callback)));
    return;
  }

  // Fourth, handle update.
  std::unique_ptr<ShortcutInfo> desired_shortcut_info =
      BuildShortcutInfoWithoutFavicon(
          app_id, provider_->registrar_unsafe().GetAppStartUrl(app_id),
          profile_->GetPath(),
          profile_->GetPrefs()->GetString(prefs::kProfileName), desired_state);

  // The following section decides if an update needs to occur or not. To
  // optimize for the least number of serializations, the 'update' callback is
  // created first and then used below.

  // If no update is detected, we still need to call the `callback` argument, so
  // split the callback to allow us to pass ownership of one to the 'update'
  // part.
  auto [callback_for_update, callback_for_no_update] =
      base::SplitOnceCallback(std::move(callback));

  DCHECK(desired_state.has_shortcut());
  DCHECK(current_state.has_shortcut());

  // Note: This callback is either called immediately (and synchronously), or
  // not at all. This is why the usage of `std::ref` and `app` is safe.
  auto do_update = base::BindOnce(
      &PopulateFaviconForShortcutInfo, app, std::ref(provider_->icon_manager()),
      std::move(desired_shortcut_info),
      base::BindOnce(&ShortcutSubManager::UpdateShortcut,
                     weak_ptr_factory_.GetWeakPtr(), app_id,
                     synchronize_options,
                     base::UTF8ToUTF16(current_state.shortcut().title()),
                     std::move(callback_for_update)));

  // Shortcut update detection.
  std::string desired, current;
  desired = desired_state.shortcut().SerializeAsString();
  current = current_state.shortcut().SerializeAsString();
  if (desired != current || force_update_shortcuts) {
    std::move(do_update).Run();
    return;
  }

#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
  // Protocol handler update detection. Shortcuts need to be updated in this
  // case on Linux & Mac because the shortcut itself includes the protocol
  // handling metadata.
  if (desired_state.has_protocols_handled() !=
      current_state.has_protocols_handled()) {
    std::move(do_update).Run();
    return;
  }
  if (desired_state.has_protocols_handled() &&
      current_state.has_protocols_handled()) {
    desired = desired_state.protocols_handled().SerializeAsString();
    current = current_state.protocols_handled().SerializeAsString();
    if (desired != current) {
      std::move(do_update).Run();
      return;
    }
  }
#endif

#if BUILDFLAG(IS_MAC)
  // Protocol handler update detection. Shortcuts need to be updated in this
  // case on Mac because the shortcut itself includes the protocol
  // handling metadata.
  if (desired_state.has_file_handling() != current_state.has_file_handling()) {
    std::move(do_update).Run();
    return;
  }
  if (desired_state.has_file_handling() && current_state.has_file_handling()) {
    desired = desired_state.file_handling().SerializeAsString();
    current = current_state.file_handling().SerializeAsString();
    if (desired != current) {
      std::move(do_update).Run();
      return;
    }
  }
#endif

  // Fifth, no update is required.
  std::move(callback_for_no_update).Run();
}

void ShortcutSubManager::ForceUnregister(const webapps::AppId& app_id,
                                         base::OnceClosure callback) {
  base::FilePath shortcut_data_dir = GetOsIntegrationResourcesDirectoryForApp(
      profile_->GetPath(), app_id,
      provider_->registrar_unsafe().GetAppStartUrl(app_id));

  auto current_shortcut_info = std::make_unique<ShortcutInfo>();
  current_shortcut_info->app_id = app_id;
  current_shortcut_info->profile_path = profile_->GetPath();
  current_shortcut_info->title =
      base::UTF8ToUTF16(provider_->registrar_unsafe().GetAppShortName(app_id));

  internals::ScheduleDeletePlatformShortcuts(
      shortcut_data_dir, std::move(current_shortcut_info),
      base::BindOnce(&ShortcutSubManager::OnShortcutsDeleted,
                     weak_ptr_factory_.GetWeakPtr(), app_id,
                     std::move(callback)));
}

void ShortcutSubManager::CreateShortcut(
    const webapps::AppId& app_id,
    std::optional<SynchronizeOsOptions> synchronize_options,
    base::OnceClosure on_complete,
    std::unique_ptr<ShortcutInfo> shortcut_info) {
  SynchronizeOsOptions options =
      synchronize_options.value_or(SynchronizeOsOptions());

  ShortcutLocations locations;
  locations.on_desktop = options.add_shortcut_to_desktop;
  locations.applications_menu_location = APP_MENU_LOCATION_SUBDIR_CHROMEAPPS;
  locations.in_quick_launch_bar = options.add_to_quick_launch_bar;

  base::FilePath shortcut_data_dir =
      internals::GetShortcutDataDir(*shortcut_info);

  internals::ScheduleCreatePlatformShortcuts(
      shortcut_data_dir, locations, options.reason, std::move(shortcut_info),
      base::BindOnce([](bool success) {
        base::UmaHistogramEnumeration(
            "WebApp.Shortcuts.Creation.Result",
            success ? CreationResult::kSuccess
                    : CreationResult::kFailToCreateShortcut);
      }).Then(std::move(on_complete)));
}

void ShortcutSubManager::UpdateShortcut(
    const webapps::AppId& app_id,
    std::optional<SynchronizeOsOptions> synchronize_options,
    const std::u16string& old_app_title,
    base::OnceClosure on_complete,
    std::unique_ptr<ShortcutInfo> shortcut_info) {
  std::optional<ShortcutLocations> locations = std::nullopt;
  if (synchronize_options.has_value()) {
    ShortcutLocations creation_locations;
    creation_locations.on_desktop =
        synchronize_options->add_shortcut_to_desktop;
    creation_locations.in_quick_launch_bar =
        synchronize_options->add_to_quick_launch_bar;
    // Leaving `locations` as null if there are no creation locations will avoid
    // creating duplicates of existing shortcuts because the creation locations
    // don't match the existing locations (e.g., UpdatePlatformShortcuts in
    // web_app_shortcut_win.cc).
    if (creation_locations.in_quick_launch_bar ||
        creation_locations.on_desktop) {
      locations = creation_locations;
    }
  }

  base::FilePath shortcut_data_dir =
      internals::GetShortcutDataDir(*shortcut_info);

  internals::ScheduleUpdatePlatformShortcuts(
      shortcut_data_dir, old_app_title, locations,
      base::BindOnce([](Result result) {
        base::UmaHistogramBoolean("WebApp.Shortcuts.Update.Result",
                                  (result == Result::kOk));
      }).Then(std::move(on_complete)),
      std::move(shortcut_info));
}

void ShortcutSubManager::OnShortcutsDeleted(const webapps::AppId& app_id,
                                            base::OnceClosure final_callback,
                                            bool success) {
  ResultCallback final_result_callback =
      base::BindOnce([](Result result) {
        bool final_success = result == Result::kOk;
        base::UmaHistogramBoolean("WebApp.Shortcuts.Delete.Result",
                                  final_success);
      }).Then(std::move(final_callback));

#if BUILDFLAG(IS_MAC)
  bool delete_multi_profile_shortcuts =
      AppShimRegistry::Get()->OnAppUninstalledForProfile(app_id,
                                                         profile_->GetPath());
  if (delete_multi_profile_shortcuts) {
    internals::ScheduleDeleteMultiProfileShortcutsForApp(
        app_id, std::move(final_result_callback));
  } else {
    std::move(final_result_callback)
        .Run(success ? Result::kOk : Result::kError);
  }
#else
  std::move(final_result_callback).Run(success ? Result::kOk : Result::kError);
#endif
}

void ShortcutSubManager::StoreIconDataFromDisk(
    proto::os_state::ShortcutDescription* shortcut,
    base::flat_map<SquareSizePx, base::Time> time_map) {
  for (const auto& [size, time] : time_map) {
    auto* shortcut_icon_data = shortcut->add_icon_data_any();
    shortcut_icon_data->set_icon_size(size);
    shortcut_icon_data->set_timestamp(syncer::TimeToProtoTime(time));
  }
}

}  // namespace web_app