1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/public/test/mock_render_process_host.h"
#include <algorithm>
#include <utility>
#include <vector>
#include "base/lazy_instance.h"
#include "base/message_loop/message_loop.h"
#include "base/process/process_handle.h"
#include "base/time/time.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/common/child_process_host_impl.h"
#include "content/common/frame_messages.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_widget_host_iterator.h"
#include "content/public/browser/storage_partition.h"
#if defined(ENABLE_BROWSER_CDMS)
#include "media/base/media_keys.h"
#endif
namespace content {
MockRenderProcessHost::MockRenderProcessHost(BrowserContext* browser_context)
: bad_msg_count_(0),
factory_(NULL),
id_(ChildProcessHostImpl::GenerateChildProcessUniqueId()),
has_connection_(false),
browser_context_(browser_context),
prev_routing_id_(0),
fast_shutdown_started_(false),
deletion_callback_called_(false),
is_for_guests_only_(false),
is_process_backgrounded_(false),
worker_ref_count_(0) {
// Child process security operations can't be unit tested unless we add
// ourselves as an existing child process.
ChildProcessSecurityPolicyImpl::GetInstance()->Add(GetID());
RenderProcessHostImpl::RegisterHost(GetID(), this);
}
MockRenderProcessHost::~MockRenderProcessHost() {
ChildProcessSecurityPolicyImpl::GetInstance()->Remove(GetID());
if (factory_)
factory_->Remove(this);
// In unit tests, Cleanup() might not have been called.
if (!deletion_callback_called_) {
FOR_EACH_OBSERVER(RenderProcessHostObserver,
observers_,
RenderProcessHostDestroyed(this));
RenderProcessHostImpl::UnregisterHost(GetID());
}
}
void MockRenderProcessHost::SimulateCrash() {
has_connection_ = false;
RenderProcessHost::RendererClosedDetails details(
base::TERMINATION_STATUS_PROCESS_CRASHED, 0);
NotificationService::current()->Notify(
NOTIFICATION_RENDERER_PROCESS_CLOSED, Source<RenderProcessHost>(this),
Details<RenderProcessHost::RendererClosedDetails>(&details));
FOR_EACH_OBSERVER(
RenderProcessHostObserver, observers_,
RenderProcessExited(this, details.status, details.exit_code));
// Send every routing ID a FrameHostMsg_RenderProcessGone message. To ensure a
// predictable order for unittests which may assert against the order, we sort
// the listeners by descending routing ID, instead of using the arbitrary
// hash-map order like RenderProcessHostImpl.
std::vector<std::pair<int32_t, IPC::Listener*>> sorted_listeners_;
IDMap<IPC::Listener>::iterator iter(&listeners_);
while (!iter.IsAtEnd()) {
sorted_listeners_.push_back(
std::make_pair(iter.GetCurrentKey(), iter.GetCurrentValue()));
iter.Advance();
}
std::sort(sorted_listeners_.rbegin(), sorted_listeners_.rend());
for (auto& entry_pair : sorted_listeners_) {
entry_pair.second->OnMessageReceived(FrameHostMsg_RenderProcessGone(
entry_pair.first, static_cast<int>(details.status), details.exit_code));
}
}
void MockRenderProcessHost::EnableSendQueue() {
}
bool MockRenderProcessHost::Init() {
has_connection_ = true;
return true;
}
int MockRenderProcessHost::GetNextRoutingID() {
return ++prev_routing_id_;
}
void MockRenderProcessHost::AddRoute(int32_t routing_id,
IPC::Listener* listener) {
listeners_.AddWithID(listener, routing_id);
}
void MockRenderProcessHost::RemoveRoute(int32_t routing_id) {
DCHECK(listeners_.Lookup(routing_id) != NULL);
listeners_.Remove(routing_id);
Cleanup();
}
void MockRenderProcessHost::AddObserver(RenderProcessHostObserver* observer) {
observers_.AddObserver(observer);
}
void MockRenderProcessHost::RemoveObserver(
RenderProcessHostObserver* observer) {
observers_.RemoveObserver(observer);
}
void MockRenderProcessHost::ShutdownForBadMessage() {
++bad_msg_count_;
}
void MockRenderProcessHost::WidgetRestored() {
}
void MockRenderProcessHost::WidgetHidden() {
}
int MockRenderProcessHost::VisibleWidgetCount() const {
return 1;
}
void MockRenderProcessHost::AudioStateChanged() {}
bool MockRenderProcessHost::IsForGuestsOnly() const {
return is_for_guests_only_;
}
StoragePartition* MockRenderProcessHost::GetStoragePartition() const {
return BrowserContext::GetDefaultStoragePartition(browser_context_);
}
void MockRenderProcessHost::AddWord(const base::string16& word) {
}
bool MockRenderProcessHost::Shutdown(int exit_code, bool wait) {
return true;
}
bool MockRenderProcessHost::FastShutdownIfPossible() {
// We aren't actually going to do anything, but set |fast_shutdown_started_|
// to true so that tests know we've been called.
fast_shutdown_started_ = true;
return true;
}
bool MockRenderProcessHost::FastShutdownStarted() const {
return fast_shutdown_started_;
}
base::ProcessHandle MockRenderProcessHost::GetHandle() const {
// Return the current-process handle for the IPC::GetFileHandleForProcess
// function.
if (process_handle)
return *process_handle;
return base::GetCurrentProcessHandle();
}
bool MockRenderProcessHost::IsReady() const {
return false;
}
bool MockRenderProcessHost::Send(IPC::Message* msg) {
// Save the message in the sink.
sink_.OnMessageReceived(*msg);
delete msg;
return true;
}
int MockRenderProcessHost::GetID() const {
return id_;
}
bool MockRenderProcessHost::HasConnection() const {
return has_connection_;
}
void MockRenderProcessHost::SetIgnoreInputEvents(bool ignore_input_events) {
}
bool MockRenderProcessHost::IgnoreInputEvents() const {
return false;
}
void MockRenderProcessHost::Cleanup() {
if (listeners_.IsEmpty()) {
FOR_EACH_OBSERVER(RenderProcessHostObserver,
observers_,
RenderProcessHostDestroyed(this));
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
RenderProcessHostImpl::UnregisterHost(GetID());
deletion_callback_called_ = true;
}
}
void MockRenderProcessHost::AddPendingView() {
}
void MockRenderProcessHost::RemovePendingView() {
}
void MockRenderProcessHost::SetSuddenTerminationAllowed(bool allowed) {
}
bool MockRenderProcessHost::SuddenTerminationAllowed() const {
return true;
}
BrowserContext* MockRenderProcessHost::GetBrowserContext() const {
return browser_context_;
}
bool MockRenderProcessHost::InSameStoragePartition(
StoragePartition* partition) const {
// Mock RPHs only have one partition.
return true;
}
IPC::ChannelProxy* MockRenderProcessHost::GetChannel() {
return NULL;
}
void MockRenderProcessHost::AddFilter(BrowserMessageFilter* filter) {
}
bool MockRenderProcessHost::FastShutdownForPageCount(size_t count) {
if (GetActiveViewCount() == count)
return FastShutdownIfPossible();
return false;
}
base::TimeDelta MockRenderProcessHost::GetChildProcessIdleTime() const {
return base::TimeDelta::FromMilliseconds(0);
}
void MockRenderProcessHost::NotifyTimezoneChange(const std::string& zone_id) {
}
ServiceRegistry* MockRenderProcessHost::GetServiceRegistry() {
return service_registry_.get();
}
const base::TimeTicks& MockRenderProcessHost::GetInitTimeForNavigationMetrics()
const {
static base::TimeTicks dummy_time = base::TimeTicks::Now();
return dummy_time;
}
bool MockRenderProcessHost::SubscribeUniformEnabled() const {
return false;
}
void MockRenderProcessHost::OnAddSubscription(unsigned int target) {
}
void MockRenderProcessHost::OnRemoveSubscription(unsigned int target) {
}
void MockRenderProcessHost::SendUpdateValueState(
unsigned int target, const gpu::ValueState& state) {
}
#if defined(ENABLE_BROWSER_CDMS)
scoped_refptr<media::MediaKeys> MockRenderProcessHost::GetCdm(
int render_frame_id,
int cdm_id) const {
return nullptr;
}
#endif
bool MockRenderProcessHost::IsProcessBackgrounded() const {
return is_process_backgrounded_;
}
void MockRenderProcessHost::IncrementWorkerRefCount() {
++worker_ref_count_;
}
void MockRenderProcessHost::DecrementWorkerRefCount() {
--worker_ref_count_;
}
void MockRenderProcessHost::FilterURL(bool empty_allowed, GURL* url) {
RenderProcessHostImpl::FilterURL(this, empty_allowed, url);
}
#if defined(ENABLE_WEBRTC)
void MockRenderProcessHost::EnableAudioDebugRecordings(
const base::FilePath& file) {
}
void MockRenderProcessHost::DisableAudioDebugRecordings() {
}
void MockRenderProcessHost::EnableEventLogRecordings(
const base::FilePath& file) {}
void MockRenderProcessHost::DisableEventLogRecordings() {}
void MockRenderProcessHost::SetWebRtcLogMessageCallback(
base::Callback<void(const std::string&)> callback) {
}
RenderProcessHost::WebRtcStopRtpDumpCallback
MockRenderProcessHost::StartRtpDump(
bool incoming,
bool outgoing,
const WebRtcRtpPacketCallback& packet_callback) {
return WebRtcStopRtpDumpCallback();
}
#endif
void MockRenderProcessHost::ResumeDeferredNavigation(
const GlobalRequestID& request_id) {}
bool MockRenderProcessHost::OnMessageReceived(const IPC::Message& msg) {
IPC::Listener* listener = listeners_.Lookup(msg.routing_id());
if (listener)
return listener->OnMessageReceived(msg);
return false;
}
void MockRenderProcessHost::OnChannelConnected(int32_t peer_pid) {}
MockRenderProcessHostFactory::MockRenderProcessHostFactory() {}
MockRenderProcessHostFactory::~MockRenderProcessHostFactory() {
// Detach this object from MockRenderProcesses to prevent STLDeleteElements()
// from calling MockRenderProcessHostFactory::Remove().
for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin();
it != processes_.end(); ++it) {
(*it)->SetFactory(NULL);
}
}
RenderProcessHost* MockRenderProcessHostFactory::CreateRenderProcessHost(
BrowserContext* browser_context,
SiteInstance* site_instance) const {
MockRenderProcessHost* host = new MockRenderProcessHost(browser_context);
if (host) {
processes_.push_back(host);
host->SetFactory(this);
}
return host;
}
void MockRenderProcessHostFactory::Remove(MockRenderProcessHost* host) const {
for (ScopedVector<MockRenderProcessHost>::iterator it = processes_.begin();
it != processes_.end(); ++it) {
if (*it == host) {
processes_.weak_erase(it);
break;
}
}
}
} // namespace content
|