File: os_integration_synchronize_command.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 (112 lines) | stat: -rw-r--r-- 4,687 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
// 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/commands/os_integration_synchronize_command.h"

#include <memory>
#include <utility>

#include "base/functional/callback.h"
#include "base/values.h"
#include "chrome/browser/web_applications/commands/web_app_command.h"
#include "chrome/browser/web_applications/locks/app_lock.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.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/web_app.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "components/webapps/common/web_app_id.h"

namespace web_app {

namespace {

base::Value SynchronizeOptionsDebugValue(const SynchronizeOsOptions& options) {
  base::Value::Dict debug_dict;
  debug_dict.Set("force_unregister_os_integration",
                 options.force_unregister_os_integration);
  debug_dict.Set("add_shortcut_to_desktop", options.add_shortcut_to_desktop);
  debug_dict.Set("add_to_quick_launch_bar", options.add_to_quick_launch_bar);
  debug_dict.Set("force_create_shortcuts", options.force_create_shortcuts);
  if (options.reason == SHORTCUT_CREATION_AUTOMATED) {
    debug_dict.Set("reason", "SHORTCUT_CREATION_AUTOMATED");
  } else {
    debug_dict.Set("reason", "SHORTCUT_CREATION_BY_USER");
  }
  return base::Value(std::move(debug_dict));
}

}  // namespace

OsIntegrationSynchronizeCommand::OsIntegrationSynchronizeCommand(
    const webapps::AppId& app_id,
    std::optional<SynchronizeOsOptions> synchronize_options,
    bool upgrade_to_fully_installed_if_installed,
    base::OnceClosure synchronize_callback)
    : WebAppCommand<AppLock>("OsIntegrationSynchronizeCommand",
                             AppLockDescription(app_id),
                             std::move(synchronize_callback)),
      app_id_(app_id),
      synchronize_options_(synchronize_options),
      upgrade_to_fully_installed_if_installed_(
          upgrade_to_fully_installed_if_installed) {
  GetMutableDebugValue().Set("app_id", app_id_);
  if (synchronize_options_.has_value()) {
    GetMutableDebugValue().Set(
        "synchronize_options",
        SynchronizeOptionsDebugValue(synchronize_options_.value()));
  }
  GetMutableDebugValue().Set("upgrade_to_fully_installed_if_installed",
                             upgrade_to_fully_installed_if_installed);
}

OsIntegrationSynchronizeCommand::~OsIntegrationSynchronizeCommand() = default;

void OsIntegrationSynchronizeCommand::StartWithLock(
    std::unique_ptr<AppLock> app_lock) {
  app_lock_ = std::move(app_lock);

  // The app may not be installed, as this command can be scheduled from the
  // command line uninstalling an app from the operating system integration. In
  // that case, `Synchronize` still needs to be called below to attempt cleanup.

  bool in_registrar = app_lock_->registrar().IsInRegistrar(app_id_);
  bool was_installed_with_os_integration =
      app_lock_->registrar().GetInstallState(app_id_) ==
      proto::INSTALLED_WITH_OS_INTEGRATION;
  GetMutableDebugValue().Set("in_registrar", in_registrar);
  GetMutableDebugValue().Set("was_fully_installed",
                             was_installed_with_os_integration);

  if (in_registrar && !was_installed_with_os_integration &&
      upgrade_to_fully_installed_if_installed_) {
    ScopedRegistryUpdate update = app_lock_->sync_bridge().BeginUpdate();
    WebApp* app = update->UpdateApp(app_id_);
    CHECK(app);
    app->SetInstallState(proto::InstallState::INSTALLED_WITH_OS_INTEGRATION);
  }
  const bool force_unregister_os_integration =
      synchronize_options_.has_value() &&
      synchronize_options_->force_unregister_os_integration;
  GetMutableDebugValue().Set("force_unregister_os_integration",
                             force_unregister_os_integration);
  if (!force_unregister_os_integration && !in_registrar) {
    // The app must have been uninstalled since the command was scheduled.
    CompleteAndSelfDestruct(CommandResult::kSuccess);
    return;
  }

  app_lock_->os_integration_manager().Synchronize(
      app_id_,
      base::BindOnce(&OsIntegrationSynchronizeCommand::OnSynchronizeComplete,
                     weak_factory_.GetWeakPtr()),
      synchronize_options_);
}

void OsIntegrationSynchronizeCommand::OnSynchronizeComplete() {
  CompleteAndSelfDestruct(CommandResult::kSuccess);
}

}  // namespace web_app