Commit 90d08bbc authored by Asami Doi's avatar Asami Doi Committed by Chromium LUCI CQ

ServiceWorker: Fix the lifetime of OnFetchEventFinished()

SWFetchDispatcher is destroyed after response is reached. Once the
dispatcher is destroyed, SWVersion::FinishRequest() in
FetchEventFinished() is never called. A service worker can't be
terminated without calling FinishRequest() and it leads to increase
the memory usage.

This CL makes OnFetchEventFinished() static to enable to call it after
SWFetchDispatcher is destructed.

The new browser tests introduced in this CL make sure
SWVersion::StartRequest() and SWVersion::FinishRequest() are called the
same number of times. Without this CL, the tests fail.

Bug: 1145551
Change-Id: I3f6ac9d2bdb19e58802f37650288b24e22bb7d54
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2543534
Commit-Queue: Asami Doi <asamidoi@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#834142}
parent 7fb39c81
......@@ -556,6 +556,7 @@ void ServiceWorkerFetchDispatcher::DidStartWorker(
DidFail(status);
return;
}
if (!IsEventDispatched()) {
DispatchFetchEvent();
}
......@@ -781,13 +782,16 @@ bool ServiceWorkerFetchDispatcher::IsEventDispatched() const {
return request_.is_null();
}
// static
void ServiceWorkerFetchDispatcher::OnFetchEventFinished(
base::WeakPtr<ServiceWorkerFetchDispatcher> fetch_dispatcher,
ServiceWorkerVersion* version,
int event_finish_id,
scoped_refptr<URLLoaderAssets> url_loader_assets,
blink::mojom::ServiceWorkerEventStatus status) {
if (status == blink::mojom::ServiceWorkerEventStatus::TIMEOUT) {
DidFail(blink::ServiceWorkerStatusCode::kErrorTimeout);
if (fetch_dispatcher &&
status == blink::mojom::ServiceWorkerEventStatus::TIMEOUT) {
fetch_dispatcher->DidFail(blink::ServiceWorkerStatusCode::kErrorTimeout);
}
version->FinishRequest(
event_finish_id,
......
......@@ -75,6 +75,8 @@ class CONTENT_EXPORT ServiceWorkerFetchDispatcher {
// the version is activated and running.
void Run();
bool FetchCallbackIsNull() { return fetch_callback_.is_null(); }
private:
class ResponseCallback;
class URLLoaderAssets;
......@@ -101,10 +103,12 @@ class CONTENT_EXPORT ServiceWorkerFetchDispatcher {
// are settled. This function is called once the renderer signals that
// happened. |fetch_callback_| can run before this, once respondWith() is
// settled.
void OnFetchEventFinished(ServiceWorkerVersion* version,
int event_finish_id,
scoped_refptr<URLLoaderAssets> url_loader_assets,
blink::mojom::ServiceWorkerEventStatus status);
static void OnFetchEventFinished(
base::WeakPtr<ServiceWorkerFetchDispatcher> fetch_dispatcher,
ServiceWorkerVersion* version,
int event_finish_id,
scoped_refptr<URLLoaderAssets> url_loader_assets,
blink::mojom::ServiceWorkerEventStatus status);
ServiceWorkerMetrics::EventType GetEventType() const;
......
......@@ -2289,6 +2289,9 @@ void ServiceWorkerVersion::OnNoWorkInBrowser() {
context_->GetLiveRegistration(registration_id());
if (registration)
registration->OnNoWork(this);
for (auto& observer : observers_)
observer.OnNoWork(this);
}
}
......
......@@ -184,6 +184,7 @@ class CONTENT_EXPORT ServiceWorkerVersion
const GURL& source_url) {}
virtual void OnCachedMetadataUpdated(ServiceWorkerVersion* version,
size_t size) {}
virtual void OnNoWork(ServiceWorkerVersion* version) {}
protected:
virtual ~Observer() {}
......
......@@ -1134,6 +1134,7 @@ test("content_browsertests") {
"../browser/service_worker/service_worker_auth_browsertest.cc",
"../browser/service_worker/service_worker_browsertest.cc",
"../browser/service_worker/service_worker_clients_api_browsertest.cc",
"../browser/service_worker/service_worker_fetch_dispatcher_browsertest.cc",
"../browser/service_worker/service_worker_file_upload_browsertest.cc",
"../browser/service_worker/service_worker_no_best_effort_tasks_browsertest.cc",
"../browser/service_worker/service_worker_offline_capability_check_browsertest.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment