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
|
// Copyright 2023 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/isolated_web_app_downloader.h"
#include <memory>
#include <optional>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/thread_pool.h"
#include "net/base/net_errors.h"
#include "net/http/http_request_headers.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "url/gurl.h"
namespace web_app {
void ScopedTempWebBundleFile::Create(
base::OnceCallback<void(ScopedTempWebBundleFile)> callback) {
base::ThreadPool::PostTaskAndReplyWithResult(
FROM_HERE, {base::MayBlock()},
base::BindOnce([]() -> ScopedTempWebBundleFile {
auto file = std::make_unique<base::ScopedTempFile>();
if (!file->Create()) {
return ScopedTempWebBundleFile(/*file=*/nullptr);
}
return ScopedTempWebBundleFile(std::move(file));
}),
std::move(callback));
}
ScopedTempWebBundleFile::ScopedTempWebBundleFile(
std::unique_ptr<base::ScopedTempFile> file)
: file_(std::move(file)) {}
ScopedTempWebBundleFile::~ScopedTempWebBundleFile() {
if (!file_) {
return;
}
// Deleting the file must happen on a thread that allows blocking.
base::ThreadPool::PostTask(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(
[](std::unique_ptr<base::ScopedTempFile> file) {
// `file` is deleted here.
},
std::move(file_)));
}
ScopedTempWebBundleFile& ScopedTempWebBundleFile::operator=(
ScopedTempWebBundleFile&&) = default;
ScopedTempWebBundleFile::ScopedTempWebBundleFile(ScopedTempWebBundleFile&&) =
default;
const base::FilePath& ScopedTempWebBundleFile::path() const {
CHECK(file_) << "`path()` must not be called on a nullptr `file_`.";
return file_->path();
}
// static
std::unique_ptr<IsolatedWebAppDownloader> IsolatedWebAppDownloader::Create(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
return base::WrapUnique(
new IsolatedWebAppDownloader(std::move(url_loader_factory)));
}
// static
std::unique_ptr<IsolatedWebAppDownloader>
IsolatedWebAppDownloader::CreateAndStartDownloading(
GURL url,
base::FilePath destination,
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
IsolatedWebAppDownloader::DownloadCallback download_callback) {
auto downloader = Create(std::move(url_loader_factory));
downloader->DownloadSignedWebBundle(std::move(url), std::move(destination),
std::move(partial_traffic_annotation),
std::move(download_callback));
return downloader;
}
IsolatedWebAppDownloader::IsolatedWebAppDownloader(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory)
: url_loader_factory_(std::move(url_loader_factory)) {}
IsolatedWebAppDownloader::~IsolatedWebAppDownloader() = default;
namespace {
net::NetworkTrafficAnnotationTag GetNetworkTrafficAnnotationTag(
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation) {
return net::CompleteNetworkTrafficAnnotation("iwa_bundle_downloader",
partial_traffic_annotation,
R"(
semantics {
data:
"This request does not send any user data. Its destination is the URL "
"of a Signed Web Bundle of an Isolated Web App that is installed for "
"the user."
destination: OTHER
internal {
contacts {
owners: "//chrome/browser/web_applications/isolated_web_apps/OWNERS"
}
}
user_data {
type: NONE
}
last_reviewed: "2023-06-01"
}
policy {
cookies_allowed: NO
})");
}
} // namespace
void IsolatedWebAppDownloader::DownloadSignedWebBundle(
GURL url,
base::FilePath destination,
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation,
DownloadCallback download_callback) {
net::NetworkTrafficAnnotationTag traffic_annotation =
GetNetworkTrafficAnnotationTag(std::move(partial_traffic_annotation));
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = url;
// Cookies are not allowed.
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), std::move(traffic_annotation));
simple_url_loader_->SetRetryOptions(
/* max_retries=*/3,
network::SimpleURLLoader::RETRY_ON_5XX |
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
simple_url_loader_->DownloadToFile(
url_loader_factory_.get(),
base::BindOnce(&IsolatedWebAppDownloader::OnSignedWebBundleDownloaded,
// The callback will never run if `this` is deleted,
// because `simple_url_loader_` is a member of `this`.
base::Unretained(this), destination)
.Then(std::move(download_callback)),
destination);
}
void IsolatedWebAppDownloader::DownloadInitialBytes(
GURL url,
net::PartialNetworkTrafficAnnotationTag partial_traffic_annotation,
PartialDownloadCallback download_callback) {
net::NetworkTrafficAnnotationTag traffic_annotation =
GetNetworkTrafficAnnotationTag(std::move(partial_traffic_annotation));
// 8 KiB - this should be enough to contain the entire integrity block of any
// signed web bundle. While it is technically possible to create a bigger one,
// there's no reason to do that. In such case, if it happens, this whole
// heuristic is skipped and the entire bundle is downloaded.
constexpr size_t kMaxInitialBytes = 8 * 1024;
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->url = std::move(url);
// Cookies are not allowed.
resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
resource_request->headers.SetHeader(
net::HttpRequestHeaders::kRange,
net::HttpByteRange::Bounded(0, kMaxInitialBytes - 1).GetHeaderValue());
simple_url_loader_ = network::SimpleURLLoader::Create(
std::move(resource_request), std::move(traffic_annotation));
simple_url_loader_->SetRetryOptions(
/* max_retries=*/3,
network::SimpleURLLoader::RETRY_ON_5XX |
network::SimpleURLLoader::RETRY_ON_NETWORK_CHANGE);
simple_url_loader_->SetAllowPartialResults(true);
simple_url_loader_->DownloadToString(url_loader_factory_.get(),
std::move(download_callback),
kMaxInitialBytes);
}
int32_t IsolatedWebAppDownloader::OnSignedWebBundleDownloaded(
base::FilePath destination,
base::FilePath actual_destination) {
if (actual_destination.empty()) {
int32_t net_error = simple_url_loader_->NetError();
CHECK_NE(net_error, net::OK);
return net_error;
}
CHECK_EQ(actual_destination, destination);
return net::OK;
}
} // namespace web_app
|