blob: 79d7361ac4b0d9ccdb33ed94334ef7897ec5f227 [file] [log] [blame]
[email protected]a61890e2012-07-27 22:27:111// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c6d068ff2011-10-14 17:28:232// 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]116d0962012-08-24 23:22:287#include <vector>
[email protected]c6d068ff2011-10-14 17:28:238
[email protected]116d0962012-08-24 23:22:289#include "base/values.h"
[email protected]c6d068ff2011-10-14 17:28:2310#include "chrome/common/extensions/extension_messages.h"
11#include "chrome/common/print_messages.h"
[email protected]116d0962012-08-24 23:22:2812#include "chrome/renderer/mock_printer.h"
[email protected]c6d068ff2011-10-14 17:28:2313#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]116d0962012-08-24 23:22:2818#if defined(OS_CHROMEOS)
19#include <fcntl.h>
20
21#include "base/file_util.h"
22#endif
23
[email protected]c6d068ff2011-10-14 17:28:2324ChromeMockRenderThread::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
31ChromeMockRenderThread::~ChromeMockRenderThread() {
32}
33
34bool 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]3d9ec5052013-01-02 22:05:2543 OnOpenChannelToExtension)
[email protected]c6d068ff2011-10-14 17:28:2344 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]3d9ec5052013-01-02 22:05:2569void ChromeMockRenderThread::OnOpenChannelToExtension(
[email protected]116d0962012-08-24 23:22:2870 int routing_id,
[email protected]686914f2013-04-25 04:54:5871 const ExtensionMsg_ExternalConnectionInfo& info,
[email protected]116d0962012-08-24 23:22:2872 const std::string& channel_name,
73 int* port_id) {
[email protected]c6d068ff2011-10-14 17:28:2374 *port_id = 0;
75}
76
77#if defined(OS_CHROMEOS)
78void 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]3ea1b182013-02-08 22:38:4184 base::FilePath path;
[email protected]c6d068ff2011-10-14 17:28:2385 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]b5b79d72012-05-24 19:42:2892void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id,
93 int browser_fd) {
[email protected]c6d068ff2011-10-14 17:28:2394 close(browser_fd);
95}
96#endif // defined(OS_CHROMEOS)
97
98void ChromeMockRenderThread::OnGetDefaultPrintSettings(
99 PrintMsg_Print_Params* params) {
[email protected]116d0962012-08-24 23:22:28100 printer_->GetDefaultPrintSettings(params);
[email protected]c6d068ff2011-10-14 17:28:23101}
102
103void ChromeMockRenderThread::OnScriptedPrint(
104 const PrintHostMsg_ScriptedPrint_Params& params,
105 PrintMsg_PrintPages_Params* settings) {
[email protected]116d0962012-08-24 23:22:28106 if (print_dialog_user_response_) {
[email protected]c6d068ff2011-10-14 17:28:23107 printer_->ScriptedPrint(params.cookie,
108 params.expected_pages_count,
109 params.has_selection,
110 settings);
111 }
112}
113
114void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
115 int cookie, int number_pages) {
[email protected]116d0962012-08-24 23:22:28116 printer_->SetPrintedPagesCount(cookie, number_pages);
[email protected]c6d068ff2011-10-14 17:28:23117}
118
119void ChromeMockRenderThread::OnDidPrintPage(
120 const PrintHostMsg_DidPrintPage_Params& params) {
[email protected]116d0962012-08-24 23:22:28121 printer_->PrintPage(params);
[email protected]c6d068ff2011-10-14 17:28:23122}
123
124void ChromeMockRenderThread::OnDidGetPreviewPageCount(
125 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
126 print_preview_pages_remaining_ = params.page_count;
127}
128
129void ChromeMockRenderThread::OnDidPreviewPage(
130 const PrintHostMsg_DidPreviewPage_Params& params) {
[email protected]116d0962012-08-24 23:22:28131 DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX);
[email protected]c6d068ff2011-10-14 17:28:23132 print_preview_pages_remaining_--;
133}
134
[email protected]116d0962012-08-24 23:22:28135void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id,
136 int preview_request_id,
137 bool* cancel) {
[email protected]c6d068ff2011-10-14 17:28:23138 *cancel =
139 (print_preview_pages_remaining_ == print_preview_cancel_page_number_);
140}
141
142void 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]732b8132012-01-10 23:17:32149 int margins_type = 0;
[email protected]c6d068ff2011-10-14 17:28:23150 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]116d0962012-08-24 23:22:28158 !job_settings.GetInteger(printing::kPreviewUIID, NULL) ||
[email protected]732b8132012-01-10 23:17:32159 !job_settings.GetInteger(printing::kPreviewRequestID, NULL) ||
160 !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) {
[email protected]c6d068ff2011-10-14 17:28:23161 return;
162 }
163
164 // Just return the default settings.
[email protected]116d0962012-08-24 23:22:28165 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]c6d068ff2011-10-14 17:28:23176 }
[email protected]116d0962012-08-24 23:22:28177 // 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]c6d068ff2011-10-14 17:28:23182 }
[email protected]c6d068ff2011-10-14 17:28:23183 }
[email protected]116d0962012-08-24 23:22:28184 std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
185 printer_->UpdateSettings(document_cookie, params, pages, margins_type);
[email protected]edb363a82013-01-29 12:11:29186
187 job_settings.GetBoolean(printing::kSettingShouldPrintSelectionOnly,
188 &params->params.selection_only);
189 job_settings.GetBoolean(printing::kSettingShouldPrintBackgrounds,
190 &params->params.should_print_backgrounds);
[email protected]116d0962012-08-24 23:22:28191}
192
193MockPrinter* ChromeMockRenderThread::printer() {
194 return printer_.get();
[email protected]c6d068ff2011-10-14 17:28:23195}
196
197void ChromeMockRenderThread::set_print_dialog_user_response(bool response) {
198 print_dialog_user_response_ = response;
199}
200
201void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page) {
202 print_preview_cancel_page_number_ = page;
203}
204
[email protected]116d0962012-08-24 23:22:28205int ChromeMockRenderThread::print_preview_pages_remaining() const {
[email protected]c6d068ff2011-10-14 17:28:23206 return print_preview_pages_remaining_;
207}