Commit 2cc524da authored by Rayan Kanso's avatar Rayan Kanso Committed by Commit Bot

[ServiceWorker] Expose registration id in offline capability checker

Bug: 1122646
Change-Id: I263f5348b05cca1b1604bdcede21a3c9249b8654
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2440868Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Commit-Queue: Rayan Kanso <rayankans@chromium.org>
Cr-Commit-Position: refs/heads/master@{#813214}
parent aaa2ab23
...@@ -756,7 +756,8 @@ void InstallableManager::OnDidCheckHasServiceWorker( ...@@ -756,7 +756,8 @@ void InstallableManager::OnDidCheckHasServiceWorker(
void InstallableManager::OnDidCheckOfflineCapability( void InstallableManager::OnDidCheckOfflineCapability(
base::TimeTicks check_service_worker_start_time, base::TimeTicks check_service_worker_start_time,
content::OfflineCapability capability) { content::OfflineCapability capability,
int64_t service_worker_registration_id) {
switch (capability) { switch (capability) {
case content::OfflineCapability::kSupported: case content::OfflineCapability::kSupported:
worker_->has_worker = true; worker_->has_worker = true;
......
...@@ -218,7 +218,8 @@ class InstallableManager ...@@ -218,7 +218,8 @@ class InstallableManager
content::ServiceWorkerCapability capability); content::ServiceWorkerCapability capability);
void OnDidCheckOfflineCapability( void OnDidCheckOfflineCapability(
base::TimeTicks check_service_worker_start_time, base::TimeTicks check_service_worker_start_time,
content::OfflineCapability capability); content::OfflineCapability capability,
int64_t service_worker_registration_id);
void CheckAndFetchBestIcon(int ideal_icon_size_in_px, void CheckAndFetchBestIcon(int ideal_icon_size_in_px,
int minimum_icon_size_in_px, int minimum_icon_size_in_px,
......
...@@ -821,7 +821,9 @@ void ServiceWorkerContextCore::CheckOfflineCapability( ...@@ -821,7 +821,9 @@ void ServiceWorkerContextCore::CheckOfflineCapability(
base::BindOnce( base::BindOnce(
[](std::unique_ptr<ServiceWorkerOfflineCapabilityChecker> checker, [](std::unique_ptr<ServiceWorkerOfflineCapabilityChecker> checker,
ServiceWorkerContext::CheckOfflineCapabilityCallback callback, ServiceWorkerContext::CheckOfflineCapabilityCallback callback,
OfflineCapability result) { std::move(callback).Run(result); }, OfflineCapability result, int64_t registration_id) {
std::move(callback).Run(result, registration_id);
},
std::move(checker), std::move(callback))); std::move(checker), std::move(callback)));
} }
......
...@@ -735,7 +735,8 @@ void ServiceWorkerContextWrapper::CheckOfflineCapability( ...@@ -735,7 +735,8 @@ void ServiceWorkerContextWrapper::CheckOfflineCapability(
if (!context_core_) { if (!context_core_) {
GetUIThreadTaskRunner({})->PostTask( GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce(std::move(callback), OfflineCapability::kUnsupported)); base::BindOnce(std::move(callback), OfflineCapability::kUnsupported,
blink::mojom::kInvalidServiceWorkerRegistrationId));
return; return;
} }
context()->CheckOfflineCapability( context()->CheckOfflineCapability(
...@@ -1727,10 +1728,12 @@ void ServiceWorkerContextWrapper::DidCheckHasServiceWorker( ...@@ -1727,10 +1728,12 @@ void ServiceWorkerContextWrapper::DidCheckHasServiceWorker(
void ServiceWorkerContextWrapper::DidCheckOfflineCapability( void ServiceWorkerContextWrapper::DidCheckOfflineCapability(
CheckOfflineCapabilityCallback callback, CheckOfflineCapabilityCallback callback,
OfflineCapability capability) { OfflineCapability capability,
int64_t registration_id) {
DCHECK_CURRENTLY_ON(GetCoreThreadId()); DCHECK_CURRENTLY_ON(GetCoreThreadId());
GetUIThreadTaskRunner({})->PostTask( GetUIThreadTaskRunner({})->PostTask(
FROM_HERE, base::BindOnce(std::move(callback), capability)); FROM_HERE,
base::BindOnce(std::move(callback), capability, registration_id));
} }
void ServiceWorkerContextWrapper::DidFindRegistrationForUpdate( void ServiceWorkerContextWrapper::DidFindRegistrationForUpdate(
......
...@@ -431,7 +431,8 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper ...@@ -431,7 +431,8 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper
void DidCheckHasServiceWorker(CheckHasServiceWorkerCallback callback, void DidCheckHasServiceWorker(CheckHasServiceWorkerCallback callback,
content::ServiceWorkerCapability status); content::ServiceWorkerCapability status);
void DidCheckOfflineCapability(CheckOfflineCapabilityCallback callback, void DidCheckOfflineCapability(CheckOfflineCapabilityCallback callback,
content::OfflineCapability status); content::OfflineCapability status,
int64_t registration_id);
void DidFindRegistrationForUpdate( void DidFindRegistrationForUpdate(
blink::ServiceWorkerStatusCode status, blink::ServiceWorkerStatusCode status,
......
...@@ -277,15 +277,21 @@ class ServiceWorkerOfflineCapabilityCheckBrowserTest ...@@ -277,15 +277,21 @@ class ServiceWorkerOfflineCapabilityCheckBrowserTest
base::Optional<OfflineCapability> out_offline_capability; base::Optional<OfflineCapability> out_offline_capability;
RunOrPostTaskOnThread( RunOrPostTaskOnThread(
FROM_HERE, ServiceWorkerContext::GetCoreThreadId(), FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce(&ServiceWorkerOfflineCapabilityCheckBrowserTest:: base::BindOnce(
CheckOfflineCapabilityOnCoreThread, &ServiceWorkerOfflineCapabilityCheckBrowserTest::
base::Unretained(this), path, CheckOfflineCapabilityOnCoreThread,
base::BindLambdaForTesting( base::Unretained(this), path,
[&out_offline_capability, &fetch_run_loop]( base::BindLambdaForTesting([&out_offline_capability,
OfflineCapability offline_capability) { &fetch_run_loop](
out_offline_capability = offline_capability; OfflineCapability offline_capability,
fetch_run_loop.Quit(); int64_t registration_id) {
}))); out_offline_capability = offline_capability;
if (offline_capability == OfflineCapability::kSupported) {
EXPECT_NE(registration_id,
blink::mojom::kInvalidServiceWorkerRegistrationId);
}
fetch_run_loop.Quit();
})));
fetch_run_loop.Run(); fetch_run_loop.Run();
DCHECK(out_offline_capability.has_value()); DCHECK(out_offline_capability.has_value());
return *out_offline_capability; return *out_offline_capability;
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "content/browser/service_worker/service_worker_version.h" #include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/storage_partition_impl.h" #include "content/browser/storage_partition_impl.h"
#include "content/common/fetch/fetch_request_type_converters.h" #include "content/common/fetch/fetch_request_type_converters.h"
#include "third_party/blink/public/mojom/service_worker/service_worker_database.mojom.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace content {
...@@ -41,12 +42,14 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration( ...@@ -41,12 +42,14 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration(
blink::ServiceWorkerStatusCode status, blink::ServiceWorkerStatusCode status,
scoped_refptr<ServiceWorkerRegistration> registration) { scoped_refptr<ServiceWorkerRegistration> registration) {
if (status != blink::ServiceWorkerStatusCode::kOk) { if (status != blink::ServiceWorkerStatusCode::kOk) {
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
blink::mojom::kInvalidServiceWorkerRegistrationId);
return; return;
} }
if (registration->is_uninstalling() || registration->is_uninstalled()) { if (registration->is_uninstalling() || registration->is_uninstalled()) {
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
blink::mojom::kInvalidServiceWorkerRegistrationId);
return; return;
} }
...@@ -56,7 +59,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration( ...@@ -56,7 +59,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration(
preferred_version = registration->waiting_version(); preferred_version = registration->waiting_version();
} }
if (!preferred_version) { if (!preferred_version) {
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
blink::mojom::kInvalidServiceWorkerRegistrationId);
return; return;
} }
...@@ -66,7 +70,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration( ...@@ -66,7 +70,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration(
DCHECK_NE(existence, ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN); DCHECK_NE(existence, ServiceWorkerVersion::FetchHandlerExistence::UNKNOWN);
if (existence != ServiceWorkerVersion::FetchHandlerExistence::EXISTS) { if (existence != ServiceWorkerVersion::FetchHandlerExistence::EXISTS) {
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
preferred_version->registration_id());
return; return;
} }
...@@ -79,7 +84,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration( ...@@ -79,7 +84,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration(
// TODO(hayato): We can do a bit better, such as 1) trigger the activation // TODO(hayato): We can do a bit better, such as 1) trigger the activation
// and wait, or 2) return a value to indicate the service worker is // and wait, or 2) return a value to indicate the service worker is
// installed but not yet activated. // installed but not yet activated.
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
preferred_version->registration_id());
return; return;
} }
...@@ -95,7 +101,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration( ...@@ -95,7 +101,8 @@ void ServiceWorkerOfflineCapabilityChecker::DidFindRegistration(
base::WeakPtr<ServiceWorkerContextCore> context = base::WeakPtr<ServiceWorkerContextCore> context =
preferred_version->context(); preferred_version->context();
if (!context) { if (!context) {
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
preferred_version->registration_id());
return; return;
} }
...@@ -121,18 +128,20 @@ void ServiceWorkerOfflineCapabilityChecker::OnFetchResult( ...@@ -121,18 +128,20 @@ void ServiceWorkerOfflineCapabilityChecker::OnFetchResult(
blink::mojom::FetchAPIResponsePtr response, blink::mojom::FetchAPIResponsePtr response,
blink::mojom::ServiceWorkerStreamHandlePtr /* stream */, blink::mojom::ServiceWorkerStreamHandlePtr /* stream */,
blink::mojom::ServiceWorkerFetchEventTimingPtr /* timing */, blink::mojom::ServiceWorkerFetchEventTimingPtr /* timing */,
scoped_refptr<ServiceWorkerVersion>) { scoped_refptr<ServiceWorkerVersion> version) {
if (status == blink::ServiceWorkerStatusCode::kOk && if (status == blink::ServiceWorkerStatusCode::kOk &&
result == ServiceWorkerFetchDispatcher::FetchEventResult::kGotResponse && result == ServiceWorkerFetchDispatcher::FetchEventResult::kGotResponse &&
// TODO(hayato): Investigate whether any 2xx should be accepted or not. // TODO(hayato): Investigate whether any 2xx should be accepted or not.
response->status_code == 200) { response->status_code == 200) {
std::move(callback_).Run(OfflineCapability::kSupported); std::move(callback_).Run(OfflineCapability::kSupported,
version->registration_id());
} else { } else {
// TODO(hayato): At present, we return kUnsupported even if the detection // TODO(hayato): At present, we return kUnsupported even if the detection
// failed due to internal errors (disk fialures, timeout, etc). In the // failed due to internal errors (disk fialures, timeout, etc). In the
// future, we might want to return another enum value so that the callside // future, we might want to return another enum value so that the callside
// can know whether internal errors happened or not. // can know whether internal errors happened or not.
std::move(callback_).Run(OfflineCapability::kUnsupported); std::move(callback_).Run(OfflineCapability::kUnsupported,
version->registration_id());
} }
} }
......
...@@ -77,7 +77,8 @@ class CONTENT_EXPORT ServiceWorkerContext { ...@@ -77,7 +77,8 @@ class CONTENT_EXPORT ServiceWorkerContext {
base::OnceCallback<void(ServiceWorkerCapability capability)>; base::OnceCallback<void(ServiceWorkerCapability capability)>;
using CheckOfflineCapabilityCallback = using CheckOfflineCapabilityCallback =
base::OnceCallback<void(OfflineCapability capability)>; base::OnceCallback<void(OfflineCapability capability,
int64_t registration_id)>;
using CountExternalRequestsCallback = using CountExternalRequestsCallback =
base::OnceCallback<void(size_t external_request_count)>; base::OnceCallback<void(size_t external_request_count)>;
...@@ -208,8 +209,8 @@ class CONTENT_EXPORT ServiceWorkerContext { ...@@ -208,8 +209,8 @@ class CONTENT_EXPORT ServiceWorkerContext {
CheckHasServiceWorkerCallback callback) = 0; CheckHasServiceWorkerCallback callback) = 0;
// Simulates a navigation request in the offline state and dispatches a fetch // Simulates a navigation request in the offline state and dispatches a fetch
// event. Returns OfflineCapability::kSupported if the response's status code // event. Returns OfflineCapability::kSupported and the registration id if
// is 200. // the response's status code is 200.
// //
// This function can be called from any thread, but the callback will always // This function can be called from any thread, but the callback will always
// be called on the UI thread. // be called on the UI thread.
......
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