Make sure webrtc::VideoSourceInterface is released on the main render thread.
[chromium-blink-merge.git] / content / public / test / mock_render_process_host.cc
blob50c19ee849d81fe6876e80482f353b4ed4ce8328
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"
19 namespace content {
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_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());
34 RenderProcessHostImpl::RegisterHost(GetID(), this);
37 MockRenderProcessHost::~MockRenderProcessHost() {
38 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
39 if (factory_)
40 factory_->Remove(this);
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());
51 void MockRenderProcessHost::EnableSendQueue() {
54 bool MockRenderProcessHost::Init() {
55 return true;
58 int MockRenderProcessHost::GetNextRoutingID() {
59 return ++prev_routing_id_;
62 void MockRenderProcessHost::AddRoute(
63 int32 routing_id,
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);
71 Cleanup();
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(
84 int render_widget_id,
85 const base::TimeDelta& max_delay,
86 IPC::Message* msg) {
87 return false;
90 void MockRenderProcessHost::ReceivedBadMessage() {
91 ++bad_msg_count_;
94 void MockRenderProcessHost::WidgetRestored() {
97 void MockRenderProcessHost::WidgetHidden() {
100 int MockRenderProcessHost::VisibleWidgetCount() const {
101 return 1;
104 bool MockRenderProcessHost::IsGuest() const {
105 return is_guest_;
108 StoragePartition* MockRenderProcessHost::GetStoragePartition() const {
109 return NULL;
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;
119 return 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
131 // function.
132 return base::Process::Current().handle();
135 bool MockRenderProcessHost::Send(IPC::Message* msg) {
136 // Save the message in the sink.
137 sink_.OnMessageReceived(*msg);
138 delete msg;
139 return true;
142 int MockRenderProcessHost::GetID() const {
143 return id_;
146 bool MockRenderProcessHost::HasConnection() const {
147 return true;
150 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) {
153 bool MockRenderProcessHost::IgnoreInputEvents() const {
154 return false;
157 void MockRenderProcessHost::Cleanup() {
158 if (listeners_.IsEmpty()) {
159 FOR_EACH_OBSERVER(RenderProcessHostObserver,
160 observers_,
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 {
178 return true;
181 BrowserContext* MockRenderProcessHost::GetBrowserContext() const {
182 return browser_context_;
185 bool MockRenderProcessHost::InSameStoragePartition(
186 StoragePartition* partition) const {
187 // Mock RPHs only have one partition.
188 return true;
191 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() {
192 return NULL;
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())
205 num_active_views++;
207 return num_active_views;
210 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) {
211 if (static_cast<size_t>(GetActiveViewCount()) == count)
212 return FastShutdownIfPossible();
213 return false;
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) {
240 #endif
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());
247 if (listener)
248 return listener->OnMessageReceived(msg);
249 return false;
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);
270 if (host) {
271 processes_.push_back(host);
272 host->SetFactory(this);
274 return host;
277 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const {
278 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin();
279 it != processes_.end(); ++it) {
280 if (*it == host) {
281 processes_.weak_erase(it);
282 break;
287 } // content