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.
5 #include "content/public/test/mock_render_process_host.h"
7 #include "base/lazy_instance.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/process/process_handle.h"
10 #include "base/time/time.h"
11 #include "content/browser/child_process_security_policy_impl.h"
12 #include "content/browser/renderer_host/render_process_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/renderer_host/render_widget_host_impl.h"
15 #include "content/common/child_process_host_impl.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/global_request_id.h"
18 #include "content/public/browser/render_widget_host_iterator.h"
19 #include "content/public/browser/storage_partition.h"
23 MockRenderProcessHost::MockRenderProcessHost(BrowserContext
* browser_context
)
26 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
27 browser_context_(browser_context
),
29 fast_shutdown_started_(false),
30 deletion_callback_called_(false),
31 is_isolated_guest_(false) {
32 // Child process security operations can't be unit tested unless we add
33 // ourselves as an existing child process.
34 ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID());
36 RenderProcessHostImpl::RegisterHost(GetID(), this);
39 MockRenderProcessHost::~MockRenderProcessHost() {
40 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
42 factory_
->Remove(this);
44 // In unit tests, Cleanup() might not have been called.
45 if (!deletion_callback_called_
) {
46 FOR_EACH_OBSERVER(RenderProcessHostObserver
,
48 RenderProcessHostDestroyed(this));
49 RenderProcessHostImpl::UnregisterHost(GetID());
53 void MockRenderProcessHost::EnableSendQueue() {
56 bool MockRenderProcessHost::Init() {
60 int MockRenderProcessHost::GetNextRoutingID() {
61 return ++prev_routing_id_
;
64 void MockRenderProcessHost::AddRoute(
66 IPC::Listener
* listener
) {
67 listeners_
.AddWithID(listener
, routing_id
);
70 void MockRenderProcessHost::RemoveRoute(int32 routing_id
) {
71 DCHECK(listeners_
.Lookup(routing_id
) != NULL
);
72 listeners_
.Remove(routing_id
);
76 void MockRenderProcessHost::AddObserver(RenderProcessHostObserver
* observer
) {
77 observers_
.AddObserver(observer
);
80 void MockRenderProcessHost::RemoveObserver(
81 RenderProcessHostObserver
* observer
) {
82 observers_
.RemoveObserver(observer
);
85 void MockRenderProcessHost::ReceivedBadMessage() {
89 void MockRenderProcessHost::WidgetRestored() {
92 void MockRenderProcessHost::WidgetHidden() {
95 int MockRenderProcessHost::VisibleWidgetCount() const {
99 bool MockRenderProcessHost::IsIsolatedGuest() const {
100 return is_isolated_guest_
;
103 StoragePartition
* MockRenderProcessHost::GetStoragePartition() const {
104 return BrowserContext::GetDefaultStoragePartition(browser_context_
);
107 void MockRenderProcessHost::AddWord(const base::string16
& word
) {
110 bool MockRenderProcessHost::FastShutdownIfPossible() {
111 // We aren't actually going to do anything, but set |fast_shutdown_started_|
112 // to true so that tests know we've been called.
113 fast_shutdown_started_
= true;
117 bool MockRenderProcessHost::FastShutdownStarted() const {
118 return fast_shutdown_started_
;
121 void MockRenderProcessHost::DumpHandles() {
124 base::ProcessHandle
MockRenderProcessHost::GetHandle() const {
125 // Return the current-process handle for the IPC::GetFileHandleForProcess
128 return *process_handle
;
129 return base::GetCurrentProcessHandle();
132 bool MockRenderProcessHost::Send(IPC::Message
* msg
) {
133 // Save the message in the sink.
134 sink_
.OnMessageReceived(*msg
);
139 int MockRenderProcessHost::GetID() const {
143 bool MockRenderProcessHost::HasConnection() const {
147 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events
) {
150 bool MockRenderProcessHost::IgnoreInputEvents() const {
154 void MockRenderProcessHost::Cleanup() {
155 if (listeners_
.IsEmpty()) {
156 FOR_EACH_OBSERVER(RenderProcessHostObserver
,
158 RenderProcessHostDestroyed(this));
159 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
160 RenderProcessHostImpl::UnregisterHost(GetID());
161 deletion_callback_called_
= true;
165 void MockRenderProcessHost::AddPendingView() {
168 void MockRenderProcessHost::RemovePendingView() {
171 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed
) {
174 bool MockRenderProcessHost::SuddenTerminationAllowed() const {
178 BrowserContext
* MockRenderProcessHost::GetBrowserContext() const {
179 return browser_context_
;
182 bool MockRenderProcessHost::InSameStoragePartition(
183 StoragePartition
* partition
) const {
184 // Mock RPHs only have one partition.
188 IPC::ChannelProxy
* MockRenderProcessHost::GetChannel() {
192 void MockRenderProcessHost::AddFilter(BrowserMessageFilter
* filter
) {
195 int MockRenderProcessHost::GetActiveViewCount() {
196 int num_active_views
= 0;
197 scoped_ptr
<RenderWidgetHostIterator
> widgets(
198 RenderWidgetHost::GetRenderWidgetHosts());
199 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
200 // Count only RenderWidgetHosts in this process.
201 if (widget
->GetProcess()->GetID() == GetID())
204 return num_active_views
;
207 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count
) {
208 if (static_cast<size_t>(GetActiveViewCount()) == count
)
209 return FastShutdownIfPossible();
213 base::TimeDelta
MockRenderProcessHost::GetChildProcessIdleTime() const {
214 return base::TimeDelta::FromMilliseconds(0);
217 void MockRenderProcessHost::ResumeRequestsForView(int route_id
) {
220 void MockRenderProcessHost::NotifyTimezoneChange() {
223 ServiceRegistry
* MockRenderProcessHost::GetServiceRegistry() {
227 const base::TimeTicks
& MockRenderProcessHost::GetInitTimeForNavigationMetrics()
229 static base::TimeTicks dummy_time
= base::TimeTicks::Now();
233 void MockRenderProcessHost::FilterURL(bool empty_allowed
, GURL
* url
) {
234 RenderProcessHostImpl::FilterURL(this, empty_allowed
, url
);
237 #if defined(ENABLE_WEBRTC)
238 void MockRenderProcessHost::EnableAecDump(const base::FilePath
& file
) {
241 void MockRenderProcessHost::DisableAecDump() {
244 void MockRenderProcessHost::SetWebRtcLogMessageCallback(
245 base::Callback
<void(const std::string
&)> callback
) {
248 RenderProcessHost::WebRtcStopRtpDumpCallback
249 MockRenderProcessHost::StartRtpDump(
252 const WebRtcRtpPacketCallback
& packet_callback
) {
253 return WebRtcStopRtpDumpCallback();
257 void MockRenderProcessHost::ResumeDeferredNavigation(
258 const GlobalRequestID
& request_id
) {}
260 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message
& msg
) {
261 IPC::Listener
* listener
= listeners_
.Lookup(msg
.routing_id());
263 return listener
->OnMessageReceived(msg
);
267 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid
) {
270 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {}
272 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() {
273 // Detach this object from MockRenderProcesses to prevent STLDeleteElements()
274 // from calling MockRenderProcessHostFactory::Remove().
275 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
276 it
!= processes_
.end(); ++it
) {
277 (*it
)->SetFactory(NULL
);
281 RenderProcessHost
* MockRenderProcessHostFactory::CreateRenderProcessHost(
282 BrowserContext
* browser_context
,
283 SiteInstance
* site_instance
) const {
284 MockRenderProcessHost
* host
= new MockRenderProcessHost(browser_context
);
286 processes_
.push_back(host
);
287 host
->SetFactory(this);
292 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost
* host
) const {
293 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
294 it
!= processes_
.end(); ++it
) {
296 processes_
.weak_erase(it
);