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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/service_worker/service_worker_client.h"
#include <set>
#include <variant>
#include "base/check_is_test.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/debug/alias.h"
#include "base/debug/crash_logging.h"
#include "base/notreached.h"
#include "base/task/single_thread_task_runner.h"
#include "base/types/optional_util.h"
#include "base/uuid.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/loader/navigation_url_loader_impl.h"
#include "content/browser/preloading/prefetch/prefetch_features.h"
#include "content/browser/renderer_host/frame_tree_node.h"
#include "content/browser/renderer_host/render_frame_host_impl.h"
#include "content/browser/service_worker/service_worker_container_host.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/browser/service_worker/service_worker_security_utils.h"
#include "content/browser/worker_host/dedicated_worker_host.h"
#include "content/browser/worker_host/dedicated_worker_service_impl.h"
#include "content/browser/worker_host/shared_worker_host.h"
#include "content/browser/worker_host/shared_worker_service_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/common/content_client.h"
#include "content/public/common/origin_util.h"
#include "mojo/public/cpp/bindings/remote_set.h"
#include "net/base/url_util.h"
#include "services/network/public/cpp/single_request_url_loader_factory.h"
#include "services/network/public/cpp/url_loader_factory_builder.h"
#include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "third_party/blink/public/common/service_worker/service_worker_scope_match.h"
#include "third_party/blink/public/common/storage_key/storage_key.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_running_status_callback.mojom.h"
namespace content {
namespace {
void RunCallbacks(
std::vector<ServiceWorkerClient::ExecutionReadyCallback> callbacks) {
for (auto& callback : callbacks) {
std::move(callback).Run();
}
}
} // namespace
// RAII helper class for keeping track of versions waiting for an update hint
// from the renderer.
//
// This class is move-only.
class ServiceWorkerClient::PendingUpdateVersion {
public:
explicit PendingUpdateVersion(scoped_refptr<ServiceWorkerVersion> version)
: version_(std::move(version)) {
version_->IncrementPendingUpdateHintCount();
}
PendingUpdateVersion(PendingUpdateVersion&& other) {
version_ = std::move(other.version_);
}
PendingUpdateVersion(const PendingUpdateVersion&) = delete;
PendingUpdateVersion& operator=(const PendingUpdateVersion&) = delete;
~PendingUpdateVersion() {
if (version_) {
version_->DecrementPendingUpdateHintCount();
}
}
PendingUpdateVersion& operator=(PendingUpdateVersion&& other) {
version_ = std::move(other.version_);
return *this;
}
// Needed for base::flat_set.
bool operator<(const PendingUpdateVersion& other) const {
return version_ < other.version_;
}
private:
scoped_refptr<ServiceWorkerVersion> version_;
};
// To realize the running status condition in the ServiceWorker static routing
// API, the latest ServiceWorker running status is notified to all renderers
// that execute ServiceWorker subresource loaders and controlled by the
// ServiceWorker.
//
// This is an observer to receive the ServiceWorker running status change,
// and make the update notified to all the renderers via mojo IPC.
//
// See:
// https://w3c.github.io/ServiceWorker/#dom-routercondition-runningstatus
class ServiceWorkerClient::ServiceWorkerRunningStatusObserver final
: public ServiceWorkerVersion::Observer {
public:
void OnRunningStateChanged(ServiceWorkerVersion* version) override {
Notify(version->running_status());
}
void Notify(blink::EmbeddedWorkerStatus status) {
for (const auto& callback : callbacks_) {
callback->OnStatusChanged(status);
}
}
void AddCallback(
mojo::PendingRemote<blink::mojom::ServiceWorkerRunningStatusCallback>
callback) {
callbacks_.Add(std::move(callback));
}
private:
mojo::RemoteSet<blink::mojom::ServiceWorkerRunningStatusCallback> callbacks_;
};
ServiceWorkerClient::ServiceWorkerClient(
base::WeakPtr<ServiceWorkerContextCore> context,
bool is_parent_frame_secure,
FrameTreeNodeId ongoing_navigation_frame_tree_node_id)
: context_(std::move(context)),
owner_(context_->service_worker_client_owner()),
create_time_(base::TimeTicks::Now()),
client_uuid_(base::Uuid::GenerateRandomV4().AsLowercaseString()),
is_parent_frame_secure_(is_parent_frame_secure),
is_initiated_by_prefetch_(false),
client_info_(ServiceWorkerClientInfo()),
process_id_for_worker_client_(ChildProcessHost::kInvalidUniqueID),
ongoing_navigation_frame_tree_node_id_(
ongoing_navigation_frame_tree_node_id) {
DCHECK(context_);
}
ServiceWorkerClient::ServiceWorkerClient(
base::WeakPtr<ServiceWorkerContextCore> context,
bool is_parent_frame_secure,
scoped_refptr<network::SharedURLLoaderFactory>
network_url_loader_factory_for_prefetch)
: context_(std::move(context)),
owner_(context_->service_worker_client_owner()),
create_time_(base::TimeTicks::Now()),
client_uuid_(base::Uuid::GenerateRandomV4().AsLowercaseString()),
is_parent_frame_secure_(is_parent_frame_secure),
is_initiated_by_prefetch_(true),
client_info_(ServiceWorkerClientInfo()),
process_id_for_worker_client_(ChildProcessHost::kInvalidUniqueID),
network_url_loader_factory_for_prefetch_(
std::move(network_url_loader_factory_for_prefetch)) {
DCHECK(context_);
}
ServiceWorkerClient::ServiceWorkerClient(
base::WeakPtr<ServiceWorkerContextCore> context,
int process_id,
ServiceWorkerClientInfo client_info)
: context_(std::move(context)),
owner_(context_->service_worker_client_owner()),
create_time_(base::TimeTicks::Now()),
client_uuid_(base::Uuid::GenerateRandomV4().AsLowercaseString()),
is_parent_frame_secure_(true),
is_initiated_by_prefetch_(false),
client_info_(client_info),
process_id_for_worker_client_(process_id) {
DCHECK(context_);
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(process_id_for_worker_client_, ChildProcessHost::kInvalidUniqueID);
}
ServiceWorkerClient::~ServiceWorkerClient() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsContainerForWindowClient()) {
auto* rfh = RenderFrameHostImpl::FromID(GetRenderFrameHostId());
if (rfh) {
rfh->RemoveServiceWorkerClient(client_uuid());
}
}
if (controller_) {
controller_->Uncontrol(client_uuid());
if (running_status_observer_) {
controller_->RemoveObserver(running_status_observer_.get());
running_status_observer_.reset();
}
}
// Remove |this| as an observer of ServiceWorkerRegistrations.
// TODO(falken): Use base::ScopedObservation instead of this explicit call.
controller_.reset();
controller_registration_.reset();
// Ensure callbacks awaiting execution ready are notified.
RunExecutionReadyCallbacks();
RemoveAllMatchingRegistrations();
}
void ServiceWorkerClient::HintToUpdateServiceWorker() {
// The destructors notify the ServiceWorkerVersions to update.
versions_to_update_.clear();
}
void ServiceWorkerClient::EnsureFileAccess(
const std::vector<base::FilePath>& file_paths,
blink::mojom::ServiceWorkerContainerHost::EnsureFileAccessCallback
callback) {
ServiceWorkerVersion* version =
controller_registration_ ? controller_registration_->active_version()
: nullptr;
// The controller might have legitimately been lost due to
// NotifyControllerLost(), so don't ReportBadMessage() here.
if (version) {
int controller_process_id = version->embedded_worker()->process_id();
ChildProcessSecurityPolicyImpl* policy =
ChildProcessSecurityPolicyImpl::GetInstance();
for (const auto& file : file_paths) {
if (!policy->CanReadFile(GetProcessId(), file)) {
mojo::ReportBadMessage(
"The renderer doesn't have access to the file "
"but it tried to grant access to the controller.");
return;
}
if (!policy->CanReadFile(controller_process_id, file)) {
policy->GrantReadFile(controller_process_id, file);
}
}
}
std::move(callback).Run();
}
void ServiceWorkerClient::OnExecutionReady() {
// Since `OnExecutionReady()` is a part of `ServiceWorkerContainerHost`,
// this method is called only if `is_container_ready()` is true.
CHECK(is_container_ready());
if (is_execution_ready()) {
mojo::ReportBadMessage("SWPH_OER_ALREADY_READY");
return;
}
// The controller was sent on navigation commit but we must send it again here
// because 1) the controller might have changed since navigation commit due to
// skipWaiting(), and 2) the UseCounter might have changed since navigation
// commit, in such cases the updated information was prevented being sent due
// to false is_execution_ready().
// TODO(leonhsl): Create some layout tests covering the above case 1), in
// which case we may also need to set |notify_controllerchange| correctly.
container_host()->SendSetController(false /* notify_controllerchange */);
SetExecutionReady();
}
void ServiceWorkerClient::OnVersionAttributesChanged(
ServiceWorkerRegistration* registration,
blink::mojom::ChangedServiceWorkerObjectsMaskPtr changed_mask) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (container_host()) {
container_host()->OnVersionAttributesChanged(registration,
std::move(changed_mask));
}
}
void ServiceWorkerClient::OnRegistrationFailed(
ServiceWorkerRegistration* registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RemoveMatchingRegistration(registration);
}
void ServiceWorkerClient::OnRegistrationFinishedUninstalling(
ServiceWorkerRegistration* registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RemoveMatchingRegistration(registration);
}
void ServiceWorkerClient::OnSkippedWaiting(
ServiceWorkerRegistration* registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (controller_registration_ != registration) {
return;
}
#if DCHECK_IS_ON()
DCHECK(controller_);
ServiceWorkerVersion* active = controller_registration_->active_version();
DCHECK(active);
DCHECK_NE(active, controller_.get());
DCHECK_EQ(active->status(), ServiceWorkerVersion::ACTIVATING);
#endif // DCHECK_IS_ON()
if (IsBackForwardCacheEnabled() && IsInBackForwardCache()) {
// This ServiceWorkerContainerHost is evicted from BackForwardCache in
// |ActivateWaitingVersion|, but not deleted yet. This can happen because
// asynchronous eviction and |OnSkippedWaiting| are in the same task.
// The controller does not have to be updated because |this| will be evicted
// from BackForwardCache.
// TODO(yuzus): Wire registration with ServiceWorkerContainerHost so that we
// can check on the caller side.
return;
}
UpdateController(true /* notify_controllerchange */);
}
void ServiceWorkerClient::AddMatchingRegistration(
ServiceWorkerRegistration* registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(blink::ServiceWorkerScopeMatches(registration->scope(),
GetUrlForScopeMatch()));
DCHECK(registration->key() == key());
if (!IsEligibleForServiceWorkerController()) {
return;
}
size_t key = registration->scope().spec().size();
if (base::Contains(matching_registrations_, key)) {
return;
}
registration->AddListener(this);
matching_registrations_[key] = registration;
if (container_host()) {
container_host()->ReturnRegistrationForReadyIfNeeded();
}
}
void ServiceWorkerClient::RemoveMatchingRegistration(
ServiceWorkerRegistration* registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK_NE(controller_registration_, registration);
#if DCHECK_IS_ON()
DCHECK(IsMatchingRegistration(registration));
#endif // DCHECK_IS_ON()
registration->RemoveListener(this);
size_t key = registration->scope().spec().size();
matching_registrations_.erase(key);
}
ServiceWorkerRegistration* ServiceWorkerClient::MatchRegistration() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& registration : base::Reversed(matching_registrations_)) {
if (registration.second->is_uninstalled()) {
continue;
}
if (registration.second->is_uninstalling()) {
return nullptr;
}
return registration.second.get();
}
return nullptr;
}
void ServiceWorkerClient::AddServiceWorkerToUpdate(
scoped_refptr<ServiceWorkerVersion> version) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// This is only called for windows now, but it should be called for all
// clients someday.
DCHECK(IsContainerForWindowClient());
versions_to_update_.emplace(std::move(version));
}
void ServiceWorkerClient::CountFeature(blink::mojom::WebFeature feature) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// `container_` can be used only if ServiceWorkerContainerInfoForClient has
// been passed to the renderer process. Otherwise, the method call will crash
// inside the mojo library (See crbug.com/40918057).
if (!is_container_ready()) {
buffered_used_features_.insert(feature);
return;
}
// And only when loading finished so the controller is really settled.
if (!is_execution_ready()) {
buffered_used_features_.insert(feature);
return;
}
container_host()->CountFeature(feature);
}
void ServiceWorkerClient::NotifyControllerLost() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (IsBackForwardCacheEnabled() && IsInBackForwardCache()) {
// The controller was unregistered, which usually does not happen while it
// has controllees. Since the document is in the back/forward cache, it does
// not count as a controllee. However, this means if it were to be restored,
// the page would be in an unexpected state, so evict the bfcache entry.
EvictFromBackForwardCache(BackForwardCacheMetrics::NotRestoredReason::
kServiceWorkerUnregistration);
}
SetControllerRegistration(nullptr, true /* notify_controllerchange */);
}
void ServiceWorkerClient::ClaimedByRegistration(
scoped_refptr<ServiceWorkerRegistration> registration) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(registration->active_version());
DCHECK(is_execution_ready());
// TODO(falken): This should just early return, or DCHECK. claim() should have
// no effect on a page that's already using the registration.
if (registration == controller_registration_) {
UpdateController(true /* notify_controllerchange */);
return;
}
SetControllerRegistration(registration, true /* notify_controllerchange */);
}
blink::mojom::ServiceWorkerClientType ServiceWorkerClient::GetClientType()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return std::visit(
absl::Overload(
[](GlobalRenderFrameHostId render_frame_host_id) {
return blink::mojom::ServiceWorkerClientType::kWindow;
},
[](blink::DedicatedWorkerToken dedicated_worker_token) {
return blink::mojom::ServiceWorkerClientType::kDedicatedWorker;
},
[](blink::SharedWorkerToken shared_worker_token) {
return blink::mojom::ServiceWorkerClientType::kSharedWorker;
}),
client_info_);
}
bool ServiceWorkerClient::IsContainerForWindowClient() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return std::holds_alternative<GlobalRenderFrameHostId>(client_info_);
}
bool ServiceWorkerClient::IsContainerForWorkerClient() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return std::holds_alternative<blink::DedicatedWorkerToken>(client_info_) ||
std::holds_alternative<blink::SharedWorkerToken>(client_info_);
}
ServiceWorkerClientInfo ServiceWorkerClient::GetServiceWorkerClientInfo()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return client_info_;
}
blink::mojom::ServiceWorkerContainerInfoForClientPtr
ServiceWorkerClient::CommitResponse(
base::PassKey<ScopedServiceWorkerClient>,
std::optional<GlobalRenderFrameHostId> rfh_id,
const PolicyContainerPolicies& policy_container_policies,
mojo::PendingRemote<network::mojom::CrossOriginEmbedderPolicyReporter>
coep_reporter,
mojo::PendingRemote<network::mojom::DocumentIsolationPolicyReporter>
dip_reporter,
ukm::SourceId ukm_source_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK_EQ(client_phase_, ClientPhase::kInitial);
if (IsContainerForWindowClient()) {
CHECK(coep_reporter);
CHECK(rfh_id);
ongoing_navigation_frame_tree_node_id_ = FrameTreeNodeId();
client_info_ = *rfh_id;
if (controller_) {
controller_->UpdateForegroundPriority();
}
auto* rfh = RenderFrameHostImpl::FromID(*rfh_id);
// `rfh` may be null in tests (but it should not happen in production).
if (rfh) {
rfh->AddServiceWorkerClient(client_uuid(),
weak_ptr_factory_.GetWeakPtr());
}
}
CHECK(!container_host_);
auto container_info =
blink::mojom::ServiceWorkerContainerInfoForClient::New();
container_host_ = std::make_unique<ServiceWorkerContainerHostForClient>(
base::PassKey<ServiceWorkerClient>(), AsWeakPtr(), container_info,
policy_container_policies, std::move(coep_reporter),
std::move(dip_reporter), std::move(ukm_source_id));
// `network_url_loader_factory_for_prefetch_` is no longer used after commit.
network_url_loader_factory_for_prefetch_.reset();
TransitionToClientPhase(ClientPhase::kResponseCommitted);
return container_info;
}
void ServiceWorkerClient::OnEndNavigationCommit() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(IsContainerForWindowClient());
DCHECK(!navigation_commit_ended_);
navigation_commit_ended_ = true;
if (controller_) {
controller_->OnControlleeNavigationCommitted(client_uuid_,
GetRenderFrameHostId());
}
}
void ServiceWorkerClient::UpdateUrlsInternal(
const GURL& creation_url,
const std::optional<url::Origin>& top_frame_origin,
const blink::StorageKey& storage_key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const GURL url = creation_url.is_valid()
? net::SimplifyUrlForRequest(creation_url)
: creation_url;
GURL previous_url = url_;
creation_url_ = creation_url;
// The url_ needs the URL fragment removed, but the creation URL needs to be
// the original URL including the fragment.
url_ = url;
top_frame_origin_ = top_frame_origin;
key_ = storage_key;
service_worker_security_utils::CheckOnUpdateUrls(GetUrlForScopeMatch(), key_);
if (previous_url != url) {
// Revoke the token on URL change since any service worker holding the token
// may no longer be the potential controller of this frame and shouldn't
// have the power to display SSL dialogs for it.
if (IsContainerForWindowClient()) {
fetch_request_window_id_ = base::UnguessableToken::Create();
}
}
// Update client id on cross origin redirects. This corresponds to the HTML
// standard's "process a navigation fetch" algorithm's step for discarding
// |reservedEnvironment|.
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#process-a-navigate-fetch
// "If |reservedEnvironment| is not null and |currentURL|'s origin is not the
// same as |reservedEnvironment|'s creation URL's origin, then:
// 1. Run the environment discarding steps for |reservedEnvironment|.
// 2. Set |reservedEnvironment| to null."
if (previous_url.is_valid() && !url::IsSameOriginWith(previous_url, url)) {
// Remove old controller since we know the controller is definitely
// changed. We need to remove |this| from |controller_|'s controllee before
// updating UUID since ServiceWorkerVersion has a map from uuid to provider
// hosts.
SetControllerRegistration(nullptr, false /* notify_controllerchange */);
// Set UUID to the new one.
std::string previous_client_uuid = client_uuid_;
client_uuid_ = base::Uuid::GenerateRandomV4().AsLowercaseString();
owner_->UpdateServiceWorkerClientClientID(previous_client_uuid,
client_uuid_);
}
SyncMatchingRegistrations();
}
namespace {
// Attempt to get the storage key from |RenderFrameHostImpl|. This correctly
// accounts for extension URLs. The absence of this logic was a potential cause
// for https://crbug.com/1346450.
std::optional<blink::StorageKey> GetStorageKeyFromRenderFrameHost(
FrameTreeNodeId frame_tree_node_id,
const url::Origin& origin,
const base::UnguessableToken* nonce) {
FrameTreeNode* frame_tree_node =
FrameTreeNode::GloballyFindByID(frame_tree_node_id);
if (!frame_tree_node) {
return std::nullopt;
}
RenderFrameHostImpl* frame_host = frame_tree_node->current_frame_host();
if (!frame_host) {
return std::nullopt;
}
return frame_host->CalculateStorageKey(origin, nonce);
}
// For dedicated/shared worker cases, if a storage key is returned, it will have
// its origin replaced by |origin|. This would mean that the origin of the
// WorkerHost and the origin as used by the service worker code don't match,
// however in cases where these wouldn't match the load will be aborted later
// anyway.
std::optional<blink::StorageKey> GetStorageKeyFromDedicatedWorkerHost(
content::StoragePartition* storage_partition,
blink::DedicatedWorkerToken dedicated_worker_token,
const url::Origin& origin) {
auto* worker_service = static_cast<DedicatedWorkerServiceImpl*>(
storage_partition->GetDedicatedWorkerService());
auto* worker_host =
worker_service->GetDedicatedWorkerHostFromToken(dedicated_worker_token);
if (worker_host) {
return worker_host->GetStorageKey().WithOrigin(origin);
}
return std::nullopt;
}
std::optional<blink::StorageKey> GetStorageKeyFromSharedWorkerHost(
content::StoragePartition* storage_partition,
blink::SharedWorkerToken shared_worker_token,
const url::Origin& origin) {
auto* worker_service = static_cast<SharedWorkerServiceImpl*>(
storage_partition->GetSharedWorkerService());
auto* worker_host =
worker_service->GetSharedWorkerHostFromToken(shared_worker_token);
if (worker_host) {
return worker_host->GetStorageKey().WithOrigin(origin);
}
return std::nullopt;
}
} // namespace
blink::StorageKey ServiceWorkerClient::CalculateStorageKeyForUpdateUrls(
const GURL& url,
const net::IsolationInfo& isolation_info_from_handle) const {
CHECK(!is_response_committed());
const url::Origin origin = url::Origin::Create(url);
const std::optional<blink::StorageKey> storage_key = std::visit(
absl::Overload(
[&](GlobalRenderFrameHostId render_frame_host_id) {
if (is_initiated_by_prefetch_) {
// Falls back to the `CreateFromOriginAndIsolationInfo()` case
// below.
// Navigation isn't served by prefetch if the key for prefetch
// calculated here is wrong/mismatching, checked at
// `PrefetchURLLoaderInterceptor::OnGetPrefetchComplete()`.
// https://crbug.com/413207408.
return std::optional<blink::StorageKey>(std::nullopt);
}
// We use `ongoing_navigation_frame_tree_node_id_` instead of
// `render_frame_host_id` because this method is called before
// response commit.
return GetStorageKeyFromRenderFrameHost(
ongoing_navigation_frame_tree_node_id_, origin,
base::OptionalToPtr(isolation_info_from_handle.nonce()));
},
[&](blink::DedicatedWorkerToken dedicated_worker_token) {
auto* process = RenderProcessHost::FromID(GetProcessId());
return process ? GetStorageKeyFromDedicatedWorkerHost(
process->GetStoragePartition(),
dedicated_worker_token, origin)
: std::nullopt;
},
[&](blink::SharedWorkerToken shared_worker_token) {
auto* process = RenderProcessHost::FromID(GetProcessId());
return process ? GetStorageKeyFromSharedWorkerHost(
process->GetStoragePartition(),
shared_worker_token, origin)
: std::nullopt;
}),
client_info_);
if (storage_key) {
return *storage_key;
}
// If we're in this case then we couldn't get the StorageKey from the RFH,
// which means we also can't get the storage partitioning status from
// RuntimeFeatureState(Read)Context. Using
// CreateFromOriginAndIsolationInfo() will create a key based on
// net::features::kThirdPartyStoragePartitioning state.
return blink::StorageKey::CreateFromOriginAndIsolationInfo(
origin, isolation_info_from_handle);
}
void ServiceWorkerClient::UpdateUrls(
const GURL& creation_url,
const std::optional<url::Origin>& top_frame_origin,
const blink::StorageKey& storage_key) {
CHECK(!is_response_committed());
UpdateUrlsInternal(creation_url, top_frame_origin, storage_key);
}
void ServiceWorkerClient::UpdateUrlsAfterCommitResponseForTesting(
const GURL& url,
const std::optional<url::Origin>& top_frame_origin,
const blink::StorageKey& storage_key) {
CHECK(is_response_committed());
UpdateUrlsInternal(url, top_frame_origin, storage_key);
}
void ServiceWorkerClient::SetControllerRegistration(
scoped_refptr<ServiceWorkerRegistration> controller_registration,
bool notify_controllerchange) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (controller_registration) {
CHECK(IsEligibleForServiceWorkerController());
DCHECK(controller_registration->active_version());
#if DCHECK_IS_ON()
DCHECK(IsMatchingRegistration(controller_registration.get()));
#endif // DCHECK_IS_ON()
}
controller_registration_ = controller_registration;
UpdateController(notify_controllerchange);
}
bool ServiceWorkerClient::IsEligibleForServiceWorkerController() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!url_.is_valid()) {
return false;
}
// Pass GetUrlForScopeMatch() instead of `url_` because we cannot take the
// origin of `url_` when it's a blob URL (see https://crbug.com/1144717). It's
// guaranteed that the URL returned by GetURLForScopeMatch() has the same
// logical origin as `url_`.
// TODO(asamidoi): Add url::Origin member for ServiceWorkerContainerHost and
// use it as the argument of OriginCanAccessServiceWorkers().
if (!OriginCanAccessServiceWorkers(GetUrlForScopeMatch())) {
return false;
}
if (is_parent_frame_secure_) {
return true;
}
std::set<std::string> schemes;
GetContentClient()->browser()->GetSchemesBypassingSecureContextCheckAllowlist(
&schemes);
return schemes.find(url_.scheme()) != schemes.end();
}
bool ServiceWorkerClient::is_response_committed() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (client_phase_) {
case ClientPhase::kInitial:
case ClientPhase::kResponseNotCommitted:
return false;
case ClientPhase::kResponseCommitted:
case ClientPhase::kContainerReady:
case ClientPhase::kExecutionReady:
return true;
}
}
bool ServiceWorkerClient::is_container_ready() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (client_phase_) {
case ClientPhase::kInitial:
case ClientPhase::kResponseCommitted:
case ClientPhase::kResponseNotCommitted:
return false;
case ClientPhase::kContainerReady:
case ClientPhase::kExecutionReady:
return true;
}
}
void ServiceWorkerClient::AddExecutionReadyCallback(
ExecutionReadyCallback callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!is_execution_ready());
execution_ready_callbacks_.push_back(std::move(callback));
}
bool ServiceWorkerClient::is_execution_ready() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (client_phase_) {
case ClientPhase::kInitial:
case ClientPhase::kResponseCommitted:
case ClientPhase::kResponseNotCommitted:
case ClientPhase::kContainerReady:
return false;
case ClientPhase::kExecutionReady:
return true;
}
}
GlobalRenderFrameHostId ServiceWorkerClient::GetRenderFrameHostId() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(IsContainerForWindowClient());
return std::get<GlobalRenderFrameHostId>(client_info_);
}
int ServiceWorkerClient::GetProcessId() const {
if (IsContainerForWindowClient()) {
return GetRenderFrameHostId().child_id;
}
DCHECK(IsContainerForWorkerClient());
return process_id_for_worker_client_;
}
NavigationRequest* ServiceWorkerClient::GetOngoingNavigationRequestBeforeCommit(
base::PassKey<StoragePartitionImpl>) const {
DCHECK(IsContainerForWindowClient());
DCHECK(!GetRenderFrameHostId());
// For Window clients for prefetch,
// `GetOngoingNavigationRequestBeforeCommit()` isn't called at all, because
// prefetching requests don't set `URLLoaderNetworkServiceObserver`.
CHECK(!is_initiated_by_prefetch_);
// It is safe to use `ongoing_navigation_frame_tree_node_id_` to obtain the
// corresponding navigation request without being concerned about the case
// that a new navigation had started and the old navigation had been deleted,
// because the owner of this instance will reset the key that can be used to
// retrieve this instance, which makes the old key stale and cannot locate
// this instance. This mechanism guarantees that this instance would always be
// associated with the latest navigation.
// However, this design requires callers to carefully get the
// `ServiceWorkerContainerHost` instance from scratch instead of using a
// stored one, and it would be better to optimize the design when possible.
FrameTreeNode* frame_tree_node =
FrameTreeNode::GloballyFindByID(ongoing_navigation_frame_tree_node_id_);
return frame_tree_node ? frame_tree_node->navigation_request() : nullptr;
}
std::string ServiceWorkerClient::GetFrameTreeNodeTypeStringBeforeCommit()
const {
CHECK(!is_response_committed());
// TODO(https://crbug.com/40947546): If needed, assign a proper metrics name
// for clients for prefetch where `ongoing_navigation_frame_tree_node_id` is
// null.
if (FrameTreeNode* frame_tree_node = FrameTreeNode::GloballyFindByID(
ongoing_navigation_frame_tree_node_id_)) {
CHECK(IsContainerForWindowClient());
return frame_tree_node->IsOutermostMainFrame() ? "OutermostMainFrame"
: "NotOutermostMainFrame";
}
return "Unknown";
}
const std::string& ServiceWorkerClient::client_uuid() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return client_uuid_;
}
std::string ServiceWorkerClient::client_uuid_for_resulting_client_id() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (is_initiated_by_prefetch_) {
return "";
}
return client_uuid_;
}
ServiceWorkerVersion* ServiceWorkerClient::controller() const {
#if DCHECK_IS_ON()
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CheckControllerConsistency(false);
#endif // DCHECK_IS_ON()
return controller_.get();
}
ServiceWorkerRegistration* ServiceWorkerClient::controller_registration()
const {
#if DCHECK_IS_ON()
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CheckControllerConsistency(false);
#endif // DCHECK_IS_ON()
return controller_registration_.get();
}
bool ServiceWorkerClient::IsInBackForwardCache() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return is_in_back_forward_cache_;
}
void ServiceWorkerClient::EvictFromBackForwardCache(
BackForwardCacheMetrics::NotRestoredReason reason) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(IsBackForwardCacheEnabled());
is_in_back_forward_cache_ = false;
if (!IsContainerForWindowClient()) {
return;
}
auto* rfh = RenderFrameHostImpl::FromID(GetRenderFrameHostId());
// |rfh| could be evicted before this function is called.
if (rfh && rfh->IsInBackForwardCache()) {
rfh->EvictFromBackForwardCacheWithReason(reason);
}
}
void ServiceWorkerClient::OnEnterBackForwardCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(IsBackForwardCacheEnabled());
if (controller_) {
// TODO(crbug.com/330928087): remove check when this issue resolved.
SCOPED_CRASH_KEY_NUMBER("SWC_OnEBFC", "client_type",
static_cast<int32_t>(GetClientType()));
SCOPED_CRASH_KEY_BOOL("SWC_OnEBFC", "is_execution_ready",
is_execution_ready());
SCOPED_CRASH_KEY_BOOL("SWC_OnEBFC", "is_blob_or_about_url",
url() != GetUrlForScopeMatch());
SCOPED_CRASH_KEY_BOOL("SWC_OnEBFC", "is_inherited", is_inherited());
CHECK(!controller_->BFCacheContainsControllee(client_uuid()));
controller_->MoveControlleeToBackForwardCacheMap(client_uuid());
}
is_in_back_forward_cache_ = true;
}
void ServiceWorkerClient::OnRestoreFromBackForwardCache() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(IsBackForwardCacheEnabled());
// TODO(crbug.com/330928087): remove check when this issue resolved.
SCOPED_CRASH_KEY_BOOL("SWC_OnRFBFC", "is_in_bfcache",
is_in_back_forward_cache_);
SCOPED_CRASH_KEY_BOOL("SWC_OnRFBFC", "is_blob_or_about_url",
url() != GetUrlForScopeMatch());
SCOPED_CRASH_KEY_BOOL("SWC_OnRFBFC", "is_inherited", is_inherited());
if (controller_) {
controller_->RestoreControlleeFromBackForwardCacheMap(client_uuid());
}
is_in_back_forward_cache_ = false;
}
void ServiceWorkerClient::SyncMatchingRegistrations() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!controller_registration_);
RemoveAllMatchingRegistrations();
if (!context_) {
return;
}
const auto& registrations = context_->GetLiveRegistrations();
for (const auto& key_registration : registrations) {
ServiceWorkerRegistration* registration = key_registration.second;
if (!registration->is_uninstalled() && registration->key() == key() &&
blink::ServiceWorkerScopeMatches(registration->scope(),
GetUrlForScopeMatch())) {
AddMatchingRegistration(registration);
}
}
}
#if DCHECK_IS_ON()
bool ServiceWorkerClient::IsMatchingRegistration(
ServiceWorkerRegistration* registration) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::string spec = registration->scope().spec();
size_t key = spec.size();
auto iter = matching_registrations_.find(key);
if (iter == matching_registrations_.end()) {
return false;
}
if (iter->second.get() != registration) {
return false;
}
return true;
}
#endif // DCHECK_IS_ON()
void ServiceWorkerClient::RemoveAllMatchingRegistrations() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!controller_registration_);
for (const auto& it : matching_registrations_) {
ServiceWorkerRegistration* registration = it.second.get();
registration->RemoveListener(this);
}
matching_registrations_.clear();
}
void ServiceWorkerClient::SetExecutionReady() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
switch (client_phase_) {
case ClientPhase::kInitial:
// When `CommitResponse()` is not yet called, it should be skipped because
// the service worker client initialization failed somewhere, and thus
// ignore the `SetExecutionReady()` call here and transition to
// `kResponseNotCommitted` to confirm that no further transitions are
// attempted. See also the comment at `kResponseNotCommitted` in the
// header file.
TransitionToClientPhase(ClientPhase::kResponseNotCommitted);
break;
case ClientPhase::kContainerReady:
// Successful case.
TransitionToClientPhase(ClientPhase::kExecutionReady);
RunExecutionReadyCallbacks();
if (context_) {
context_->NotifyClientIsExecutionReady(*this);
}
FlushFeatures();
break;
case ClientPhase::kResponseCommitted:
case ClientPhase::kExecutionReady:
case ClientPhase::kResponseNotCommitted:
// Invalid state transition.
NOTREACHED()
<< "ServiceWorkerClient::SetExecutionReady() called on ClientPhase "
<< static_cast<int>(client_phase_);
}
}
void ServiceWorkerClient::RunExecutionReadyCallbacks() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<ExecutionReadyCallback> callbacks;
execution_ready_callbacks_.swap(callbacks);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&RunCallbacks, std::move(callbacks)));
}
void ServiceWorkerClient::TransitionToClientPhase(ClientPhase new_phase) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (client_phase_ == new_phase) {
return;
}
switch (client_phase_) {
case ClientPhase::kInitial:
CHECK(new_phase == ClientPhase::kResponseCommitted ||
new_phase == ClientPhase::kResponseNotCommitted);
break;
case ClientPhase::kResponseCommitted:
CHECK_EQ(new_phase, ClientPhase::kContainerReady);
break;
case ClientPhase::kContainerReady:
CHECK_EQ(new_phase, ClientPhase::kExecutionReady);
break;
case ClientPhase::kExecutionReady:
case ClientPhase::kResponseNotCommitted:
NOTREACHED() << "Invalid transition from "
<< static_cast<int>(client_phase_);
}
client_phase_ = new_phase;
}
void ServiceWorkerClient::UpdateController(bool notify_controllerchange) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
ServiceWorkerVersion* version =
controller_registration_ ? controller_registration_->active_version()
: nullptr;
CHECK(!version || IsEligibleForServiceWorkerController());
if (version == controller_.get()) {
return;
}
scoped_refptr<ServiceWorkerVersion> previous_version = controller_;
controller_ = version;
if (version) {
// TODO(crbug.com/330928087): remove check when this issue resolved.
SCOPED_CRASH_KEY_NUMBER("SWV_RCFBCM", "client_type",
static_cast<int32_t>(GetClientType()));
SCOPED_CRASH_KEY_BOOL("SWV_RCFBCM", "is_execution_ready",
is_execution_ready());
SCOPED_CRASH_KEY_BOOL("SWV_RCFBCM", "is_blob_or_about_url",
url() != GetUrlForScopeMatch());
SCOPED_CRASH_KEY_BOOL("SWV_RCFBCM", "is_inherited", is_inherited());
CHECK(!version->BFCacheContainsControllee(client_uuid()));
version->AddControllee(this);
if (IsBackForwardCacheEnabled() && IsInBackForwardCache()) {
// |this| was not |version|'s controllee when |OnEnterBackForwardCache|
// was called.
version->MoveControlleeToBackForwardCacheMap(client_uuid());
}
if (running_status_observer_) {
version->AddObserver(running_status_observer_.get());
running_status_observer_->Notify(version->running_status());
}
}
if (previous_version) {
previous_version->Uncontrol(client_uuid());
if (running_status_observer_) {
previous_version->RemoveObserver(running_status_observer_.get());
}
}
// No need to `SetController` if the container is not ready because
// when the container gets ready, `ControllerServiceWorkerInfoPtr` is also
// sent in the same IPC call. Moreover, it is harmful to resend the past
// SetController to the renderer because it moves the controller in the
// renderer to the past one.
if (!is_container_ready()) {
return;
}
if (!is_execution_ready()) {
return;
}
container_host()->SendSetController(notify_controllerchange);
}
#if DCHECK_IS_ON()
void ServiceWorkerClient::CheckControllerConsistency(bool should_crash) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!controller_) {
DCHECK(!controller_registration_);
return;
}
DCHECK(controller_registration_);
DCHECK_EQ(controller_->registration_id(), controller_registration_->id());
switch (controller_->status()) {
case ServiceWorkerVersion::NEW:
case ServiceWorkerVersion::INSTALLING:
case ServiceWorkerVersion::INSTALLED:
if (should_crash) {
ServiceWorkerVersion::Status status = controller_->status();
base::debug::Alias(&status);
NOTREACHED() << "Controller service worker has a bad status: "
<< ServiceWorkerVersion::VersionStatusToString(status);
}
break;
case ServiceWorkerVersion::REDUNDANT: {
if (should_crash) {
NOTREACHED();
}
break;
}
case ServiceWorkerVersion::ACTIVATING:
case ServiceWorkerVersion::ACTIVATED:
// Valid status, controller is being activated.
break;
}
}
#endif // DCHECK_IS_ON()
const GURL& ServiceWorkerClient::GetUrlForScopeMatch() const {
if (!scope_match_url_for_client_.is_empty()) {
return scope_match_url_for_client_;
}
return url_;
}
void ServiceWorkerClient::InheritControllerFrom(
ServiceWorkerClient& creator_host,
const GURL& client_url) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(GetClientType() ==
blink::mojom::ServiceWorkerClientType::kDedicatedWorker ||
(base::FeatureList::IsEnabled(kSharedWorkerBlobURLFix) &&
GetClientType() ==
blink::mojom::ServiceWorkerClientType::kSharedWorker) ||
(base::FeatureList::IsEnabled(features::kServiceWorkerSrcdocSupport) &&
GetClientType() == blink::mojom::ServiceWorkerClientType::kWindow &&
client_url.IsAboutSrcdoc()));
// Only expect srcdoc url or blob url.
DCHECK(client_url.SchemeIsBlob() ||
(base::FeatureList::IsEnabled(features::kServiceWorkerSrcdocSupport) &&
client_url.IsAboutSrcdoc()));
// origins of blob client_url and creator host should be the same or both
// opaque.
if (client_url.SchemeIsBlob()) {
service_worker_security_utils::CheckOnUpdateUrls(client_url,
creator_host.key());
}
// Let `scope_match_url_for_client_` be the creator's url for scope match
// because a client should be handled by the service worker of its creator.
// Update it before UpdateUrls so that CheckOnUpdateUrls inside UpdateUrls
// checks with the updated GetUrlForScopeMatch().
scope_match_url_for_client_ = creator_host.GetUrlForScopeMatch();
UpdateUrls(client_url, creator_host.top_frame_origin(), creator_host.key());
// Inherit the controller of the creator.
if (creator_host.controller_registration()) {
AddMatchingRegistration(creator_host.controller_registration());
// If the creator is in back forward cache, the client should also
// be in back forward cache. Otherwise, CHECK fail during restoring from
// back forward cache.
is_in_back_forward_cache_ = creator_host.is_in_back_forward_cache();
// TODO(crbug.com/341322515): remove this CHECK.
// This CHECK is to ensure this path does not cause the crash at
// ServiceWorkerVersion::RemoveControlleeFromBackForwardCacheMap().
CHECK(creator_host.controller_registration()->active_version());
SetControllerRegistration(creator_host.controller_registration(),
false /* notify_controllerchange */);
}
creator_host.SetInherited();
}
mojo::PendingReceiver<blink::mojom::ServiceWorkerRunningStatusCallback>
ServiceWorkerClient::GetRunningStatusCallbackReceiver() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(controller_);
if (!running_status_observer_) {
running_status_observer_ =
absl::make_unique<ServiceWorkerRunningStatusObserver>();
controller_->AddObserver(running_status_observer_.get());
}
mojo::PendingRemote<blink::mojom::ServiceWorkerRunningStatusCallback>
remote_callback;
auto receiver = remote_callback.InitWithNewPipeAndPassReceiver();
running_status_observer_->AddCallback(std::move(remote_callback));
return receiver;
}
void ServiceWorkerClient::SetContainerReady() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
TransitionToClientPhase(ClientPhase::kContainerReady);
CHECK(container_host()->IsContainerRemoteConnected());
FlushFeatures();
}
void ServiceWorkerClient::FlushFeatures() {
std::set<blink::mojom::WebFeature> features;
features.swap(buffered_used_features_);
for (const auto& feature : features) {
CountFeature(feature);
}
}
void ServiceWorkerClient::SetNetworkURLLoaderFactoryForTesting(
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory) {
CHECK_IS_TEST();
network_url_loader_factory_override_for_testing_ = url_loader_factory;
}
scoped_refptr<network::SharedURLLoaderFactory>
ServiceWorkerClient::CreateNetworkURLLoaderFactory(
CreateNetworkURLLoaderFactoryType type,
StoragePartitionImpl* storage_partition,
const network::ResourceRequest& resource_request) {
CHECK(!is_response_committed());
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (network_url_loader_factory_override_for_testing_) {
CHECK_IS_TEST();
return network_url_loader_factory_override_for_testing_;
}
if (is_initiated_by_prefetch_) {
// We skip `WillCreateURLLoaderFactory` below, because it is already
// included in `network_url_loader_factory_for_prefetch_` (see
// `PrefetchNetworkContext::CreateNewURLLoaderFactory()`).
// We also skip `CreateURLLoaderHandlerForServiceWorkerNavigationPreload`,
// because this is a prefetch request and don't have to consult with search
// prefetch cache via
// `CreateURLLoaderHandlerForServiceWorkerNavigationPreload`.
return network_url_loader_factory_for_prefetch_;
}
switch (type) {
case CreateNetworkURLLoaderFactoryType::kNavigationPreload:
// Allow the embedder to intercept the URLLoader request if necessary.
// This must be a synchronous decision by the embedder. In the future, we
// may wish to support asynchronous decisions using
// |URLLoaderRequestInterceptor| in the same fashion that they are used
// for navigation requests.
if (ContentBrowserClient::URLLoaderRequestHandler
embedder_url_loader_handler =
GetContentClient()
->browser()
->CreateURLLoaderHandlerForServiceWorkerNavigationPreload(
ongoing_navigation_frame_tree_node_id_,
resource_request)) {
return base::MakeRefCounted<network::SingleRequestURLLoaderFactory>(
std::move(embedder_url_loader_handler));
}
break;
case CreateNetworkURLLoaderFactoryType::kRaceNetworkRequest:
case CreateNetworkURLLoaderFactoryType::kSyntheticNetworkRequest:
break;
}
// For worker clients, `ongoing_navigation_frame_tree_node_id_` is null.
// TODO(falken): Can `navigation_request` check be a DCHECK now that the
// caller does not post a task to this function?
auto* frame_tree_node =
FrameTreeNode::GloballyFindByID(ongoing_navigation_frame_tree_node_id_);
if (!frame_tree_node || !storage_partition ||
!frame_tree_node->navigation_request()) {
// The navigation was cancelled. Just drop the request. Otherwise, we might
// go to network without consulting the embedder first, which would break
// guarantees.
//
// TODO(https://crbug.com/40947546): Clients for prefetch (where
// `ongoing_navigation_frame_tree_node_id` is null) also fall into this case
// and thus don't support navigationPreload and race network requests. Fix
// this.
mojo::PendingRemote<network::mojom::URLLoaderFactory> network_factory;
return base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
std::move(network_factory));
}
// We ignore the value of |bypass_redirect_checks_unused| since a redirect is
// just relayed to the service worker where preloadResponse is resolved as
// redirect.
bool bypass_redirect_checks_unused;
// Consult the embedder.
mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>
header_client;
network::URLLoaderFactoryBuilder factory_builder;
// Here we give nullptr for |factory_override|, because CORS is no-op
// for navigations.
GetContentClient()->browser()->WillCreateURLLoaderFactory(
storage_partition->browser_context(),
frame_tree_node->current_frame_host(),
frame_tree_node->current_frame_host()->GetProcess()->GetDeprecatedID(),
ContentBrowserClient::URLLoaderFactoryType::kNavigation, url::Origin(),
net::IsolationInfo(),
frame_tree_node->navigation_request()->GetNavigationId(),
ukm::SourceIdObj::FromInt64(
frame_tree_node->navigation_request()->GetNextPageUkmSourceId()),
factory_builder, &header_client, &bypass_redirect_checks_unused,
/*disable_secure_dns=*/nullptr, /*factory_override=*/nullptr,
GetUIThreadTaskRunner({BrowserTaskType::kNavigationNetworkResponse}));
// Make the network factory.
return base::MakeRefCounted<network::WrapperSharedURLLoaderFactory>(
NavigationURLLoaderImpl::CreateURLLoaderFactoryWithHeaderClient(
std::move(header_client), std::move(factory_builder),
storage_partition,
// TODO(crbug.com/390003764): Consider whether/how to apply devtools
// cookies setting overrides for a service worker.
/*devtools_cookie_overrides=*/std::nullopt,
/*cookie_overrides=*/std::nullopt));
}
// If a blob URL is used for a SharedWorker script's URL, a controller will be
// inherited.
BASE_FEATURE(kSharedWorkerBlobURLFix,
"SharedWorkerBlobURLFix",
base::FEATURE_DISABLED_BY_DEFAULT);
} // namespace content
|