| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/renderer/mock_render_thread.h" | |
| 6 | |
| 7 #include <fcntl.h> | |
| 8 | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/process_util.h" | |
| 11 #include "chrome/common/extensions/extension_messages.h" | |
| 12 #include "chrome/common/print_messages.h" | |
| 13 #include "chrome/common/render_messages.h" | |
| 14 #include "chrome/common/url_constants.h" | |
| 15 #include "content/common/view_messages.h" | |
| 16 #include "ipc/ipc_message_utils.h" | |
| 17 #include "ipc/ipc_sync_message.h" | |
| 18 #include "printing/print_job_constants.h" | |
| 19 #include "printing/page_range.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 MockRenderThread::MockRenderThread() | |
| 23 : routing_id_(0), | |
| 24 opener_id_(0), | |
| 25 widget_(NULL), | |
| 26 reply_deserializer_(NULL), | |
| 27 printer_(new MockPrinter), | |
| 28 print_dialog_user_response_(true), | |
| 29 print_preview_cancel_page_number_(-1), | |
| 30 print_preview_pages_remaining_(0) { | |
| 31 } | |
| 32 | |
| 33 MockRenderThread::~MockRenderThread() { | |
| 34 } | |
| 35 | |
| 36 // Called by the Widget. Used to send messages to the browser. | |
| 37 // We short-circuit the mechanism and handle the messages right here on this | |
| 38 // class. | |
| 39 bool MockRenderThread::Send(IPC::Message* msg) { | |
| 40 // We need to simulate a synchronous channel, thus we are going to receive | |
| 41 // through this function messages, messages with reply and reply messages. | |
| 42 // We can only handle one synchronous message at a time. | |
| 43 if (msg->is_reply()) { | |
| 44 if (reply_deserializer_.get()) { | |
| 45 reply_deserializer_->SerializeOutputParameters(*msg); | |
| 46 reply_deserializer_.reset(); | |
| 47 } | |
| 48 } else { | |
| 49 if (msg->is_sync()) { | |
| 50 // We actually need to handle deleting the reply deserializer for sync | |
| 51 // messages. | |
| 52 reply_deserializer_.reset( | |
| 53 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer()); | |
| 54 } | |
| 55 OnMessageReceived(*msg); | |
| 56 } | |
| 57 delete msg; | |
| 58 return true; | |
| 59 } | |
| 60 | |
| 61 MessageLoop* MockRenderThread::GetMessageLoop() { | |
| 62 return NULL; | |
| 63 } | |
| 64 | |
| 65 IPC::SyncChannel* MockRenderThread::GetChannel() { | |
| 66 return NULL; | |
| 67 } | |
| 68 | |
| 69 std::string MockRenderThread::GetLocale() { | |
| 70 return std::string(); | |
| 71 } | |
| 72 | |
| 73 void MockRenderThread::AddRoute(int32 routing_id, | |
| 74 IPC::Channel::Listener* listener) { | |
| 75 EXPECT_EQ(routing_id_, routing_id); | |
| 76 widget_ = listener; | |
| 77 } | |
| 78 | |
| 79 void MockRenderThread::RemoveRoute(int32 routing_id) { | |
| 80 EXPECT_EQ(routing_id_, routing_id); | |
| 81 widget_ = NULL; | |
| 82 } | |
| 83 | |
| 84 void MockRenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { | |
| 85 filter->OnFilterAdded(&sink()); | |
| 86 } | |
| 87 | |
| 88 void MockRenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { | |
| 89 filter->OnFilterRemoved(); | |
| 90 } | |
| 91 | |
| 92 void MockRenderThread::SetOutgoingMessageFilter( | |
| 93 IPC::ChannelProxy::OutgoingMessageFilter* filter) { | |
| 94 } | |
| 95 | |
| 96 void MockRenderThread::AddObserver(content::RenderProcessObserver* observer) { | |
| 97 } | |
| 98 | |
| 99 void MockRenderThread::RemoveObserver( | |
| 100 content::RenderProcessObserver* observer) { | |
| 101 } | |
| 102 | |
| 103 void MockRenderThread::SetResourceDispatcherDelegate( | |
| 104 content::ResourceDispatcherDelegate* delegate) { | |
| 105 } | |
| 106 | |
| 107 void MockRenderThread::WidgetHidden() { | |
| 108 } | |
| 109 | |
| 110 void MockRenderThread::WidgetRestored() { | |
| 111 } | |
| 112 | |
| 113 void MockRenderThread::EnsureWebKitInitialized() { | |
| 114 } | |
| 115 | |
| 116 void MockRenderThread::RecordUserMetrics(const std::string& action) { | |
| 117 } | |
| 118 | |
| 119 base::SharedMemoryHandle MockRenderThread::HostAllocateSharedMemoryBuffer( | |
| 120 uint32 buffer_size) { | |
| 121 base::SharedMemory shared_buf; | |
| 122 if (!shared_buf.CreateAndMapAnonymous(buffer_size)) { | |
| 123 NOTREACHED() << "Cannot map shared memory buffer"; | |
| 124 return base::SharedMemory::NULLHandle(); | |
| 125 } | |
| 126 base::SharedMemoryHandle handle; | |
| 127 shared_buf.GiveToProcess(base::GetCurrentProcessHandle(), &handle); | |
| 128 return handle; | |
| 129 } | |
| 130 | |
| 131 void MockRenderThread::RegisterExtension(v8::Extension* extension) { | |
| 132 } | |
| 133 | |
| 134 bool MockRenderThread::IsRegisteredExtension( | |
| 135 const std::string& v8_extension_name) const { | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 void MockRenderThread::ScheduleIdleHandler(double initial_delay_s) { | |
| 140 } | |
| 141 | |
| 142 void MockRenderThread::IdleHandler() { | |
| 143 } | |
| 144 | |
| 145 double MockRenderThread::GetIdleNotificationDelayInS() const { | |
| 146 return 0.0; | |
| 147 } | |
| 148 | |
| 149 void MockRenderThread::SetIdleNotificationDelayInS( | |
| 150 double idle_notification_delay_in_s) { | |
| 151 } | |
| 152 | |
| 153 #if defined(OS_WIN) | |
| 154 void MockRenderThread::PreCacheFont(const LOGFONT& log_font) { | |
| 155 } | |
| 156 | |
| 157 void MockRenderThread::ReleaseCachedFonts() { | |
| 158 } | |
| 159 | |
| 160 #endif // OS_WIN | |
| 161 | |
| 162 void MockRenderThread::SendCloseMessage() { | |
| 163 ViewMsg_Close msg(routing_id_); | |
| 164 widget_->OnMessageReceived(msg); | |
| 165 } | |
| 166 | |
| 167 bool MockRenderThread::OnMessageReceived(const IPC::Message& msg) { | |
| 168 // Save the message in the sink. | |
| 169 sink_.OnMessageReceived(msg); | |
| 170 | |
| 171 // Some messages we do special handling. | |
| 172 bool handled = true; | |
| 173 bool msg_is_ok = true; | |
| 174 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok) | |
| 175 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget) | |
| 176 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension, | |
| 177 OnMsgOpenChannelToExtension) | |
| 178 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings, | |
| 179 OnGetDefaultPrintSettings) | |
| 180 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint) | |
| 181 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings) | |
| 182 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, | |
| 183 OnDidGetPrintedPagesCount) | |
| 184 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | |
| 185 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | |
| 186 OnDidGetPreviewPageCount) | |
| 187 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) | |
| 188 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel) | |
| 189 #if defined(OS_WIN) | |
| 190 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection) | |
| 191 #endif | |
| 192 #if defined(OS_CHROMEOS) | |
| 193 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting, | |
| 194 OnAllocateTempFileForPrinting) | |
| 195 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten, | |
| 196 OnTempFileForPrintingWritten) | |
| 197 #endif | |
| 198 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 199 IPC_END_MESSAGE_MAP_EX() | |
| 200 return handled; | |
| 201 } | |
| 202 | |
| 203 // The Widget expects to be returned valid route_id. | |
| 204 void MockRenderThread::OnMsgCreateWidget(int opener_id, | |
| 205 WebKit::WebPopupType popup_type, | |
| 206 int* route_id) { | |
| 207 opener_id_ = opener_id; | |
| 208 *route_id = routing_id_; | |
| 209 } | |
| 210 | |
| 211 void MockRenderThread::OnMsgOpenChannelToExtension( | |
| 212 int routing_id, const std::string& source_extension_id, | |
| 213 const std::string& target_extension_id, | |
| 214 const std::string& channel_name, int* port_id) { | |
| 215 *port_id = 0; | |
| 216 } | |
| 217 | |
| 218 #if defined(OS_WIN) | |
| 219 void MockRenderThread::OnDuplicateSection( | |
| 220 base::SharedMemoryHandle renderer_handle, | |
| 221 base::SharedMemoryHandle* browser_handle) { | |
| 222 // We don't have to duplicate the input handles since RenderViewTest does not | |
| 223 // separate a browser process from a renderer process. | |
| 224 *browser_handle = renderer_handle; | |
| 225 } | |
| 226 #endif // defined(OS_WIN) | |
| 227 | |
| 228 #if defined(OS_CHROMEOS) | |
| 229 void MockRenderThread::OnAllocateTempFileForPrinting( | |
| 230 base::FileDescriptor* renderer_fd, | |
| 231 int* browser_fd) { | |
| 232 renderer_fd->fd = *browser_fd = -1; | |
| 233 renderer_fd->auto_close = false; | |
| 234 | |
| 235 FilePath path; | |
| 236 if (file_util::CreateTemporaryFile(&path)) { | |
| 237 int fd = open(path.value().c_str(), O_WRONLY); | |
| 238 DCHECK_GE(fd, 0); | |
| 239 renderer_fd->fd = *browser_fd = fd; | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 void MockRenderThread::OnTempFileForPrintingWritten(int browser_fd) { | |
| 244 close(browser_fd); | |
| 245 } | |
| 246 #endif // defined(OS_CHROMEOS) | |
| 247 | |
| 248 void MockRenderThread::OnGetDefaultPrintSettings( | |
| 249 PrintMsg_Print_Params* params) { | |
| 250 if (printer_.get()) | |
| 251 printer_->GetDefaultPrintSettings(params); | |
| 252 } | |
| 253 | |
| 254 void MockRenderThread::OnScriptedPrint( | |
| 255 const PrintHostMsg_ScriptedPrint_Params& params, | |
| 256 PrintMsg_PrintPages_Params* settings) { | |
| 257 if (print_dialog_user_response_ && printer_.get()) { | |
| 258 printer_->ScriptedPrint(params.cookie, | |
| 259 params.expected_pages_count, | |
| 260 params.has_selection, | |
| 261 settings); | |
| 262 } | |
| 263 } | |
| 264 | |
| 265 void MockRenderThread::OnDidGetPrintedPagesCount(int cookie, int number_pages) { | |
| 266 if (printer_.get()) | |
| 267 printer_->SetPrintedPagesCount(cookie, number_pages); | |
| 268 } | |
| 269 | |
| 270 void MockRenderThread::OnDidPrintPage( | |
| 271 const PrintHostMsg_DidPrintPage_Params& params) { | |
| 272 if (printer_.get()) | |
| 273 printer_->PrintPage(params); | |
| 274 } | |
| 275 | |
| 276 void MockRenderThread::OnDidGetPreviewPageCount( | |
| 277 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | |
| 278 print_preview_pages_remaining_ = params.page_count; | |
| 279 } | |
| 280 | |
| 281 void MockRenderThread::OnDidPreviewPage( | |
| 282 const PrintHostMsg_DidPreviewPage_Params& params) { | |
| 283 DCHECK(params.page_number >= printing::FIRST_PAGE_INDEX); | |
| 284 print_preview_pages_remaining_--; | |
| 285 } | |
| 286 | |
| 287 void MockRenderThread::OnCheckForCancel(const std::string& preview_ui_addr, | |
| 288 int preview_request_id, | |
| 289 bool* cancel) { | |
| 290 *cancel = | |
| 291 (print_preview_pages_remaining_ == print_preview_cancel_page_number_); | |
| 292 } | |
| 293 | |
| 294 void MockRenderThread::OnUpdatePrintSettings( | |
| 295 int document_cookie, | |
| 296 const base::DictionaryValue& job_settings, | |
| 297 PrintMsg_PrintPages_Params* params) { | |
| 298 // Check and make sure the required settings are all there. | |
| 299 // We don't actually care about the values. | |
| 300 std::string dummy_string; | |
| 301 if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) || | |
| 302 !job_settings.GetBoolean(printing::kSettingCollate, NULL) || | |
| 303 !job_settings.GetInteger(printing::kSettingColor, NULL) || | |
| 304 !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) || | |
| 305 !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) || | |
| 306 !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) || | |
| 307 !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) || | |
| 308 !job_settings.GetInteger(printing::kSettingCopies, NULL) || | |
| 309 !job_settings.GetString(printing::kPreviewUIAddr, &dummy_string) || | |
| 310 !job_settings.GetInteger(printing::kPreviewRequestID, NULL)) { | |
| 311 return; | |
| 312 } | |
| 313 | |
| 314 // Just return the default settings. | |
| 315 if (printer_.get()) { | |
| 316 ListValue* page_range_array; | |
| 317 printing::PageRanges new_ranges; | |
| 318 if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) { | |
| 319 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { | |
| 320 base::DictionaryValue* dict; | |
| 321 if (!page_range_array->GetDictionary(index, &dict)) | |
| 322 continue; | |
| 323 printing::PageRange range; | |
| 324 if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) || | |
| 325 !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) { | |
| 326 continue; | |
| 327 } | |
| 328 // Page numbers are 1-based in the dictionary. | |
| 329 // Page numbers are 0-based for the printing context. | |
| 330 range.from--; | |
| 331 range.to--; | |
| 332 new_ranges.push_back(range); | |
| 333 } | |
| 334 } | |
| 335 std::vector<int> pages(printing::PageRange::GetPages(new_ranges)); | |
| 336 printer_->UpdateSettings(document_cookie, params, pages); | |
| 337 } | |
| 338 } | |
| 339 | |
| 340 void MockRenderThread::set_print_dialog_user_response(bool response) { | |
| 341 print_dialog_user_response_ = response; | |
| 342 } | |
| 343 | |
| 344 void MockRenderThread::set_print_preview_cancel_page_number(int page) { | |
| 345 print_preview_cancel_page_number_ = page; | |
| 346 } | |
| 347 | |
| 348 int MockRenderThread::print_preview_pages_remaining() { | |
| 349 return print_preview_pages_remaining_; | |
| 350 } | |
| OLD | NEW |