IDB: Make readonly transactions wait for earlier readwrite transactions
[chromium-blink-merge.git] / content / public / test / mock_render_process_host.cc
blobd98f6e83c2c5dc3e7799260a577827ccbfd90c76
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::ReceivedBadMessage() {
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::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;
114 return 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
126 // function.
127 if (process_handle)
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);
135 delete msg;
136 return true;
139 int MockRenderProcessHost::GetID() const {
140 return id_;
143 bool MockRenderProcessHost::HasConnection() const {
144 return true;
147 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) {
150 bool MockRenderProcessHost::IgnoreInputEvents() const {
151 return false;
154 void MockRenderProcessHost::Cleanup() {
155 if (listeners_.IsEmpty()) {
156 FOR_EACH_OBSERVER(RenderProcessHostObserver,
157 observers_,
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 {
175 return true;
178 BrowserContext* MockRenderProcessHost::GetBrowserContext() const {
179 return browser_context_;
182 bool MockRenderProcessHost::InSameStoragePartition(
183 StoragePartition* partition) const {
184 // Mock RPHs only have one partition.
185 return true;
188 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() {
189 return NULL;
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())
202 num_active_views++;
204 return num_active_views;
207 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) {
208 if (static_cast<size_t>(GetActiveViewCount()) == count)
209 return FastShutdownIfPossible();
210 return false;
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() {
224 return NULL;
227 const base::TimeTicks& MockRenderProcessHost::GetInitTimeForNavigationMetrics()
228 const {
229 static base::TimeTicks dummy_time = base::TimeTicks::Now();
230 return dummy_time;
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(
250 bool incoming,
251 bool outgoing,
252 const WebRtcRtpPacketCallback& packet_callback) {
253 return WebRtcStopRtpDumpCallback();
255 #endif
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());
262 if (listener)
263 return listener->OnMessageReceived(msg);
264 return false;
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);
285 if (host) {
286 processes_.push_back(host);
287 host->SetFactory(this);
289 return host;
292 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const {
293 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin();
294 it != processes_.end(); ++it) {
295 if (*it == host) {
296 processes_.weak_erase(it);
297 break;
302 } // content