[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 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 "chrome/renderer/mock_render_thread.h" |
| 6 | |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 7 | #include <fcntl.h> |
| 8 | |
| 9 | #include "base/file_util.h" |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 10 | #include "base/process_util.h" |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 11 | #include "chrome/common/render_messages.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 12 | #include "chrome/common/render_messages_params.h" |
[email protected] | 583d45c1 | 2010-08-31 02:48:12 | [diff] [blame] | 13 | #include "chrome/common/url_constants.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 14 | #include "ipc/ipc_message_utils.h" |
[email protected] | a83d4229 | 2010-08-17 22:51:10 | [diff] [blame] | 15 | #include "ipc/ipc_sync_message.h" |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | MockRenderThread::MockRenderThread() |
| 19 | : routing_id_(0), |
| 20 | opener_id_(0), |
| 21 | widget_(NULL), |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 22 | reply_deserializer_(NULL), |
[email protected] | b8f41a19 | 2010-04-19 18:25:04 | [diff] [blame] | 23 | printer_(new MockPrinter), |
| 24 | is_extension_process_(false) { |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | MockRenderThread::~MockRenderThread() { |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 28 | } |
| 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. |
| 32 | void 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. |
| 39 | void MockRenderThread::RemoveRoute(int32 routing_id) { |
| 40 | EXPECT_EQ(routing_id_, routing_id); |
| 41 | widget_ = NULL; |
| 42 | } |
| 43 | |
[email protected] | 90b721e6 | 2010-04-05 17:35:01 | [diff] [blame] | 44 | // Called by, for example, RenderView::Init(), when adding a new message filter. |
| 45 | void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 46 | filter->OnFilterAdded(&sink()); |
| 47 | } |
| 48 | |
| 49 | // Called when the filter is removed. |
| 50 | void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 51 | filter->OnFilterRemoved(); |
| 52 | } |
| 53 | |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 54 | // 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. |
| 57 | bool 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] | 87cfff0 | 2009-01-06 19:18:02 | [diff] [blame] | 62 | if (reply_deserializer_.get()) { |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 63 | reply_deserializer_->SerializeOutputParameters(*msg); |
[email protected] | 87cfff0 | 2009-01-06 19:18:02 | [diff] [blame] | 64 | reply_deserializer_.reset(); |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 65 | } |
| 66 | } else { |
| 67 | if (msg->is_sync()) { |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 68 | // We actually need to handle deleting the reply deserializer for sync |
[email protected] | 87cfff0 | 2009-01-06 19:18:02 | [diff] [blame] | 69 | // messages. |
| 70 | reply_deserializer_.reset( |
| 71 | static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer()); |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 72 | } |
| 73 | OnMessageReceived(*msg); |
| 74 | } |
| 75 | delete msg; |
| 76 | return true; |
| 77 | } |
| 78 | |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 79 | void MockRenderThread::SendCloseMessage() { |
| 80 | ViewMsg_Close msg(routing_id_); |
| 81 | widget_->OnMessageReceived(msg); |
| 82 | } |
| 83 | |
| 84 | void MockRenderThread::OnMessageReceived(const IPC::Message& msg) { |
[email protected] | cde96e1 | 2009-01-17 05:41:48 | [diff] [blame] | 85 | // Save the message in the sink. |
[email protected] | 445623e6 | 2010-03-25 23:20:24 | [diff] [blame] | 86 | sink_.OnMessageReceived(msg); |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 87 | |
| 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] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 92 | IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget) |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 93 | IPC_MESSAGE_HANDLER(ViewHostMsg_OpenChannelToExtension, |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 94 | OnMsgOpenChannelToExtension) |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 95 | IPC_MESSAGE_HANDLER(ViewHostMsg_GetDefaultPrintSettings, |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 96 | OnGetDefaultPrintSettings) |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 97 | IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptedPrint, |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 98 | OnScriptedPrint) |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame^] | 99 | #if defined(OS_WIN) || defined(OS_MACOSX) |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 100 | IPC_MESSAGE_HANDLER(ViewHostMsg_DidGetPrintedPagesCount, |
| 101 | OnDidGetPrintedPagesCount) |
| 102 | IPC_MESSAGE_HANDLER(ViewHostMsg_DidPrintPage, OnDidPrintPage) |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 103 | #endif |
| 104 | #if defined(OS_WIN) |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 105 | IPC_MESSAGE_HANDLER(ViewHostMsg_DuplicateSection, OnDuplicateSection) |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 106 | #endif |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 107 | #if defined(OS_MACOSX) |
| 108 | IPC_MESSAGE_HANDLER(ViewHostMsg_AllocatePDFTransport, |
| 109 | OnAllocatePDFTransport) |
| 110 | #endif |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 111 | #if defined(OS_LINUX) |
| 112 | IPC_MESSAGE_HANDLER(ViewHostMsg_AllocateTempFileForPrinting, |
| 113 | OnAllocateTempFileForPrinting) |
| 114 | IPC_MESSAGE_HANDLER(ViewHostMsg_TempFileForPrintingWritten, |
| 115 | OnTempFileForPrintingWritten) |
| 116 | #endif |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 117 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 118 | IPC_END_MESSAGE_MAP_EX() |
| 119 | } |
| 120 | |
| 121 | // The Widget expects to be returned valid route_id. |
| 122 | void MockRenderThread::OnMsgCreateWidget(int opener_id, |
[email protected] | 3e2b375b | 2010-04-07 17:03:12 | [diff] [blame] | 123 | WebKit::WebPopupType popup_type, |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 124 | int* route_id) { |
| 125 | opener_id_ = opener_id; |
| 126 | *route_id = routing_id_; |
| 127 | } |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 128 | |
| 129 | void MockRenderThread::OnMsgOpenChannelToExtension( |
[email protected] | ea4d0790d | 2009-10-09 18:13:27 | [diff] [blame] | 130 | int routing_id, const std::string& source_extension_id, |
| 131 | const std::string& target_extension_id, |
[email protected] | 2933580 | 2009-07-13 20:38:56 | [diff] [blame] | 132 | const std::string& channel_name, int* port_id) { |
| 133 | *port_id = 0; |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 134 | } |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 135 | |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 136 | #if defined(OS_WIN) |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 137 | void 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] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | #if defined(OS_MACOSX) |
| 147 | void MockRenderThread::OnAllocatePDFTransport( |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 148 | uint32 buffer_size, base::SharedMemoryHandle* handle) { |
[email protected] | b23aee0f | 2009-10-13 22:54:12 | [diff] [blame] | 149 | 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] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 159 | |
[email protected] | 15d890b1a | 2010-05-24 20:01:07 | [diff] [blame] | 160 | #if defined(OS_LINUX) |
| 161 | void 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 | |
| 175 | void MockRenderThread::OnTempFileForPrintingWritten(int browser_fd) { |
| 176 | close(browser_fd); |
| 177 | } |
| 178 | #endif |
| 179 | |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 180 | void MockRenderThread::OnGetDefaultPrintSettings(ViewMsg_Print_Params* params) { |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 181 | if (printer_.get()) |
| 182 | printer_->GetDefaultPrintSettings(params); |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 183 | } |
| 184 | |
[email protected] | 1b9af29 | 2009-07-09 04:30:30 | [diff] [blame] | 185 | void MockRenderThread::OnScriptedPrint( |
| 186 | const ViewHostMsg_ScriptedPrint_Params& params, |
| 187 | ViewMsg_PrintPages_Params* settings) { |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 188 | if (printer_.get()) { |
[email protected] | 1b9af29 | 2009-07-09 04:30:30 | [diff] [blame] | 189 | printer_->ScriptedPrint(params.cookie, |
| 190 | params.expected_pages_count, |
| 191 | params.has_selection, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 192 | settings); |
| 193 | } |
[email protected] | fa8a2b1b | 2009-05-23 03:09:52 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void MockRenderThread::OnDidGetPrintedPagesCount(int cookie, int number_pages) { |
| 197 | if (printer_.get()) |
| 198 | printer_->SetPrintedPagesCount(cookie, number_pages); |
| 199 | } |
| 200 | |
| 201 | void MockRenderThread::OnDidPrintPage( |
| 202 | const ViewHostMsg_DidPrintPage_Params& params) { |
| 203 | if (printer_.get()) |
| 204 | printer_->PrintPage(params); |
[email protected] | da91155 | 2009-04-16 05:30:54 | [diff] [blame] | 205 | } |