OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/public/test/mock_render_process_host.h" | |
6 | |
7 #include "base/lazy_instance.h" | |
8 #include "base/message_loop.h" | |
9 #include "base/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/common/child_process_host_impl.h" | |
13 #include "content/public/browser/notification_service.h" | |
14 #include "content/public/browser/notification_types.h" | |
15 | |
16 namespace content { | |
17 | |
18 MockRenderProcessHost::MockRenderProcessHost( | |
19 BrowserContext* browser_context) | |
20 : transport_dib_(NULL), | |
21 bad_msg_count_(0), | |
22 factory_(NULL), | |
23 id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()), | |
24 browser_context_(browser_context), | |
25 fast_shutdown_started_(false) { | |
26 // Child process security operations can't be unit tested unless we add | |
27 // ourselves as an existing child process. | |
28 ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID()); | |
29 | |
30 RenderProcessHostImpl::RegisterHost(GetID(), this); | |
31 } | |
32 | |
33 MockRenderProcessHost::~MockRenderProcessHost() { | |
34 ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID()); | |
35 delete transport_dib_; | |
36 if (factory_) | |
37 factory_->Remove(this); | |
38 // In unit tests, Release() might not have been called. | |
39 RenderProcessHostImpl::UnregisterHost(GetID()); | |
40 } | |
41 | |
42 void MockRenderProcessHost::EnableSendQueue() { | |
43 } | |
44 | |
45 bool MockRenderProcessHost::Init() { | |
46 return true; | |
47 } | |
48 | |
49 int MockRenderProcessHost::GetNextRoutingID() { | |
50 static int prev_routing_id = 0; | |
51 return ++prev_routing_id; | |
52 } | |
53 | |
54 void MockRenderProcessHost::CancelResourceRequests(int render_widget_id) { | |
55 } | |
56 | |
57 void MockRenderProcessHost::CrossSiteSwapOutACK( | |
58 const ViewMsg_SwapOut_Params& params) { | |
59 } | |
60 | |
61 bool MockRenderProcessHost::WaitForBackingStoreMsg( | |
62 int render_widget_id, | |
63 const base::TimeDelta& max_delay, | |
64 IPC::Message* msg) { | |
65 return false; | |
66 } | |
67 | |
68 void MockRenderProcessHost::ReceivedBadMessage() { | |
69 ++bad_msg_count_; | |
70 } | |
71 | |
72 void MockRenderProcessHost::WidgetRestored() { | |
73 } | |
74 | |
75 void MockRenderProcessHost::WidgetHidden() { | |
76 } | |
77 | |
78 int MockRenderProcessHost::VisibleWidgetCount() const { | |
79 return 1; | |
80 } | |
81 | |
82 bool MockRenderProcessHost::IsGuest() const { | |
83 return false; | |
84 } | |
85 | |
86 void MockRenderProcessHost::AddWord(const string16& word) { | |
87 } | |
88 | |
89 bool MockRenderProcessHost::FastShutdownIfPossible() { | |
90 // We aren't actually going to do anything, but set |fast_shutdown_started_| | |
91 // to true so that tests know we've been called. | |
92 fast_shutdown_started_ = true; | |
93 return true; | |
94 } | |
95 | |
96 bool MockRenderProcessHost::FastShutdownStarted() const { | |
97 return fast_shutdown_started_; | |
98 } | |
99 | |
100 void MockRenderProcessHost::DumpHandles() { | |
101 } | |
102 | |
103 base::ProcessHandle MockRenderProcessHost::GetHandle() { | |
104 return base::kNullProcessHandle; | |
105 } | |
106 | |
107 bool MockRenderProcessHost::Send(IPC::Message* msg) { | |
108 // Save the message in the sink. | |
109 sink_.OnMessageReceived(*msg); | |
110 delete msg; | |
111 return true; | |
112 } | |
113 | |
114 TransportDIB* MockRenderProcessHost::GetTransportDIB(TransportDIB::Id dib_id) { | |
115 if (transport_dib_) | |
116 return transport_dib_; | |
117 #if defined(OS_WIN) | |
118 HANDLE duped; | |
119 DuplicateHandle(GetCurrentProcess(), dib_id.handle, GetCurrentProcess(), | |
120 &duped, 0, TRUE, DUPLICATE_SAME_ACCESS); | |
121 transport_dib_ = TransportDIB::Map(duped); | |
122 #elif defined(OS_MACOSX) | |
123 // On Mac, TransportDIBs are always created in the browser, so we cannot map | |
124 // one from a dib_id. | |
125 transport_dib_ = TransportDIB::Create(100 * 100 * 4, 0); | |
126 #elif defined(OS_ANDROID) | |
127 // On Android, Handles and Ids are the same underlying type. | |
128 transport_dib_ = TransportDIB::Map(dib_id); | |
129 #elif defined(OS_POSIX) | |
130 transport_dib_ = TransportDIB::Map(dib_id.shmkey); | |
131 #endif | |
132 | |
133 return transport_dib_; | |
134 } | |
135 | |
136 int MockRenderProcessHost::GetID() const { | |
137 return id_; | |
138 } | |
139 | |
140 bool MockRenderProcessHost::HasConnection() const { | |
141 return true; | |
142 } | |
143 | |
144 void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) { | |
145 } | |
146 | |
147 bool MockRenderProcessHost::IgnoreInputEvents() const { | |
148 return false; | |
149 } | |
150 | |
151 void MockRenderProcessHost::Attach(RenderWidgetHost* host, | |
152 int routing_id) { | |
153 render_widget_hosts_.AddWithID(host, routing_id); | |
154 } | |
155 | |
156 void MockRenderProcessHost::Release(int routing_id) { | |
157 render_widget_hosts_.Remove(routing_id); | |
158 Cleanup(); | |
159 } | |
160 | |
161 void MockRenderProcessHost::Cleanup() { | |
162 if (render_widget_hosts_.IsEmpty()) { | |
163 NotificationService::current()->Notify( | |
164 NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
165 Source<RenderProcessHost>(this), | |
166 NotificationService::NoDetails()); | |
167 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
168 RenderProcessHostImpl::UnregisterHost(GetID()); | |
169 } | |
170 } | |
171 | |
172 void MockRenderProcessHost::AddPendingView() { | |
173 } | |
174 | |
175 void MockRenderProcessHost::RemovePendingView() { | |
176 } | |
177 | |
178 void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) { | |
179 } | |
180 | |
181 bool MockRenderProcessHost::SuddenTerminationAllowed() const { | |
182 return true; | |
183 } | |
184 | |
185 content::RenderWidgetHost* MockRenderProcessHost::GetRenderWidgetHostByID( | |
186 int routing_id) { | |
187 return render_widget_hosts_.Lookup(routing_id); | |
188 } | |
189 | |
190 BrowserContext* MockRenderProcessHost::GetBrowserContext() const { | |
191 return browser_context_; | |
192 } | |
193 | |
194 IPC::ChannelProxy* MockRenderProcessHost::GetChannel() { | |
195 return NULL; | |
196 } | |
197 | |
198 bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) { | |
199 if (render_widget_hosts_.size() == count) | |
200 return FastShutdownIfPossible(); | |
201 return false; | |
202 } | |
203 | |
204 base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const { | |
205 return base::TimeDelta::FromMilliseconds(0); | |
206 } | |
207 | |
208 void MockRenderProcessHost::SurfaceUpdated(int32 surface_id) { | |
209 } | |
210 | |
211 void MockRenderProcessHost::ResumeRequestsForView(int route_id) { | |
212 } | |
213 | |
214 RenderProcessHost::RenderWidgetHostsIterator | |
215 MockRenderProcessHost::GetRenderWidgetHostsIterator() { | |
216 return RenderWidgetHostsIterator(&render_widget_hosts_); | |
217 } | |
218 | |
219 bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) { | |
220 return false; | |
221 } | |
222 | |
223 void MockRenderProcessHost::OnChannelConnected(int32 peer_pid) { | |
224 } | |
225 | |
226 MockRenderProcessHostFactory::MockRenderProcessHostFactory() {} | |
227 | |
228 MockRenderProcessHostFactory::~MockRenderProcessHostFactory() { | |
229 // Detach this object from MockRenderProcesses to prevent STLDeleteElements() | |
230 // from calling MockRenderProcessHostFactory::Remove(). | |
231 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); | |
232 it != processes_.end(); ++it) { | |
233 (*it)->SetFactory(NULL); | |
234 } | |
235 } | |
236 | |
237 RenderProcessHost* MockRenderProcessHostFactory::CreateRenderProcessHost( | |
238 BrowserContext* browser_context) const { | |
239 MockRenderProcessHost* host = new MockRenderProcessHost(browser_context); | |
240 if (host) { | |
241 processes_.push_back(host); | |
242 host->SetFactory(this); | |
243 } | |
244 return host; | |
245 } | |
246 | |
247 void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const { | |
248 for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin(); | |
249 it != processes_.end(); ++it) { | |
250 if (*it == host) { | |
251 processes_.weak_erase(it); | |
252 break; | |
253 } | |
254 } | |
255 } | |
256 | |
257 } // content | |
OLD | NEW |