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
|
// Copyright 2018 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/web_contents/web_app_data_retriever.h"
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.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_icon_generator.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_install_utils.h"
#include "components/webapps/browser/installable/installable_data.h"
#include "components/webapps/browser/installable/installable_logging.h"
#include "components/webapps/browser/installable/installable_manager.h"
#include "components/webapps/browser/installable/installable_params.h"
#include "components/webapps/common/web_app_id.h"
#include "components/webapps/common/web_page_metadata.mojom.h"
#include "components/webapps/common/web_page_metadata_agent.mojom.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
#include "third_party/blink/public/common/manifest/manifest_util.h"
#include "third_party/blink/public/mojom/manifest/manifest.mojom.h"
#include "third_party/skia/include/core/SkColor.h"
namespace web_app {
// static
void WebAppDataRetriever::PopulateWebAppInfoFromMetadata(
WebAppInstallInfo* info,
const webapps::mojom::WebPageMetadata& metadata) {
CHECK(info);
if (!metadata.application_name.empty()) {
info->title = metadata.application_name;
}
if (!metadata.description.empty()) {
info->description = metadata.description;
}
if (metadata.application_url.is_valid()) {
const GURL& start_url = metadata.application_url;
info->SetManifestIdAndStartUrl(
web_app::GenerateManifestIdFromStartUrlOnly(start_url), start_url);
}
for (const auto& icon : metadata.icons) {
apps::IconInfo icon_info;
icon_info.url = icon->url;
if (icon->square_size_px > 0) {
icon_info.square_size_px = icon->square_size_px;
}
info->manifest_icons.push_back(icon_info);
}
switch (metadata.mobile_capable) {
case webapps::mojom::WebPageMobileCapable::UNSPECIFIED:
info->mobile_capable = WebAppInstallInfo::MOBILE_CAPABLE_UNSPECIFIED;
break;
case webapps::mojom::WebPageMobileCapable::ENABLED:
info->mobile_capable = WebAppInstallInfo::MOBILE_CAPABLE;
break;
case webapps::mojom::WebPageMobileCapable::ENABLED_APPLE:
info->mobile_capable = WebAppInstallInfo::MOBILE_CAPABLE_APPLE;
break;
}
}
WebAppDataRetriever::WebAppDataRetriever() = default;
WebAppDataRetriever::~WebAppDataRetriever() = default;
void WebAppDataRetriever::GetWebAppInstallInfo(
content::WebContents* web_contents,
GetWebAppInstallInfoCallback callback) {
DCHECK(!web_contents->IsBeingDestroyed());
Observe(web_contents);
// Concurrent calls are not allowed.
DCHECK(!get_web_app_info_callback_);
get_web_app_info_callback_ = std::move(callback);
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
content::NavigationEntry* entry =
web_contents->GetController().GetLastCommittedEntry();
if (entry->IsInitialEntry()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
// Makes a copy of WebContents fields right after Commit but before a mojo
// request to the renderer process.
GURL start_url = web_contents->GetLastCommittedURL();
webapps::ManifestId manifest_id =
GenerateManifestIdFromStartUrlOnly(start_url);
fallback_install_info_ =
std::make_unique<WebAppInstallInfo>(manifest_id, start_url);
fallback_install_info_->title = web_contents->GetTitle();
if (fallback_install_info_->title.empty()) {
fallback_install_info_->title = base::UTF8ToUTF16(start_url.spec());
}
mojo::AssociatedRemote<webapps::mojom::WebPageMetadataAgent> metadata_agent;
web_contents->GetPrimaryMainFrame()
->GetRemoteAssociatedInterfaces()
->GetInterface(&metadata_agent);
// Set the error handler so that we can run |get_web_app_info_callback_| if
// the WebContents or the RenderFrameHost are destroyed and the connection
// to ChromeRenderFrame is lost.
metadata_agent.set_disconnect_handler(base::BindOnce(
&WebAppDataRetriever::CallCallbackOnError, weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
// Bind the InterfacePtr into the callback so that it's kept alive
// until there's either a connection error or a response.
auto* web_page_metadata_proxy = metadata_agent.get();
web_page_metadata_proxy->GetWebPageMetadata(
base::BindOnce(&WebAppDataRetriever::OnGetWebPageMetadata,
weak_ptr_factory_.GetWeakPtr(), std::move(metadata_agent),
entry->GetUniqueID()));
}
void WebAppDataRetriever::CheckInstallabilityAndRetrieveManifest(
content::WebContents* web_contents,
CheckInstallabilityCallback callback,
std::optional<webapps::InstallableParams> params) {
DCHECK(!web_contents->IsBeingDestroyed());
Observe(web_contents);
// Concurrent calls are not allowed.
DCHECK(!check_installability_callback_);
check_installability_callback_ = std::move(callback);
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
// TODO(crbug.com/41380939) Unify with other calls to GetData.
if (!params.has_value()) {
webapps::InstallableParams data_params;
data_params.check_eligibility = true;
data_params.valid_primary_icon = true;
data_params.installable_criteria =
webapps::InstallableCriteria::kValidManifestIgnoreDisplay;
params = data_params;
}
webapps::InstallableManager* installable_manager =
webapps::InstallableManager::FromWebContents(web_contents);
DCHECK(installable_manager);
// Do not wait_for_worker. OnDidPerformInstallableCheck is always invoked.
installable_manager->GetData(
params.value(),
base::BindOnce(&WebAppDataRetriever::OnDidPerformInstallableCheck,
weak_ptr_factory_.GetWeakPtr()));
}
void WebAppDataRetriever::GetIcons(content::WebContents* web_contents,
const IconUrlSizeSet& extra_icon_urls,
bool skip_page_favicons,
bool fail_all_if_any_fail,
GetIconsCallback callback) {
DCHECK(!web_contents->IsBeingDestroyed());
Observe(web_contents);
// Concurrent calls are not allowed.
CHECK(!get_icons_callback_);
get_icons_callback_ = std::move(callback);
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
IconDownloaderOptions options = {
.skip_page_favicons = skip_page_favicons,
.fail_all_if_any_fail = fail_all_if_any_fail};
icon_downloader_ = std::make_unique<WebAppIconDownloader>();
icon_downloader_->Start(
web_contents, extra_icon_urls,
base::BindOnce(&WebAppDataRetriever::OnIconsDownloaded,
weak_ptr_factory_.GetWeakPtr()),
options);
}
void WebAppDataRetriever::WebContentsDestroyed() {
Observe(nullptr);
// Avoid initiating new work during web contents destruction.
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
}
void WebAppDataRetriever::PrimaryMainFrameRenderProcessGone(
base::TerminationStatus status) {
CallCallbackOnError(webapps::InstallableStatusCode::RENDERER_CANCELLED);
}
void WebAppDataRetriever::OnGetWebPageMetadata(
mojo::AssociatedRemote<webapps::mojom::WebPageMetadataAgent> metadata_agent,
int last_committed_nav_entry_unique_id,
webapps::mojom::WebPageMetadataPtr metadata) {
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
DCHECK(fallback_install_info_);
content::WebContents* contents = web_contents();
Observe(nullptr);
content::NavigationEntry* entry =
contents->GetController().GetLastCommittedEntry();
CHECK(!get_web_app_info_callback_.is_null());
if (entry->IsInitialEntry()) {
// Possibly impossible to get to this state, treat it as an error.
fallback_install_info_.reset();
std::move(get_web_app_info_callback_).Run(nullptr);
return;
}
if (entry->GetUniqueID() != last_committed_nav_entry_unique_id) {
// WebContents navigation state changed during the call. Ignore the mojo
// request result and use default initial info instead.
std::move(get_web_app_info_callback_)
.Run(std::move(fallback_install_info_));
return;
}
CHECK(metadata);
std::unique_ptr<WebAppInstallInfo> info = std::move(fallback_install_info_);
PopulateWebAppInfoFromMetadata(info.get(), *metadata);
std::move(get_web_app_info_callback_).Run(std::move(info));
}
void WebAppDataRetriever::OnDidPerformInstallableCheck(
const webapps::InstallableData& data) {
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
Observe(nullptr);
const bool is_installable = data.errors.empty();
CHECK(!is_installable || data.installable_check_passed);
blink::mojom::ManifestPtr opt_manifest;
if (!blink::IsEmptyManifest(*data.manifest)) {
opt_manifest = data.manifest->Clone();
}
CHECK(!check_installability_callback_.is_null());
std::move(check_installability_callback_)
.Run(std::move(opt_manifest), data.installable_check_passed,
data.GetFirstError());
}
void WebAppDataRetriever::OnIconsDownloaded(
IconsDownloadedResult result,
IconsMap icons_map,
DownloadedIconsHttpResults icons_http_results) {
if (ShouldStopRetrieval()) {
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&WebAppDataRetriever::CallCallbackOnError,
weak_ptr_factory_.GetWeakPtr(),
webapps::InstallableStatusCode::RENDERER_CANCELLED));
return;
}
Observe(nullptr);
icon_downloader_.reset();
DCHECK(!get_icons_callback_.is_null());
std::move(get_icons_callback_)
.Run(result, std::move(icons_map), std::move(icons_http_results));
}
void WebAppDataRetriever::CallCallbackOnError(
webapps::InstallableStatusCode error_code) {
Observe(nullptr);
DCHECK(ShouldStopRetrieval());
icon_downloader_.reset();
fallback_install_info_.reset();
weak_ptr_factory_.InvalidateWeakPtrs();
// Call a callback as a tail call. The callback may destroy |this|.
if (get_web_app_info_callback_) {
std::move(get_web_app_info_callback_).Run(nullptr);
} else if (check_installability_callback_) {
std::move(check_installability_callback_)
.Run(/*manifest=*/nullptr,
/*installable_check_passed_for_web_app=*/false,
/*error_code=*/
error_code);
} else if (get_icons_callback_) {
std::move(get_icons_callback_)
.Run(IconsDownloadedResult::kPrimaryPageChanged, IconsMap{},
DownloadedIconsHttpResults{});
}
}
// TODO(b/302531937): Make this a utility that can be used through out the
// web_applications/ system.
bool WebAppDataRetriever::ShouldStopRetrieval() const {
return !web_contents() || web_contents()->IsBeingDestroyed() ||
web_contents()->GetBrowserContext()->ShutdownStarted();
}
} // namespace web_app
|