blob: 359b7131456d26e869b5cee0175dd6a345f2c350 [file] [log] [blame]
[email protected]90b721e62010-04-05 17:35:011// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]81a34412009-01-05 19:17:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/renderer/mock_render_thread.h"
6
[email protected]15d890b1a2010-05-24 20:01:077#include <fcntl.h>
8
9#include "base/file_util.h"
[email protected]b23aee0f2009-10-13 22:54:1210#include "base/process_util.h"
[email protected]81a34412009-01-05 19:17:2411#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0212#include "chrome/common/render_messages_params.h"
[email protected]583d45c12010-08-31 02:48:1213#include "chrome/common/url_constants.h"
[email protected]946d1b22009-07-22 23:57:2114#include "ipc/ipc_message_utils.h"
[email protected]a83d42292010-08-17 22:51:1015#include "ipc/ipc_sync_message.h"
[email protected]81a34412009-01-05 19:17:2416#include "testing/gtest/include/gtest/gtest.h"
17
18MockRenderThread::MockRenderThread()
19 : routing_id_(0),
20 opener_id_(0),
21 widget_(NULL),
[email protected]fa8a2b1b2009-05-23 03:09:5222 reply_deserializer_(NULL),
[email protected]b8f41a192010-04-19 18:25:0423 printer_(new MockPrinter),
24 is_extension_process_(false) {
[email protected]81a34412009-01-05 19:17:2425}
26
27MockRenderThread::~MockRenderThread() {
[email protected]81a34412009-01-05 19:17:2428}
29
30// Called by the Widget. The routing_id must match the routing id assigned
31// to the Widget in reply to ViewHostMsg_CreateWidget message.
32void MockRenderThread::AddRoute(int32 routing_id,
33 IPC::Channel::Listener* listener) {
34 EXPECT_EQ(routing_id_, routing_id);
35 widget_ = listener;
36}
37
38// Called by the Widget. The routing id must match the routing id of AddRoute.
39void MockRenderThread::RemoveRoute(int32 routing_id) {
40 EXPECT_EQ(routing_id_, routing_id);
41 widget_ = NULL;
42}
43
[email protected]90b721e62010-04-05 17:35:0144// Called by, for example, RenderView::Init(), when adding a new message filter.
45void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
46 filter->OnFilterAdded(&sink());
47}
48
49// Called when the filter is removed.
50void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
51 filter->OnFilterRemoved();
52}
53
[email protected]81a34412009-01-05 19:17:2454// Called by the Widget. Used to send messages to the browser.
55// We short-circuit the mechanim and handle the messages right here on this
56// class.
57bool MockRenderThread::Send(IPC::Message* msg) {
58 // We need to simulate a synchronous channel, thus we are going to receive
59 // through this function messages, messages with reply and reply messages.
60 // We can only handle one synchronous message at a time.
61 if (msg->is_reply()) {
[email protected]87cfff02009-01-06 19:18:0262 if (reply_deserializer_.get()) {
[email protected]81a34412009-01-05 19:17:2463 reply_deserializer_->SerializeOutputParameters(*msg);
[email protected]87cfff02009-01-06 19:18:0264 reply_deserializer_.reset();
[email protected]81a34412009-01-05 19:17:2465 }
66 } else {
67 if (msg->is_sync()) {
[email protected]15d890b1a2010-05-24 20:01:0768 // We actually need to handle deleting the reply deserializer for sync
[email protected]87cfff02009-01-06 19:18:0269 // messages.
70 reply_deserializer_.reset(
71 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer());
[email protected]81a34412009-01-05 19:17:2472 }
73 OnMessageReceived(*msg);
74 }
75 delete msg;
76 return true;
77}
78
[email protected]81a34412009-01-05 19:17:2479void MockRenderThread::SendCloseMessage() {
80 ViewMsg_Close msg(routing_id_);
81 widget_->OnMessageReceived(msg);
82}
83
84void MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
[email protected]cde96e12009-01-17 05:41:4885 // Save the message in the sink.
[email protected]445623e62010-03-25 23:20:2486 sink_.OnMessageReceived(msg);
[email protected]81a34412009-01-05 19:17:2487
88 // Some messages we do special handling.
89 bool handled = true;
90 bool msg_is_ok = true;
91 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok)
[email protected]15d890b1a2010-05-24 20:01:0792 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget)
[email protected]75e5a872009-04-02 23:56:1193 IPC_MESSAGE_HANDLER(ViewHostMsg_OpenChannelToExtension,
[email protected]15d890b1a2010-05-24 20:01:0794 OnMsgOpenChannelToExtension)
[email protected]da911552009-04-16 05:30:5495 IPC_MESSAGE_HANDLER(ViewHostMsg_GetDefaultPrintSettings,
[email protected]15d890b1a2010-05-24 20:01:0796 OnGetDefaultPrintSettings)
[email protected]da911552009-04-16 05:30:5497 IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptedPrint,
[email protected]15d890b1a2010-05-24 20:01:0798 OnScriptedPrint)
[email protected]b7191422010-09-21 19:18:0599#if defined(OS_WIN) || defined(OS_MACOSX)
[email protected]fa8a2b1b2009-05-23 03:09:52100 IPC_MESSAGE_HANDLER(ViewHostMsg_DidGetPrintedPagesCount,
101 OnDidGetPrintedPagesCount)
102 IPC_MESSAGE_HANDLER(ViewHostMsg_DidPrintPage, OnDidPrintPage)
[email protected]b23aee0f2009-10-13 22:54:12103#endif
104#if defined(OS_WIN)
[email protected]fa8a2b1b2009-05-23 03:09:52105 IPC_MESSAGE_HANDLER(ViewHostMsg_DuplicateSection, OnDuplicateSection)
[email protected]da911552009-04-16 05:30:54106#endif
[email protected]b23aee0f2009-10-13 22:54:12107#if defined(OS_MACOSX)
108 IPC_MESSAGE_HANDLER(ViewHostMsg_AllocatePDFTransport,
109 OnAllocatePDFTransport)
110#endif
[email protected]15d890b1a2010-05-24 20:01:07111#if defined(OS_LINUX)
112 IPC_MESSAGE_HANDLER(ViewHostMsg_AllocateTempFileForPrinting,
113 OnAllocateTempFileForPrinting)
114 IPC_MESSAGE_HANDLER(ViewHostMsg_TempFileForPrintingWritten,
115 OnTempFileForPrintingWritten)
116#endif
[email protected]81a34412009-01-05 19:17:24117 IPC_MESSAGE_UNHANDLED(handled = false)
118 IPC_END_MESSAGE_MAP_EX()
119}
120
121// The Widget expects to be returned valid route_id.
122void MockRenderThread::OnMsgCreateWidget(int opener_id,
[email protected]3e2b375b2010-04-07 17:03:12123 WebKit::WebPopupType popup_type,
[email protected]81a34412009-01-05 19:17:24124 int* route_id) {
125 opener_id_ = opener_id;
126 *route_id = routing_id_;
127}
[email protected]75e5a872009-04-02 23:56:11128
129void MockRenderThread::OnMsgOpenChannelToExtension(
[email protected]ea4d0790d2009-10-09 18:13:27130 int routing_id, const std::string& source_extension_id,
131 const std::string& target_extension_id,
[email protected]29335802009-07-13 20:38:56132 const std::string& channel_name, int* port_id) {
133 *port_id = 0;
[email protected]75e5a872009-04-02 23:56:11134}
[email protected]da911552009-04-16 05:30:54135
[email protected]b23aee0f2009-10-13 22:54:12136#if defined(OS_WIN)
[email protected]fa8a2b1b2009-05-23 03:09:52137void MockRenderThread::OnDuplicateSection(
138 base::SharedMemoryHandle renderer_handle,
139 base::SharedMemoryHandle* browser_handle) {
140 // We don't have to duplicate the input handles since RenderViewTest does not
141 // separate a browser process from a renderer process.
142 *browser_handle = renderer_handle;
143}
[email protected]b23aee0f2009-10-13 22:54:12144#endif
145
146#if defined(OS_MACOSX)
147void MockRenderThread::OnAllocatePDFTransport(
[email protected]b5ab3982010-02-16 23:58:27148 uint32 buffer_size, base::SharedMemoryHandle* handle) {
[email protected]b23aee0f2009-10-13 22:54:12149 base::SharedMemory shared_buf;
150 shared_buf.Create(L"", false, false, buffer_size);
151 if (!shared_buf.Map(buffer_size)) {
152 *handle = base::SharedMemory::NULLHandle();
153 NOTREACHED() << "Cannot map PDF transport buffer";
154 return;
155 }
156 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), handle);
157}
158#endif
[email protected]fa8a2b1b2009-05-23 03:09:52159
[email protected]15d890b1a2010-05-24 20:01:07160#if defined(OS_LINUX)
161void MockRenderThread::OnAllocateTempFileForPrinting(
162 base::FileDescriptor* renderer_fd,
163 int* browser_fd) {
164 renderer_fd->fd = *browser_fd = -1;
165 renderer_fd->auto_close = false;
166
167 FilePath path;
168 if (file_util::CreateTemporaryFile(&path)) {
169 int fd = open(path.value().c_str(), O_WRONLY);
170 DCHECK_GE(fd, 0);
171 renderer_fd->fd = *browser_fd = fd;
172 }
173}
174
175void MockRenderThread::OnTempFileForPrintingWritten(int browser_fd) {
176 close(browser_fd);
177}
178#endif
179
[email protected]da911552009-04-16 05:30:54180void MockRenderThread::OnGetDefaultPrintSettings(ViewMsg_Print_Params* params) {
[email protected]fa8a2b1b2009-05-23 03:09:52181 if (printer_.get())
182 printer_->GetDefaultPrintSettings(params);
[email protected]da911552009-04-16 05:30:54183}
184
[email protected]1b9af292009-07-09 04:30:30185void MockRenderThread::OnScriptedPrint(
186 const ViewHostMsg_ScriptedPrint_Params& params,
187 ViewMsg_PrintPages_Params* settings) {
[email protected]c8ad40c2009-06-08 17:05:21188 if (printer_.get()) {
[email protected]1b9af292009-07-09 04:30:30189 printer_->ScriptedPrint(params.cookie,
190 params.expected_pages_count,
191 params.has_selection,
[email protected]c8ad40c2009-06-08 17:05:21192 settings);
193 }
[email protected]fa8a2b1b2009-05-23 03:09:52194}
195
196void MockRenderThread::OnDidGetPrintedPagesCount(int cookie, int number_pages) {
197 if (printer_.get())
198 printer_->SetPrintedPagesCount(cookie, number_pages);
199}
200
201void MockRenderThread::OnDidPrintPage(
202 const ViewHostMsg_DidPrintPage_Params& params) {
203 if (printer_.get())
204 printer_->PrintPage(params);
[email protected]da911552009-04-16 05:30:54205}