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