File: add_to_homescreen_data_fetcher.cc

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (282 lines) | stat: -rw-r--r-- 10,259 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
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
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/webapps/browser/android/add_to_homescreen_data_fetcher.h"

#include <utility>
#include <vector>

#include "base/android/build_info.h"
#include "base/functional/bind.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/threading/thread_restrictions.h"
#include "components/dom_distiller/core/url_utils.h"
#include "components/favicon/content/large_icon_service_getter.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/favicon_base/favicon_types.h"
#include "components/webapps/browser/android/webapps_icon_utils.h"
#include "components/webapps/browser/android/webapps_utils.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "components/webapps/common/constants.h"
#include "components/webapps/common/web_page_metadata.mojom.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/manifest/manifest.h"
#include "third_party/blink/public/common/manifest/manifest_icon_selector.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/manifest/display_mode.mojom-shared.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
#include "url/gurl.h"

namespace webapps {

namespace {

// Looks up the original, online, visible URL of |web_contents|. The current
// visible URL may be a distilled article which is not appropriate for a home
// screen shortcut.
GURL GetShortcutUrl(content::WebContents* web_contents) {
  return dom_distiller::url_utils::GetOriginalUrlFromDistillerUrl(
      web_contents->GetVisibleURL());
}

InstallableParams ParamsToFetchInstallableData() {
  // Fetch manifest and metadata.
  InstallableParams params;
  params.fetch_metadata = true;
  return params;
}

InstallableParams ParamsToFetchPrimaryIcon() {
  InstallableParams params;
  params.valid_primary_icon = true;
  params.prefer_maskable_icon = true;
  params.fetch_favicon = true;
  return params;
}

InstallableParams ParamsToPerformInstallableCheck() {
  InstallableParams params;
  params.check_eligibility = true;
    params.installable_criteria = InstallableCriteria::kNoManifestAtRootScope;
  return params;
}

void RecordAddToHomescreenDialogDuration(base::TimeDelta duration) {
  UMA_HISTOGRAM_TIMES("Webapp.AddToHomescreenDialog.Timeout", duration);
}

void RecordMobileCapableUserActions(mojom::WebPageMobileCapable mobile_capable,
                                    bool has_manifest) {
  if (has_manifest) {
    base::RecordAction(base::UserMetricsAction("webapps.AddShortcut.Manifest"));
    return;
  }

  // Record the use of web-app-capable meta flag.
  switch (mobile_capable) {
    case mojom::WebPageMobileCapable::ENABLED:
      base::RecordAction(
          base::UserMetricsAction("webapps.AddShortcut.AppShortcut"));
      break;
    case mojom::WebPageMobileCapable::ENABLED_APPLE:
      base::RecordAction(
          base::UserMetricsAction("webapps.AddShortcut.AppShortcutApple"));
      break;
    case mojom::WebPageMobileCapable::UNSPECIFIED:
      base::RecordAction(
          base::UserMetricsAction("webapps.AddShortcut.Bookmark"));
      break;
  }
}

}  // namespace

AddToHomescreenDataFetcher::AddToHomescreenDataFetcher(
    content::WebContents* web_contents,
    int data_timeout_ms,
    Observer* observer)
    : web_contents_(web_contents->GetWeakPtr()),
      installable_manager_(InstallableManager::FromWebContents(web_contents)),
      observer_(observer),
      shortcut_info_(GetShortcutUrl(web_contents)),
      data_timeout_ms_(base::Milliseconds(data_timeout_ms)) {
  DCHECK(shortcut_info_.url.is_valid());
  shortcut_info_.user_title = web_contents_->GetTitle();

  FetchInstallableData();
}

AddToHomescreenDataFetcher::~AddToHomescreenDataFetcher() = default;

void AddToHomescreenDataFetcher::FetchInstallableData() {
  // Kick off a timeout for downloading web app data. If we haven't
  // finished within the timeout, fall back to using any fetched icon, or at
  // worst, a dynamically-generated launcher icon.
  data_timeout_timer_.Start(
      FROM_HERE, data_timeout_ms_,
      base::BindOnce(&AddToHomescreenDataFetcher::OnDataTimedout,
                     weak_ptr_factory_.GetWeakPtr()));
  start_time_ = base::TimeTicks::Now();

  installable_manager_->GetData(
      ParamsToFetchInstallableData(),
      base::BindOnce(&AddToHomescreenDataFetcher::OnDidGetInstallableData,
                     weak_ptr_factory_.GetWeakPtr()));
}

void AddToHomescreenDataFetcher::StopTimer() {
  if (data_timeout_timer_.IsRunning()) {
    data_timeout_timer_.Stop();
    RecordAddToHomescreenDialogDuration(base::TimeTicks::Now() - start_time_);
  }
}

void AddToHomescreenDataFetcher::OnDataTimedout() {
  RecordAddToHomescreenDialogDuration(data_timeout_ms_);
  weak_ptr_factory_.InvalidateWeakPtrs();

  if (!web_contents_)
    return;

  installable_status_code_ = InstallableStatusCode::DATA_TIMED_OUT;
  PrepareToAddShortcut();
}

void AddToHomescreenDataFetcher::OnDidGetInstallableData(
    const InstallableData& data) {
  if (!web_contents_)
    return;

  RecordMobileCapableUserActions(
      data.web_page_metadata->mobile_capable,
      /*has_manifest=*/!data.manifest_url->is_empty());

  shortcut_info_.UpdateFromWebPageMetadata(*data.web_page_metadata);
  shortcut_info_.UpdateFromManifest(*data.manifest);
  shortcut_info_.manifest_url = (*data.manifest_url);
  // Save the splash screen URL for the later download.
  shortcut_info_.UpdateBestSplashIcon(*data.manifest);

  installable_manager_->GetData(
      ParamsToFetchPrimaryIcon(),
      base::BindOnce(&AddToHomescreenDataFetcher::OnDidGetPrimaryIcon,
                     weak_ptr_factory_.GetWeakPtr()));
}

void AddToHomescreenDataFetcher::OnDidGetPrimaryIcon(
    const InstallableData& data) {
  if (!web_contents_) {
    return;
  }

  if (!data.primary_icon) {
    installable_status_code_ = data.GetFirstError();
    PrepareToAddShortcut();
    return;
  }

  raw_primary_icon_ = *data.primary_icon;
  shortcut_info_.best_primary_icon_url = (*data.primary_icon_url);
  shortcut_info_.is_primary_icon_maskable = data.has_maskable_primary_icon;

  installable_manager_->GetData(
      ParamsToPerformInstallableCheck(),
      base::BindOnce(&AddToHomescreenDataFetcher::OnDidPerformInstallableCheck,
                     weak_ptr_factory_.GetWeakPtr()));
}

void AddToHomescreenDataFetcher::OnDidPerformInstallableCheck(
    const InstallableData& data) {
  StopTimer();

  if (!web_contents_)
    return;

  bool webapk_compatible =
      (data.errors.empty() && data.installable_check_passed &&
       WebappsUtils::AreWebManifestUrlsWebApkCompatible(*data.manifest));

  installable_status_code_ = data.GetFirstError();

  // Desktop Android is allowed to install incompatible sites as apps.
  // Regular Android installs them as shortcuts.
  // TODO(b/362975239): Fix the compatibility check on desktop Android.
  if (!webapk_compatible &&
      !base::android::BuildInfo::GetInstance()->is_desktop()) {
    PrepareToAddShortcut();
    return;
  }

  shortcut_info_.UpdateDisplayMode(webapk_compatible);

  AddToHomescreenParams::AppType app_type =
      data.manifest_url->is_empty() ? AddToHomescreenParams::AppType::WEBAPK_DIY
                                    : AddToHomescreenParams::AppType::WEBAPK;

  observer_->OnUserTitleAvailable(
      webapk_compatible ? shortcut_info_.name : shortcut_info_.user_title,
      shortcut_info_.url, app_type);

  // WebAPKs should always use the raw icon for the launcher whether or not
  // that icon is maskable.
  primary_icon_ = raw_primary_icon_;
  observer_->OnDataAvailable(shortcut_info_, primary_icon_, app_type,
                             installable_status_code_);
}

void AddToHomescreenDataFetcher::PrepareToAddShortcut() {
  observer_->OnUserTitleAvailable(shortcut_info_.user_title, shortcut_info_.url,
                                  AddToHomescreenParams::AppType::SHORTCUT);
  StopTimer();
  CreateIconForView(raw_primary_icon_);
}

void AddToHomescreenDataFetcher::CreateIconForView(const SkBitmap& base_icon) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // The user is waiting for the icon to be processed before they can proceed
  // with add to homescreen. But if we shut down, there's no point starting the
  // image processing. Use USER_VISIBLE with MayBlock and SKIP_ON_SHUTDOWN.
  base::ThreadPool::PostTask(
      FROM_HERE,
      {base::MayBlock(), base::TaskPriority::USER_VISIBLE,
       base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN},
      base::BindOnce(&WebappsIconUtils::FinalizeLauncherIconInBackground,
                     base_icon, shortcut_info_.url,
                     base::SingleThreadTaskRunner::GetCurrentDefault(),
                     base::BindOnce(&AddToHomescreenDataFetcher::OnIconCreated,
                                    weak_ptr_factory_.GetWeakPtr())));
}

void AddToHomescreenDataFetcher::OnIconCreated(const SkBitmap& icon_for_view,
                                               bool is_icon_generated) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!web_contents_)
    return;

  primary_icon_ = icon_for_view;
  if (is_icon_generated) {
    shortcut_info_.best_primary_icon_url = GURL();
    shortcut_info_.is_primary_icon_maskable = false;
  }

  observer_->OnDataAvailable(shortcut_info_, icon_for_view,
                             AddToHomescreenParams::AppType::SHORTCUT,
                             installable_status_code_);
}

}  // namespace webapps