1 // Copyright (c) 2012 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/chrome_mock_render_thread.h" 6 7 #include <vector> 8 9 #include "base/values.h" 10 #include "chrome/common/extensions/extension_messages.h" 11 #include "chrome/renderer/mock_printer.h" 12 #include "ipc/ipc_sync_message.h" 13 #include "printing/print_job_constants.h" 14 #include "printing/page_range.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 #if defined(OS_CHROMEOS) 18 #include <fcntl.h> 19 20 #include "base/file_util.h" 21 #endif 22 23 #if defined(ENABLE_PRINTING) 24 #include "chrome/common/print_messages.h" 25 #endif 26 27 ChromeMockRenderThread::ChromeMockRenderThread() 28 #if defined(ENABLE_PRINTING) 29 : printer_(new MockPrinter), 30 print_dialog_user_response_(true), 31 print_preview_cancel_page_number_(-1), 32 print_preview_pages_remaining_(0) 33 #endif 34 { 35 } 36 37 ChromeMockRenderThread::~ChromeMockRenderThread() { 38 } 39 40 bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message& msg) { 41 if (content::MockRenderThread::OnMessageReceived(msg)) 42 return true; 43 44 // Some messages we do special handling. 45 bool handled = true; 46 bool msg_is_ok = true; 47 IPC_BEGIN_MESSAGE_MAP_EX(ChromeMockRenderThread, msg, msg_is_ok) 48 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension, 49 OnOpenChannelToExtension) 50 #if defined(ENABLE_PRINTING) 51 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings, 52 OnGetDefaultPrintSettings) 53 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint, OnScriptedPrint) 54 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings, OnUpdatePrintSettings) 55 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount, 56 OnDidGetPrintedPagesCount) 57 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) 58 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, 59 OnDidGetPreviewPageCount) 60 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) 61 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel, OnCheckForCancel) 62 #if defined(OS_WIN) 63 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection, OnDuplicateSection) 64 #endif 65 #if defined(OS_CHROMEOS) 66 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting, 67 OnAllocateTempFileForPrinting) 68 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten, 69 OnTempFileForPrintingWritten) 70 #endif // defined(OS_CHROMEOS) 71 #endif // defined(ENABLE_PRINTING) 72 IPC_MESSAGE_UNHANDLED(handled = false) 73 IPC_END_MESSAGE_MAP_EX() 74 return handled; 75 } 76 77 void ChromeMockRenderThread::OnOpenChannelToExtension( 78 int routing_id, 79 const ExtensionMsg_ExternalConnectionInfo& info, 80 const std::string& channel_name, 81 int* port_id) { 82 *port_id = 0; 83 } 84 85 #if defined(ENABLE_PRINTING) 86 #if defined(OS_CHROMEOS) 87 void ChromeMockRenderThread::OnAllocateTempFileForPrinting( 88 int render_view_id, 89 base::FileDescriptor* renderer_fd, 90 int* browser_fd) { 91 renderer_fd->fd = *browser_fd = -1; 92 renderer_fd->auto_close = false; 93 94 base::FilePath path; 95 if (file_util::CreateTemporaryFile(&path)) { 96 int fd = open(path.value().c_str(), O_WRONLY); 97 DCHECK_GE(fd, 0); 98 renderer_fd->fd = *browser_fd = fd; 99 } 100 } 101 102 void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id, 103 int browser_fd) { 104 close(browser_fd); 105 } 106 #endif // defined(OS_CHROMEOS) 107 108 void ChromeMockRenderThread::OnGetDefaultPrintSettings( 109 PrintMsg_Print_Params* params) { 110 printer_->GetDefaultPrintSettings(params); 111 } 112 113 void ChromeMockRenderThread::OnScriptedPrint( 114 const PrintHostMsg_ScriptedPrint_Params& params, 115 PrintMsg_PrintPages_Params* settings) { 116 if (print_dialog_user_response_) { 117 printer_->ScriptedPrint(params.cookie, 118 params.expected_pages_count, 119 params.has_selection, 120 settings); 121 } 122 } 123 124 void ChromeMockRenderThread::OnDidGetPrintedPagesCount( 125 int cookie, int number_pages) { 126 printer_->SetPrintedPagesCount(cookie, number_pages); 127 } 128 129 void ChromeMockRenderThread::OnDidPrintPage( 130 const PrintHostMsg_DidPrintPage_Params& params) { 131 printer_->PrintPage(params); 132 } 133 134 void ChromeMockRenderThread::OnDidGetPreviewPageCount( 135 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { 136 print_preview_pages_remaining_ = params.page_count; 137 } 138 139 void ChromeMockRenderThread::OnDidPreviewPage( 140 const PrintHostMsg_DidPreviewPage_Params& params) { 141 DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX); 142 print_preview_pages_remaining_--; 143 } 144 145 void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id, 146 int preview_request_id, 147 bool* cancel) { 148 *cancel = 149 (print_preview_pages_remaining_ == print_preview_cancel_page_number_); 150 } 151 152 void ChromeMockRenderThread::OnUpdatePrintSettings( 153 int document_cookie, 154 const base::DictionaryValue& job_settings, 155 PrintMsg_PrintPages_Params* params) { 156 // Check and make sure the required settings are all there. 157 // We don't actually care about the values. 158 std::string dummy_string; 159 int margins_type = 0; 160 if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) || 161 !job_settings.GetBoolean(printing::kSettingCollate, NULL) || 162 !job_settings.GetInteger(printing::kSettingColor, NULL) || 163 !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) || 164 !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) || 165 !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) || 166 !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) || 167 !job_settings.GetInteger(printing::kSettingCopies, NULL) || 168 !job_settings.GetInteger(printing::kPreviewUIID, NULL) || 169 !job_settings.GetInteger(printing::kPreviewRequestID, NULL) || 170 !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) { 171 return; 172 } 173 174 // Just return the default settings. 175 const ListValue* page_range_array; 176 printing::PageRanges new_ranges; 177 if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) { 178 for (size_t index = 0; index < page_range_array->GetSize(); ++index) { 179 const base::DictionaryValue* dict; 180 if (!page_range_array->GetDictionary(index, &dict)) 181 continue; 182 printing::PageRange range; 183 if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) || 184 !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) { 185 continue; 186 } 187 // Page numbers are 1-based in the dictionary. 188 // Page numbers are 0-based for the printing context. 189 range.from--; 190 range.to--; 191 new_ranges.push_back(range); 192 } 193 } 194 std::vector<int> pages(printing::PageRange::GetPages(new_ranges)); 195 printer_->UpdateSettings(document_cookie, params, pages, margins_type); 196 197 job_settings.GetBoolean(printing::kSettingShouldPrintSelectionOnly, 198 ¶ms->params.selection_only); 199 job_settings.GetBoolean(printing::kSettingShouldPrintBackgrounds, 200 ¶ms->params.should_print_backgrounds); 201 } 202 203 MockPrinter* ChromeMockRenderThread::printer() { 204 return printer_.get(); 205 } 206 207 void ChromeMockRenderThread::set_print_dialog_user_response(bool response) { 208 print_dialog_user_response_ = response; 209 } 210 211 void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page) { 212 print_preview_cancel_page_number_ = page; 213 } 214 215 int ChromeMockRenderThread::print_preview_pages_remaining() const { 216 return print_preview_pages_remaining_; 217 } 218 #endif // defined(ENABLE_PRINTING) 219