summaryrefslogtreecommitdiffstats
path: root/content/browser/service_worker
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-06 01:34:28 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-06 01:34:28 +0000
commitd2f46b1d31044a6cb76b2a4695cadfd8ed786d2f (patch)
treea955a91e6d074aeb7c91c0cb75d289dbcd39ca3d /content/browser/service_worker
parent5def89eb4f851f06ed7d322ec5308aae6d50fd6b (diff)
downloadchromium_src-d2f46b1d31044a6cb76b2a4695cadfd8ed786d2f.zip
chromium_src-d2f46b1d31044a6cb76b2a4695cadfd8ed786d2f.tar.gz
chromium_src-d2f46b1d31044a6cb76b2a4695cadfd8ed786d2f.tar.bz2
Implement ServiceWorkerVersion::DispatchInstallEvent()
This doesn't hook any blink code yet, but sends InstallEvent to the child process and dispatches callback for InstallEventFinished response. This depends on: https://codereview.chromium.org/139923005/ BUG=313530 TEST=ServiceWorkerBrowserTest.Install Review URL: https://codereview.chromium.org/142863009 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@249201 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'content/browser/service_worker')
-rw-r--r--content/browser/service_worker/embedded_worker_registry.cc13
-rw-r--r--content/browser/service_worker/service_worker_browsertest.cc73
-rw-r--r--content/browser/service_worker/service_worker_version.cc25
-rw-r--r--content/browser/service_worker/service_worker_version.h8
4 files changed, 95 insertions, 24 deletions
diff --git a/content/browser/service_worker/embedded_worker_registry.cc b/content/browser/service_worker/embedded_worker_registry.cc
index 17f69b2..90418a0 100644
--- a/content/browser/service_worker/embedded_worker_registry.cc
+++ b/content/browser/service_worker/embedded_worker_registry.cc
@@ -8,6 +8,7 @@
#include "content/browser/service_worker/embedded_worker_instance.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/common/service_worker/embedded_worker_messages.h"
+#include "content/common/service_worker/service_worker_messages.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_sender.h"
@@ -74,10 +75,14 @@ void EmbeddedWorkerRegistry::OnSendMessageToBrowser(
LOG(ERROR) << "Worker " << embedded_worker_id << " not registered";
return;
}
- // TODO(kinuko): Filter out unexpected messages here and uncomment below
- // when we actually define messages that are to be sent from child process
- // to the browser via this channel. (We don't have any yet)
- found->second->OnMessageReceived(request_id, message);
+ // Perform security check to filter out any unexpected (and non-test)
+ // messages. This must list up all message types that can go through here.
+ if (message.type() == ServiceWorkerHostMsg_InstallEventFinished::ID ||
+ IPC_MESSAGE_CLASS(message) == TestMsgStart) {
+ found->second->OnMessageReceived(request_id, message);
+ return;
+ }
+ NOTREACHED() << "Got unexpected message: " << message.type();
}
void EmbeddedWorkerRegistry::AddChildProcessSender(
diff --git a/content/browser/service_worker/service_worker_browsertest.cc b/content/browser/service_worker/service_worker_browsertest.cc
index 50c364c..1b64818 100644
--- a/content/browser/service_worker/service_worker_browsertest.cc
+++ b/content/browser/service_worker/service_worker_browsertest.cc
@@ -59,6 +59,13 @@ class ServiceWorkerBrowserTest : public ContentBrowserTest {
StoragePartition* partition = BrowserContext::GetDefaultStoragePartition(
shell()->web_contents()->GetBrowserContext());
wrapper_ = partition->GetServiceWorkerContext();
+
+ // Navigate to the page to set up a provider.
+ NavigateToURLBlockUntilNavigationsComplete(
+ shell(),
+ embedded_test_server()->GetURL("/service_worker/index.html"), 1);
+
+ RunOnIOThread(base::Bind(&self::SetUpOnIOThread, this));
}
virtual void TearDownOnMainThread() OVERRIDE {
@@ -66,6 +73,7 @@ class ServiceWorkerBrowserTest : public ContentBrowserTest {
wrapper_ = NULL;
}
+ virtual void SetUpOnIOThread() {}
virtual void TearDownOnIOThread() {}
ServiceWorkerContextWrapper* wrapper() { return wrapper_.get(); }
@@ -172,7 +180,7 @@ class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
public:
typedef ServiceWorkerVersionBrowserTest self;
- ServiceWorkerVersionBrowserTest() {}
+ ServiceWorkerVersionBrowserTest() : next_registration_id_(1) {}
virtual ~ServiceWorkerVersionBrowserTest() {}
virtual void TearDownOnIOThread() OVERRIDE {
@@ -186,25 +194,32 @@ class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
}
}
- void StartOnIOThread(const std::string& worker_url,
- const base::Closure& done,
- ServiceWorkerStatusCode* result) {
- ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
-
- const int64 registration_id = 1L;
+ void SetUpRegistrationOnIOThread(const std::string& worker_url) {
const int64 version_id = 1L;
registration_ = new ServiceWorkerRegistration(
embedded_test_server()->GetURL("/*"),
embedded_test_server()->GetURL(worker_url),
- registration_id);
+ next_registration_id_++);
version_ = new ServiceWorkerVersion(
registration_,
wrapper()->context()->embedded_worker_registry(),
version_id);
AssociateProcessToWorker(version_->embedded_worker());
+ }
+
+ void StartOnIOThread(const base::Closure& done,
+ ServiceWorkerStatusCode* result) {
+ ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
version_->StartWorker(CreateReceiver(BrowserThread::UI, done, result));
}
+ void InstallOnIOThread(const base::Closure& done,
+ ServiceWorkerStatusCode* result) {
+ ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ version_->DispatchInstallEvent(
+ -1, CreateReceiver(BrowserThread::UI, done, result));
+ }
+
void StopOnIOThread(const base::Closure& done,
ServiceWorkerStatusCode* result) {
ASSERT_TRUE(version_);
@@ -212,15 +227,12 @@ class ServiceWorkerVersionBrowserTest : public ServiceWorkerBrowserTest {
}
protected:
+ int64 next_registration_id_;
scoped_refptr<ServiceWorkerRegistration> registration_;
scoped_refptr<ServiceWorkerVersion> version_;
};
IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) {
- // Navigate to the page to set up a provider.
- NavigateToURLBlockUntilNavigationsComplete(
- shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1);
-
// Start a worker and wait until OnStarted() is called.
base::RunLoop start_run_loop;
done_closure_ = start_run_loop.QuitClosure();
@@ -241,16 +253,14 @@ IN_PROC_BROWSER_TEST_F(EmbeddedWorkerBrowserTest, StartAndStop) {
}
IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartAndStop) {
- // Navigate to the page to set up a provider.
- NavigateToURLBlockUntilNavigationsComplete(
- shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1);
+ RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
+ "/service_worker/worker.js"));
// Start a worker.
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
base::RunLoop start_run_loop;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&self::StartOnIOThread, this,
- "/service_worker/worker.js",
start_run_loop.QuitClosure(),
&status));
start_run_loop.Run();
@@ -268,20 +278,43 @@ IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartAndStop) {
}
IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) {
- // Navigate to the page to set up a provider.
- NavigateToURLBlockUntilNavigationsComplete(
- shell(), embedded_test_server()->GetURL("/service_worker/index.html"), 1);
+ RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
+ "/service_worker/nonexistent.js"));
// Start a worker for nonexistent URL.
ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
base::RunLoop start_run_loop;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&self::StartOnIOThread, this,
- "/service_worker/nonexistent.js",
start_run_loop.QuitClosure(),
&status));
start_run_loop.Run();
ASSERT_EQ(SERVICE_WORKER_ERROR_START_WORKER_FAILED, status);
}
+IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) {
+ RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this,
+ "/service_worker/worker.js"));
+
+ // Dispatch install on a worker.
+ ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED;
+ base::RunLoop install_run_loop;
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&self::InstallOnIOThread, this,
+ install_run_loop.QuitClosure(),
+ &status));
+ install_run_loop.Run();
+ ASSERT_EQ(SERVICE_WORKER_OK, status);
+
+ // Stop the worker.
+ status = SERVICE_WORKER_ERROR_FAILED;
+ base::RunLoop stop_run_loop;
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&self::StopOnIOThread, this,
+ stop_run_loop.QuitClosure(),
+ &status));
+ stop_run_loop.Run();
+ ASSERT_EQ(SERVICE_WORKER_OK, status);
+}
+
} // namespace content
diff --git a/content/browser/service_worker/service_worker_version.cc b/content/browser/service_worker/service_worker_version.cc
index 8fe826d..3a81ca4 100644
--- a/content/browser/service_worker/service_worker_version.cc
+++ b/content/browser/service_worker/service_worker_version.cc
@@ -57,6 +57,23 @@ void RunEmptyMessageCallback(const MessageCallback& callback,
callback.Run(status, IPC::Message());
}
+void HandleInstallFinished(const StatusCallback& callback,
+ ServiceWorkerStatusCode status,
+ const IPC::Message& message) {
+ if (status != SERVICE_WORKER_OK) {
+ callback.Run(status);
+ return;
+ }
+
+ if (message.type() != ServiceWorkerHostMsg_InstallEventFinished::ID) {
+ NOTREACHED() << "Got unexpected response for InstallEvent: "
+ << message.type();
+ callback.Run(SERVICE_WORKER_ERROR_FAILED);
+ return;
+ }
+ callback.Run(SERVICE_WORKER_OK);
+}
+
} // namespace
ServiceWorkerVersion::ServiceWorkerVersion(
@@ -168,6 +185,14 @@ void ServiceWorkerVersion::SendMessageAndRegisterCallback(
}
}
+void ServiceWorkerVersion::DispatchInstallEvent(
+ int active_version_embedded_worker_id,
+ const StatusCallback& callback) {
+ SendMessageAndRegisterCallback(
+ ServiceWorkerMsg_InstallEvent(active_version_embedded_worker_id),
+ base::Bind(&HandleInstallFinished, callback));
+}
+
bool ServiceWorkerVersion::DispatchFetchEvent(
const ServiceWorkerFetchRequest& request) {
if (status() != RUNNING)
diff --git a/content/browser/service_worker/service_worker_version.h b/content/browser/service_worker/service_worker_version.h
index b1daad82..ba668b4 100644
--- a/content/browser/service_worker/service_worker_version.h
+++ b/content/browser/service_worker/service_worker_version.h
@@ -113,6 +113,14 @@ class CONTENT_EXPORT ServiceWorkerVersion
void SendMessageAndRegisterCallback(const IPC::Message& message,
const MessageCallback& callback);
+ // Sends install event to the associated embedded worker and asynchronously
+ // calls |callback| when it errors out or it gets response from the worker
+ // to notify install completion.
+ // |active_version_embedded_worker_id| must be a valid positive ID
+ // if there's an active (previous) version running.
+ void DispatchInstallEvent(int active_version_embedded_worker_id,
+ const StatusCallback& callback);
+
// Sends fetch event to the associated embedded worker.
// This immediately returns false if the worker is not running
// or sending a message to the child process fails.