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 3// found in the LICENSE file. 4 5#include "content/public/test/mock_render_process_host.h" 6 7#include "base/lazy_instance.h" 8#include "base/message_loop/message_loop.h" 9#include "base/time/time.h" 10#include "content/browser/child_process_security_policy_impl.h" 11#include "content/browser/renderer_host/render_process_host_impl.h" 12#include "content/browser/renderer_host/render_view_host_impl.h" 13#include "content/browser/renderer_host/render_widget_host_impl.h" 14#include "content/common/child_process_host_impl.h" 15#include "content/public/browser/global_request_id.h" 16#include "content/public/browser/render_widget_host_iterator.h" 17#include "content/public/browser/storage_partition.h" 18 19namespace content { 20 21MockRenderProcessHost::MockRenderProcessHost(BrowserContext* browser_context) 22 : bad_msg_count_(0), 23 factory_(NULL), 24 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()), 25 browser_context_(browser_context), 26 prev_routing_id_(0), 27 fast_shutdown_started_(false), 28 deletion_callback_called_(false), 29 is_isolated_guest_(false) { 30 // Child process security operations can't be unit tested unless we add 31 // ourselves as an existing child process. 32 ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID()); 33 34 RenderProcessHostImpl::RegisterHost(GetID(), this); 35} 36 37MockRenderProcessHost::~MockRenderProcessHost() { 38 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID()); 39 if (factory_) 40 factory_->Remove(this); 41 42 // In unit tests, Cleanup() might not have been called. 43 if (!deletion_callback_called_) { 44 FOR_EACH_OBSERVER(RenderProcessHostObserver, 45 observers_, 46 RenderProcessHostDestroyed(this)); 47 RenderProcessHostImpl::UnregisterHost(GetID()); 48 } 49} 50 51void MockRenderProcessHost::EnableSendQueue() { 52} 53 54bool MockRenderProcessHost::Init() { 55 return true; 56} 57 58int MockRenderProcessHost::GetNextRoutingID() { 59 return ++prev_routing_id_; 60} 61 62void MockRenderProcessHost::AddRoute( 63 int32 routing_id, 64 IPC::Listener* listener) { 65 listeners_.AddWithID(listener, routing_id); 66} 67 68void MockRenderProcessHost::RemoveRoute(int32 routing_id) { 69 DCHECK(listeners_.Lookup(routing_id) != NULL); 70 listeners_.Remove(routing_id); 71 Cleanup(); 72} 73 74void MockRenderProcessHost::AddObserver(RenderProcessHostObserver* observer) { 75 observers_.AddObserver(observer); 76} 77 78void MockRenderProcessHost::RemoveObserver( 79 RenderProcessHostObserver* observer) { 80 observers_.RemoveObserver(observer); 81} 82 83void MockRenderProcessHost::ReceivedBadMessage() { 84 ++bad_msg_count_; 85} 86 87void MockRenderProcessHost::WidgetRestored() { 88} 89 90void MockRenderProcessHost::WidgetHidden() { 91} 92 93int MockRenderProcessHost::VisibleWidgetCount() const { 94 return 1; 95} 96 97bool MockRenderProcessHost::IsIsolatedGuest() const { 98 return is_isolated_guest_; 99} 100 101StoragePartition* MockRenderProcessHost::GetStoragePartition() const { 102 return NULL; 103} 104 105void MockRenderProcessHost::AddWord(const base::string16& word) { 106} 107 108bool MockRenderProcessHost::FastShutdownIfPossible() { 109 // We aren't actually going to do anything, but set |fast_shutdown_started_| 110 // to true so that tests know we've been called. 111 fast_shutdown_started_ = true; 112 return true; 113} 114 115bool MockRenderProcessHost::FastShutdownStarted() const { 116 return fast_shutdown_started_; 117} 118 119void MockRenderProcessHost::DumpHandles() { 120} 121 122base::ProcessHandle MockRenderProcessHost::GetHandle() const { 123 // Return the current-process handle for the IPC::GetFileHandleForProcess 124 // function. 125 if (process_handle) 126 return *process_handle; 127 return base::Process::Current().handle(); 128} 129 130bool MockRenderProcessHost::Send(IPC::Message* msg) { 131 // Save the message in the sink. 132 sink_.OnMessageReceived(*msg); 133 delete msg; 134 return true; 135} 136 137int MockRenderProcessHost::GetID() const { 138 return id_; 139} 140 141bool MockRenderProcessHost::HasConnection() const { 142 return true; 143} 144 145void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { 146} 147 148bool MockRenderProcessHost::IgnoreInputEvents() const { 149 return false; 150} 151 152void MockRenderProcessHost::Cleanup() { 153 if (listeners_.IsEmpty()) { 154 FOR_EACH_OBSERVER(RenderProcessHostObserver, 155 observers_, 156 RenderProcessHostDestroyed(this)); 157 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); 158 RenderProcessHostImpl::UnregisterHost(GetID()); 159 deletion_callback_called_ = true; 160 } 161} 162 163void MockRenderProcessHost::AddPendingView() { 164} 165 166void MockRenderProcessHost::RemovePendingView() { 167} 168 169void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { 170} 171 172bool MockRenderProcessHost::SuddenTerminationAllowed() const { 173 return true; 174} 175 176BrowserContext* MockRenderProcessHost::GetBrowserContext() const { 177 return browser_context_; 178} 179 180bool MockRenderProcessHost::InSameStoragePartition( 181 StoragePartition* partition) const { 182 // Mock RPHs only have one partition. 183 return true; 184} 185 186IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { 187 return NULL; 188} 189 190void MockRenderProcessHost::AddFilter(BrowserMessageFilter* filter) { 191} 192 193int MockRenderProcessHost::GetActiveViewCount() { 194 int num_active_views = 0; 195 scoped_ptr<RenderWidgetHostIterator> widgets( 196 RenderWidgetHost::GetRenderWidgetHosts()); 197 while (RenderWidgetHost* widget = widgets->GetNextHost()) { 198 // Count only RenderWidgetHosts in this process. 199 if (widget->GetProcess()->GetID() == GetID()) 200 num_active_views++; 201 } 202 return num_active_views; 203} 204 205bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { 206 if (static_cast<size_t>(GetActiveViewCount()) == count) 207 return FastShutdownIfPossible(); 208 return false; 209} 210 211base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { 212 return base::TimeDelta::FromMilliseconds(0); 213} 214 215void MockRenderProcessHost::ResumeRequestsForView(int route_id) { 216} 217 218void MockRenderProcessHost::NotifyTimezoneChange() { 219} 220 221ServiceRegistry* MockRenderProcessHost::GetServiceRegistry() { 222 return NULL; 223} 224 225void MockRenderProcessHost::FilterURL(bool empty_allowed, GURL* url) { 226 RenderProcessHostImpl::FilterURL(this, empty_allowed, url); 227} 228 229#if defined(ENABLE_WEBRTC) 230void MockRenderProcessHost::EnableAecDump(const base::FilePath& file) { 231} 232 233void MockRenderProcessHost::DisableAecDump() { 234} 235 236void MockRenderProcessHost::SetWebRtcLogMessageCallback( 237 base::Callback<void(const std::string&)> callback) { 238} 239 240RenderProcessHost::WebRtcStopRtpDumpCallback 241MockRenderProcessHost::StartRtpDump( 242 bool incoming, 243 bool outgoing, 244 const WebRtcRtpPacketCallback& packet_callback) { 245 return WebRtcStopRtpDumpCallback(); 246} 247#endif 248 249void MockRenderProcessHost::ResumeDeferredNavigation( 250 const GlobalRequestID& request_id) {} 251 252bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { 253 IPC::Listener* listener = listeners_.Lookup(msg.routing_id()); 254 if (listener) 255 return listener->OnMessageReceived(msg); 256 return false; 257} 258 259void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { 260} 261 262MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} 263 264MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { 265 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() 266 // from calling MockRenderProcessHostFactory::Remove(). 267 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); 268 it != processes_.end(); ++it) { 269 (*it)->SetFactory(NULL); 270 } 271} 272 273RenderProcessHost* MockRenderProcessHostFactory::CreateRenderProcessHost( 274 BrowserContext* browser_context, 275 SiteInstance* site_instance) const { 276 MockRenderProcessHost* host = new MockRenderProcessHost(browser_context); 277 if (host) { 278 processes_.push_back(host); 279 host->SetFactory(this); 280 } 281 return host; 282} 283 284void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const { 285 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); 286 it != processes_.end(); ++it) { 287 if (*it == host) { 288 processes_.weak_erase(it); 289 break; 290 } 291 } 292} 293 294} // content 295