Skip to content

Commit 75baffd

Browse files
committed
M63: Revert "[ServiceWorker] Implement ServiceWorkerContainer.SetController"
This reverts commit 24e5c55. Bug: 774681 Change-Id: I2624c46835a32d60a7361077b3bbe20939faa307 TBR: kinuko Reviewed-on: https://chromium-review.googlesource.com/728879 Reviewed-by: Matt Falkenhagen <[email protected]> Cr-Commit-Position: refs/branch-heads/3239@{#85} Cr-Branched-From: adb61db-refs/heads/master@{#508578}
1 parent 860a95c commit 75baffd

16 files changed

+315
-368
lines changed

content/browser/service_worker/service_worker_provider_host.cc

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -559,13 +559,6 @@ ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() {
559559
DCHECK_EQ(kDocumentMainThreadId, render_thread_id_);
560560
DCHECK_NE(SERVICE_WORKER_PROVIDER_UNKNOWN, info_.type);
561561

562-
// Clear the controller from the renderer-side provider, since no one knows
563-
// what's going to happen until after cross-site transfer finishes.
564-
if (controller_) {
565-
SendSetControllerServiceWorker(nullptr,
566-
false /* notify_controllerchange */);
567-
}
568-
569562
std::unique_ptr<ServiceWorkerProviderHost> provisional_host =
570563
base::WrapUnique(new ServiceWorkerProviderHost(
571564
process_id(),
@@ -578,6 +571,13 @@ ServiceWorkerProviderHost::PrepareForCrossSiteTransfer() {
578571

579572
RemoveAllMatchingRegistrations();
580573

574+
// Clear the controller from the renderer-side provider, since no one knows
575+
// what's going to happen until after cross-site transfer finishes.
576+
if (controller_) {
577+
SendSetControllerServiceWorker(nullptr,
578+
false /* notify_controllerchange */);
579+
}
580+
581581
render_process_id_ = ChildProcessHost::kInvalidUniqueID;
582582
render_thread_id_ = kInvalidEmbeddedWorkerThreadId;
583583
dispatcher_host_ = nullptr;
@@ -881,20 +881,14 @@ void ServiceWorkerProviderHost::SendSetControllerServiceWorker(
881881
DCHECK_EQ(controller_.get(), version);
882882
}
883883

884-
std::vector<blink::mojom::WebFeature> used_features;
885-
if (version) {
886-
for (const auto& feature : version->used_features()) {
887-
// TODO: version->used_features() should never have a feature outside the
888-
// known feature range. But there is special case, see the details in
889-
// crbug.com/758419.
890-
if (feature >= 0 &&
891-
feature < static_cast<uint32_t>(
892-
blink::mojom::WebFeature::kNumberOfFeatures))
893-
used_features.push_back(static_cast<blink::mojom::WebFeature>(feature));
894-
}
895-
}
896-
container_->SetController(GetOrCreateServiceWorkerHandle(version),
897-
used_features, notify_controllerchange);
884+
ServiceWorkerMsg_SetControllerServiceWorker_Params params;
885+
params.thread_id = render_thread_id_;
886+
params.provider_id = provider_id();
887+
params.object_info = GetOrCreateServiceWorkerHandle(version);
888+
params.should_notify_controllerchange = notify_controllerchange;
889+
if (version)
890+
params.used_features = version->used_features();
891+
Send(new ServiceWorkerMsg_SetControllerServiceWorker(params));
898892
}
899893

900894
void ServiceWorkerProviderHost::Register(

content/browser/service_worker/service_worker_provider_host_unittest.cc

Lines changed: 23 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ namespace content {
3535

3636
namespace {
3737

38+
// Sets the document URL for |host| and associates it with |registration|.
39+
// A dumb version of
40+
// ServiceWorkerControlleeRequestHandler::PrepareForMainResource().
41+
void SimulateServiceWorkerControlleeRequestHandler(
42+
ServiceWorkerProviderHost* host,
43+
ServiceWorkerRegistration* registration) {
44+
host->SetDocumentUrl(GURL("https://www.example.com/page"));
45+
host->AssociateRegistration(registration,
46+
false /* notify_controllerchange */);
47+
}
48+
3849
const char kServiceWorkerScheme[] = "i-can-use-service-worker";
3950

4051
class ServiceWorkerTestContentClient : public TestContentClient {
@@ -361,27 +372,6 @@ TEST_F(ServiceWorkerProviderHostTest, RemoveProvider) {
361372
EXPECT_FALSE(context_->GetProviderHost(process_id, provider_id));
362373
}
363374

364-
class MockServiceWorkerContainer : public mojom::ServiceWorkerContainer {
365-
public:
366-
explicit MockServiceWorkerContainer(
367-
mojom::ServiceWorkerContainerAssociatedRequest request)
368-
: binding_(this, std::move(request)) {}
369-
370-
~MockServiceWorkerContainer() override = default;
371-
372-
void SetController(const ServiceWorkerObjectInfo& controller,
373-
const std::vector<blink::mojom::WebFeature>& used_features,
374-
bool should_notify_controllerchange) override {
375-
was_set_controller_called_ = true;
376-
}
377-
378-
bool was_set_controller_called() const { return was_set_controller_called_; }
379-
380-
private:
381-
bool was_set_controller_called_ = false;
382-
mojo::AssociatedBinding<mojom::ServiceWorkerContainer> binding_;
383-
};
384-
385375
TEST_F(ServiceWorkerProviderHostTest, Controller) {
386376
base::CommandLine::ForCurrentProcess()->AppendSwitch(
387377
switches::kEnableBrowserSideNavigation);
@@ -395,31 +385,27 @@ TEST_F(ServiceWorkerProviderHostTest, Controller) {
395385
true /* is_parent_frame_secure */);
396386
remote_endpoints_.emplace_back();
397387
remote_endpoints_.back().BindWithProviderHostInfo(&info);
398-
auto container = base::MakeUnique<MockServiceWorkerContainer>(
399-
std::move(*remote_endpoints_.back().client_request()));
400388

401389
// Create an active version and then start the navigation.
402390
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
403391
registration1_.get(), GURL("https://www.example.com/sw.js"),
404392
1 /* version_id */, helper_->context()->AsWeakPtr());
405393
registration1_->SetActiveVersion(version);
394+
SimulateServiceWorkerControlleeRequestHandler(host.get(),
395+
registration1_.get());
406396

407397
// Finish the navigation.
408-
host->SetDocumentUrl(GURL("https://www.example.com/page"));
409398
host->CompleteNavigationInitialized(
410399
helper_->mock_render_process_id(), std::move(info),
411400
helper_->GetDispatcherHostForProcess(helper_->mock_render_process_id())
412401
->AsWeakPtr());
413402

414-
host->AssociateRegistration(registration1_.get(),
415-
false /* notify_controllerchange */);
416-
base::RunLoop().RunUntilIdle();
417-
418403
// The page should be controlled since there was an active version at the
419404
// time navigation started. The SetController IPC should have been sent.
420405
EXPECT_TRUE(host->active_version());
421406
EXPECT_EQ(host->active_version(), host->controller());
422-
EXPECT_TRUE(container->was_set_controller_called());
407+
EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
408+
ServiceWorkerMsg_SetControllerServiceWorker::ID));
423409
}
424410

425411
TEST_F(ServiceWorkerProviderHostTest, ActiveIsNotController) {
@@ -435,34 +421,31 @@ TEST_F(ServiceWorkerProviderHostTest, ActiveIsNotController) {
435421
true /* is_parent_frame_secure */);
436422
remote_endpoints_.emplace_back();
437423
remote_endpoints_.back().BindWithProviderHostInfo(&info);
438-
auto container = base::MakeUnique<MockServiceWorkerContainer>(
439-
std::move(*remote_endpoints_.back().client_request()));
440424

441-
// Create an installing version and then start the navigation.
425+
// Associate it with an installing registration then start the navigation.
442426
scoped_refptr<ServiceWorkerVersion> version = new ServiceWorkerVersion(
443427
registration1_.get(), GURL("https://www.example.com/sw.js"),
444428
1 /* version_id */, helper_->context()->AsWeakPtr());
445429
registration1_->SetInstallingVersion(version);
430+
SimulateServiceWorkerControlleeRequestHandler(host.get(),
431+
registration1_.get());
432+
433+
// Promote the worker to active while navigation is still happening.
434+
registration1_->SetActiveVersion(version);
446435

447436
// Finish the navigation.
448-
host->SetDocumentUrl(GURL("https://www.example.com/page"));
449437
host->CompleteNavigationInitialized(
450438
helper_->mock_render_process_id(), std::move(info),
451439
helper_->GetDispatcherHostForProcess(helper_->mock_render_process_id())
452440
->AsWeakPtr());
453441

454-
host->AssociateRegistration(registration1_.get(),
455-
false /* notify_controllerchange */);
456-
// Promote the worker to active while navigation is still happening.
457-
registration1_->SetActiveVersion(version);
458-
base::RunLoop().RunUntilIdle();
459-
460442
// The page should not be controlled since there was no active version at the
461443
// time navigation started. Furthermore, no SetController IPC should have been
462444
// sent.
463445
EXPECT_TRUE(host->active_version());
464446
EXPECT_FALSE(host->controller());
465-
EXPECT_FALSE(container->was_set_controller_called());
447+
EXPECT_EQ(nullptr, helper_->ipc_sink()->GetFirstMessageMatching(
448+
ServiceWorkerMsg_SetControllerServiceWorker::ID));
466449
}
467450

468451
} // namespace content

content/child/service_worker/service_worker_dispatcher.cc

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ void ServiceWorkerDispatcher::OnMessageReceived(const IPC::Message& msg) {
9393
OnSetVersionAttributes)
9494
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_UpdateFound,
9595
OnUpdateFound)
96+
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SetControllerServiceWorker,
97+
OnSetControllerServiceWorker)
9698
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToDocument,
9799
OnPostMessage)
98100
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CountFeature, OnCountFeature)
@@ -184,18 +186,9 @@ void ServiceWorkerDispatcher::AddProviderClient(
184186
}
185187

186188
void ServiceWorkerDispatcher::RemoveProviderClient(int provider_id) {
187-
auto iter = provider_clients_.find(provider_id);
188189
// This could be possibly called multiple times to ensure termination.
189-
if (iter != provider_clients_.end())
190-
provider_clients_.erase(iter);
191-
}
192-
193-
blink::WebServiceWorkerProviderClient*
194-
ServiceWorkerDispatcher::GetProviderClient(int provider_id) {
195-
auto iter = provider_clients_.find(provider_id);
196-
if (iter != provider_clients_.end())
197-
return iter->second;
198-
return nullptr;
190+
if (base::ContainsKey(provider_clients_, provider_id))
191+
provider_clients_.erase(provider_id);
199192
}
200193

201194
ServiceWorkerDispatcher*
@@ -224,11 +217,6 @@ ServiceWorkerDispatcher* ServiceWorkerDispatcher::GetThreadSpecificInstance() {
224217
g_dispatcher_tls.Pointer()->Get());
225218
}
226219

227-
std::unique_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt(
228-
const ServiceWorkerObjectInfo& info) {
229-
return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get());
230-
}
231-
232220
void ServiceWorkerDispatcher::WillStopCurrentWorkerThread() {
233221
delete this;
234222
}
@@ -522,6 +510,43 @@ void ServiceWorkerDispatcher::OnUpdateFound(
522510
found->second->OnUpdateFound();
523511
}
524512

513+
void ServiceWorkerDispatcher::OnSetControllerServiceWorker(
514+
const ServiceWorkerMsg_SetControllerServiceWorker_Params& params) {
515+
TRACE_EVENT2(
516+
"ServiceWorker", "ServiceWorkerDispatcher::OnSetControllerServiceWorker",
517+
"Thread ID", params.thread_id, "Provider ID", params.provider_id);
518+
519+
// Adopt the reference sent from the browser process and pass it to the
520+
// provider context if it exists.
521+
std::unique_ptr<ServiceWorkerHandleReference> handle_ref =
522+
Adopt(params.object_info);
523+
ProviderContextMap::iterator provider =
524+
provider_contexts_.find(params.provider_id);
525+
if (provider != provider_contexts_.end()) {
526+
provider->second->SetController(std::move(handle_ref),
527+
params.used_features);
528+
}
529+
530+
ProviderClientMap::iterator found =
531+
provider_clients_.find(params.provider_id);
532+
if (found != provider_clients_.end()) {
533+
// Sync the controllee's use counter with the service worker's one.
534+
for (uint32_t feature : params.used_features)
535+
found->second->CountFeature(feature);
536+
537+
// Get the existing worker object or create a new one with a new reference
538+
// to populate the .controller field.
539+
scoped_refptr<WebServiceWorkerImpl> worker =
540+
GetOrCreateServiceWorker(ServiceWorkerHandleReference::Create(
541+
params.object_info, thread_safe_sender_.get()));
542+
found->second->SetController(WebServiceWorkerImpl::CreateHandle(worker),
543+
params.should_notify_controllerchange);
544+
// You must not access |found| after setController() because it may fire the
545+
// controllerchange event that may remove the provider client, for example,
546+
// by detaching an iframe.
547+
}
548+
}
549+
525550
void ServiceWorkerDispatcher::OnPostMessage(
526551
const ServiceWorkerMsg_MessageToDocument_Params& params) {
527552
// Make sure we're on the main document thread. (That must be the only
@@ -586,4 +611,9 @@ void ServiceWorkerDispatcher::RemoveServiceWorkerRegistration(
586611
registrations_.erase(registration_handle_id);
587612
}
588613

614+
std::unique_ptr<ServiceWorkerHandleReference> ServiceWorkerDispatcher::Adopt(
615+
const ServiceWorkerObjectInfo& info) {
616+
return ServiceWorkerHandleReference::Adopt(info, thread_safe_sender_.get());
617+
}
618+
589619
} // namespace content

content/child/service_worker/service_worker_dispatcher.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
112112
blink::WebServiceWorkerProviderClient* client);
113113
void RemoveProviderClient(int provider_id);
114114

115-
blink::WebServiceWorkerProviderClient* GetProviderClient(int provider_id);
116-
117115
// Returns the existing service worker or a newly created one with the given
118116
// handle reference. Returns nullptr if the given reference is invalid.
119117
scoped_refptr<WebServiceWorkerImpl> GetOrCreateServiceWorker(
@@ -141,11 +139,6 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
141139
// instance if thread-local instance doesn't exist.
142140
static ServiceWorkerDispatcher* GetThreadSpecificInstance();
143141

144-
// Assumes that the given object information retains an interprocess handle
145-
// reference passed from the browser process, and adopts it.
146-
std::unique_ptr<ServiceWorkerHandleReference> Adopt(
147-
const ServiceWorkerObjectInfo& info);
148-
149142
base::SingleThreadTaskRunner* main_thread_task_runner() {
150143
return main_thread_task_runner_.get();
151144
}
@@ -234,6 +227,11 @@ class CONTENT_EXPORT ServiceWorkerDispatcher : public WorkerThread::Observer {
234227
void RemoveServiceWorkerRegistration(
235228
int registration_handle_id);
236229

230+
// Assumes that the given object information retains an interprocess handle
231+
// reference passed from the browser process, and adopts it.
232+
std::unique_ptr<ServiceWorkerHandleReference> Adopt(
233+
const ServiceWorkerObjectInfo& info);
234+
237235
UpdateCallbackMap pending_update_callbacks_;
238236
UnregistrationCallbackMap pending_unregistration_callbacks_;
239237
EnableNavigationPreloadCallbackMap enable_navigation_preload_callbacks_;

0 commit comments

Comments
 (0)