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