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
|
// Copyright (c) 2011 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 "chrome/renderer/mock_render_thread.h"
#include <fcntl.h>
#include "base/file_util.h"
#include "base/process_util.h"
#include "chrome/common/extensions/extension_messages.h"
#include "chrome/common/print_messages.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/url_constants.h"
#include "content/common/view_messages.h"
#include "ipc/ipc_message_utils.h"
#include "ipc/ipc_sync_message.h"
#include "printing/print_job_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
MockRenderThread::MockRenderThread()
: routing_id_(0),
opener_id_(0),
widget_(NULL),
reply_deserializer_(NULL),
printer_(new MockPrinter),
print_dialog_user_response_(true),
print_preview_pages_remaining_(0) {
}
MockRenderThread::~MockRenderThread() {
}
// Called by the Widget. The routing_id must match the routing id assigned
// to the Widget in reply to ViewHostMsg_CreateWidget message.
void MockRenderThread::AddRoute(int32 routing_id,
IPC::Channel::Listener* listener) {
EXPECT_EQ(routing_id_, routing_id);
widget_ = listener;
}
// Called by the Widget. The routing id must match the routing id of AddRoute.
void MockRenderThread::RemoveRoute(int32 routing_id) {
EXPECT_EQ(routing_id_, routing_id);
widget_ = NULL;
}
// Called by, for example, RenderView::Init(), when adding a new message filter.
void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
filter->OnFilterAdded(&sink());
}
// Called when the filter is removed.
void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
filter->OnFilterRemoved();
}
bool MockRenderThread::IsIncognitoProcess() const {
return false;
}
// Called by the Widget. Used to send messages to the browser.
// We short-circuit the mechanism and handle the messages right here on this
// class.
bool MockRenderThread::Send(IPC::Message* msg) {
// We need to simulate a synchronous channel, thus we are going to receive
// through this function messages, messages with reply and reply messages.
// We can only handle one synchronous message at a time.
if (msg->is_reply()) {
if (reply_deserializer_.get()) {
reply_deserializer_->SerializeOutputParameters(*msg);
reply_deserializer_.reset();
}
} else {
if (msg->is_sync()) {
// We actually need to handle deleting the reply deserializer for sync
// messages.
reply_deserializer_.reset(
static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer());
}
OnMessageReceived(*msg);
}
delete msg;
return true;
}
void MockRenderThread::SendCloseMessage() {
ViewMsg_Close msg(routing_id_);
widget_->OnMessageReceived(msg);
}
bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
// Save the message in the sink.
sink_.OnMessageReceived(msg);
// Some messages we do special handling.
bool handled = true;
bool msg_is_ok = true;
IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok)
IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget)
IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension,
OnMsgOpenChannelToExtension)
IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings,
OnGetDefaultPrintSettings)
IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint)
IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount,
OnDidGetPrintedPagesCount)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount,
OnDidGetPreviewPageCount)
IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage)
#if defined(OS_WIN)
IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection)
#endif
IPC_MESSAGE_HANDLER(ViewHostMsg_AllocateSharedMemoryBuffer,
OnAllocateSharedMemoryBuffer)
#if defined(OS_CHROMEOS)
IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting,
OnAllocateTempFileForPrinting)
IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten,
OnTempFileForPrintingWritten)
#endif
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
return handled;
}
// The Widget expects to be returned valid route_id.
void MockRenderThread::OnMsgCreateWidget(int opener_id,
WebKit::WebPopupType popup_type,
int* route_id) {
opener_id_ = opener_id;
*route_id = routing_id_;
}
void MockRenderThread::OnMsgOpenChannelToExtension(
int routing_id, const std::string& source_extension_id,
const std::string& target_extension_id,
const std::string& channel_name, int* port_id) {
*port_id = 0;
}
#if defined(OS_WIN)
void MockRenderThread::OnDuplicateSection(
base::SharedMemoryHandle renderer_handle,
base::SharedMemoryHandle* browser_handle) {
// We don't have to duplicate the input handles since RenderViewTest does not
// separate a browser process from a renderer process.
*browser_handle = renderer_handle;
}
#endif // defined(OS_WIN)
void MockRenderThread::OnAllocateSharedMemoryBuffer(
uint32 buffer_size, base::SharedMemoryHandle* handle) {
base::SharedMemory shared_buf;
if (!shared_buf.CreateAndMapAnonymous(buffer_size)) {
*handle = base::SharedMemory::NULLHandle();
NOTREACHED() << "Cannot map shared memory buffer";
return;
}
shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), handle);
}
#if defined(OS_CHROMEOS)
void MockRenderThread::OnAllocateTempFileForPrinting(
base::FileDescriptor* renderer_fd,
int* browser_fd) {
renderer_fd->fd = *browser_fd = -1;
renderer_fd->auto_close = false;
FilePath path;
if (file_util::CreateTemporaryFile(&path)) {
int fd = open(path.value().c_str(), O_WRONLY);
DCHECK_GE(fd, 0);
renderer_fd->fd = *browser_fd = fd;
}
}
void MockRenderThread::OnTempFileForPrintingWritten(int browser_fd) {
close(browser_fd);
}
#endif // defined(OS_CHROMEOS)
void MockRenderThread::OnGetDefaultPrintSettings(
PrintMsg_Print_Params* params) {
if (printer_.get())
printer_->GetDefaultPrintSettings(params);
}
void MockRenderThread::OnScriptedPrint(
const PrintHostMsg_ScriptedPrint_Params& params,
PrintMsg_PrintPages_Params* settings) {
if (print_dialog_user_response_ && printer_.get()) {
printer_->ScriptedPrint(params.cookie,
params.expected_pages_count,
params.has_selection,
settings);
}
}
void MockRenderThread::OnDidGetPrintedPagesCount(int cookie, int number_pages) {
if (printer_.get())
printer_->SetPrintedPagesCount(cookie, number_pages);
}
void MockRenderThread::OnDidPrintPage(
const PrintHostMsg_DidPrintPage_Params& params) {
if (printer_.get())
printer_->PrintPage(params);
}
void MockRenderThread::OnDidGetPreviewPageCount(int document_cookie,
int number_pages,
bool is_modifiable) {
print_preview_pages_remaining_ = number_pages;
}
void MockRenderThread::OnDidPreviewPage(
const PrintHostMsg_DidPreviewPage_Params& params) {
if (params.page_number < printing::FIRST_PAGE_INDEX)
return;
print_preview_pages_remaining_--;
}
void MockRenderThread::OnUpdatePrintSettings(
int document_cookie,
const DictionaryValue& job_settings,
PrintMsg_PrintPages_Params* params) {
// Check and make sure the required settings are all there.
// We don't actually care about the values.
std::string dummy_string;
if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
!job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
!job_settings.GetBoolean(printing::kSettingColor, NULL) ||
!job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
!job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
!job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
!job_settings.GetInteger(printing::kSettingCopies, NULL)) {
return;
}
// Just return the default settings.
if (printer_.get())
printer_->UpdateSettings(document_cookie, params);
}
void MockRenderThread::set_print_dialog_user_response(bool response) {
print_dialog_user_response_ = response;
}
int MockRenderThread::print_preview_pages_remaining() {
return print_preview_pages_remaining_;
}
|