blob: dd72285f729d2bc991f53db2cbba8ec901faa88e [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,
43 OnMsgOpenChannelToExtension)
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
69void ChromeMockRenderThread::OnMsgOpenChannelToExtension(
[email protected]116d0962012-08-24 23:22:2870 int routing_id,
71 const std::string& source_extension_id,
[email protected]c6d068ff2011-10-14 17:28:2372 const std::string& target_extension_id,
[email protected]116d0962012-08-24 23:22:2873 const std::string& channel_name,
74 int* port_id) {
[email protected]c6d068ff2011-10-14 17:28:2375 *port_id = 0;
76}
77
78#if defined(OS_CHROMEOS)
79void ChromeMockRenderThread::OnAllocateTempFileForPrinting(
80 base::FileDescriptor* renderer_fd,
81 int* browser_fd) {
82 renderer_fd->fd = *browser_fd = -1;
83 renderer_fd->auto_close = false;
84
85 FilePath path;
86 if (file_util::CreateTemporaryFile(&path)) {
87 int fd = open(path.value().c_str(), O_WRONLY);
88 DCHECK_GE(fd, 0);
89 renderer_fd->fd = *browser_fd = fd;
90 }
91}
92
[email protected]b5b79d72012-05-24 19:42:2893void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id,
94 int browser_fd) {
[email protected]c6d068ff2011-10-14 17:28:2395 close(browser_fd);
96}
97#endif // defined(OS_CHROMEOS)
98
99void ChromeMockRenderThread::OnGetDefaultPrintSettings(
100 PrintMsg_Print_Params* params) {
[email protected]116d0962012-08-24 23:22:28101 printer_->GetDefaultPrintSettings(params);
[email protected]c6d068ff2011-10-14 17:28:23102}
103
104void ChromeMockRenderThread::OnScriptedPrint(
105 const PrintHostMsg_ScriptedPrint_Params& params,
106 PrintMsg_PrintPages_Params* settings) {
[email protected]116d0962012-08-24 23:22:28107 if (print_dialog_user_response_) {
[email protected]c6d068ff2011-10-14 17:28:23108 printer_->ScriptedPrint(params.cookie,
109 params.expected_pages_count,
110 params.has_selection,
111 settings);
112 }
113}
114
115void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
116 int cookie, int number_pages) {
[email protected]116d0962012-08-24 23:22:28117 printer_->SetPrintedPagesCount(cookie, number_pages);
[email protected]c6d068ff2011-10-14 17:28:23118}
119
120void ChromeMockRenderThread::OnDidPrintPage(
121 const PrintHostMsg_DidPrintPage_Params& params) {
[email protected]116d0962012-08-24 23:22:28122 printer_->PrintPage(params);
[email protected]c6d068ff2011-10-14 17:28:23123}
124
125void ChromeMockRenderThread::OnDidGetPreviewPageCount(
126 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
127 print_preview_pages_remaining_ = params.page_count;
128}
129
130void ChromeMockRenderThread::OnDidPreviewPage(
131 const PrintHostMsg_DidPreviewPage_Params& params) {
[email protected]116d0962012-08-24 23:22:28132 DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX);
[email protected]c6d068ff2011-10-14 17:28:23133 print_preview_pages_remaining_--;
134}
135
[email protected]116d0962012-08-24 23:22:28136void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id,
137 int preview_request_id,
138 bool* cancel) {
[email protected]c6d068ff2011-10-14 17:28:23139 *cancel =
140 (print_preview_pages_remaining_ == print_preview_cancel_page_number_);
141}
142
143void ChromeMockRenderThread::OnUpdatePrintSettings(
144 int document_cookie,
145 const base::DictionaryValue& job_settings,
146 PrintMsg_PrintPages_Params* params) {
147 // Check and make sure the required settings are all there.
148 // We don't actually care about the values.
149 std::string dummy_string;
[email protected]732b8132012-01-10 23:17:32150 int margins_type = 0;
[email protected]c6d068ff2011-10-14 17:28:23151 if (!job_settings.GetBoolean(printing::kSettingLandscape, NULL) ||
152 !job_settings.GetBoolean(printing::kSettingCollate, NULL) ||
153 !job_settings.GetInteger(printing::kSettingColor, NULL) ||
154 !job_settings.GetBoolean(printing::kSettingPrintToPDF, NULL) ||
155 !job_settings.GetBoolean(printing::kIsFirstRequest, NULL) ||
156 !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) ||
157 !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) ||
158 !job_settings.GetInteger(printing::kSettingCopies, NULL) ||
[email protected]116d0962012-08-24 23:22:28159 !job_settings.GetInteger(printing::kPreviewUIID, NULL) ||
[email protected]732b8132012-01-10 23:17:32160 !job_settings.GetInteger(printing::kPreviewRequestID, NULL) ||
161 !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) {
[email protected]c6d068ff2011-10-14 17:28:23162 return;
163 }
164
165 // Just return the default settings.
[email protected]116d0962012-08-24 23:22:28166 const ListValue* page_range_array;
167 printing::PageRanges new_ranges;
168 if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) {
169 for (size_t index = 0; index < page_range_array->GetSize(); ++index) {
170 const base::DictionaryValue* dict;
171 if (!page_range_array->GetDictionary(index, &dict))
172 continue;
173 printing::PageRange range;
174 if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) ||
175 !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) {
176 continue;
[email protected]c6d068ff2011-10-14 17:28:23177 }
[email protected]116d0962012-08-24 23:22:28178 // Page numbers are 1-based in the dictionary.
179 // Page numbers are 0-based for the printing context.
180 range.from--;
181 range.to--;
182 new_ranges.push_back(range);
[email protected]c6d068ff2011-10-14 17:28:23183 }
[email protected]c6d068ff2011-10-14 17:28:23184 }
[email protected]116d0962012-08-24 23:22:28185 std::vector<int> pages(printing::PageRange::GetPages(new_ranges));
186 printer_->UpdateSettings(document_cookie, params, pages, margins_type);
187}
188
189MockPrinter* ChromeMockRenderThread::printer() {
190 return printer_.get();
[email protected]c6d068ff2011-10-14 17:28:23191}
192
193void ChromeMockRenderThread::set_print_dialog_user_response(bool response) {
194 print_dialog_user_response_ = response;
195}
196
197void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page) {
198 print_preview_cancel_page_number_ = page;
199}
200
[email protected]116d0962012-08-24 23:22:28201int ChromeMockRenderThread::print_preview_pages_remaining() const {
[email protected]c6d068ff2011-10-14 17:28:23202 return print_preview_pages_remaining_;
203}