File: dedupe_install_urls_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 (254 lines) | stat: -rw-r--r-- 8,666 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
// Copyright 2023 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/dedupe_install_urls_command.h"

#include "base/auto_reset.h"
#include "base/barrier_closure.h"
#include "base/containers/extend.h"
#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/to_string.h"
#include "chrome/browser/web_applications/callback_utils.h"
#include "chrome/browser/web_applications/jobs/uninstall/remove_install_url_job.h"
#include "chrome/browser/web_applications/locks/all_apps_lock.h"
#include "chrome/browser/web_applications/web_app.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_install_finalizer.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"

namespace web_app {

using ExternalManagementConfig = WebApp::ExternalManagementConfig;
using ExternalConfigMap = WebApp::ExternalConfigMap;

namespace {

bool g_suppress_for_testing = false;

base::flat_map<GURL, base::flat_set<webapps::AppId>> BuildInstallUrlToAppIdsMap(
    const WebAppRegistrar& registrar,
    base::Value::Dict& debug_value) {
  base::flat_map<GURL, base::flat_set<webapps::AppId>> result;

  for (const WebApp& app : registrar.GetApps()) {
    for (const auto& [install_source, config] :
         app.management_to_external_config_map()) {
      for (const GURL& install_url : config.install_urls) {
        result[install_url].insert(app.app_id());
        debug_value.EnsureList(install_url.spec())->Append(app.app_id());
      }
    }
  }

  return result;
}

const webapps::AppId& SelectWebAppToDedupeInto(
    const WebAppRegistrar& registrar,
    const base::flat_set<webapps::AppId>& app_ids_with_common_install_url) {
  CHECK(app_ids_with_common_install_url.size() > 1);

  const webapps::AppId* best = nullptr;
  bool best_looks_like_placeholder = false;
  base::Time best_install_time;

  for (const webapps::AppId& app_id : app_ids_with_common_install_url) {
    const WebApp& candidate = *registrar.GetAppById(app_id);
    bool candidate_looks_like_placeholder = LooksLikePlaceholder(candidate);

    if (
        // Is this the first candidate we've seen?
        !best ||
        // Is the candidate an upgrade from a placeholder?
        (best_looks_like_placeholder && !candidate_looks_like_placeholder) ||
        // Is the candidate more recently installed and not a downgrade?
        (best_looks_like_placeholder == candidate_looks_like_placeholder &&
         candidate.latest_install_time() > best_install_time)) {
      best = &app_id;
      best_looks_like_placeholder = candidate_looks_like_placeholder;
      best_install_time = candidate.latest_install_time();
    }
  }

  CHECK(best);
  return *best;
}

std::vector<std::unique_ptr<RemoveInstallUrlJob>>
BuildOperationsToDedupeInstallUrlConfigsIntoSelectedApp(
    Profile& profile,
    base::Value::Dict& jobs_debug_value,
    const WebAppRegistrar& registrar,
    ScopedRegistryUpdate& update,
    const GURL& install_url,
    const base::flat_set<webapps::AppId>& app_ids_with_common_install_url,
    const webapps::AppId& id_to_dedupe_into) {
  std::vector<std::unique_ptr<RemoveInstallUrlJob>> result;

  WebApp& app_to_dedupe_into = *update->UpdateApp(id_to_dedupe_into);

  for (const webapps::AppId& id_to_dedupe_out_of :
       app_ids_with_common_install_url) {
    if (id_to_dedupe_out_of == id_to_dedupe_into) {
      continue;
    }

    const WebApp& app_to_dedupe_out_of =
        *registrar.GetAppById(id_to_dedupe_out_of);

    for (auto const& [install_source, config_to_dedupe_out_of] :
         app_to_dedupe_out_of.management_to_external_config_map()) {
      if (!config_to_dedupe_out_of.install_urls.contains(install_url)) {
        continue;
      }

      app_to_dedupe_into.AddSource(install_source);
      app_to_dedupe_into.AddInstallURLToManagementExternalConfigMap(
          install_source, install_url);
      for (const std::string& policy_id :
           config_to_dedupe_out_of.additional_policy_ids) {
        app_to_dedupe_into.AddPolicyIdToManagementExternalConfigMap(
            install_source, policy_id);
      }

      // Create job to remove deduped install URL from existing app.
      result.push_back(std::make_unique<RemoveInstallUrlJob>(
          webapps::WebappUninstallSource::kInstallUrlDeduping, profile,
          *jobs_debug_value.EnsureDict(id_to_dedupe_out_of),
          id_to_dedupe_out_of, install_source, install_url));
    }
  }

  return result;
}

struct DedupeOperations {
  std::vector<std::unique_ptr<RemoveInstallUrlJob>> remove_install_url_jobs;
  base::flat_map<GURL, webapps::AppId> dedupe_choices;
};

DedupeOperations BuildOperationsToHaveOneAppPerInstallUrl(
    Profile& profile,
    base::Value::Dict& debug_value,
    const WebAppRegistrar& registrar,
    ScopedRegistryUpdate& update,
    const base::flat_map<GURL, base::flat_set<webapps::AppId>>&
        install_url_to_apps) {
  DedupeOperations result;

  for (const auto& [install_url, app_ids] : install_url_to_apps) {
    if (app_ids.size() <= 1) {
      continue;
    }

    const webapps::AppId& id_to_dedupe_into =
        SelectWebAppToDedupeInto(registrar, app_ids);
    result.dedupe_choices[install_url] = id_to_dedupe_into;
    debug_value.EnsureDict("dedupe_choices")
        ->Set(install_url.spec(), id_to_dedupe_into);

    base::Extend(
        result.remove_install_url_jobs,
        BuildOperationsToDedupeInstallUrlConfigsIntoSelectedApp(
            profile, *debug_value.EnsureDict("removal_jobs"), registrar, update,
            install_url, app_ids, id_to_dedupe_into));
  }

  return result;
}

}  // namespace

base::AutoReset<bool> DedupeInstallUrlsCommand::ScopedSuppressForTesting() {
  return {&g_suppress_for_testing, true};
}

DedupeInstallUrlsCommand::DedupeInstallUrlsCommand(
    Profile& profile,
    base::OnceClosure completed_callback)
    : WebAppCommand("DedupeInstallUrlsCommand",
                    AllAppsLockDescription(),
                    std::move(completed_callback)),
      profile_(profile) {}

DedupeInstallUrlsCommand::~DedupeInstallUrlsCommand() = default;

void DedupeInstallUrlsCommand::StartWithLock(
    std::unique_ptr<AllAppsLock> lock) {
  if (g_suppress_for_testing) {
    CompleteAndSelfDestruct(CommandResult::kSuccess);
    return;
  }

  lock_ = std::move(lock);

  install_url_to_apps_ = BuildInstallUrlToAppIdsMap(
      lock_->registrar(),
      *GetMutableDebugValue().EnsureDict("duplicate_install_urls"));

  {
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    DedupeOperations pending_dedupe_operations =
        BuildOperationsToHaveOneAppPerInstallUrl(
            profile_.get(), GetMutableDebugValue(), lock_->registrar(), update,
            install_url_to_apps_);

    dedupe_choices_ = std::move(pending_dedupe_operations.dedupe_choices);
    pending_jobs_ =
        std::move(pending_dedupe_operations.remove_install_url_jobs);
  }

  ProcessPendingJobsOrComplete();
}

void DedupeInstallUrlsCommand::ProcessPendingJobsOrComplete() {
  CHECK(!active_job_);

  if (!pending_jobs_.empty()) {
    std::swap(active_job_, pending_jobs_.back());
    pending_jobs_.pop_back();
    active_job_->Start(*lock_,
                       base::BindOnce(&DedupeInstallUrlsCommand::JobComplete,
                                      weak_ptr_factory_.GetWeakPtr()));
    return;
  }

  RecordMetrics();
  CompleteAndSelfDestruct(any_errors_ ? CommandResult::kFailure
                                      : CommandResult::kSuccess);
}

void DedupeInstallUrlsCommand::JobComplete(webapps::UninstallResultCode code) {
  CHECK(active_job_);

  if (!UninstallSucceeded(code)) {
    any_errors_ = true;
  }

  active_job_.reset();

  ProcessPendingJobsOrComplete();
}

void DedupeInstallUrlsCommand::RecordMetrics() {
  size_t dedupe_count = 0;
  for (const auto& [install_url, app_ids] : install_url_to_apps_) {
    if (app_ids.size() <= 1) {
      continue;
    }
    ++dedupe_count;
    base::UmaHistogramCounts100("WebApp.DedupeInstallUrls.AppsDeduped",
                                app_ids.size());
  }
  base::UmaHistogramCounts100("WebApp.DedupeInstallUrls.InstallUrlsDeduped",
                              dedupe_count);
}

}  // namespace web_app