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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <iterator> | 10 #include <iterator> |
(...skipping 13 matching lines...) Expand all Loading... |
24 #include "content/common/worker_messages.h" | 24 #include "content/common/worker_messages.h" |
25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/worker_service_observer.h" | 26 #include "content/public/browser/worker_service_observer.h" |
27 | 27 |
28 namespace content { | 28 namespace content { |
29 | 29 |
30 WorkerService* WorkerService::GetInstance() { | 30 WorkerService* WorkerService::GetInstance() { |
31 return SharedWorkerServiceImpl::GetInstance(); | 31 return SharedWorkerServiceImpl::GetInstance(); |
32 } | 32 } |
33 | 33 |
| 34 bool IsHostAlive(RenderProcessHost* host) { |
| 35 return host && !host->FastShutdownStarted() && |
| 36 !host->IsWorkerRefCountDisabled(); |
| 37 } |
| 38 |
34 namespace { | 39 namespace { |
35 | 40 |
36 class ScopedWorkerDependencyChecker { | 41 class ScopedWorkerDependencyChecker { |
37 public: | 42 public: |
38 explicit ScopedWorkerDependencyChecker(SharedWorkerServiceImpl* service) | 43 explicit ScopedWorkerDependencyChecker(SharedWorkerServiceImpl* service) |
39 : service_(service) {} | 44 : service_(service) {} |
40 ScopedWorkerDependencyChecker(SharedWorkerServiceImpl* service, | 45 ScopedWorkerDependencyChecker(SharedWorkerServiceImpl* service, |
41 base::Closure done_closure) | 46 base::Closure done_closure) |
42 : service_(service), done_closure_(done_closure) {} | 47 : service_(service), done_closure_(done_closure) {} |
43 ~ScopedWorkerDependencyChecker() { | 48 ~ScopedWorkerDependencyChecker() { |
44 service_->CheckWorkerDependency(); | 49 service_->CheckWorkerDependency(); |
45 if (!done_closure_.is_null()) | 50 if (!done_closure_.is_null()) |
46 done_closure_.Run(); | 51 done_closure_.Run(); |
47 } | 52 } |
48 | 53 |
49 private: | 54 private: |
50 SharedWorkerServiceImpl* service_; | 55 SharedWorkerServiceImpl* service_; |
51 base::Closure done_closure_; | 56 base::Closure done_closure_; |
52 DISALLOW_COPY_AND_ASSIGN(ScopedWorkerDependencyChecker); | 57 DISALLOW_COPY_AND_ASSIGN(ScopedWorkerDependencyChecker); |
53 }; | 58 }; |
54 | 59 |
55 void UpdateWorkerDependencyOnUI(const std::vector<int>& added_ids, | 60 void UpdateWorkerDependencyOnUI(const std::vector<int>& added_ids, |
56 const std::vector<int>& removed_ids) { | 61 const std::vector<int>& removed_ids) { |
57 for (int id : added_ids) { | 62 for (int id : added_ids) { |
58 RenderProcessHostImpl* render_process_host_impl = | 63 RenderProcessHostImpl* render_process_host_impl = |
59 static_cast<RenderProcessHostImpl*>(RenderProcessHost::FromID(id)); | 64 static_cast<RenderProcessHostImpl*>(RenderProcessHost::FromID(id)); |
60 if (!render_process_host_impl) | 65 if (!IsHostAlive(render_process_host_impl)) |
61 continue; | 66 continue; |
62 render_process_host_impl->IncrementSharedWorkerRefCount(); | 67 render_process_host_impl->IncrementSharedWorkerRefCount(); |
63 } | 68 } |
64 for (int id : removed_ids) { | 69 for (int id : removed_ids) { |
65 RenderProcessHostImpl* render_process_host_impl = | 70 RenderProcessHostImpl* render_process_host_impl = |
66 static_cast<RenderProcessHostImpl*>(RenderProcessHost::FromID(id)); | 71 static_cast<RenderProcessHostImpl*>(RenderProcessHost::FromID(id)); |
67 if (!render_process_host_impl) | 72 if (!IsHostAlive(render_process_host_impl)) |
68 continue; | 73 continue; |
69 render_process_host_impl->DecrementSharedWorkerRefCount(); | 74 render_process_host_impl->DecrementSharedWorkerRefCount(); |
70 } | 75 } |
71 } | 76 } |
72 | 77 |
73 void UpdateWorkerDependency(const std::vector<int>& added_ids, | 78 void UpdateWorkerDependency(const std::vector<int>& added_ids, |
74 const std::vector<int>& removed_ids) { | 79 const std::vector<int>& removed_ids) { |
75 BrowserThread::PostTask( | 80 BrowserThread::PostTask( |
76 BrowserThread::UI, | 81 BrowserThread::UI, |
77 FROM_HERE, | 82 FROM_HERE, |
78 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); | 83 base::Bind(&UpdateWorkerDependencyOnUI, added_ids, removed_ids)); |
79 } | 84 } |
80 | 85 |
81 void DecrementWorkerRefCount(int process_id) { | 86 void DecrementWorkerRefCount(int process_id) { |
82 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 87 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
83 BrowserThread::PostTask(BrowserThread::UI, | 88 BrowserThread::PostTask(BrowserThread::UI, |
84 FROM_HERE, | 89 FROM_HERE, |
85 base::Bind(&DecrementWorkerRefCount, process_id)); | 90 base::Bind(&DecrementWorkerRefCount, process_id)); |
86 return; | 91 return; |
87 } | 92 } |
88 RenderProcessHostImpl* render_process_host_impl = | 93 RenderProcessHostImpl* render_process_host_impl = |
89 static_cast<RenderProcessHostImpl*>( | 94 static_cast<RenderProcessHostImpl*>( |
90 RenderProcessHost::FromID(process_id)); | 95 RenderProcessHost::FromID(process_id)); |
91 if (render_process_host_impl) | 96 if (IsHostAlive(render_process_host_impl)) |
92 render_process_host_impl->DecrementSharedWorkerRefCount(); | 97 render_process_host_impl->DecrementSharedWorkerRefCount(); |
93 } | 98 } |
94 | 99 |
95 bool TryIncrementWorkerRefCount(int worker_process_id) { | 100 bool TryIncrementWorkerRefCount(int worker_process_id) { |
96 RenderProcessHostImpl* render_process = static_cast<RenderProcessHostImpl*>( | 101 RenderProcessHostImpl* render_process = static_cast<RenderProcessHostImpl*>( |
97 RenderProcessHost::FromID(worker_process_id)); | 102 RenderProcessHost::FromID(worker_process_id)); |
98 if (!render_process || render_process->FastShutdownStarted()) | 103 if (!IsHostAlive(render_process)) |
99 return false; | 104 return false; |
100 render_process->IncrementSharedWorkerRefCount(); | 105 render_process->IncrementSharedWorkerRefCount(); |
101 return true; | 106 return true; |
102 } | 107 } |
103 | 108 |
104 } // namespace | 109 } // namespace |
105 | 110 |
106 class SharedWorkerServiceImpl::SharedWorkerPendingInstance { | 111 class SharedWorkerServiceImpl::SharedWorkerPendingInstance { |
107 public: | 112 public: |
108 struct SharedWorkerPendingRequest { | 113 struct SharedWorkerPendingRequest { |
(...skipping 574 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 UpdateWorkerDependencyFunc new_func) { | 688 UpdateWorkerDependencyFunc new_func) { |
684 update_worker_dependency_ = new_func; | 689 update_worker_dependency_ = new_func; |
685 } | 690 } |
686 | 691 |
687 void SharedWorkerServiceImpl::ChangeTryIncrementWorkerRefCountFuncForTesting( | 692 void SharedWorkerServiceImpl::ChangeTryIncrementWorkerRefCountFuncForTesting( |
688 bool (*new_func)(int)) { | 693 bool (*new_func)(int)) { |
689 s_try_increment_worker_ref_count_ = new_func; | 694 s_try_increment_worker_ref_count_ = new_func; |
690 } | 695 } |
691 | 696 |
692 } // namespace content | 697 } // namespace content |
OLD | NEW |