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
|
// Copyright 2025 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/isolated_web_apps/chrome_iwa_client.h"
#include "base/strings/stringprintf.h"
#include "base/types/expected_macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/isolated_web_apps/commands/isolated_web_app_install_command_helper.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_features.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_trust_checker.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_update_manager.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/pending_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/url_constants.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "components/webapps/isolated_web_apps/types/url_loading_types.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/isolated_web_apps_policy.h"
#include "content/public/browser/storage_partition_config.h"
#include "content/public/browser/web_contents.h"
namespace web_app {
namespace {
constexpr char kInstallPagePath[] = "/.well-known/_generated_install_page.html";
constexpr char kInstallPageContent[] = R"(
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">
<link rel="manifest" href="/.well-known/manifest.webmanifest" />
</head>
</html>
)";
base::expected<IwaSourceWithModeOrGeneratedResponse, std::string> GetIwaSource(
base::WeakPtr<Profile> profile,
const webapps::AppId& iwa_id) {
if (!profile) {
return base::unexpected("Profile has shut down.");
}
WebAppRegistrar& registrar =
WebAppProvider::GetForWebApps(profile.get())->registrar_unsafe();
const WebApp* iwa = registrar.GetAppById(iwa_id);
if (!iwa || !iwa->isolation_data()) {
return base::unexpected(
base::StringPrintf("There's no matching Isolated Web App installed."));
}
if (iwa->isolation_data()->location().dev_mode() &&
!IsIwaDevModeEnabled(profile.get())) {
return base::unexpected(
"Isolated Web App Developer Mode is not enabled or blocked by policy.");
}
return IwaSourceWithMode::FromStorageLocation(
profile->GetPath(), iwa->isolation_data()->location());
}
void GetIwaSourceForRequestImpl(
base::WeakPtr<Profile> profile,
const web_package::SignedWebBundleId& web_bundle_id,
const network::ResourceRequest& request,
const std::optional<content::FrameTreeNodeId>& frame_tree_node,
base::OnceCallback<void(base::expected<IwaSourceWithModeOrGeneratedResponse,
std::string>)> callback) {
if (!profile) {
std::move(callback).Run(base::unexpected("Profile has shut down."));
return;
}
if (frame_tree_node) {
auto* web_contents =
content::WebContents::FromFrameTreeNodeId(*frame_tree_node);
if (web_contents == nullptr) {
// `web_contents` can be `nullptr` in certain edge cases, such as when
// the browser window closes concurrently with an ongoing request (see
// crbug.com/1477761). Return an error if that is the case, instead of
// silently not querying `IsolatedWebAppPendingInstallInfo`. Should we
// ever find a case where we _do_ want to continue request processing
// even though the `WebContents` no longer exists, we can change the
// below code to skip checking `IsolatedWebAppPendingInstallInfo`
// instead of returning an error.
std::move(callback).Run(base::unexpected(
"Unable to find WebContents based on frame tree node id."));
return;
}
if (const auto& source =
IsolatedWebAppPendingInstallInfo::FromWebContents(*web_contents)
.source()) {
if (request.url.path() == kInstallPagePath) {
std::move(callback).Run(
GeneratedResponse{.response_body = kInstallPageContent});
} else {
std::move(callback).Run(*source);
}
return;
}
}
webapps::AppId iwa_id =
IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(web_bundle_id)
.app_id();
auto* provider = WebAppProvider::GetForWebApps(profile.get());
if (provider->iwa_update_manager().IsUpdateBeingApplied(iwa_id)) {
// TODO(crbug.com/432676258): How likely is this case?
provider->iwa_update_manager().PrioritizeUpdateAndWait(
iwa_id,
// We ignore whether or not the update was applied successfully - if it
// succeeds, we send the request to the updated version. If it fails, we
// send the request to the previous version and rely on the update
// system to retry the update at a later point.
base::IgnoreArgs<IsolatedWebAppUpdateApplyTask::CompletionStatus>(
base::BindOnce(&GetIwaSource, profile, iwa_id)
.Then(std::move(callback))));
return;
}
return std::move(callback).Run(GetIwaSource(profile, iwa_id));
}
} // namespace
void ChromeIwaClient::CreateSingleton() {
static base::NoDestructor<ChromeIwaClient> instance;
instance.get();
}
base::expected<void, std::string> ChromeIwaClient::ValidateTrust(
content::BrowserContext* browser_context,
const web_package::SignedWebBundleId& web_bundle_id,
bool dev_mode) {
return IsolatedWebAppTrustChecker::IsTrusted(
*Profile::FromBrowserContext(browser_context), web_bundle_id, dev_mode);
}
base::expected<web_package::SignedWebBundleId, std::string>
ChromeIwaClient::CreateWebBundleIdFromURL(const GURL& url) {
return IsolatedWebAppUrlInfo::Create(url).transform(
[](const auto& url_info) { return url_info.web_bundle_id(); });
}
GURL ChromeIwaClient::CreateBaseURLForWebBundleId(
const web_package::SignedWebBundleId& web_bundle_id) {
return GURL(
base::StrCat({chrome::kIsolatedAppScheme, url::kStandardSchemeSeparator,
web_bundle_id.id()}));
}
void ChromeIwaClient::RunWhenAppCloses(
content::BrowserContext* browser_context,
const web_package::SignedWebBundleId& web_bundle_id,
base::OnceClosure callback) {
WebAppProvider::GetForWebApps(Profile::FromBrowserContext(browser_context))
->ui_manager()
.NotifyOnAllAppWindowsClosed(
IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(web_bundle_id)
.app_id(),
std::move(callback));
}
void ChromeIwaClient::GetIwaSourceForRequest(
content::BrowserContext* browser_context,
const web_package::SignedWebBundleId& web_bundle_id,
const network::ResourceRequest& request,
const std::optional<content::FrameTreeNodeId>& frame_tree_node,
base::OnceCallback<void(base::expected<IwaSourceWithModeOrGeneratedResponse,
std::string>)> callback) {
Profile* profile = Profile::FromBrowserContext(browser_context);
WebAppProvider::GetForWebApps(profile)->on_registry_ready().Post(
FROM_HERE, base::BindOnce(&GetIwaSourceForRequestImpl,
profile->GetWeakPtr(), web_bundle_id, request,
frame_tree_node, std::move(callback)));
}
content::StoragePartition* ChromeIwaClient::GetStoragePartition(
content::BrowserContext* browser_context,
const web_package::SignedWebBundleId& web_bundle_id) {
return browser_context->GetStoragePartition(
IsolatedWebAppUrlInfo::CreateFromSignedWebBundleId(web_bundle_id)
.storage_partition_config(browser_context),
/*can_create=*/false);
}
} // namespace web_app
|