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::ShutdownForBadMessage() {
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::Shutdown(int exit_code
, bool wait
) {
114 bool MockRenderProcessHost::FastShutdownIfPossible() {
115 // We aren't actually going to do anything, but set |fast_shutdown_started_|
116 // to true so that tests know we've been called.
117 fast_shutdown_started_
= true;
121 bool MockRenderProcessHost::FastShutdownStarted() const {
122 return fast_shutdown_started_
;
125 void MockRenderProcessHost::DumpHandles() {
128 base::ProcessHandle
MockRenderProcessHost::GetHandle() const {
129 // Return the current-process handle for the IPC::GetFileHandleForProcess
132 return *process_handle
;
133 return base::GetCurrentProcessHandle();
136 bool MockRenderProcessHost::Send(IPC::Message
* msg
) {
137 // Save the message in the sink.
138 sink_
.OnMessageReceived(*msg
);
143 int MockRenderProcessHost::GetID() const {
147 bool MockRenderProcessHost::HasConnection() const {
151 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events
) {
154 bool MockRenderProcessHost::IgnoreInputEvents() const {
158 void MockRenderProcessHost::Cleanup() {
159 if (listeners_
.IsEmpty()) {
160 FOR_EACH_OBSERVER(RenderProcessHostObserver
,
162 RenderProcessHostDestroyed(this));
163 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
164 RenderProcessHostImpl::UnregisterHost(GetID());
165 deletion_callback_called_
= true;
169 void MockRenderProcessHost::AddPendingView() {
172 void MockRenderProcessHost::RemovePendingView() {
175 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed
) {
178 bool MockRenderProcessHost::SuddenTerminationAllowed() const {
182 BrowserContext
* MockRenderProcessHost::GetBrowserContext() const {
183 return browser_context_
;
186 bool MockRenderProcessHost::InSameStoragePartition(
187 StoragePartition
* partition
) const {
188 // Mock RPHs only have one partition.
192 IPC::ChannelProxy
* MockRenderProcessHost::GetChannel() {
196 void MockRenderProcessHost::AddFilter(BrowserMessageFilter
* filter
) {
199 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count
) {
200 if (static_cast<size_t>(GetActiveViewCount()) == count
)
201 return FastShutdownIfPossible();
205 base::TimeDelta
MockRenderProcessHost::GetChildProcessIdleTime() const {
206 return base::TimeDelta::FromMilliseconds(0);
209 void MockRenderProcessHost::ResumeRequestsForView(int route_id
) {
212 void MockRenderProcessHost::NotifyTimezoneChange() {
215 ServiceRegistry
* MockRenderProcessHost::GetServiceRegistry() {
219 const base::TimeTicks
& MockRenderProcessHost::GetInitTimeForNavigationMetrics()
221 static base::TimeTicks dummy_time
= base::TimeTicks::Now();
225 bool MockRenderProcessHost::SubscribeUniformEnabled() const {
229 void MockRenderProcessHost::OnAddSubscription(unsigned int target
) {
232 void MockRenderProcessHost::OnRemoveSubscription(unsigned int target
) {
235 void MockRenderProcessHost::SendUpdateValueState(
236 unsigned int target
, const gpu::ValueState
& state
) {
239 #if defined(ENABLE_BROWSER_CDMS)
240 media::BrowserCdm
* MockRenderProcessHost::GetBrowserCdm(int render_frame_id
,
246 void MockRenderProcessHost::FilterURL(bool empty_allowed
, GURL
* url
) {
247 RenderProcessHostImpl::FilterURL(this, empty_allowed
, url
);
250 #if defined(ENABLE_WEBRTC)
251 void MockRenderProcessHost::EnableAecDump(const base::FilePath
& file
) {
254 void MockRenderProcessHost::DisableAecDump() {
257 void MockRenderProcessHost::SetWebRtcLogMessageCallback(
258 base::Callback
<void(const std::string
&)> callback
) {
261 RenderProcessHost::WebRtcStopRtpDumpCallback
262 MockRenderProcessHost::StartRtpDump(
265 const WebRtcRtpPacketCallback
& packet_callback
) {
266 return WebRtcStopRtpDumpCallback();
270 void MockRenderProcessHost::ResumeDeferredNavigation(
271 const GlobalRequestID
& request_id
) {}
273 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message
& msg
) {
274 IPC::Listener
* listener
= listeners_
.Lookup(msg
.routing_id());
276 return listener
->OnMessageReceived(msg
);
280 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid
) {
283 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {}
285 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() {
286 // Detach this object from MockRenderProcesses to prevent STLDeleteElements()
287 // from calling MockRenderProcessHostFactory::Remove().
288 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
289 it
!= processes_
.end(); ++it
) {
290 (*it
)->SetFactory(NULL
);
294 RenderProcessHost
* MockRenderProcessHostFactory::CreateRenderProcessHost(
295 BrowserContext
* browser_context
,
296 SiteInstance
* site_instance
) const {
297 MockRenderProcessHost
* host
= new MockRenderProcessHost(browser_context
);
299 processes_
.push_back(host
);
300 host
->SetFactory(this);
305 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost
* host
) const {
306 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
307 it
!= processes_
.end(); ++it
) {
309 processes_
.weak_erase(it
);