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/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"
21 MockRenderProcessHost::MockRenderProcessHost(BrowserContext
* browser_context
)
24 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
25 browser_context_(browser_context
),
27 fast_shutdown_started_(false),
28 deletion_callback_called_(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());
34 RenderProcessHostImpl::RegisterHost(GetID(), this);
37 MockRenderProcessHost::~MockRenderProcessHost() {
38 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
40 factory_
->Remove(this);
42 // In unit tests, Cleanup() might not have been called.
43 if (!deletion_callback_called_
) {
44 FOR_EACH_OBSERVER(RenderProcessHostObserver
,
46 RenderProcessHostDestroyed(this));
47 RenderProcessHostImpl::UnregisterHost(GetID());
51 void MockRenderProcessHost::EnableSendQueue() {
54 bool MockRenderProcessHost::Init() {
58 int MockRenderProcessHost::GetNextRoutingID() {
59 return ++prev_routing_id_
;
62 void MockRenderProcessHost::AddRoute(
64 IPC::Listener
* listener
) {
65 listeners_
.AddWithID(listener
, routing_id
);
68 void MockRenderProcessHost::RemoveRoute(int32 routing_id
) {
69 DCHECK(listeners_
.Lookup(routing_id
) != NULL
);
70 listeners_
.Remove(routing_id
);
74 void MockRenderProcessHost::AddObserver(RenderProcessHostObserver
* observer
) {
75 observers_
.AddObserver(observer
);
78 void MockRenderProcessHost::RemoveObserver(
79 RenderProcessHostObserver
* observer
) {
80 observers_
.RemoveObserver(observer
);
83 bool MockRenderProcessHost::WaitForBackingStoreMsg(
85 const base::TimeDelta
& max_delay
,
90 void MockRenderProcessHost::ReceivedBadMessage() {
94 void MockRenderProcessHost::WidgetRestored() {
97 void MockRenderProcessHost::WidgetHidden() {
100 int MockRenderProcessHost::VisibleWidgetCount() const {
104 bool MockRenderProcessHost::IsGuest() const {
108 StoragePartition
* MockRenderProcessHost::GetStoragePartition() const {
112 void MockRenderProcessHost::AddWord(const base::string16
& word
) {
115 bool MockRenderProcessHost::FastShutdownIfPossible() {
116 // We aren't actually going to do anything, but set |fast_shutdown_started_|
117 // to true so that tests know we've been called.
118 fast_shutdown_started_
= true;
122 bool MockRenderProcessHost::FastShutdownStarted() const {
123 return fast_shutdown_started_
;
126 void MockRenderProcessHost::DumpHandles() {
129 base::ProcessHandle
MockRenderProcessHost::GetHandle() const {
130 // Return the current-process handle for the IPC::GetFileHandleForProcess
132 return base::Process::Current().handle();
135 bool MockRenderProcessHost::Send(IPC::Message
* msg
) {
136 // Save the message in the sink.
137 sink_
.OnMessageReceived(*msg
);
142 int MockRenderProcessHost::GetID() const {
146 bool MockRenderProcessHost::HasConnection() const {
150 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events
) {
153 bool MockRenderProcessHost::IgnoreInputEvents() const {
157 void MockRenderProcessHost::Cleanup() {
158 if (listeners_
.IsEmpty()) {
159 FOR_EACH_OBSERVER(RenderProcessHostObserver
,
161 RenderProcessHostDestroyed(this));
162 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
163 RenderProcessHostImpl::UnregisterHost(GetID());
164 deletion_callback_called_
= true;
168 void MockRenderProcessHost::AddPendingView() {
171 void MockRenderProcessHost::RemovePendingView() {
174 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed
) {
177 bool MockRenderProcessHost::SuddenTerminationAllowed() const {
181 BrowserContext
* MockRenderProcessHost::GetBrowserContext() const {
182 return browser_context_
;
185 bool MockRenderProcessHost::InSameStoragePartition(
186 StoragePartition
* partition
) const {
187 // Mock RPHs only have one partition.
191 IPC::ChannelProxy
* MockRenderProcessHost::GetChannel() {
195 void MockRenderProcessHost::AddFilter(BrowserMessageFilter
* filter
) {
198 int MockRenderProcessHost::GetActiveViewCount() {
199 int num_active_views
= 0;
200 scoped_ptr
<RenderWidgetHostIterator
> widgets(
201 RenderWidgetHost::GetRenderWidgetHosts());
202 while (RenderWidgetHost
* widget
= widgets
->GetNextHost()) {
203 // Count only RenderWidgetHosts in this process.
204 if (widget
->GetProcess()->GetID() == GetID())
207 return num_active_views
;
210 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count
) {
211 if (static_cast<size_t>(GetActiveViewCount()) == count
)
212 return FastShutdownIfPossible();
216 base::TimeDelta
MockRenderProcessHost::GetChildProcessIdleTime() const {
217 return base::TimeDelta::FromMilliseconds(0);
220 void MockRenderProcessHost::ResumeRequestsForView(int route_id
) {
223 void MockRenderProcessHost::NotifyTimezoneChange() {
226 void MockRenderProcessHost::FilterURL(bool empty_allowed
, GURL
* url
) {
227 RenderProcessHostImpl::FilterURL(this, empty_allowed
, url
);
230 #if defined(ENABLE_WEBRTC)
231 void MockRenderProcessHost::EnableAecDump(const base::FilePath
& file
) {
234 void MockRenderProcessHost::DisableAecDump() {
237 void MockRenderProcessHost::SetWebRtcLogMessageCallback(
238 base::Callback
<void(const std::string
&)> callback
) {
242 void MockRenderProcessHost::ResumeDeferredNavigation(
243 const GlobalRequestID
& request_id
) {}
245 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message
& msg
) {
246 IPC::Listener
* listener
= listeners_
.Lookup(msg
.routing_id());
248 return listener
->OnMessageReceived(msg
);
252 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid
) {
255 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {}
257 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() {
258 // Detach this object from MockRenderProcesses to prevent STLDeleteElements()
259 // from calling MockRenderProcessHostFactory::Remove().
260 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
261 it
!= processes_
.end(); ++it
) {
262 (*it
)->SetFactory(NULL
);
266 RenderProcessHost
* MockRenderProcessHostFactory::CreateRenderProcessHost(
267 BrowserContext
* browser_context
,
268 SiteInstance
* site_instance
) const {
269 MockRenderProcessHost
* host
= new MockRenderProcessHost(browser_context
);
271 processes_
.push_back(host
);
272 host
->SetFactory(this);
277 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost
* host
) const {
278 for (ScopedVector
<MockRenderProcessHost
>::iterator it
= processes_
.begin();
279 it
!= processes_
.end(); ++it
) {
281 processes_
.weak_erase(it
);