- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_dispatcher_host.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_dispatcher_host.h"
6
7 #include "content/browser/service_worker/service_worker_context.h"
8 #include "content/common/service_worker_messages.h"
9 #include "ipc/ipc_message_macros.h"
10 #include "url/gurl.h"
11
12 namespace content {
13
14 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost(
15     int render_process_id,
16     ServiceWorkerContext* context) : context_(context) {}
17
18 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() {}
19
20 bool ServiceWorkerDispatcherHost::OnMessageReceived(const IPC::Message& message,
21                                                     bool* message_was_ok) {
22   if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart)
23     return false;
24
25   bool handled = true;
26   IPC_BEGIN_MESSAGE_MAP_EX(
27     ServiceWorkerDispatcherHost, message, *message_was_ok)
28     IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker,
29                         OnRegisterServiceWorker)
30     IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker,
31                         OnUnregisterServiceWorker)
32     IPC_MESSAGE_UNHANDLED(handled = false)
33   IPC_END_MESSAGE_MAP()
34
35   return handled;
36 }
37
38 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern,
39 // script) so we don't always return a new service worker id.
40 static int64 NextWorkerId() {
41   static int64 service_worker_id = 0;
42   return service_worker_id++;
43 }
44
45 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker(
46     int32 thread_id,
47     int32 request_id,
48     const GURL& scope,
49     const GURL& script_url) {
50   // TODO(alecflett): add a ServiceWorker-specific policy query in
51   // ChildProcessSecurityImpl. See http://crbug.com/311631.
52
53   // TODO(alecflett): Throw an error for origin mismatch, rather than
54   // just returning.
55   if (scope.GetOrigin() != script_url.GetOrigin())
56     return;
57
58   Send(new ServiceWorkerMsg_ServiceWorkerRegistered(
59       thread_id, request_id, NextWorkerId()));
60 }
61
62 void ServiceWorkerDispatcherHost::OnUnregisterServiceWorker(int32 thread_id,
63                                                             int32 request_id,
64                                                             const GURL& scope) {
65   // TODO(alecflett): add a ServiceWorker-specific policy query in
66   // ChildProcessSecurityImpl. See http://crbug.com/311631.
67
68   Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id));
69 }
70
71 }  // namespace content