Commit 72c6846b authored by Nidhi Jaju's avatar Nidhi Jaju Committed by Commit Bot

Use FakeServiceWorkerContext in IsolatedPrerenderTabHelper unittests

This CL removes the ForTest() functions in service_worker_context.h,
specifically:
- WaitForRegistrationsInitializedForTest()
- AddRegistrationToRegisteredOriginsForTest(const url::Origin& origin)

The motivation of this change is to avoid unnecessarily exposing
methods from service_worker_context_wrapper.cc, and use the
FakeServiceWorkerContext instead of using the real context when it is
not needed. This leads to better consistency with the storage as well.

Bug: 807440
Change-Id: I01791b82062d8e3c3bef63b029e6f77d1fe02469
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2262398Reviewed-by: default avatarFrançois Doray <fdoray@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarKenichi Ishibashi <bashi@chromium.org>
Reviewed-by: default avatarRobert Ogden <robertogden@chromium.org>
Reviewed-by: default avatarPatrick Monette <pmonette@chromium.org>
Commit-Queue: Nidhi Jaju <nidhijaju@google.com>
Cr-Commit-Position: refs/heads/master@{#784201}
parent 95a026b9
...@@ -190,6 +190,15 @@ const void* IsolatedPrerenderTabHelper::PrefetchingLikelyEventKey() { ...@@ -190,6 +190,15 @@ const void* IsolatedPrerenderTabHelper::PrefetchingLikelyEventKey() {
return &kPrefetchingLikelyEventKey; return &kPrefetchingLikelyEventKey;
} }
static content::ServiceWorkerContext* g_service_worker_context_for_test =
nullptr;
// static
void IsolatedPrerenderTabHelper::SetServiceWorkerContextForTest(
content::ServiceWorkerContext* context) {
g_service_worker_context_for_test = context;
}
IsolatedPrerenderTabHelper::IsolatedPrerenderTabHelper( IsolatedPrerenderTabHelper::IsolatedPrerenderTabHelper(
content::WebContents* web_contents) content::WebContents* web_contents)
: content::WebContentsObserver(web_contents) { : content::WebContentsObserver(web_contents) {
...@@ -917,6 +926,15 @@ void IsolatedPrerenderTabHelper::OnPredictionUpdated( ...@@ -917,6 +926,15 @@ void IsolatedPrerenderTabHelper::OnPredictionUpdated(
} }
} }
// static
content::ServiceWorkerContext*
IsolatedPrerenderTabHelper::GetServiceWorkerContext(Profile* profile) {
if (g_service_worker_context_for_test)
return g_service_worker_context_for_test;
return content::BrowserContext::GetDefaultStoragePartition(profile)
->GetServiceWorkerContext();
}
// static // static
void IsolatedPrerenderTabHelper::CheckEligibilityOfURL( void IsolatedPrerenderTabHelper::CheckEligibilityOfURL(
Profile* profile, Profile* profile,
...@@ -971,7 +989,7 @@ void IsolatedPrerenderTabHelper::CheckEligibilityOfURL( ...@@ -971,7 +989,7 @@ void IsolatedPrerenderTabHelper::CheckEligibilityOfURL(
} }
content::ServiceWorkerContext* service_worker_context_ = content::ServiceWorkerContext* service_worker_context_ =
default_storage_partition->GetServiceWorkerContext(); GetServiceWorkerContext(profile);
bool site_has_service_worker = bool site_has_service_worker =
service_worker_context_->MaybeHasRegistrationForOrigin( service_worker_context_->MaybeHasRegistrationForOrigin(
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service.h" #include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service.h"
#include "chrome/browser/prerender/isolated/prefetched_mainframe_response_container.h" #include "chrome/browser/prerender/isolated/prefetched_mainframe_response_container.h"
#include "content/public/browser/service_worker_context.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
#include "mojo/public/cpp/bindings/pending_receiver.h" #include "mojo/public/cpp/bindings/pending_receiver.h"
...@@ -224,6 +225,13 @@ class IsolatedPrerenderTabHelper ...@@ -224,6 +225,13 @@ class IsolatedPrerenderTabHelper
base::Optional<base::TimeDelta> probe_latency_; base::Optional<base::TimeDelta> probe_latency_;
}; };
// Checks if a |service_worker_context_for_test_| is available, and if not,
// returns the real service worker context from the default storage partition.
// This is used for passing the |service_worker_context| to
// CheckEligibilityOfURL().
static content::ServiceWorkerContext* GetServiceWorkerContext(
Profile* profile);
// Used to determine if |url| is eligible for isolated prefetching. Also gives // Used to determine if |url| is eligible for isolated prefetching. Also gives
// a reason in |status| if one is applicable. // a reason in |status| if one is applicable.
using OnEligibilityResultCallback = base::OnceCallback<void( using OnEligibilityResultCallback = base::OnceCallback<void(
...@@ -276,6 +284,12 @@ class IsolatedPrerenderTabHelper ...@@ -276,6 +284,12 @@ class IsolatedPrerenderTabHelper
network::mojom::NetworkContext* GetIsolatedContextForTesting() const; network::mojom::NetworkContext* GetIsolatedContextForTesting() const;
// Sets the service_worker_context_for_test_ with a FakeServiceWorkerContext
// for the the purpose of testing.
// Used in the SetUp() method in isolated_prerender_tab_helper_unittest.cc.
static void SetServiceWorkerContextForTest(
content::ServiceWorkerContext* context);
protected: protected:
// Exposed for testing. // Exposed for testing.
explicit IsolatedPrerenderTabHelper(content::WebContents* web_contents); explicit IsolatedPrerenderTabHelper(content::WebContents* web_contents);
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "content/public/browser/service_worker_context.h" #include "content/public/browser/service_worker_context.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/test/fake_service_worker_context.h"
#include "content/public/test/mock_navigation_handle.h" #include "content/public/test/mock_navigation_handle.h"
#include "mojo/public/cpp/bindings/remote.h" #include "mojo/public/cpp/bindings/remote.h"
#include "net/base/isolation_info.h" #include "net/base/isolation_info.h"
...@@ -97,14 +98,10 @@ class IsolatedPrerenderTabHelperTest : public ChromeRenderViewHostTestHarness { ...@@ -97,14 +98,10 @@ class IsolatedPrerenderTabHelperTest : public ChromeRenderViewHostTestHarness {
void SetUp() override { void SetUp() override {
ChromeRenderViewHostTestHarness::SetUp(); ChromeRenderViewHostTestHarness::SetUp();
content::ServiceWorkerContext* service_worker_context_ =
content::BrowserContext::GetDefaultStoragePartition(profile())
->GetServiceWorkerContext();
service_worker_context_->WaitForRegistrationsInitializedForTest();
tab_helper_ = tab_helper_ =
std::make_unique<TestIsolatedPrerenderTabHelper>(web_contents()); std::make_unique<TestIsolatedPrerenderTabHelper>(web_contents());
tab_helper_->SetURLLoaderFactory(test_shared_loader_factory_); tab_helper_->SetURLLoaderFactory(test_shared_loader_factory_);
tab_helper_->SetServiceWorkerContextForTest(&service_worker_context_);
SetDataSaverEnabled(true); SetDataSaverEnabled(true);
} }
...@@ -325,6 +322,8 @@ class IsolatedPrerenderTabHelperTest : public ChromeRenderViewHostTestHarness { ...@@ -325,6 +322,8 @@ class IsolatedPrerenderTabHelperTest : public ChromeRenderViewHostTestHarness {
network::TestURLLoaderFactory test_url_loader_factory_; network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_; scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
content::FakeServiceWorkerContext service_worker_context_;
private: private:
std::unique_ptr<TestIsolatedPrerenderTabHelper> tab_helper_; std::unique_ptr<TestIsolatedPrerenderTabHelper> tab_helper_;
}; };
...@@ -1322,10 +1321,7 @@ TEST_F(IsolatedPrerenderTabHelperTest, ServiceWorkerRegistered) { ...@@ -1322,10 +1321,7 @@ TEST_F(IsolatedPrerenderTabHelperTest, ServiceWorkerRegistered) {
GURL doc_url("https://www.google.com/search?q=cats"); GURL doc_url("https://www.google.com/search?q=cats");
GURL prediction_url("https://www.cat-food.com/"); GURL prediction_url("https://www.cat-food.com/");
content::ServiceWorkerContext* service_worker_context_ = service_worker_context_.AddRegistrationToRegisteredOrigins(
content::BrowserContext::GetDefaultStoragePartition(profile())
->GetServiceWorkerContext();
service_worker_context_->AddRegistrationToRegisteredOriginsForTest(
url::Origin::Create(prediction_url)); url::Origin::Create(prediction_url));
MakeNavigationPrediction(web_contents(), doc_url, {prediction_url}); MakeNavigationPrediction(web_contents(), doc_url, {prediction_url});
...@@ -1354,10 +1350,7 @@ TEST_F(IsolatedPrerenderTabHelperTest, ServiceWorkerNotRegistered) { ...@@ -1354,10 +1350,7 @@ TEST_F(IsolatedPrerenderTabHelperTest, ServiceWorkerNotRegistered) {
GURL prediction_url("https://www.cat-food.com/"); GURL prediction_url("https://www.cat-food.com/");
GURL service_worker_registration("https://www.service-worker.com/"); GURL service_worker_registration("https://www.service-worker.com/");
content::ServiceWorkerContext* service_worker_context_ = service_worker_context_.AddRegistrationToRegisteredOrigins(
content::BrowserContext::GetDefaultStoragePartition(profile())
->GetServiceWorkerContext();
service_worker_context_->AddRegistrationToRegisteredOriginsForTest(
url::Origin::Create(service_worker_registration)); url::Origin::Create(service_worker_registration));
MakeNavigationPrediction(web_contents(), doc_url, {prediction_url}); MakeNavigationPrediction(web_contents(), doc_url, {prediction_url});
...@@ -1538,10 +1531,7 @@ TEST_F(IsolatedPrerenderTabHelperRedirectTest, NoRedirect_ServiceWorker) { ...@@ -1538,10 +1531,7 @@ TEST_F(IsolatedPrerenderTabHelperRedirectTest, NoRedirect_ServiceWorker) {
GURL site_with_worker("https://service-worker.com"); GURL site_with_worker("https://service-worker.com");
content::ServiceWorkerContext* service_worker_context_ = service_worker_context_.AddRegistrationToRegisteredOrigins(
content::BrowserContext::GetDefaultStoragePartition(profile())
->GetServiceWorkerContext();
service_worker_context_->AddRegistrationToRegisteredOriginsForTest(
url::Origin::Create(site_with_worker)); url::Origin::Create(site_with_worker));
RunNoRedirectTest(site_with_worker); RunNoRedirectTest(site_with_worker);
......
...@@ -122,15 +122,6 @@ bool ServiceWorkerContextAdapter::MaybeHasRegistrationForOrigin( ...@@ -122,15 +122,6 @@ bool ServiceWorkerContextAdapter::MaybeHasRegistrationForOrigin(
return false; return false;
} }
void ServiceWorkerContextAdapter::WaitForRegistrationsInitializedForTest() {
NOTIMPLEMENTED();
}
void ServiceWorkerContextAdapter::AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) {
NOTIMPLEMENTED();
}
void ServiceWorkerContextAdapter::GetAllOriginsInfo( void ServiceWorkerContextAdapter::GetAllOriginsInfo(
GetUsageInfoCallback callback) { GetUsageInfoCallback callback) {
NOTIMPLEMENTED(); NOTIMPLEMENTED();
......
...@@ -62,9 +62,6 @@ class ServiceWorkerContextAdapter ...@@ -62,9 +62,6 @@ class ServiceWorkerContextAdapter
const GURL& origin, const GURL& origin,
CountExternalRequestsCallback callback) override; CountExternalRequestsCallback callback) override;
bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override; bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override;
void WaitForRegistrationsInitializedForTest() override;
void AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) override;
void GetAllOriginsInfo(GetUsageInfoCallback callback) override; void GetAllOriginsInfo(GetUsageInfoCallback callback) override;
void DeleteForOrigin(const GURL& origin_url, void DeleteForOrigin(const GURL& origin_url,
ResultCallback callback) override; ResultCallback callback) override;
......
...@@ -579,20 +579,6 @@ bool ServiceWorkerContextWrapper::MaybeHasRegistrationForOrigin( ...@@ -579,20 +579,6 @@ bool ServiceWorkerContextWrapper::MaybeHasRegistrationForOrigin(
return false; return false;
} }
void ServiceWorkerContextWrapper::WaitForRegistrationsInitializedForTest() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (registrations_initialized_)
return;
base::RunLoop loop;
on_registrations_initialized_ = loop.QuitClosure();
loop.Run();
}
void ServiceWorkerContextWrapper::AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) {
registered_origins_.insert(origin);
}
void ServiceWorkerContextWrapper::GetAllOriginsInfo( void ServiceWorkerContextWrapper::GetAllOriginsInfo(
GetUsageInfoCallback callback) { GetUsageInfoCallback callback) {
RunOrPostTaskOnCoreThread( RunOrPostTaskOnCoreThread(
...@@ -2089,6 +2075,15 @@ void ServiceWorkerContextWrapper::DidSetUpLoaderFactoryForUpdateCheck( ...@@ -2089,6 +2075,15 @@ void ServiceWorkerContextWrapper::DidSetUpLoaderFactoryForUpdateCheck(
std::move(callback).Run(std::move(loader_factory)); std::move(callback).Run(std::move(loader_factory));
} }
void ServiceWorkerContextWrapper::WaitForRegistrationsInitializedForTest() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (registrations_initialized_)
return;
base::RunLoop loop;
on_registrations_initialized_ = loop.QuitClosure();
loop.Run();
}
void ServiceWorkerContextWrapper::DidGetRegisteredOrigins( void ServiceWorkerContextWrapper::DidGetRegisteredOrigins(
std::vector<url::Origin> origins) { std::vector<url::Origin> origins) {
DCHECK_CURRENTLY_ON(GetCoreThreadId()); DCHECK_CURRENTLY_ON(GetCoreThreadId());
......
...@@ -168,9 +168,6 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper ...@@ -168,9 +168,6 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper
const GURL& url, const GURL& url,
CountExternalRequestsCallback callback) override; CountExternalRequestsCallback callback) override;
bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override; bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override;
void WaitForRegistrationsInitializedForTest() override;
void AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) override;
void GetAllOriginsInfo(GetUsageInfoCallback callback) override; void GetAllOriginsInfo(GetUsageInfoCallback callback) override;
void DeleteForOrigin(const GURL& origin, ResultCallback callback) override; void DeleteForOrigin(const GURL& origin, ResultCallback callback) override;
void PerformStorageCleanup(base::OnceClosure callback) override; void PerformStorageCleanup(base::OnceClosure callback) override;
...@@ -360,6 +357,12 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper ...@@ -360,6 +357,12 @@ class CONTENT_EXPORT ServiceWorkerContextWrapper
// DeleteAndStartOver fails. // DeleteAndStartOver fails.
ServiceWorkerContextCore* context(); ServiceWorkerContextCore* context();
// This method waits for service worker registrations to be initialized on the
// UI thread, and depends on |on_registrations_initialized_| and
// |registrations_initialized_| which are called in
// InitializeRegisteredOriginsOnUI().
void WaitForRegistrationsInitializedForTest();
// This must be called on the core thread, and the |callback| also runs on // This must be called on the core thread, and the |callback| also runs on
// the core thread which can be called with nullptr on failure. // the core thread which can be called with nullptr on failure.
void GetLoaderFactoryForUpdateCheck( void GetLoaderFactoryForUpdateCheck(
......
...@@ -165,13 +165,6 @@ class CONTENT_EXPORT ServiceWorkerContext { ...@@ -165,13 +165,6 @@ class CONTENT_EXPORT ServiceWorkerContext {
// Must be called on the UI thread. // Must be called on the UI thread.
virtual bool MaybeHasRegistrationForOrigin(const url::Origin& origin) = 0; virtual bool MaybeHasRegistrationForOrigin(const url::Origin& origin) = 0;
// TODO(nidhijaju): Remove the ForTest() functions here and the tests that
// need it (e.g. in IsolatedPrerender) should use FakeServiceWorkerContext
// instead.
virtual void WaitForRegistrationsInitializedForTest() = 0;
virtual void AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) = 0;
// May be called from any thread, and the callback is called on that thread. // May be called from any thread, and the callback is called on that thread.
virtual void GetAllOriginsInfo(GetUsageInfoCallback callback) = 0; virtual void GetAllOriginsInfo(GetUsageInfoCallback callback) = 0;
......
...@@ -57,15 +57,7 @@ void FakeServiceWorkerContext::CountExternalRequestsForTest( ...@@ -57,15 +57,7 @@ void FakeServiceWorkerContext::CountExternalRequestsForTest(
} }
bool FakeServiceWorkerContext::MaybeHasRegistrationForOrigin( bool FakeServiceWorkerContext::MaybeHasRegistrationForOrigin(
const url::Origin& origin) { const url::Origin& origin) {
NOTREACHED(); return registered_origins_.find(origin) != registered_origins_.end();
return false;
}
void FakeServiceWorkerContext::WaitForRegistrationsInitializedForTest() {
NOTREACHED();
}
void FakeServiceWorkerContext::AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) {
NOTREACHED();
} }
void FakeServiceWorkerContext::GetAllOriginsInfo( void FakeServiceWorkerContext::GetAllOriginsInfo(
GetUsageInfoCallback callback) { GetUsageInfoCallback callback) {
...@@ -152,4 +144,9 @@ void FakeServiceWorkerContext::NotifyObserversOnNoControllees( ...@@ -152,4 +144,9 @@ void FakeServiceWorkerContext::NotifyObserversOnNoControllees(
observer.OnNoControllees(version_id, scope); observer.OnNoControllees(version_id, scope);
} }
void FakeServiceWorkerContext::AddRegistrationToRegisteredOrigins(
const url::Origin& origin) {
registered_origins_.insert(origin);
}
} // namespace content } // namespace content
...@@ -49,9 +49,6 @@ class FakeServiceWorkerContext : public ServiceWorkerContext { ...@@ -49,9 +49,6 @@ class FakeServiceWorkerContext : public ServiceWorkerContext {
const GURL& url, const GURL& url,
CountExternalRequestsCallback callback) override; CountExternalRequestsCallback callback) override;
bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override; bool MaybeHasRegistrationForOrigin(const url::Origin& origin) override;
void WaitForRegistrationsInitializedForTest() override;
void AddRegistrationToRegisteredOriginsForTest(
const url::Origin& origin) override;
void GetAllOriginsInfo(GetUsageInfoCallback callback) override; void GetAllOriginsInfo(GetUsageInfoCallback callback) override;
void DeleteForOrigin(const GURL& origin, ResultCallback callback) override; void DeleteForOrigin(const GURL& origin, ResultCallback callback) override;
void PerformStorageCleanup(base::OnceClosure callback) override; void PerformStorageCleanup(base::OnceClosure callback) override;
...@@ -83,6 +80,9 @@ class FakeServiceWorkerContext : public ServiceWorkerContext { ...@@ -83,6 +80,9 @@ class FakeServiceWorkerContext : public ServiceWorkerContext {
void NotifyObserversOnVersionRedundant(int64_t version_id, const GURL& scope); void NotifyObserversOnVersionRedundant(int64_t version_id, const GURL& scope);
void NotifyObserversOnNoControllees(int64_t version_id, const GURL& scope); void NotifyObserversOnNoControllees(int64_t version_id, const GURL& scope);
// Inserts |origin| into |registered_origins_| if it doesn't already exist.
void AddRegistrationToRegisteredOrigins(const url::Origin& origin);
bool start_service_worker_for_navigation_hint_called() { bool start_service_worker_for_navigation_hint_called() {
return start_service_worker_for_navigation_hint_called_; return start_service_worker_for_navigation_hint_called_;
} }
...@@ -115,6 +115,8 @@ class FakeServiceWorkerContext : public ServiceWorkerContext { ...@@ -115,6 +115,8 @@ class FakeServiceWorkerContext : public ServiceWorkerContext {
base::ObserverList<ServiceWorkerContextObserver, true>::Unchecked observers_; base::ObserverList<ServiceWorkerContextObserver, true>::Unchecked observers_;
DISALLOW_COPY_AND_ASSIGN(FakeServiceWorkerContext); DISALLOW_COPY_AND_ASSIGN(FakeServiceWorkerContext);
std::set<url::Origin> registered_origins_;
}; };
} // namespace content } // namespace content
......
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