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/service_worker/service_worker_process_manager.h" | 5 #include "content/browser/service_worker/service_worker_process_manager.h" |
6 | 6 |
7 #include "content/browser/renderer_host/render_process_host_impl.h" | 7 #include "content/browser/renderer_host/render_process_host_impl.h" |
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 static bool IncrementWorkerRefCountByPid( | |
16 int process_id) { | |
kinuko
2014/05/20 06:30:03
nit: line 15-16 would probably fit into single lin
Jeffrey Yasskin
2014/05/21 02:01:11
Done.
| |
17 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
18 if (!rph || rph->FastShutdownStarted()) | |
19 return false; | |
20 | |
21 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | |
22 return true; | |
23 } | |
24 | |
15 ServiceWorkerProcessManager::ServiceWorkerProcessManager( | 25 ServiceWorkerProcessManager::ServiceWorkerProcessManager( |
16 ServiceWorkerContextWrapper* context_wrapper) | 26 ServiceWorkerContextWrapper* context_wrapper) |
17 : context_wrapper_(context_wrapper), | 27 : context_wrapper_(context_wrapper), |
28 process_id_for_test_(-1), | |
18 weak_this_factory_(this), | 29 weak_this_factory_(this), |
19 weak_this_(weak_this_factory_.GetWeakPtr()) { | 30 weak_this_(weak_this_factory_.GetWeakPtr()) { |
20 } | 31 } |
21 | 32 |
22 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { | 33 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { |
23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 34 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
24 } | 35 } |
25 | 36 |
26 void ServiceWorkerProcessManager::AllocateWorkerProcess( | 37 void ServiceWorkerProcessManager::AllocateWorkerProcess( |
38 int embedded_worker_id, | |
27 const std::vector<int>& process_ids, | 39 const std::vector<int>& process_ids, |
28 const GURL& script_url, | 40 const GURL& script_url, |
29 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 41 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
30 callback) const { | 42 callback) { |
31 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 43 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
32 BrowserThread::PostTask( | 44 BrowserThread::PostTask( |
33 BrowserThread::UI, | 45 BrowserThread::UI, |
34 FROM_HERE, | 46 FROM_HERE, |
35 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, | 47 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, |
36 weak_this_, | 48 weak_this_, |
49 embedded_worker_id, | |
37 process_ids, | 50 process_ids, |
38 script_url, | 51 script_url, |
39 callback)); | 52 callback)); |
40 return; | 53 return; |
41 } | 54 } |
42 | 55 |
56 if (process_id_for_test_ != -1) { | |
57 // Let tests specify the returned process ID. Note: We may need to be able | |
58 // to specify the error code too. | |
59 BrowserThread::PostTask( | |
60 BrowserThread::IO, | |
61 FROM_HERE, | |
62 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); | |
63 return; | |
64 } | |
65 | |
66 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) | |
67 << embedded_worker_id << " already has a process allocated"; | |
68 | |
43 for (std::vector<int>::const_iterator it = process_ids.begin(); | 69 for (std::vector<int>::const_iterator it = process_ids.begin(); |
44 it != process_ids.end(); | 70 it != process_ids.end(); |
45 ++it) { | 71 ++it) { |
46 if (IncrementWorkerRefcountByPid(*it)) { | 72 if (IncrementWorkerRefCountByPid(*it)) { |
73 instance_info_.insert( | |
74 std::make_pair(embedded_worker_id, ProcessInfo(*it))); | |
47 BrowserThread::PostTask(BrowserThread::IO, | 75 BrowserThread::PostTask(BrowserThread::IO, |
48 FROM_HERE, | 76 FROM_HERE, |
49 base::Bind(callback, SERVICE_WORKER_OK, *it)); | 77 base::Bind(callback, SERVICE_WORKER_OK, *it)); |
50 return; | 78 return; |
51 } | 79 } |
52 } | 80 } |
53 | 81 |
54 if (!context_wrapper_->browser_context_) { | 82 if (!context_wrapper_->browser_context_) { |
55 // Shutdown has started. | 83 // Shutdown has started. |
56 BrowserThread::PostTask( | 84 BrowserThread::PostTask( |
(...skipping 11 matching lines...) Expand all Loading... | |
68 // EmbeddedWorkerRegistry::process_sender_map_. | 96 // EmbeddedWorkerRegistry::process_sender_map_. |
69 if (!rph->Init()) { | 97 if (!rph->Init()) { |
70 LOG(ERROR) << "Couldn't start a new process!"; | 98 LOG(ERROR) << "Couldn't start a new process!"; |
71 BrowserThread::PostTask( | 99 BrowserThread::PostTask( |
72 BrowserThread::IO, | 100 BrowserThread::IO, |
73 FROM_HERE, | 101 FROM_HERE, |
74 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 102 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); |
75 return; | 103 return; |
76 } | 104 } |
77 | 105 |
106 instance_info_.insert( | |
107 std::make_pair(embedded_worker_id, ProcessInfo(site_instance))); | |
108 | |
78 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | 109 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
79 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
80 BrowserThread::IO, | 111 BrowserThread::IO, |
81 FROM_HERE, | 112 FROM_HERE, |
82 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); | 113 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); |
83 } | 114 } |
84 | 115 |
85 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int process_id) { | 116 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int embedded_worker_id) { |
86 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 117 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
87 BrowserThread::PostTask( | 118 BrowserThread::PostTask( |
88 BrowserThread::UI, | 119 BrowserThread::UI, |
89 FROM_HERE, | 120 FROM_HERE, |
90 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, | 121 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, |
91 weak_this_, | 122 weak_this_, |
92 process_id)); | 123 embedded_worker_id)); |
93 return; | 124 return; |
94 } | 125 } |
95 if (!DecrementWorkerRefcountByPid(process_id)) { | 126 if (process_id_for_test_ != -1) { |
96 DCHECK(false) << "DecrementWorkerRef(" << process_id | 127 // Unittests don't increment or decrement the worker refcount of a |
97 << ") doesn't match a previous IncrementWorkerRef"; | 128 // RenderProcessHost. |
129 return; | |
98 } | 130 } |
131 std::map<int, ProcessInfo>::iterator info = | |
132 instance_info_.find(embedded_worker_id); | |
133 DCHECK(info != instance_info_.end()); | |
134 RenderProcessHost* rph = NULL; | |
135 if (info->second.site_instance) { | |
136 rph = info->second.site_instance->GetProcess(); | |
137 DCHECK_EQ(info->second.process_id, rph->GetID()) | |
138 << "A SiteInstance's process shouldn't get destroyed while we're " | |
139 "holding a reference to it. Was the reference actually held?"; | |
140 } else { | |
141 rph = RenderProcessHost::FromID(info->second.process_id); | |
142 DCHECK(rph) | |
143 << "Process " << info->second.process_id | |
144 << " was destroyed unexpectedly. Did we actually hold a reference?"; | |
145 } | |
146 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | |
147 instance_info_.erase(info); | |
99 } | 148 } |
100 | 149 |
101 void ServiceWorkerProcessManager::SetProcessRefcountOpsForTest( | 150 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( |
kinuko
2014/05/20 06:30:03
nit: can these ProcessInfo definitions be placed b
Jeffrey Yasskin
2014/05/21 02:01:11
Done.
| |
102 const base::Callback<bool(int)>& increment_for_test, | 151 const scoped_refptr<SiteInstance>& site_instance) |
103 const base::Callback<bool(int)>& decrement_for_test) { | 152 : site_instance(site_instance), |
104 increment_for_test_ = increment_for_test; | 153 process_id(site_instance->GetProcess()->GetID()) { |
105 decrement_for_test_ = decrement_for_test; | |
106 } | 154 } |
107 | 155 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo(int process_id) |
108 bool ServiceWorkerProcessManager::IncrementWorkerRefcountByPid( | 156 : process_id(process_id) { |
109 int process_id) const { | |
110 if (!increment_for_test_.is_null()) | |
111 return increment_for_test_.Run(process_id); | |
112 | |
113 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
114 if (rph && !rph->FastShutdownStarted()) { | |
115 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | |
116 return true; | |
117 } | |
118 | |
119 return false; | |
120 } | 157 } |
121 | 158 ServiceWorkerProcessManager::ProcessInfo::~ProcessInfo() { |
122 bool ServiceWorkerProcessManager::DecrementWorkerRefcountByPid( | |
123 int process_id) const { | |
124 if (!decrement_for_test_.is_null()) | |
125 return decrement_for_test_.Run(process_id); | |
126 | |
127 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
128 if (rph) | |
129 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | |
130 | |
131 return rph != NULL; | |
132 } | 159 } |
133 | 160 |
134 } // namespace content | 161 } // namespace content |
135 | 162 |
136 namespace base { | 163 namespace base { |
137 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the | 164 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the |
138 // member WeakPtr to safely guard the object's lifetime when used on that | 165 // member WeakPtr to safely guard the object's lifetime when used on that |
139 // thread. | 166 // thread. |
140 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( | 167 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( |
141 content::ServiceWorkerProcessManager* ptr) const { | 168 content::ServiceWorkerProcessManager* ptr) const { |
142 content::BrowserThread::DeleteSoon( | 169 content::BrowserThread::DeleteSoon( |
143 content::BrowserThread::UI, FROM_HERE, ptr); | 170 content::BrowserThread::UI, FROM_HERE, ptr); |
144 } | 171 } |
145 } // namespace base | 172 } // namespace base |
OLD | NEW |