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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/manifest/manifest_manager.h"
#include <utility>
#include "base/functional/bind.h"
#include "third_party/blink/public/platform/interface_registry.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/frame_console.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame_client.h"
#include "third_party/blink/renderer/core/frame/web_feature.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/html/html_link_element.h"
#include "third_party/blink/renderer/core/inspector/console_message.h"
#include "third_party/blink/renderer/modules/manifest/manifest_change_notifier.h"
#include "third_party/blink/renderer/modules/manifest/manifest_fetcher.h"
#include "third_party/blink/renderer/modules/manifest/manifest_parser.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
namespace blink {
ManifestManager::Result::Result(mojom::blink::ManifestRequestResult result,
KURL manifest_url,
mojom::blink::ManifestPtr manifest)
: result_(result),
manifest_url_(manifest_url),
manifest_(manifest ? std::move(manifest) : mojom::blink::Manifest::New()),
debug_info_(mojom::blink::ManifestDebugInfo::New()) {
// The default constructor for ManifestDebugInfo does not initialize
// `raw_manifest` with a valid value, so do so here instead.
debug_info_->raw_manifest = "";
}
ManifestManager::Result::Result(Result&&) = default;
ManifestManager::Result& ManifestManager::Result::operator=(Result&&) = default;
void ManifestManager::Result::SetManifest(mojom::blink::ManifestPtr manifest) {
CHECK(manifest);
manifest_ = std::move(manifest);
}
// static
const char ManifestManager::kSupplementName[] = "ManifestManager";
// static
void WebManifestManager::RequestManifestForTesting(WebLocalFrame* web_frame,
Callback callback) {
auto* window = To<WebLocalFrameImpl>(web_frame)->GetFrame()->DomWindow();
ManifestManager* manifest_manager = ManifestManager::From(*window);
manifest_manager->RequestManifestForTesting(std::move(callback));
}
// static
ManifestManager* ManifestManager::From(LocalDOMWindow& window) {
auto* manager = Supplement<LocalDOMWindow>::From<ManifestManager>(window);
if (!manager) {
manager = MakeGarbageCollected<ManifestManager>(window);
Supplement<LocalDOMWindow>::ProvideTo(window, manager);
}
return manager;
}
ManifestManager::ManifestManager(LocalDOMWindow& window)
: Supplement<LocalDOMWindow>(window),
ExecutionContextLifecycleObserver(&window),
receivers_(this, GetExecutionContext()) {
if (window.GetFrame()->IsMainFrame()) {
manifest_change_notifier_ =
MakeGarbageCollected<ManifestChangeNotifier>(window);
window.GetFrame()->GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
&ManifestManager::BindReceiver, WrapWeakPersistent(this)));
}
}
ManifestManager::~ManifestManager() = default;
void ManifestManager::RequestManifest(RequestManifestCallback callback) {
RequestManifestImpl(WTF::BindOnce(
[](RequestManifestCallback callback, const Result& result) {
std::move(callback).Run(result.result(), result.manifest_url(),
result.manifest().Clone());
},
std::move(callback)));
}
void ManifestManager::RequestManifestDebugInfo(
RequestManifestDebugInfoCallback callback) {
RequestManifestImpl(WTF::BindOnce(
[](RequestManifestDebugInfoCallback callback, const Result& result) {
std::move(callback).Run(result.manifest_url(),
result.manifest().Clone(),
result.debug_info().Clone());
},
std::move(callback)));
}
void ManifestManager::ParseManifestFromString(
const KURL& document_url,
const KURL& manifest_url,
const String& manifest_contents,
ParseManifestFromStringCallback callback) {
ManifestParser parser(manifest_contents, manifest_url, document_url,
GetExecutionContext());
parser.Parse();
mojom::blink::ManifestPtr result;
if (!parser.failed()) {
result = parser.TakeManifest();
}
std::move(callback).Run(std::move(result));
}
void ManifestManager::RequestManifestForTesting(
WebManifestManager::Callback callback) {
RequestManifestImpl(WTF::BindOnce(
[](WebManifestManager::Callback callback, const Result& result) {
std::move(callback).Run(result.manifest_url());
},
std::move(callback)));
}
bool ManifestManager::CanFetchManifest() {
// Do not fetch the manifest if we are on an opaque origin.
return !GetSupplementable()->GetSecurityOrigin()->IsOpaque() &&
GetSupplementable()->Url().IsValid();
}
void ManifestManager::RequestManifestImpl(
InternalRequestManifestCallback callback) {
if (!GetSupplementable()->GetFrame()) {
std::move(callback).Run(
Result(mojom::blink::ManifestRequestResult::kUnexpectedFailure));
return;
}
if (cached_result_) {
std::move(callback).Run(*cached_result_);
return;
}
pending_callbacks_.push_back(std::move(callback));
// Just wait for the running call to be done if there are other callbacks.
if (pending_callbacks_.size() > 1)
return;
FetchManifest();
}
void ManifestManager::DidChangeManifest() {
cached_result_.reset();
if (manifest_change_notifier_) {
manifest_change_notifier_->DidChangeManifest();
}
}
void ManifestManager::FetchManifest() {
if (!CanFetchManifest()) {
ResolveCallbacks(
Result(mojom::blink::ManifestRequestResult::kNoManifestAllowed,
ManifestURL()));
return;
}
LocalDOMWindow& window = *GetSupplementable();
KURL manifest_url = ManifestURL();
if (manifest_url.IsEmpty()) {
ResolveCallbacks(
Result(mojom::blink::ManifestRequestResult::kNoManifestSpecified,
KURL(), DefaultManifest()));
return;
}
ResourceFetcher* document_fetcher = window.document()->Fetcher();
fetcher_ = MakeGarbageCollected<ManifestFetcher>(manifest_url);
fetcher_->Start(window, ManifestUseCredentials(), document_fetcher,
WTF::BindOnce(&ManifestManager::OnManifestFetchComplete,
WrapWeakPersistent(this), window.Url()));
}
void ManifestManager::OnManifestFetchComplete(const KURL& document_url,
const ResourceResponse& response,
const String& data) {
fetcher_ = nullptr;
if (response.IsNull() && data.empty()) {
// The only time we don't produce the default manifest is when there is a
// resource fetching problem of the manifest link. This allows callers to
// catch this error appropriately as a network issue instead of using a
// 'default' manifest that wasn't intended by the developer.
ResolveCallbacks(
Result(mojom::blink::ManifestRequestResult::kManifestFailedToFetch,
response.CurrentRequestUrl(), DefaultManifest()));
return;
}
// 3** range, redirects, should not be considered as failure, load still can
// be successful. Test to see this:
// PwaInstallViewBrowserTest.ListedRelatedChromeAppInstalled
if (response.HttpStatusCode() >= 200 && response.HttpStatusCode() < 400) {
ParseManifestFromPage(document_url, response.CurrentRequestUrl(), data);
} else {
const String message = WTF::String::Format(
"Manifest fetch from %s failed, code %d",
response.CurrentRequestUrl().GetString().Utf8().c_str(),
response.HttpStatusCode());
GetSupplementable()->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
mojom::blink::ConsoleMessageSource::kOther,
mojom::blink::ConsoleMessageLevel::kError, message,
CaptureSourceLocation()));
ResolveCallbacks(
Result(mojom::blink::ManifestRequestResult::kManifestFailedToFetch,
response.CurrentRequestUrl(), DefaultManifest()));
}
}
void ManifestManager::ParseManifestFromPage(const KURL& document_url,
std::optional<KURL> manifest_url,
const String& data) {
CHECK(document_url.IsValid());
// We are using the document as our FeatureContext for checking origin trials.
// Note that any origin trials delivered in the manifest HTTP headers will be
// ignored, only ones associated with the page will be used.
// For default manifests, the manifest_url is `std::nullopt`, so use the
// document_url instead for the parsing algorithm.
ManifestParser parser(data, manifest_url.value_or(document_url), document_url,
GetExecutionContext());
// Monitoring whether the manifest has comments is temporary. Once
// warning/deprecation period is over, we should remove this as it's
// technically incorrect JSON syntax anyway. See crbug.com/1264024
bool has_comments = parser.Parse();
if (has_comments) {
UseCounter::Count(GetSupplementable(),
WebFeature::kWebAppManifestHasComments);
}
const bool failed = parser.failed();
Result result(
failed ? mojom::blink::ManifestRequestResult::kManifestFailedToParse
: mojom::blink::ManifestRequestResult::kSuccess,
manifest_url.value_or(KURL()));
result.debug_info().raw_manifest = data.IsNull() ? "" : data;
parser.TakeErrors(&result.debug_info().errors);
for (const auto& error : result.debug_info().errors) {
auto location = std::make_unique<SourceLocation>(ManifestURL().GetString(),
String(), error->line,
error->column, nullptr, 0);
GetSupplementable()->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
mojom::blink::ConsoleMessageSource::kOther,
error->critical ? mojom::blink::ConsoleMessageLevel::kError
: mojom::blink::ConsoleMessageLevel::kWarning,
"Manifest: " + error->message, std::move(location)));
}
// Having errors while parsing the manifest doesn't mean the manifest parsing
// failed. Some properties might have been ignored but some others kept.
if (failed) {
result.SetManifest(DefaultManifest());
ResolveCallbacks(std::move(result));
return;
}
result.SetManifest(parser.TakeManifest());
// We should always have a start_url, manifest_id, and scope, as any errors
// still have fallbacks back to the document_url.
CHECK(!result.manifest().start_url.IsEmpty() &&
result.manifest().start_url.IsValid());
CHECK(!result.manifest().id.IsEmpty() && result.manifest().id.IsValid());
CHECK(!result.manifest().scope.IsEmpty() &&
result.manifest().scope.IsValid());
// At this point, the manifest is validly parsed, and is not the default one.
UseCounter::CountWebDXFeature(GetSupplementable(), WebDXFeature::kManifest);
ResolveCallbacks(std::move(result));
}
void ManifestManager::ResolveCallbacks(Result result) {
Vector<InternalRequestManifestCallback> callbacks;
callbacks.swap(pending_callbacks_);
// URLs that are too long are silently truncated by the mojo serialization.
// Since that might violate invariants the manifest is expected to have, check
// if any URLs would be too long and return an error instead if that is the
// case.
const bool has_overlong_urls =
result.manifest().manifest_url.GetString().length() > url::kMaxURLChars ||
result.manifest().id.GetString().length() > url::kMaxURLChars ||
result.manifest().start_url.GetString().length() > url::kMaxURLChars ||
result.manifest().scope.GetString().length() > url::kMaxURLChars;
if (has_overlong_urls) {
result = Result(mojom::blink::ManifestRequestResult::kUnexpectedFailure);
}
const Result* result_ptr = nullptr;
if (result.result() == mojom::blink::ManifestRequestResult::kSuccess) {
cached_result_ = std::move(result);
result_ptr = &cached_result_.value();
} else {
result_ptr = &result;
}
for (auto& callback : callbacks) {
std::move(callback).Run(*result_ptr);
}
}
KURL ManifestManager::ManifestURL() const {
HTMLLinkElement* link_element =
GetSupplementable()->document()->LinkManifest();
if (!link_element)
return KURL();
return link_element->Href();
}
bool ManifestManager::ManifestUseCredentials() const {
HTMLLinkElement* link_element =
GetSupplementable()->document()->LinkManifest();
if (!link_element)
return false;
return EqualIgnoringASCIICase(
link_element->FastGetAttribute(html_names::kCrossoriginAttr),
"use-credentials");
}
void ManifestManager::BindReceiver(
mojo::PendingReceiver<mojom::blink::ManifestManager> receiver) {
receivers_.Add(std::move(receiver),
GetSupplementable()->GetTaskRunner(TaskType::kNetworking));
}
mojom::blink::ManifestPtr ManifestManager::DefaultManifest() {
// Generate the default manifest for failures, and use the current window url
// as the manifest_url for resolving resources in the default manifest.
LocalDOMWindow& window = *GetSupplementable();
ManifestParser parser(/*data=*/"{ }", /*manifest_url=*/window.Url(),
/*document_url=*/window.Url(), GetExecutionContext());
parser.Parse();
CHECK(!parser.failed());
auto result = parser.TakeManifest();
// Reset manifest_url in the parsed manifest, as the window url isn't really
// the url for this manifest.
result->manifest_url = KURL();
return result;
}
void ManifestManager::ContextDestroyed() {
if (fetcher_)
fetcher_->Cancel();
// Consumers in the browser process will not receive this message but they
// will be aware of the RenderFrame dying and should act on that. Consumers
// in the renderer process should be correctly notified.
ResolveCallbacks(
Result(mojom::blink::ManifestRequestResult::kUnexpectedFailure));
}
void ManifestManager::Trace(Visitor* visitor) const {
visitor->Trace(fetcher_);
visitor->Trace(manifest_change_notifier_);
visitor->Trace(receivers_);
Supplement<LocalDOMWindow>::Trace(visitor);
ExecutionContextLifecycleObserver::Trace(visitor);
}
} // namespace blink
|