• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 namespace content {
20 
MockRenderProcessHost(BrowserContext * browser_context)21 MockRenderProcessHost::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 
~MockRenderProcessHost()37 MockRenderProcessHost::~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 
EnableSendQueue()51 void MockRenderProcessHost::EnableSendQueue() {
52 }
53 
Init()54 bool MockRenderProcessHost::Init() {
55   return true;
56 }
57 
GetNextRoutingID()58 int MockRenderProcessHost::GetNextRoutingID() {
59   return ++prev_routing_id_;
60 }
61 
AddRoute(int32 routing_id,IPC::Listener * listener)62 void MockRenderProcessHost::AddRoute(
63     int32 routing_id,
64     IPC::Listener* listener) {
65   listeners_.AddWithID(listener, routing_id);
66 }
67 
RemoveRoute(int32 routing_id)68 void MockRenderProcessHost::RemoveRoute(int32 routing_id) {
69   DCHECK(listeners_.Lookup(routing_id) != NULL);
70   listeners_.Remove(routing_id);
71   Cleanup();
72 }
73 
AddObserver(RenderProcessHostObserver * observer)74 void MockRenderProcessHost::AddObserver(RenderProcessHostObserver* observer) {
75   observers_.AddObserver(observer);
76 }
77 
RemoveObserver(RenderProcessHostObserver * observer)78 void MockRenderProcessHost::RemoveObserver(
79     RenderProcessHostObserver* observer) {
80   observers_.RemoveObserver(observer);
81 }
82 
ReceivedBadMessage()83 void MockRenderProcessHost::ReceivedBadMessage() {
84   ++bad_msg_count_;
85 }
86 
WidgetRestored()87 void MockRenderProcessHost::WidgetRestored() {
88 }
89 
WidgetHidden()90 void MockRenderProcessHost::WidgetHidden() {
91 }
92 
VisibleWidgetCount() const93 int MockRenderProcessHost::VisibleWidgetCount() const {
94   return 1;
95 }
96 
IsIsolatedGuest() const97 bool MockRenderProcessHost::IsIsolatedGuest() const {
98   return is_isolated_guest_;
99 }
100 
GetStoragePartition() const101 StoragePartition* MockRenderProcessHost::GetStoragePartition() const {
102   return NULL;
103 }
104 
AddWord(const base::string16 & word)105 void MockRenderProcessHost::AddWord(const base::string16& word) {
106 }
107 
FastShutdownIfPossible()108 bool 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 
FastShutdownStarted() const115 bool MockRenderProcessHost::FastShutdownStarted() const {
116   return fast_shutdown_started_;
117 }
118 
DumpHandles()119 void MockRenderProcessHost::DumpHandles() {
120 }
121 
GetHandle() const122 base::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 
Send(IPC::Message * msg)130 bool MockRenderProcessHost::Send(IPC::Message* msg) {
131   // Save the message in the sink.
132   sink_.OnMessageReceived(*msg);
133   delete msg;
134   return true;
135 }
136 
GetID() const137 int MockRenderProcessHost::GetID() const {
138   return id_;
139 }
140 
HasConnection() const141 bool MockRenderProcessHost::HasConnection() const {
142   return true;
143 }
144 
SetIgnoreInputEvents(bool ignore_input_events)145 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) {
146 }
147 
IgnoreInputEvents() const148 bool MockRenderProcessHost::IgnoreInputEvents() const {
149   return false;
150 }
151 
Cleanup()152 void 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 
AddPendingView()163 void MockRenderProcessHost::AddPendingView() {
164 }
165 
RemovePendingView()166 void MockRenderProcessHost::RemovePendingView() {
167 }
168 
SetSuddenTerminationAllowed(bool allowed)169 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) {
170 }
171 
SuddenTerminationAllowed() const172 bool MockRenderProcessHost::SuddenTerminationAllowed() const {
173   return true;
174 }
175 
GetBrowserContext() const176 BrowserContext* MockRenderProcessHost::GetBrowserContext() const {
177   return browser_context_;
178 }
179 
InSameStoragePartition(StoragePartition * partition) const180 bool MockRenderProcessHost::InSameStoragePartition(
181     StoragePartition* partition) const {
182   // Mock RPHs only have one partition.
183   return true;
184 }
185 
GetChannel()186 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() {
187   return NULL;
188 }
189 
AddFilter(BrowserMessageFilter * filter)190 void MockRenderProcessHost::AddFilter(BrowserMessageFilter* filter) {
191 }
192 
GetActiveViewCount()193 int 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 
FastShutdownForPageCount(size_t count)205 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) {
206   if (static_cast<size_t>(GetActiveViewCount()) == count)
207     return FastShutdownIfPossible();
208   return false;
209 }
210 
GetChildProcessIdleTime() const211 base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const {
212   return base::TimeDelta::FromMilliseconds(0);
213 }
214 
ResumeRequestsForView(int route_id)215 void MockRenderProcessHost::ResumeRequestsForView(int route_id) {
216 }
217 
NotifyTimezoneChange()218 void MockRenderProcessHost::NotifyTimezoneChange() {
219 }
220 
GetServiceRegistry()221 ServiceRegistry* MockRenderProcessHost::GetServiceRegistry() {
222   return NULL;
223 }
224 
FilterURL(bool empty_allowed,GURL * url)225 void MockRenderProcessHost::FilterURL(bool empty_allowed, GURL* url) {
226   RenderProcessHostImpl::FilterURL(this, empty_allowed, url);
227 }
228 
229 #if defined(ENABLE_WEBRTC)
EnableAecDump(const base::FilePath & file)230 void MockRenderProcessHost::EnableAecDump(const base::FilePath& file) {
231 }
232 
DisableAecDump()233 void MockRenderProcessHost::DisableAecDump() {
234 }
235 
SetWebRtcLogMessageCallback(base::Callback<void (const std::string &)> callback)236 void MockRenderProcessHost::SetWebRtcLogMessageCallback(
237     base::Callback<void(const std::string&)> callback) {
238 }
239 
240 RenderProcessHost::WebRtcStopRtpDumpCallback
StartRtpDump(bool incoming,bool outgoing,const WebRtcRtpPacketCallback & packet_callback)241 MockRenderProcessHost::StartRtpDump(
242     bool incoming,
243     bool outgoing,
244     const WebRtcRtpPacketCallback& packet_callback) {
245   return WebRtcStopRtpDumpCallback();
246 }
247 #endif
248 
ResumeDeferredNavigation(const GlobalRequestID & request_id)249 void MockRenderProcessHost::ResumeDeferredNavigation(
250     const GlobalRequestID& request_id) {}
251 
OnMessageReceived(const IPC::Message & msg)252 bool 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 
OnChannelConnected(int32 peer_pid)259 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) {
260 }
261 
MockRenderProcessHostFactory()262 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {}
263 
~MockRenderProcessHostFactory()264 MockRenderProcessHostFactory::~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 
CreateRenderProcessHost(BrowserContext * browser_context,SiteInstance * site_instance) const273 RenderProcessHost* 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 
Remove(MockRenderProcessHost * host) const284 void 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