File: remove_web_app_job.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (261 lines) | stat: -rw-r--r-- 9,880 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
// 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/jobs/uninstall/remove_web_app_job.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/to_string.h"
#include "chrome/browser/web_applications/isolated_web_apps/remove_isolated_web_app_data.h"
#include "chrome/browser/web_applications/jobs/uninstall/remove_install_source_job.h"
#include "chrome/browser/web_applications/locks/all_apps_lock.h"
#include "chrome/browser/web_applications/os_integration/os_integration_manager.h"
#include "chrome/browser/web_applications/proto/web_app_os_integration_state.pb.h"
#include "chrome/browser/web_applications/user_uninstalled_preinstalled_web_app_prefs.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_icon_manager.h"
#include "chrome/browser/web_applications/web_app_install_finalizer.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_management_type.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"
#include "chrome/browser/web_applications/web_app_translation_manager.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"

namespace web_app {

namespace {

bool IsOsIntegrationRemovedForApp(
    std::optional<proto::os_state::WebAppOsIntegration> state) {
  if (!state.has_value()) {
    return true;
  }

  return !state->has_file_handling() && !state->has_protocols_handled() &&
         !state->has_run_on_os_login() && !state->has_shortcut() &&
         !state->has_shortcut_menus() && !state->has_uninstall_registration();
}

}  // namespace

RemoveWebAppJob::RemoveWebAppJob(
    webapps::WebappUninstallSource uninstall_source,
    Profile& profile,
    base::Value::Dict& debug_value,
    webapps::AppId app_id)
    : uninstall_source_(uninstall_source),
      profile_(profile),
      debug_value_(debug_value),
      app_id_(app_id) {
  base::Value::Dict dict;
  debug_value_->Set("!job", "RemoveWebAppJob");
  debug_value_->Set("app_id", app_id_);
}

RemoveWebAppJob::~RemoveWebAppJob() = default;

void RemoveWebAppJob::Start(AllAppsLock& lock, Callback callback) {
  lock_ = &lock;
  callback_ = std::move(callback);
  debug_value_->Set("has_callback", !callback_.is_null());

  const WebApp* app = lock_->registrar().GetAppById(app_id_);
  if (!app) {
    CompleteAndSelfDestruct(webapps::UninstallResultCode::kNoAppToUninstall);
    return;
  }

  if (app->isolation_data().has_value()) {
    has_isolated_storage_ = true;
  }

  if (webapps::IsUserUninstall(uninstall_source_)) {
    CHECK(app->CanUserUninstallWebApp());
    if (app->IsPreinstalledApp()) {
      // Update the default uninstalled web_app prefs if it is a preinstalled
      // app but being removed by user.
      const WebApp::ExternalConfigMap& config_map =
          app->management_to_external_config_map();
      auto it = config_map.find(WebAppManagement::kDefault);
      if (it != config_map.end()) {
        UserUninstalledPreinstalledWebAppPrefs(profile_->GetPrefs())
            .Add(app_id_, it->second.install_urls);
      }
    }
  }

  sub_apps_pending_removal_ = lock_->registrar().GetAllSubAppIds(app_id_);
  base::Value::List* list = debug_value_->EnsureList("sub_apps_found");
  for (const webapps::AppId& sub_app_id : sub_apps_pending_removal_) {
    list->Append(sub_app_id);
  }

  lock_->install_manager().NotifyWebAppWillBeUninstalled(app_id_);

  {
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    WebApp* mutable_app = update->UpdateApp(app_id_);
    CHECK(mutable_app);
    mutable_app->SetIsUninstalling(true);
  }

  // For Isolated Web App:
  // - Sets pref value to garbage-collect StoragePartitions on next start up.
  // - Clears data on StoragePartitions to prevent data leak on reinstall
  // before GC.
  if (has_isolated_storage_) {
    profile_->GetPrefs()->SetBoolean(
        prefs::kShouldGarbageCollectStoragePartitions, true);

    location_ = app->isolation_data()->location();

    url::Origin iwa_origin = url::Origin::Create(app->scope());
    web_app::RemoveIsolatedWebAppBrowsingData(
        &profile_.get(), iwa_origin,
        base::BindOnce(&RemoveWebAppJob::OnIsolatedWebAppBrowsingDataCleared,
                       weak_ptr_factory_.GetWeakPtr()));
  }
  lock_->os_integration_manager().Synchronize(
      app_id_, base::BindOnce(&RemoveWebAppJob::SynchronizeAndUninstallOsHooks,
                              weak_ptr_factory_.GetWeakPtr()));

  // While sometimes `Synchronize` needs to read icon data, for the uninstall
  // case it never needs to be read. Thus, it is safe to schedule this now and
  // not after the `Synchronize` call completes.
  lock_->icon_manager().DeleteData(
      app_id_, base::BindOnce(&RemoveWebAppJob::OnIconDataDeleted,
                              weak_ptr_factory_.GetWeakPtr()));

  lock_->translation_manager().DeleteTranslations(
      app_id_, base::BindOnce(&RemoveWebAppJob::OnTranslationDataDeleted,
                              weak_ptr_factory_.GetWeakPtr()));

}

webapps::WebappUninstallSource RemoveWebAppJob::uninstall_source() const {
  return uninstall_source_;
}

void RemoveWebAppJob::SynchronizeAndUninstallOsHooks() {
  CHECK(!primary_removal_result_.has_value());
  CHECK(!hooks_uninstalled_);
  hooks_uninstalled_ = true;
  bool os_integration_removal_success = IsOsIntegrationRemovedForApp(
      lock_->registrar().GetAppCurrentOsIntegrationState(app_id_));
  debug_value_->Set("os_integration_removal_success",
                    os_integration_removal_success);
  errors_ = errors_ || !os_integration_removal_success;
  MaybeFinishPrimaryRemoval();
}

void RemoveWebAppJob::OnIconDataDeleted(bool success) {
  CHECK(!primary_removal_result_.has_value());
  CHECK(!app_data_deleted_);
  app_data_deleted_ = true;
  debug_value_->Set("app_data_deleted_success", success);
  errors_ = errors_ || !success;
  MaybeFinishPrimaryRemoval();
}

void RemoveWebAppJob::OnTranslationDataDeleted(bool success) {
  CHECK(!primary_removal_result_.has_value());
  CHECK(!translation_data_deleted_);
  translation_data_deleted_ = true;
  debug_value_->Set("translation_data_deleted_success", success);
  errors_ = errors_ || !success;
  MaybeFinishPrimaryRemoval();
}

void RemoveWebAppJob::OnIsolatedWebAppBrowsingDataCleared() {
  CHECK(!primary_removal_result_.has_value());
  CHECK(!isolated_web_app_browsing_data_cleared_);
  // Must be an Isolated Web App.
  CHECK(has_isolated_storage_);
  CHECK(location_.has_value());
  isolated_web_app_browsing_data_cleared_ = true;
  debug_value_->Set("isolated_web_app_browsing_data_cleared_success", true);

  web_app::CloseAndDeleteBundle(
      &profile_.get(), location_.value(),
      base::BindOnce(&RemoveWebAppJob::OnIsolatedWebAppOwnedLocationDeleted,
                     weak_ptr_factory_.GetWeakPtr()));
}

void RemoveWebAppJob::OnIsolatedWebAppOwnedLocationDeleted() {
  CHECK(!primary_removal_result_.has_value());
  CHECK(has_isolated_storage_);
  CHECK(!isolated_web_app_owned_location_deleted_);

  isolated_web_app_owned_location_deleted_ = true;
  MaybeFinishPrimaryRemoval();
}

void RemoveWebAppJob::MaybeFinishPrimaryRemoval() {
  CHECK(!primary_removal_result_.has_value());
  const bool is_iwa_fully_removed = isolated_web_app_browsing_data_cleared_ &&
                                    isolated_web_app_owned_location_deleted_;
  if (!hooks_uninstalled_ || !app_data_deleted_ || !translation_data_deleted_ ||
      (has_isolated_storage_ && !is_iwa_fully_removed)) {
    return;
  }

  {
    CHECK_NE(lock_->registrar().GetAppById(app_id_), nullptr);
    ScopedRegistryUpdate update = lock_->sync_bridge().BeginUpdate();
    update->DeleteApp(app_id_);
  }

  primary_removal_result_ = errors_ ? webapps::UninstallResultCode::kError
                                    : webapps::UninstallResultCode::kAppRemoved;
  debug_value_->Set("primary_removal_result",
                    base::ToString(primary_removal_result_.value()));
  base::UmaHistogramBoolean("WebApp.Uninstall.Result", !errors_);

  lock_->install_manager().NotifyWebAppUninstalled(app_id_, uninstall_source_);

  ProcessSubAppsPendingRemovalOrComplete();
}

void RemoveWebAppJob::ProcessSubAppsPendingRemovalOrComplete() {
  CHECK(primary_removal_result_.has_value());

  if (sub_job_) {
    sub_job_.reset();
  }

  if (sub_apps_pending_removal_.empty()) {
    CompleteAndSelfDestruct(primary_removal_result_.value());
    return;
  }

  webapps::AppId sub_app_id = std::move(sub_apps_pending_removal_.back());
  sub_apps_pending_removal_.pop_back();

  sub_job_ = std::make_unique<RemoveInstallSourceJob>(
      uninstall_source_, profile_.get(),
      *debug_value_->EnsureDict("sub_app_jobs")->EnsureDict(sub_app_id),
      sub_app_id, WebAppManagementTypes({WebAppManagement::Type::kSubApp}));
  sub_job_->Start(*lock_,
                  base::IgnoreArgs<webapps::UninstallResultCode>(base::BindOnce(
                      &RemoveWebAppJob::ProcessSubAppsPendingRemovalOrComplete,
                      weak_ptr_factory_.GetWeakPtr())));
}

void RemoveWebAppJob::CompleteAndSelfDestruct(
    webapps::UninstallResultCode code) {
  CHECK(callback_);
  debug_value_->Set("result", base::ToString(code));
  std::move(callback_).Run(code);
}

}  // namespace web_app