| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/public/test/mock_render_process_host.h" | 5 #include "content/public/test/mock_render_process_host.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "content/browser/child_process_security_policy_impl.h" | 10 #include "content/browser/child_process_security_policy_impl.h" |
| 11 #include "content/browser/renderer_host/render_process_host_impl.h" | 11 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 12 #include "content/browser/renderer_host/render_widget_host_impl.h" | 13 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 13 #include "content/common/child_process_host_impl.h" | 14 #include "content/common/child_process_host_impl.h" |
| 14 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/storage_partition.h" | 17 #include "content/public/browser/storage_partition.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 MockRenderProcessHost::MockRenderProcessHost( | 21 MockRenderProcessHost::MockRenderProcessHost( |
| 21 BrowserContext* browser_context) | 22 BrowserContext* browser_context) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 46 | 47 |
| 47 bool MockRenderProcessHost::Init() { | 48 bool MockRenderProcessHost::Init() { |
| 48 return true; | 49 return true; |
| 49 } | 50 } |
| 50 | 51 |
| 51 int MockRenderProcessHost::GetNextRoutingID() { | 52 int MockRenderProcessHost::GetNextRoutingID() { |
| 52 static int prev_routing_id = 0; | 53 static int prev_routing_id = 0; |
| 53 return ++prev_routing_id; | 54 return ++prev_routing_id; |
| 54 } | 55 } |
| 55 | 56 |
| 57 void MockRenderProcessHost::AddRoute( |
| 58 int32 routing_id, |
| 59 IPC::Listener* listener) { |
| 60 listeners_.AddWithID(listener, routing_id); |
| 61 } |
| 62 |
| 63 void MockRenderProcessHost::RemoveRoute(int32 routing_id) { |
| 64 DCHECK(listeners_.Lookup(routing_id) != NULL); |
| 65 listeners_.Remove(routing_id); |
| 66 Cleanup(); |
| 67 } |
| 68 |
| 56 bool MockRenderProcessHost::WaitForBackingStoreMsg( | 69 bool MockRenderProcessHost::WaitForBackingStoreMsg( |
| 57 int render_widget_id, | 70 int render_widget_id, |
| 58 const base::TimeDelta& max_delay, | 71 const base::TimeDelta& max_delay, |
| 59 IPC::Message* msg) { | 72 IPC::Message* msg) { |
| 60 return false; | 73 return false; |
| 61 } | 74 } |
| 62 | 75 |
| 63 void MockRenderProcessHost::ReceivedBadMessage() { | 76 void MockRenderProcessHost::ReceivedBadMessage() { |
| 64 ++bad_msg_count_; | 77 ++bad_msg_count_; |
| 65 } | 78 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 return true; | 159 return true; |
| 147 } | 160 } |
| 148 | 161 |
| 149 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { | 162 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { |
| 150 } | 163 } |
| 151 | 164 |
| 152 bool MockRenderProcessHost::IgnoreInputEvents() const { | 165 bool MockRenderProcessHost::IgnoreInputEvents() const { |
| 153 return false; | 166 return false; |
| 154 } | 167 } |
| 155 | 168 |
| 156 void MockRenderProcessHost::Attach(RenderWidgetHost* host, | |
| 157 int routing_id) { | |
| 158 render_widget_hosts_.AddWithID(host, routing_id); | |
| 159 } | |
| 160 | |
| 161 void MockRenderProcessHost::Release(int routing_id) { | |
| 162 render_widget_hosts_.Remove(routing_id); | |
| 163 Cleanup(); | |
| 164 } | |
| 165 | |
| 166 void MockRenderProcessHost::Cleanup() { | 169 void MockRenderProcessHost::Cleanup() { |
| 167 if (render_widget_hosts_.IsEmpty()) { | 170 if (listeners_.IsEmpty()) { |
| 168 NotificationService::current()->Notify( | 171 NotificationService::current()->Notify( |
| 169 NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 172 NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 170 Source<RenderProcessHost>(this), | 173 Source<RenderProcessHost>(this), |
| 171 NotificationService::NoDetails()); | 174 NotificationService::NoDetails()); |
| 172 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 175 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 173 RenderProcessHostImpl::UnregisterHost(GetID()); | 176 RenderProcessHostImpl::UnregisterHost(GetID()); |
| 174 } | 177 } |
| 175 } | 178 } |
| 176 | 179 |
| 177 void MockRenderProcessHost::AddPendingView() { | 180 void MockRenderProcessHost::AddPendingView() { |
| 178 } | 181 } |
| 179 | 182 |
| 180 void MockRenderProcessHost::RemovePendingView() { | 183 void MockRenderProcessHost::RemovePendingView() { |
| 181 } | 184 } |
| 182 | 185 |
| 183 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { | 186 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { |
| 184 } | 187 } |
| 185 | 188 |
| 186 bool MockRenderProcessHost::SuddenTerminationAllowed() const { | 189 bool MockRenderProcessHost::SuddenTerminationAllowed() const { |
| 187 return true; | 190 return true; |
| 188 } | 191 } |
| 189 | 192 |
| 190 RenderWidgetHost* MockRenderProcessHost::GetRenderWidgetHostByID( | |
| 191 int routing_id) { | |
| 192 return render_widget_hosts_.Lookup(routing_id); | |
| 193 } | |
| 194 | |
| 195 BrowserContext* MockRenderProcessHost::GetBrowserContext() const { | 193 BrowserContext* MockRenderProcessHost::GetBrowserContext() const { |
| 196 return browser_context_; | 194 return browser_context_; |
| 197 } | 195 } |
| 198 | 196 |
| 199 bool MockRenderProcessHost::InSameStoragePartition( | 197 bool MockRenderProcessHost::InSameStoragePartition( |
| 200 StoragePartition* partition) const { | 198 StoragePartition* partition) const { |
| 201 // Mock RPHs only have one partition. | 199 // Mock RPHs only have one partition. |
| 202 return true; | 200 return true; |
| 203 } | 201 } |
| 204 | 202 |
| 205 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { | 203 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { |
| 206 return NULL; | 204 return NULL; |
| 207 } | 205 } |
| 208 | 206 |
| 207 int MockRenderProcessHost::GetActiveViewCount() { |
| 208 int num_active_views = 0; |
| 209 RenderWidgetHost::List widgets = RenderWidgetHost::GetRenderWidgetHosts(); |
| 210 for (size_t i = 0; i < widgets.size(); ++i) { |
| 211 // Count only RenderWidgetHosts in this process. |
| 212 if (widgets[i]->GetProcess()->GetID() != GetID()) |
| 213 continue; |
| 214 |
| 215 // All RenderWidgetHosts are swapped in. |
| 216 if (!widgets[i]->IsRenderView()) { |
| 217 num_active_views++; |
| 218 continue; |
| 219 } |
| 220 |
| 221 // Don't count swapped out views. |
| 222 RenderViewHost* rvh = RenderViewHost::From(widgets[i]); |
| 223 if (!static_cast<RenderViewHostImpl*>(rvh)->is_swapped_out()) |
| 224 num_active_views++; |
| 225 } |
| 226 return num_active_views; |
| 227 } |
| 228 |
| 209 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { | 229 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { |
| 210 if (render_widget_hosts_.size() == count) | 230 if (static_cast<size_t>(GetActiveViewCount()) == count) |
| 211 return FastShutdownIfPossible(); | 231 return FastShutdownIfPossible(); |
| 212 return false; | 232 return false; |
| 213 } | 233 } |
| 214 | 234 |
| 215 base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { | 235 base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { |
| 216 return base::TimeDelta::FromMilliseconds(0); | 236 return base::TimeDelta::FromMilliseconds(0); |
| 217 } | 237 } |
| 218 | 238 |
| 219 void MockRenderProcessHost::SurfaceUpdated(int32 surface_id) { | 239 void MockRenderProcessHost::SurfaceUpdated(int32 surface_id) { |
| 220 } | 240 } |
| 221 | 241 |
| 222 void MockRenderProcessHost::ResumeRequestsForView(int route_id) { | 242 void MockRenderProcessHost::ResumeRequestsForView(int route_id) { |
| 223 } | 243 } |
| 224 | 244 |
| 225 RenderProcessHost::RenderWidgetHostsIterator | |
| 226 MockRenderProcessHost::GetRenderWidgetHostsIterator() { | |
| 227 return RenderWidgetHostsIterator(&render_widget_hosts_); | |
| 228 } | |
| 229 | 245 |
| 230 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { | 246 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { |
| 231 RenderWidgetHost* rwh = render_widget_hosts_.Lookup(msg.routing_id()); | 247 IPC::Listener* listener = listeners_.Lookup(msg.routing_id()); |
| 232 if (rwh) | 248 if (listener) |
| 233 return RenderWidgetHostImpl::From(rwh)->OnMessageReceived(msg); | 249 return listener->OnMessageReceived(msg); |
| 234 return false; | 250 return false; |
| 235 } | 251 } |
| 236 | 252 |
| 237 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { | 253 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { |
| 238 } | 254 } |
| 239 | 255 |
| 240 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} | 256 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} |
| 241 | 257 |
| 242 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { | 258 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { |
| 243 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() | 259 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() |
| (...skipping 19 matching lines...) Expand all Loading... |
| 263 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); | 279 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); |
| 264 it != processes_.end(); ++it) { | 280 it != processes_.end(); ++it) { |
| 265 if (*it == host) { | 281 if (*it == host) { |
| 266 processes_.weak_erase(it); | 282 processes_.weak_erase(it); |
| 267 break; | 283 break; |
| 268 } | 284 } |
| 269 } | 285 } |
| 270 } | 286 } |
| 271 | 287 |
| 272 } // content | 288 } // content |
| OLD | NEW |