OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
6 | 6 |
| 7 #include "base/stl_util.h" |
7 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
8 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
10 #include "content/common/service_worker_messages.h" | 11 #include "content/common/service_worker_messages.h" |
11 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
12 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" | 13 #include "third_party/WebKit/public/platform/WebServiceWorkerError.h" |
13 #include "url/gurl.h" | 14 #include "url/gurl.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kDisabledErrorMessage[] = | 20 const char kDisabledErrorMessage[] = |
20 "ServiceWorker is disabled"; | 21 "ServiceWorker is disabled"; |
21 const char kDomainMismatchErrorMessage[] = | 22 const char kDomainMismatchErrorMessage[] = |
22 "Scope and scripts do not have the same origin"; | 23 "Scope and scripts do not have the same origin"; |
23 | 24 |
24 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern, | 25 // TODO(alecflett): Store the service_worker_id keyed by (domain+pattern, |
25 // script) so we don't always return a new service worker id. | 26 // script) so we don't always return a new service worker id. |
26 int64 NextWorkerId() { | 27 int64 NextWorkerId() { |
27 static int64 service_worker_id = 0; | 28 static int64 service_worker_id = 0; |
28 return service_worker_id++; | 29 return service_worker_id++; |
29 } | 30 } |
30 | 31 |
31 } // namespace | 32 } // namespace |
32 | 33 |
33 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 34 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
34 int render_process_id) { | 35 int render_process_id) |
| 36 : render_process_id_(render_process_id) { |
35 } | 37 } |
36 | 38 |
37 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 39 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| 40 STLDeleteValues(&providers_); |
| 41 if (context_) |
| 42 context_->DetachProviderHostMap(render_process_id_); |
38 } | 43 } |
39 | 44 |
40 void ServiceWorkerDispatcherHost::Init( | 45 void ServiceWorkerDispatcherHost::Init( |
41 ServiceWorkerContextWrapper* context_wrapper) { | 46 ServiceWorkerContextWrapper* context_wrapper) { |
42 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 47 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
43 BrowserThread::PostTask( | 48 BrowserThread::PostTask( |
44 BrowserThread::IO, FROM_HERE, | 49 BrowserThread::IO, FROM_HERE, |
45 base::Bind(&ServiceWorkerDispatcherHost::Init, | 50 base::Bind(&ServiceWorkerDispatcherHost::Init, |
46 this, make_scoped_refptr(context_wrapper))); | 51 this, make_scoped_refptr(context_wrapper))); |
47 return; | 52 return; |
48 } | 53 } |
49 context_ = context_wrapper->context()->AsWeakPtr(); | 54 context_ = context_wrapper->context()->AsWeakPtr(); |
| 55 if (context_) |
| 56 context_->AttachProviderHostMap(render_process_id_, &providers_); |
| 57 } |
| 58 |
| 59 void ServiceWorkerDispatcherHost::OnDestruct() const { |
| 60 BrowserThread::DeleteOnIOThread::Destruct(this); |
50 } | 61 } |
51 | 62 |
52 bool ServiceWorkerDispatcherHost::OnMessageReceived( | 63 bool ServiceWorkerDispatcherHost::OnMessageReceived( |
53 const IPC::Message& message, | 64 const IPC::Message& message, |
54 bool* message_was_ok) { | 65 bool* message_was_ok) { |
55 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) | 66 if (IPC_MESSAGE_CLASS(message) != ServiceWorkerMsgStart) |
56 return false; | 67 return false; |
57 | 68 |
58 bool handled = true; | 69 bool handled = true; |
59 IPC_BEGIN_MESSAGE_MAP_EX( | 70 IPC_BEGIN_MESSAGE_MAP_EX( |
60 ServiceWorkerDispatcherHost, message, *message_was_ok) | 71 ServiceWorkerDispatcherHost, message, *message_was_ok) |
61 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, | 72 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_RegisterServiceWorker, |
62 OnRegisterServiceWorker) | 73 OnRegisterServiceWorker) |
63 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, | 74 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_UnregisterServiceWorker, |
64 OnUnregisterServiceWorker) | 75 OnUnregisterServiceWorker) |
| 76 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderCreated, |
| 77 OnProviderCreated) |
| 78 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ProviderDestroyed, |
| 79 OnProviderDestroyed) |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 80 IPC_MESSAGE_UNHANDLED(handled = false) |
66 IPC_END_MESSAGE_MAP() | 81 IPC_END_MESSAGE_MAP() |
67 | 82 |
68 return handled; | 83 return handled; |
69 } | 84 } |
70 | 85 |
71 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( | 86 void ServiceWorkerDispatcherHost::OnRegisterServiceWorker( |
72 int32 thread_id, | 87 int32 thread_id, |
73 int32 request_id, | 88 int32 request_id, |
74 const GURL& scope, | 89 const GURL& scope, |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 thread_id, | 124 thread_id, |
110 request_id, | 125 request_id, |
111 blink::WebServiceWorkerError::DisabledError, | 126 blink::WebServiceWorkerError::DisabledError, |
112 ASCIIToUTF16(kDisabledErrorMessage))); | 127 ASCIIToUTF16(kDisabledErrorMessage))); |
113 return; | 128 return; |
114 } | 129 } |
115 | 130 |
116 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); | 131 Send(new ServiceWorkerMsg_ServiceWorkerUnregistered(thread_id, request_id)); |
117 } | 132 } |
118 | 133 |
| 134 void ServiceWorkerDispatcherHost::OnProviderCreated(int provider_id) { |
| 135 ServiceWorkerProviderHostMap::iterator found = providers_.find(provider_id); |
| 136 if (found != providers_.end()) { |
| 137 BadMessageReceived(); |
| 138 return; |
| 139 } |
| 140 providers_[provider_id] = new ServiceWorkerProviderHost(provider_id); |
| 141 } |
| 142 |
| 143 void ServiceWorkerDispatcherHost::OnProviderDestroyed(int provider_id) { |
| 144 ServiceWorkerProviderHostMap::iterator found = providers_.find(provider_id); |
| 145 if (found == providers_.end()) { |
| 146 BadMessageReceived(); |
| 147 return; |
| 148 } |
| 149 delete found->second; |
| 150 providers_.erase(found); |
| 151 } |
| 152 |
| 153 ServiceWorkerProviderHost* ServiceWorkerDispatcherHost::GetProviderHost( |
| 154 int provider_id) { |
| 155 ServiceWorkerProviderHostMap::iterator found = providers_.find(provider_id); |
| 156 if (found == providers_.end()) |
| 157 return NULL; |
| 158 return found->second; |
| 159 } |
| 160 |
119 } // namespace content | 161 } // namespace content |
OLD | NEW |