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_provider_host.h" | 5 #include "content/browser/service_worker/service_worker_provider_host.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "content/browser/message_port_message_filter.h" | 8 #include "content/browser/message_port_message_filter.h" |
9 #include "content/browser/service_worker/service_worker_context_core.h" | 9 #include "content/browser/service_worker/service_worker_context_core.h" |
10 #include "content/browser/service_worker/service_worker_context_request_handler. h" | 10 #include "content/browser/service_worker/service_worker_context_request_handler. h" |
(...skipping 24 matching lines...) Expand all Loading... | |
35 if (waiting_version_) | 35 if (waiting_version_) |
36 waiting_version_->RemoveWaitingControllee(this); | 36 waiting_version_->RemoveWaitingControllee(this); |
37 } | 37 } |
38 | 38 |
39 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { | 39 void ServiceWorkerProviderHost::SetDocumentUrl(const GURL& url) { |
40 DCHECK(!url.has_ref()); | 40 DCHECK(!url.has_ref()); |
41 document_url_ = url; | 41 document_url_ = url; |
42 } | 42 } |
43 | 43 |
44 void ServiceWorkerProviderHost::SetActiveVersion( | 44 void ServiceWorkerProviderHost::SetActiveVersion( |
45 ServiceWorkerVersion* version) { | 45 ServiceWorkerVersion* version) { |
falken
2014/07/02 05:42:11
Do we want DCHECK(CanAssociateVersion) here too? O
michaeln
2014/07/02 23:17:14
Done.
| |
46 if (version == active_version_) | 46 if (version == active_version_) |
47 return; | 47 return; |
48 scoped_refptr<ServiceWorkerVersion> previous_version = active_version_; | 48 scoped_refptr<ServiceWorkerVersion> previous_version = active_version_; |
49 active_version_ = version; | 49 active_version_ = version; |
50 if (version) | 50 if (version) |
51 version->AddControllee(this); | 51 version->AddControllee(this); |
52 if (previous_version) | 52 if (previous_version) |
53 previous_version->RemoveControllee(this); | 53 previous_version->RemoveControllee(this); |
54 | 54 |
55 if (!dispatcher_host_) | 55 if (!dispatcher_host_) |
56 return; // Could be NULL in some tests. | 56 return; // Could be NULL in some tests. |
57 | 57 |
58 dispatcher_host_->Send(new ServiceWorkerMsg_SetCurrentServiceWorker( | 58 dispatcher_host_->Send(new ServiceWorkerMsg_SetCurrentServiceWorker( |
59 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | 59 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); |
60 } | 60 } |
61 | 61 |
62 void ServiceWorkerProviderHost::SetWaitingVersion( | 62 void ServiceWorkerProviderHost::SetWaitingVersion( |
63 ServiceWorkerVersion* version) { | 63 ServiceWorkerVersion* version) { |
64 DCHECK(ValidateVersionForAssociation(version)); | 64 DCHECK(CanAssociateVersion(version)); |
65 if (version == waiting_version_) | 65 if (version == waiting_version_) |
66 return; | 66 return; |
67 scoped_refptr<ServiceWorkerVersion> previous_version = waiting_version_; | 67 scoped_refptr<ServiceWorkerVersion> previous_version = waiting_version_; |
68 waiting_version_ = version; | 68 waiting_version_ = version; |
69 if (version) | 69 if (version) |
70 version->AddWaitingControllee(this); | 70 version->AddWaitingControllee(this); |
71 if (previous_version) | 71 if (previous_version) |
72 previous_version->RemoveWaitingControllee(this); | 72 previous_version->RemoveWaitingControllee(this); |
73 | 73 |
74 if (!dispatcher_host_) | 74 if (!dispatcher_host_) |
75 return; // Could be NULL in some tests. | 75 return; // Could be NULL in some tests. |
76 | 76 |
77 dispatcher_host_->Send(new ServiceWorkerMsg_SetWaitingServiceWorker( | 77 dispatcher_host_->Send(new ServiceWorkerMsg_SetWaitingServiceWorker( |
78 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | 78 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); |
79 } | 79 } |
80 | 80 |
81 void ServiceWorkerProviderHost::SetInstallingVersion( | |
82 ServiceWorkerVersion* version) { | |
83 DCHECK(CanAssociateVersion(version)); | |
84 if (version == installing_version_) | |
85 return; | |
86 scoped_refptr<ServiceWorkerVersion> previous_version = installing_version_; | |
87 installing_version_ = version; | |
88 if (version) | |
89 version->AddWaitingControllee(this); | |
90 if (previous_version) | |
91 previous_version->RemoveWaitingControllee(this); | |
nhiroki
2014/07/02 07:25:12
We are adding/removing 'this' as a waiting control
michaeln
2014/07/02 23:17:14
ooops, i've generalized this to add/rmv PotentialC
| |
92 | |
93 if (!dispatcher_host_) | |
94 return; // Could be NULL in some tests. | |
95 | |
96 dispatcher_host_->Send(new ServiceWorkerMsg_SetWaitingServiceWorker( | |
nhiroki
2014/07/02 07:25:11
nit: indent looks broken.
michaeln
2014/07/02 23:17:14
also added a comment about updating the msg once t
michaeln
2014/07/02 23:17:14
Done.
| |
97 kDocumentMainThreadId, provider_id(), CreateHandleAndPass(version))); | |
98 } | |
99 | |
100 void ServiceWorkerProviderHost::UnsetVersion(ServiceWorkerVersion* version) { | |
101 if (!version) | |
102 return; | |
103 if (installing_version_ == version) | |
104 SetInstallingVersion(NULL); | |
105 else if (waiting_version_ == version) | |
106 SetWaitingVersion(NULL); | |
107 else if (active_version_ == version) | |
108 SetActiveVersion(NULL); | |
109 } | |
110 | |
81 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { | 111 bool ServiceWorkerProviderHost::SetHostedVersionId(int64 version_id) { |
82 if (!context_) | 112 if (!context_) |
83 return true; // System is shutting down. | 113 return true; // System is shutting down. |
84 if (active_version_) | 114 if (active_version_) |
85 return false; // Unexpected bad message. | 115 return false; // Unexpected bad message. |
86 | 116 |
87 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); | 117 ServiceWorkerVersion* live_version = context_->GetLiveVersion(version_id); |
88 if (!live_version) | 118 if (!live_version) |
89 return true; // Was deleted before it got started. | 119 return true; // Was deleted before it got started. |
90 | 120 |
(...skipping 20 matching lines...) Expand all Loading... | |
111 } | 141 } |
112 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || | 142 if (ServiceWorkerUtils::IsMainResourceType(resource_type) || |
113 active_version()) { | 143 active_version()) { |
114 return scoped_ptr<ServiceWorkerRequestHandler>( | 144 return scoped_ptr<ServiceWorkerRequestHandler>( |
115 new ServiceWorkerControlleeRequestHandler( | 145 new ServiceWorkerControlleeRequestHandler( |
116 context_, AsWeakPtr(), blob_storage_context, resource_type)); | 146 context_, AsWeakPtr(), blob_storage_context, resource_type)); |
117 } | 147 } |
118 return scoped_ptr<ServiceWorkerRequestHandler>(); | 148 return scoped_ptr<ServiceWorkerRequestHandler>(); |
119 } | 149 } |
120 | 150 |
121 bool ServiceWorkerProviderHost::ValidateVersionForAssociation( | 151 bool ServiceWorkerProviderHost::CanAssociateVersion( |
122 ServiceWorkerVersion* version) { | 152 ServiceWorkerVersion* version) { |
153 if (!context_) | |
154 return false; | |
123 if (running_hosted_version_) | 155 if (running_hosted_version_) |
124 return false; | 156 return false; |
125 if (!version) | 157 if (!version) |
126 return true; | 158 return true; |
127 | 159 |
128 // A version to be associated with this provider should have the same | 160 ServiceWorkerVersion* already_associated_version = NULL; |
129 // registration (scope) as current active/waiting versions. | 161 if (active_version_) |
130 if (active_version_) { | 162 already_associated_version = active_version_; |
131 if (active_version_->registration_id() != version->registration_id()) | 163 else if (waiting_version_) |
132 return false; | 164 already_associated_version = waiting_version_; |
133 DCHECK_EQ(active_version_->scope(), version->scope()); | 165 else if (installing_version_) |
134 } | 166 already_associated_version = installing_version_; |
135 if (waiting_version_) { | 167 |
136 if (waiting_version_->registration_id() != version->registration_id()) | 168 return !already_associated_version || |
137 return false; | 169 already_associated_version->registration_id() == |
138 DCHECK_EQ(waiting_version_->scope(), version->scope()); | 170 version->registration_id(); |
139 } | |
140 return true; | |
141 } | 171 } |
142 | 172 |
143 void ServiceWorkerProviderHost::PostMessage( | 173 void ServiceWorkerProviderHost::PostMessage( |
144 const base::string16& message, | 174 const base::string16& message, |
145 const std::vector<int>& sent_message_port_ids) { | 175 const std::vector<int>& sent_message_port_ids) { |
146 if (!dispatcher_host_) | 176 if (!dispatcher_host_) |
147 return; // Could be NULL in some tests. | 177 return; // Could be NULL in some tests. |
148 | 178 |
149 std::vector<int> new_routing_ids; | 179 std::vector<int> new_routing_ids; |
150 dispatcher_host_->message_port_message_filter()-> | 180 dispatcher_host_->message_port_message_filter()-> |
151 UpdateMessagePortsWithNewRoutes(sent_message_port_ids, | 181 UpdateMessagePortsWithNewRoutes(sent_message_port_ids, |
152 &new_routing_ids); | 182 &new_routing_ids); |
153 | 183 |
154 dispatcher_host_->Send( | 184 dispatcher_host_->Send( |
155 new ServiceWorkerMsg_MessageToDocument( | 185 new ServiceWorkerMsg_MessageToDocument( |
156 kDocumentMainThreadId, provider_id(), | 186 kDocumentMainThreadId, provider_id(), |
157 message, | 187 message, |
158 sent_message_port_ids, | 188 sent_message_port_ids, |
159 new_routing_ids)); | 189 new_routing_ids)); |
160 } | 190 } |
161 | 191 |
162 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( | 192 ServiceWorkerObjectInfo ServiceWorkerProviderHost::CreateHandleAndPass( |
163 ServiceWorkerVersion* version) { | 193 ServiceWorkerVersion* version) { |
164 DCHECK(ValidateVersionForAssociation(version)); | |
165 ServiceWorkerObjectInfo info; | 194 ServiceWorkerObjectInfo info; |
166 if (context_ && version) { | 195 if (context_ && version) { |
167 scoped_ptr<ServiceWorkerHandle> handle = | 196 scoped_ptr<ServiceWorkerHandle> handle = |
168 ServiceWorkerHandle::Create(context_, dispatcher_host_, | 197 ServiceWorkerHandle::Create(context_, dispatcher_host_, |
169 kDocumentMainThreadId, version); | 198 kDocumentMainThreadId, version); |
170 info = handle->GetObjectInfo(); | 199 info = handle->GetObjectInfo(); |
171 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); | 200 dispatcher_host_->RegisterServiceWorkerHandle(handle.Pass()); |
172 } | 201 } |
173 return info; | 202 return info; |
174 } | 203 } |
175 | 204 |
176 bool ServiceWorkerProviderHost::IsContextAlive() { | 205 bool ServiceWorkerProviderHost::IsContextAlive() { |
177 return context_ != NULL; | 206 return context_ != NULL; |
178 } | 207 } |
179 | 208 |
180 } // namespace content | 209 } // namespace content |
OLD | NEW |