#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_loader_factory.h"
#include <memory>
#include <optional>
#include <vector>
#include "base/containers/span.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/functional/overloaded.h"
#include "base/location.h"
#include "base/memory/scoped_refptr.h"
#include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/types/expected.h"
#include "base/types/expected_macros.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_apply_update_command.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_reader_registry_factory.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_response_reader.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_storage_location.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/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/common/url_constants.h"
#include "components/web_package/mojom/web_bundle_parser.mojom.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "components/web_package/web_bundle_utils.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "mojo/public/cpp/bindings/self_owned_receiver.h"
#include "mojo/public/cpp/system/data_pipe.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "net/http/http_util.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/constants.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/parsed_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/early_hints.mojom.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_loader_completion_status.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace web_app {
namespace {
constexpr char kInstallPagePath[] = …;
constexpr char kInstallPageContent[] = …;
constexpr char kIsolatedAppCspTemplate[] = …;
bool IsSupportedHttpMethod(const std::string& method) { … }
const std::string& GetDefaultCsp() { … }
std::optional<std::string> ComputeCspOverride(const IwaSourceWithMode& source) { … }
void CompleteWithGeneratedResponse(
mojo::Remote<network::mojom::URLLoaderClient> loader_client,
net::HttpStatusCode http_status_code,
std::optional<std::string> body = std::nullopt,
std::string_view content_type = "text/html") { … }
void LogErrorMessageToConsole(std::optional<int> frame_tree_node_id,
const std::string& error_message) { … }
base::expected<std::reference_wrapper<const WebApp>, std::string>
FindIsolatedWebApp(WebAppProvider& provider,
const IsolatedWebAppUrlInfo& url_info) { … }
class HeaderInjectionURLLoaderClient : public network::mojom::URLLoaderClient { … };
class IsolatedWebAppURLLoader : public network::mojom::URLLoader { … };
}
IsolatedWebAppURLLoaderFactory::IsolatedWebAppURLLoaderFactory(
Profile* profile,
std::optional<url::Origin> app_origin,
std::optional<int> frame_tree_node_id,
mojo::PendingReceiver<network::mojom::URLLoaderFactory> factory_receiver)
: … { … }
IsolatedWebAppURLLoaderFactory::~IsolatedWebAppURLLoaderFactory() = default;
void IsolatedWebAppURLLoaderFactory::CreateLoaderAndStart(
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
int32_t request_id,
uint32_t options,
const network::ResourceRequest& resource_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> original_loader_client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { … }
void IsolatedWebAppURLLoaderFactory::HandleRequest(
const IsolatedWebAppUrlInfo& url_info,
const IwaSourceWithMode& source,
bool is_pending_install,
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
const network::ResourceRequest& resource_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> loader_client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { … }
void IsolatedWebAppURLLoaderFactory::OnProfileWillBeDestroyed(
Profile* profile) { … }
void IsolatedWebAppURLLoaderFactory::HandleSignedBundle(
const base::FilePath& path,
bool dev_mode,
const web_package::SignedWebBundleId& web_bundle_id,
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
const network::ResourceRequest& resource_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> loader_client) { … }
namespace {
GURL ConstructProxyUrl(const IwaSourceProxy& proxy,
const GURL& resource_request_url) { … }
network::ResourceRequest ConstructProxyRequest(
const IwaSourceProxy& proxy,
const network::ResourceRequest& resource_request) { … }
}
void IsolatedWebAppURLLoaderFactory::HandleProxy(
const IsolatedWebAppUrlInfo& url_info,
const IwaSourceProxy& proxy,
mojo::PendingReceiver<network::mojom::URLLoader> loader_receiver,
const network::ResourceRequest& resource_request,
mojo::PendingRemote<network::mojom::URLLoaderClient> loader_client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) { … }
void IsolatedWebAppURLLoaderFactory::LogErrorAndFail(
const std::string& error_message,
mojo::PendingRemote<network::mojom::URLLoaderClient> client) { … }
bool IsolatedWebAppURLLoaderFactory::CanRequestUrl(const GURL& url) const { … }
mojo::PendingRemote<network::mojom::URLLoaderFactory>
IsolatedWebAppURLLoaderFactory::CreateForFrame(
content::BrowserContext* browser_context,
std::optional<url::Origin> app_origin,
int frame_tree_node_id) { … }
mojo::PendingRemote<network::mojom::URLLoaderFactory>
IsolatedWebAppURLLoaderFactory::Create(content::BrowserContext* browser_context,
std::optional<url::Origin> app_origin) { … }
mojo::PendingRemote<network::mojom::URLLoaderFactory>
IsolatedWebAppURLLoaderFactory::CreateInternal(
content::BrowserContext* browser_context,
std::optional<url::Origin> app_origin,
std::optional<int> frame_tree_node_id) { … }
}