File: android_sms_app_setup_controller_impl.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 (380 lines) | stat: -rw-r--r-- 16,511 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Copyright 2019 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/ash/android_sms/android_sms_app_setup_controller_impl.h"

#include <optional>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_macros.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/external_install_options.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.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_management_type.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chromeos/ash/components/multidevice/logging/logging.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/webapps/browser/install_result_code.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/url_util.h"
#include "net/cookies/cookie_util.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom.h"
#include "url/gurl.h"

namespace ash {
namespace android_sms {

namespace {

const char kDefaultToPersistCookieName[] = "default_to_persist";
const char kMigrationCookieName[] = "cros_migrated_to";
const char kDefaultToPersistCookieValue[] = "true";

}  // namespace

// static
const base::TimeDelta AndroidSmsAppSetupControllerImpl::kInstallRetryDelay =
    base::Seconds(5);
const size_t AndroidSmsAppSetupControllerImpl::kMaxInstallRetryCount = 7u;

AndroidSmsAppSetupControllerImpl::PwaDelegate::PwaDelegate() = default;

AndroidSmsAppSetupControllerImpl::PwaDelegate::~PwaDelegate() = default;

std::optional<webapps::AppId>
AndroidSmsAppSetupControllerImpl::PwaDelegate::GetPwaForUrl(
    const GURL& install_url,
    Profile* profile) {
  return web_app::FindInstalledAppWithUrlInScope(profile, install_url);
}

network::mojom::CookieManager*
AndroidSmsAppSetupControllerImpl::PwaDelegate::GetCookieManager(
    Profile* profile) {
  return profile->GetDefaultStoragePartition()
      ->GetCookieManagerForBrowserProcess();
}

void AndroidSmsAppSetupControllerImpl::PwaDelegate::RemovePwa(
    const webapps::AppId& app_id,
    Profile* profile,
    SuccessCallback callback) {
  auto* provider = web_app::WebAppProvider::GetForWebApps(profile);
  if (!provider) {
    std::move(callback).Run(false);
    return;
  }

  provider->scheduler().RemoveInstallManagementMaybeUninstall(
      app_id, web_app::WebAppManagement::kDefault,
      webapps::WebappUninstallSource::kInternalPreinstalled,
      base::BindOnce(
          [](SuccessCallback callback, webapps::UninstallResultCode code) {
            std::move(callback).Run(UninstallSucceeded(code));
          },
          std::move(callback)));
}

AndroidSmsAppSetupControllerImpl::AndroidSmsAppSetupControllerImpl(
    Profile* profile,
    web_app::ExternallyManagedAppManager* externally_managed_app_manager,
    HostContentSettingsMap* host_content_settings_map)
    : profile_(profile),
      externally_managed_app_manager_(externally_managed_app_manager),
      host_content_settings_map_(host_content_settings_map),
      pwa_delegate_(std::make_unique<PwaDelegate>()) {}

AndroidSmsAppSetupControllerImpl::~AndroidSmsAppSetupControllerImpl() = default;

void AndroidSmsAppSetupControllerImpl::SetUpApp(const GURL& app_url,
                                                const GURL& install_url,
                                                SuccessCallback callback) {
  PA_LOG(VERBOSE) << "AndroidSmsAppSetupControllerImpl::SetUpApp(): Setting "
                  << "DefaultToPersist cookie at " << app_url << " before PWA "
                  << "installation.";
  net::CookieOptions options;
  options.set_same_site_cookie_context(
      net::CookieOptions::SameSiteCookieContext::MakeInclusive());
  net::CanonicalCookie cookie = *net::CanonicalCookie::CreateSanitizedCookie(
      app_url, kDefaultToPersistCookieName, kDefaultToPersistCookieValue,
      std::string() /* domain */, std::string() /* path */,
      base::Time::Now() /* creation_time */, base::Time() /* expiration_time */,
      base::Time::Now() /* last_access_time */,
      !net::IsLocalhost(app_url) /* secure */, false /* http_only */,
      net::CookieSameSite::STRICT_MODE, net::COOKIE_PRIORITY_DEFAULT,
      std::nullopt /* partition_key */, /*status=*/nullptr);
  // TODO(crbug.com/1069974): The cookie source url must be faked here because
  // otherwise, this would fail to set a secure cookie if |app_url| is insecure.
  // Consider instead to use url::Replacements to force the scheme to be https.
  pwa_delegate_->GetCookieManager(profile_)->SetCanonicalCookie(
      cookie, net::cookie_util::SimulatedCookieSource(cookie, "https"), options,
      base::BindOnce(&AndroidSmsAppSetupControllerImpl::
                         OnSetRememberDeviceByDefaultCookieResult,
                     weak_ptr_factory_.GetWeakPtr(), app_url, install_url,
                     std::move(callback)));
}

std::optional<webapps::AppId> AndroidSmsAppSetupControllerImpl::GetPwa(
    const GURL& install_url) {
  return pwa_delegate_->GetPwaForUrl(install_url, profile_);
}

void AndroidSmsAppSetupControllerImpl::DeleteRememberDeviceByDefaultCookie(
    const GURL& app_url,
    SuccessCallback callback) {
  PA_LOG(INFO) << "AndroidSmsAppSetupControllerImpl::"
               << "DeleteRememberDeviceByDefaultCookie(): Deleting "
               << "DefaultToPersist cookie at " << app_url << ".";
  network::mojom::CookieDeletionFilterPtr filter(
      network::mojom::CookieDeletionFilter::New());
  filter->url = app_url;
  filter->cookie_name = kDefaultToPersistCookieName;
  pwa_delegate_->GetCookieManager(profile_)->DeleteCookies(
      std::move(filter),
      base::BindOnce(&AndroidSmsAppSetupControllerImpl::
                         OnDeleteRememberDeviceByDefaultCookieResult,
                     weak_ptr_factory_.GetWeakPtr(), app_url,
                     std::move(callback)));
}

void AndroidSmsAppSetupControllerImpl::RemoveApp(
    const GURL& app_url,
    const GURL& install_url,
    const GURL& migrated_to_app_url,
    SuccessCallback callback) {
  std::optional<webapps::AppId> app_id =
      pwa_delegate_->GetPwaForUrl(install_url, profile_);

  // If there is no app installed at |url|, there is nothing more to do.
  if (!app_id) {
    PA_LOG(VERBOSE) << "AndroidSmsAppSetupControllerImpl::RemoveApp(): No app "
                    << "is installed at " << install_url
                    << "; skipping removal process.";
    std::move(callback).Run(true /* success */);
    return;
  }

  PA_LOG(INFO) << "AndroidSmsAppSetupControllerImpl::RemoveApp(): "
               << "Uninstalling app at " << install_url << ".";

  pwa_delegate_->RemovePwa(
      *app_id, profile_,
      base::BindOnce(&AndroidSmsAppSetupControllerImpl::OnAppRemoved,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                     app_url, install_url, migrated_to_app_url));
}

void AndroidSmsAppSetupControllerImpl::OnAppRemoved(
    SuccessCallback callback,
    const GURL& app_url,
    const GURL& install_url,
    const GURL& migrated_to_app_url,
    bool uninstalled) {
  UMA_HISTOGRAM_BOOLEAN("AndroidSms.PWAUninstallationResult", uninstalled);

  if (!uninstalled) {
    PA_LOG(ERROR) << "AndroidSmsAppSetupControllerImpl::RemoveApp(): "
                  << "PWA for " << install_url << " failed to uninstall.";
    std::move(callback).Run(false /* success */);
    return;
  }

  SetMigrationCookie(app_url, migrated_to_app_url, std::move(callback));
}

void AndroidSmsAppSetupControllerImpl::OnSetRememberDeviceByDefaultCookieResult(
    const GURL& app_url,
    const GURL& install_url,
    SuccessCallback callback,
    net::CookieAccessResult result) {
  if (!result.status.IsInclude()) {
    PA_LOG(WARNING)
        << "AndroidSmsAppSetupControllerImpl::"
        << "OnSetRememberDeviceByDefaultCookieResult(): Failed to set "
        << "DefaultToPersist cookie at " << app_url << ". Proceeding "
        << "to remove migration cookie.";
  }

  // Delete migration cookie in case it was set by a previous RemoveApp call.
  network::mojom::CookieDeletionFilterPtr filter(
      network::mojom::CookieDeletionFilter::New());
  filter->url = app_url;
  filter->cookie_name = kMigrationCookieName;
  pwa_delegate_->GetCookieManager(profile_)->DeleteCookies(
      std::move(filter),
      base::BindOnce(
          &AndroidSmsAppSetupControllerImpl::OnDeleteMigrationCookieResult,
          weak_ptr_factory_.GetWeakPtr(), app_url, install_url,
          std::move(callback)));
}

void AndroidSmsAppSetupControllerImpl::OnDeleteMigrationCookieResult(
    const GURL& app_url,
    const GURL& install_url,
    SuccessCallback callback,
    uint32_t num_deleted) {
  // If the app is already installed at |url|, there is nothing more to do.
  if (pwa_delegate_->GetPwaForUrl(install_url, profile_)) {
    PA_LOG(VERBOSE) << "AndroidSmsAppSetupControllerImpl::"
                    << "OnDeleteMigrationCookieResult():"
                    << "App is already installed at " << install_url
                    << "; skipping setup process.";
    std::move(callback).Run(true /* success */);
    return;
  }

  TryInstallApp(install_url, app_url, 0 /* num_attempts_so_far */,
                std::move(callback));
}

void AndroidSmsAppSetupControllerImpl::TryInstallApp(const GURL& install_url,
                                                     const GURL& app_url,
                                                     size_t num_attempts_so_far,
                                                     SuccessCallback callback) {
  PA_LOG(VERBOSE) << "AndroidSmsAppSetupControllerImpl::TryInstallApp(): "
                  << "Trying to install PWA for " << install_url
                  << ". Num attempts so far # " << num_attempts_so_far;
  web_app::ExternalInstallOptions options(
      install_url, web_app::mojom::UserDisplayMode::kStandalone,
      web_app::ExternalInstallSource::kInternalDefault);
  options.override_previous_user_uninstall = true;
  options.require_manifest = true;
  externally_managed_app_manager_->Install(
      std::move(options),
      base::BindOnce(&AndroidSmsAppSetupControllerImpl::OnAppInstallResult,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback),
                     num_attempts_so_far, app_url));
}

void AndroidSmsAppSetupControllerImpl::OnAppInstallResult(
    SuccessCallback callback,
    size_t num_attempts_so_far,
    const GURL& app_url,
    const GURL& install_url,
    web_app::ExternallyManagedAppManager::InstallResult result) {
  UMA_HISTOGRAM_ENUMERATION("AndroidSms.PWAInstallationResult", result.code);
  const bool install_succeeded = webapps::IsSuccess(result.code);

  if (!install_succeeded && num_attempts_so_far < kMaxInstallRetryCount) {
    base::TimeDelta retry_delay =
        kInstallRetryDelay * (1 << num_attempts_so_far);
    PA_LOG(VERBOSE)
        << "AndroidSmsAppSetupControllerImpl::OnAppInstallResult(): "
        << "PWA for " << install_url << " failed to install."
        << "InstallResultCode: " << static_cast<int>(result.code)
        << " Retrying again in " << retry_delay;
    base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce(&AndroidSmsAppSetupControllerImpl::TryInstallApp,
                       weak_ptr_factory_.GetWeakPtr(), install_url, app_url,
                       num_attempts_so_far + 1, std::move(callback)),
        retry_delay);
    return;
  }
  UMA_HISTOGRAM_BOOLEAN("AndroidSms.EffectivePWAInstallationSuccess",
                        install_succeeded);

  if (!install_succeeded) {
    PA_LOG(WARNING)
        << "AndroidSmsAppSetupControllerImpl::OnAppInstallResult(): "
        << "PWA for " << install_url << " failed to install. "
        << "InstallResultCode: " << static_cast<int>(result.code);
    std::move(callback).Run(false /* success */);
    return;
  }
  PA_LOG(INFO) << "AndroidSmsAppSetupControllerImpl::OnAppInstallResult(): "
               << "PWA for " << install_url << " was installed successfully.";

  UMA_HISTOGRAM_EXACT_LINEAR("AndroidSms.NumAttemptsForSuccessfulInstallation",
                             num_attempts_so_far + 1, kMaxInstallRetryCount);

  // Grant notification permission for the PWA.
  host_content_settings_map_->SetContentSettingDefaultScope(
      app_url, GURL() /* top_level_url */, ContentSettingsType::NOTIFICATIONS,
      ContentSetting::CONTENT_SETTING_ALLOW);

  std::move(callback).Run(true /* success */);
}

void AndroidSmsAppSetupControllerImpl::SetMigrationCookie(
    const GURL& app_url,
    const GURL& migrated_to_app_url,
    SuccessCallback callback) {
  // Set migration cookie on the client for which the PWA was just uninstalled.
  // The client checks for this cookie to redirect users to the new domain. This
  // prevents unwanted connection stealing between old and new clients should
  // the user try to open old client.
  net::CookieOptions options;
  options.set_same_site_cookie_context(
      net::CookieOptions::SameSiteCookieContext::MakeInclusive());
  net::CanonicalCookie cookie = *net::CanonicalCookie::CreateSanitizedCookie(
      app_url, kMigrationCookieName, migrated_to_app_url.GetContent(),
      std::string() /* domain */, std::string() /* path */,
      base::Time::Now() /* creation_time */, base::Time() /* expiration_time */,
      base::Time::Now() /* last_access_time */,
      !net::IsLocalhost(app_url) /* secure */, false /* http_only */,
      net::CookieSameSite::STRICT_MODE, net::COOKIE_PRIORITY_DEFAULT,
      std::nullopt /* partition_key */, /*status=*/nullptr);
  // TODO(crbug.com/1069974): The cookie source url must be faked here because
  // otherwise, this would fail to set a secure cookie if |app_url| is insecure.
  // Consider instead to use url::Replacements to force the scheme to be https.
  pwa_delegate_->GetCookieManager(profile_)->SetCanonicalCookie(
      cookie, net::cookie_util::SimulatedCookieSource(cookie, "https"), options,
      base::BindOnce(
          &AndroidSmsAppSetupControllerImpl::OnSetMigrationCookieResult,
          weak_ptr_factory_.GetWeakPtr(), app_url, std::move(callback)));
}

void AndroidSmsAppSetupControllerImpl::OnSetMigrationCookieResult(
    const GURL& app_url,
    SuccessCallback callback,
    net::CookieAccessResult result) {
  if (!result.status.IsInclude()) {
    PA_LOG(ERROR)
        << "AndroidSmsAppSetupControllerImpl::OnSetMigrationCookieResult(): "
        << "Failed to set migration cookie for " << app_url << ". Proceeding "
        << "to remove DefaultToPersist cookie.";
  }

  DeleteRememberDeviceByDefaultCookie(app_url, std::move(callback));
}

void AndroidSmsAppSetupControllerImpl::
    OnDeleteRememberDeviceByDefaultCookieResult(const GURL& app_url,
                                                SuccessCallback callback,
                                                uint32_t num_deleted) {
  if (num_deleted != 1u) {
    PA_LOG(WARNING) << "AndroidSmsAppSetupControllerImpl::"
                    << "OnDeleteRememberDeviceByDefaultCookieResult(): "
                    << "Tried to delete a single cookie at " << app_url
                    << ", but " << num_deleted << " cookies were deleted.";
  }

  // Even if an unexpected number of cookies was deleted, consider this a
  // success. If SetUpApp() failed to install a cookie earlier, the setup
  // process is still considered a success, so failing to delete a cookie should
  // also be considered a success.
  std::move(callback).Run(true /* success */);
}

void AndroidSmsAppSetupControllerImpl::SetPwaDelegateForTesting(
    std::unique_ptr<PwaDelegate> test_pwa_delegate) {
  pwa_delegate_ = std::move(test_pwa_delegate);
}

}  // namespace android_sms
}  // namespace ash