OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/shared_worker/shared_worker_service_impl.h" | 5 #include "content/browser/shared_worker/shared_worker_service_impl.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 #include <iterator> |
| 9 #include <set> |
| 10 #include <vector> |
| 11 |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
7 #include "content/browser/shared_worker/shared_worker_host.h" | 13 #include "content/browser/shared_worker/shared_worker_host.h" |
8 #include "content/browser/shared_worker/shared_worker_instance.h" | 14 #include "content/browser/shared_worker/shared_worker_instance.h" |
9 #include "content/browser/shared_worker/shared_worker_message_filter.h" | 15 #include "content/browser/shared_worker/shared_worker_message_filter.h" |
10 #include "content/browser/worker_host/worker_document_set.h" | 16 #include "content/browser/worker_host/worker_document_set.h" |
11 #include "content/common/view_messages.h" | 17 #include "content/common/view_messages.h" |
12 #include "content/common/worker_messages.h" | 18 #include "content/common/worker_messages.h" |
13 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/worker_service_observer.h" | 20 #include "content/public/browser/worker_service_observer.h" |
15 | 21 |
16 namespace content { | 22 namespace content { |
| 23 namespace { |
| 24 |
| 25 void UpdateWorkerDependencyOnUI(const std::vector<int> added_ids, |
| 26 const std::vector<int> removed_ids) { |
| 27 for (size_t i = 0; i < added_ids.size(); ++i) { |
| 28 RenderProcessHostImpl* render_process_host_impl = |
| 29 static_cast<RenderProcessHostImpl*>( |
| 30 RenderProcessHost::FromID(added_ids[i])); |
| 31 if (!render_process_host_impl) |
| 32 continue; |
| 33 render_process_host_impl->IncrementWorkerRefCount(); |
| 34 } |
| 35 for (size_t i = 0; i < removed_ids.size(); ++i) { |
| 36 RenderProcessHostImpl* render_process_host_impl = |
| 37 static_cast<RenderProcessHostImpl*>( |
| 38 RenderProcessHost::FromID(removed_ids[i])); |
| 39 if (!render_process_host_impl) |
| 40 continue; |
| 41 render_process_host_impl->DecrementWorkerRefCount(); |
| 42 } |
| 43 } |
| 44 |
| 45 } // namespace |
17 | 46 |
18 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { | 47 SharedWorkerServiceImpl* SharedWorkerServiceImpl::GetInstance() { |
19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
20 return Singleton<SharedWorkerServiceImpl>::get(); | 49 return Singleton<SharedWorkerServiceImpl>::get(); |
21 } | 50 } |
22 | 51 |
23 SharedWorkerServiceImpl::SharedWorkerServiceImpl() { | 52 SharedWorkerServiceImpl::SharedWorkerServiceImpl() { |
24 } | 53 } |
25 | 54 |
26 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() { | 55 SharedWorkerServiceImpl::~SharedWorkerServiceImpl() { |
(...skipping 21 matching lines...) Expand all Loading... |
48 } | 77 } |
49 | 78 |
50 void SharedWorkerServiceImpl::CreateWorker( | 79 void SharedWorkerServiceImpl::CreateWorker( |
51 const ViewHostMsg_CreateWorker_Params& params, | 80 const ViewHostMsg_CreateWorker_Params& params, |
52 int route_id, | 81 int route_id, |
53 SharedWorkerMessageFilter* filter, | 82 SharedWorkerMessageFilter* filter, |
54 ResourceContext* resource_context, | 83 ResourceContext* resource_context, |
55 const WorkerStoragePartition& partition, | 84 const WorkerStoragePartition& partition, |
56 bool* url_mismatch) { | 85 bool* url_mismatch) { |
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 87 ScopedWorkerDependencyChecker checker(this); |
58 *url_mismatch = false; | 88 *url_mismatch = false; |
59 SharedWorkerInstance* existing_instance = | 89 SharedWorkerInstance* existing_instance = |
60 FindSharedWorkerInstance( | 90 FindSharedWorkerInstance( |
61 params.url, params.name, partition, resource_context); | 91 params.url, params.name, partition, resource_context); |
62 if (existing_instance) { | 92 if (existing_instance) { |
63 if (params.url != existing_instance->url()) { | 93 if (params.url != existing_instance->url()) { |
64 *url_mismatch = true; | 94 *url_mismatch = true; |
65 return; | 95 return; |
66 } | 96 } |
67 if (existing_instance->load_failed()) { | 97 if (existing_instance->load_failed()) { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 iter != worker_hosts_.end(); | 140 iter != worker_hosts_.end(); |
111 ++iter) { | 141 ++iter) { |
112 if (iter->second->FilterMessage(message, filter)) | 142 if (iter->second->FilterMessage(message, filter)) |
113 return; | 143 return; |
114 } | 144 } |
115 } | 145 } |
116 | 146 |
117 void SharedWorkerServiceImpl::DocumentDetached( | 147 void SharedWorkerServiceImpl::DocumentDetached( |
118 unsigned long long document_id, | 148 unsigned long long document_id, |
119 SharedWorkerMessageFilter* filter) { | 149 SharedWorkerMessageFilter* filter) { |
| 150 ScopedWorkerDependencyChecker checker(this); |
120 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 151 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
121 iter != worker_hosts_.end(); | 152 iter != worker_hosts_.end(); |
122 ++iter) { | 153 ++iter) { |
123 iter->second->DocumentDetached(filter, document_id); | 154 iter->second->DocumentDetached(filter, document_id); |
124 } | 155 } |
125 } | 156 } |
126 | 157 |
127 void SharedWorkerServiceImpl::WorkerContextClosed( | 158 void SharedWorkerServiceImpl::WorkerContextClosed( |
128 int worker_route_id, | 159 int worker_route_id, |
129 SharedWorkerMessageFilter* filter) { | 160 SharedWorkerMessageFilter* filter) { |
| 161 ScopedWorkerDependencyChecker checker(this); |
130 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 162 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
131 host->WorkerContextClosed(); | 163 host->WorkerContextClosed(); |
132 } | 164 } |
133 | 165 |
134 void SharedWorkerServiceImpl::WorkerContextDestroyed( | 166 void SharedWorkerServiceImpl::WorkerContextDestroyed( |
135 int worker_route_id, | 167 int worker_route_id, |
136 SharedWorkerMessageFilter* filter) { | 168 SharedWorkerMessageFilter* filter) { |
| 169 ScopedWorkerDependencyChecker checker(this); |
137 scoped_ptr<SharedWorkerHost> host = | 170 scoped_ptr<SharedWorkerHost> host = |
138 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 171 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
139 worker_route_id)); | 172 worker_route_id)); |
140 if (!host) | 173 if (!host) |
141 return; | 174 return; |
142 host->WorkerContextDestroyed(); | 175 host->WorkerContextDestroyed(); |
143 } | 176 } |
144 | 177 |
145 void SharedWorkerServiceImpl::WorkerScriptLoaded( | 178 void SharedWorkerServiceImpl::WorkerScriptLoaded( |
146 int worker_route_id, | 179 int worker_route_id, |
147 SharedWorkerMessageFilter* filter) { | 180 SharedWorkerMessageFilter* filter) { |
148 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 181 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
149 host->WorkerScriptLoaded(); | 182 host->WorkerScriptLoaded(); |
150 } | 183 } |
151 | 184 |
152 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( | 185 void SharedWorkerServiceImpl::WorkerScriptLoadFailed( |
153 int worker_route_id, | 186 int worker_route_id, |
154 SharedWorkerMessageFilter* filter) { | 187 SharedWorkerMessageFilter* filter) { |
| 188 ScopedWorkerDependencyChecker checker(this); |
155 scoped_ptr<SharedWorkerHost> host = | 189 scoped_ptr<SharedWorkerHost> host = |
156 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), | 190 worker_hosts_.take_and_erase(std::make_pair(filter->render_process_id(), |
157 worker_route_id)); | 191 worker_route_id)); |
158 if (!host) | 192 if (!host) |
159 return; | 193 return; |
160 host->WorkerScriptLoadFailed(); | 194 host->WorkerScriptLoadFailed(); |
161 } | 195 } |
162 | 196 |
163 void SharedWorkerServiceImpl::WorkerConnected( | 197 void SharedWorkerServiceImpl::WorkerConnected( |
164 int message_port_id, | 198 int message_port_id, |
(...skipping 29 matching lines...) Expand all Loading... |
194 const GURL& url, | 228 const GURL& url, |
195 const base::string16& name, | 229 const base::string16& name, |
196 bool* result, | 230 bool* result, |
197 SharedWorkerMessageFilter* filter) { | 231 SharedWorkerMessageFilter* filter) { |
198 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) | 232 if (SharedWorkerHost* host = FindSharedWorkerHost(filter, worker_route_id)) |
199 host->AllowIndexedDB(url, name, result); | 233 host->AllowIndexedDB(url, name, result); |
200 } | 234 } |
201 | 235 |
202 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( | 236 void SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing( |
203 SharedWorkerMessageFilter* filter) { | 237 SharedWorkerMessageFilter* filter) { |
| 238 ScopedWorkerDependencyChecker checker(this); |
204 std::vector<ProcessRouteIdPair> remove_list; | 239 std::vector<ProcessRouteIdPair> remove_list; |
205 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); | 240 for (WorkerHostMap::iterator iter = worker_hosts_.begin(); |
206 iter != worker_hosts_.end(); | 241 iter != worker_hosts_.end(); |
207 ++iter) { | 242 ++iter) { |
208 iter->second->FilterShutdown(filter); | 243 iter->second->FilterShutdown(filter); |
209 if (iter->first.first == filter->render_process_id()) | 244 if (iter->first.first == filter->render_process_id()) |
210 remove_list.push_back(iter->first); | 245 remove_list.push_back(iter->first); |
211 } | 246 } |
212 for (size_t i = 0; i < remove_list.size(); ++i) | 247 for (size_t i = 0; i < remove_list.size(); ++i) |
213 worker_hosts_.erase(remove_list[i]); | 248 worker_hosts_.erase(remove_list[i]); |
(...skipping 14 matching lines...) Expand all Loading... |
228 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); | 263 for (WorkerHostMap::const_iterator iter = worker_hosts_.begin(); |
229 iter != worker_hosts_.end(); | 264 iter != worker_hosts_.end(); |
230 ++iter) { | 265 ++iter) { |
231 SharedWorkerInstance* instance = iter->second->instance(); | 266 SharedWorkerInstance* instance = iter->second->instance(); |
232 if (instance && instance->Matches(url, name, partition, resource_context)) | 267 if (instance && instance->Matches(url, name, partition, resource_context)) |
233 return instance; | 268 return instance; |
234 } | 269 } |
235 return NULL; | 270 return NULL; |
236 } | 271 } |
237 | 272 |
| 273 const std::set<int> SharedWorkerServiceImpl::GetWorkerDependedRenderers() { |
| 274 std::set<int> dependend_renderers; |
| 275 for (WorkerHostMap::iterator host_iter = worker_hosts_.begin(); |
| 276 host_iter != worker_hosts_.end(); |
| 277 ++host_iter) { |
| 278 const int process_id = host_iter->first.first; |
| 279 if (dependend_renderers.count(process_id)) |
| 280 continue; |
| 281 SharedWorkerInstance* instance = host_iter->second->instance(); |
| 282 if (instance && |
| 283 instance->worker_document_set()->ContainsExternalRenderer(process_id)) { |
| 284 dependend_renderers.insert(process_id); |
| 285 } |
| 286 } |
| 287 return dependend_renderers; |
| 288 } |
| 289 |
| 290 void SharedWorkerServiceImpl::CheckWorkerDependency() { |
| 291 const std::set<int> current_worker_depended_renderers = |
| 292 GetWorkerDependedRenderers(); |
| 293 std::vector<int> added_items; |
| 294 std::vector<int> removed_items; |
| 295 std::set_difference(current_worker_depended_renderers.begin(), |
| 296 current_worker_depended_renderers.end(), |
| 297 last_worker_depended_renderers_.begin(), |
| 298 last_worker_depended_renderers_.end(), |
| 299 std::back_inserter(added_items)); |
| 300 std::set_difference(last_worker_depended_renderers_.begin(), |
| 301 last_worker_depended_renderers_.end(), |
| 302 current_worker_depended_renderers.begin(), |
| 303 current_worker_depended_renderers.end(), |
| 304 std::back_inserter(removed_items)); |
| 305 if (!added_items.empty() || !removed_items.empty()) { |
| 306 last_worker_depended_renderers_ = current_worker_depended_renderers; |
| 307 BrowserThread::PostTask( |
| 308 BrowserThread::UI, |
| 309 FROM_HERE, |
| 310 base::Bind(&UpdateWorkerDependencyOnUI, added_items, removed_items)); |
| 311 } |
| 312 } |
| 313 |
238 } // namespace content | 314 } // namespace content |
OLD | NEW |