Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / mock_render_process_host.cc
bloba8d3afb4879b25b878aaf02519db22a1be0f9ef8
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"
21 namespace content {
23 MockRenderProcessHost::MockRenderProcessHost(BrowserContext* browser_context)
24 : bad_msg_count_(0),
25 factory_(NULL),
26 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
27 browser_context_(browser_context),
28 prev_routing_id_(0),
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());
41 if (factory_)
42 factory_->Remove(this);
44 // In unit tests, Cleanup() might not have been called.
45 if (!deletion_callback_called_) {
46 FOR_EACH_OBSERVER(RenderProcessHostObserver,
47 observers_,
48 RenderProcessHostDestroyed(this));
49 RenderProcessHostImpl::UnregisterHost(GetID());
53 void MockRenderProcessHost::EnableSendQueue() {
56 bool MockRenderProcessHost::Init() {
57 return true;
60 int MockRenderProcessHost::GetNextRoutingID() {
61 return ++prev_routing_id_;
64 void MockRenderProcessHost::AddRoute(
65 int32 routing_id,
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);
73 Cleanup();
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() {
86 ++bad_msg_count_;
89 void MockRenderProcessHost::WidgetRestored() {
92 void MockRenderProcessHost::WidgetHidden() {
95 int MockRenderProcessHost::VisibleWidgetCount() const {
96 return 1;
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) {
111 return true;
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;
118 return 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
130 // function.
131 if (process_handle)
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);
139 delete msg;
140 return true;
143 int MockRenderProcessHost::GetID() const {
144 return id_;
147 bool MockRenderProcessHost::HasConnection() const {
148 return true;
151 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) {
154 bool MockRenderProcessHost::IgnoreInputEvents() const {
155 return false;
158 void MockRenderProcessHost::Cleanup() {
159 if (listeners_.IsEmpty()) {
160 FOR_EACH_OBSERVER(RenderProcessHostObserver,
161 observers_,
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 {
179 return true;
182 BrowserContext* MockRenderProcessHost::GetBrowserContext() const {
183 return browser_context_;
186 bool MockRenderProcessHost::InSameStoragePartition(
187 StoragePartition* partition) const {
188 // Mock RPHs only have one partition.
189 return true;
192 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() {
193 return NULL;
196 void MockRenderProcessHost::AddFilter(BrowserMessageFilter* filter) {
199 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) {
200 if (static_cast<size_t>(GetActiveViewCount()) == count)
201 return FastShutdownIfPossible();
202 return false;
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() {
216 return NULL;
219 const base::TimeTicks& MockRenderProcessHost::GetInitTimeForNavigationMetrics()
220 const {
221 static base::TimeTicks dummy_time = base::TimeTicks::Now();
222 return dummy_time;
225 bool MockRenderProcessHost::SubscribeUniformEnabled() const {
226 return false;
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,
241 int cdm_id) const {
242 return nullptr;
244 #endif
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(
263 bool incoming,
264 bool outgoing,
265 const WebRtcRtpPacketCallback& packet_callback) {
266 return WebRtcStopRtpDumpCallback();
268 #endif
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());
275 if (listener)
276 return listener->OnMessageReceived(msg);
277 return false;
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);
298 if (host) {
299 processes_.push_back(host);
300 host->SetFactory(this);
302 return host;
305 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const {
306 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin();
307 it != processes_.end(); ++it) {
308 if (*it == host) {
309 processes_.weak_erase(it);
310 break;
315 } // content