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.
5 #include "chrome/renderer/chrome_mock_render_thread.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/extension_messages.h"
11 #include "chrome/common/print_messages.h"
12 #include "chrome/renderer/mock_printer.h"
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"
18 #if defined(OS_CHROMEOS)
21 #include "base/file_util.h"
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) {
31 ChromeMockRenderThread::~ChromeMockRenderThread() {
34 bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
35 if (content::MockRenderThread::OnMessageReceived(msg
))
38 // Some messages we do special handling.
40 bool msg_is_ok
= true;
41 IPC_BEGIN_MESSAGE_MAP_EX(ChromeMockRenderThread
, msg
, msg_is_ok
)
42 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension
,
43 OnOpenChannelToExtension
)
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
)
56 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection
, OnDuplicateSection
)
58 #if defined(OS_CHROMEOS)
59 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting
,
60 OnAllocateTempFileForPrinting
)
61 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten
,
62 OnTempFileForPrintingWritten
)
64 IPC_MESSAGE_UNHANDLED(handled
= false)
65 IPC_END_MESSAGE_MAP_EX()
69 void ChromeMockRenderThread::OnOpenChannelToExtension(
71 const ExtensionMsg_ExternalConnectionInfo
& info
,
72 const std::string
& channel_name
,
77 #if defined(OS_CHROMEOS)
78 void ChromeMockRenderThread::OnAllocateTempFileForPrinting(
79 base::FileDescriptor
* renderer_fd
,
81 renderer_fd
->fd
= *browser_fd
= -1;
82 renderer_fd
->auto_close
= false;
85 if (file_util::CreateTemporaryFile(&path
)) {
86 int fd
= open(path
.value().c_str(), O_WRONLY
);
88 renderer_fd
->fd
= *browser_fd
= fd
;
92 void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id
,
96 #endif // defined(OS_CHROMEOS)
98 void ChromeMockRenderThread::OnGetDefaultPrintSettings(
99 PrintMsg_Print_Params
* params
) {
100 printer_
->GetDefaultPrintSettings(params
);
103 void ChromeMockRenderThread::OnScriptedPrint(
104 const PrintHostMsg_ScriptedPrint_Params
& params
,
105 PrintMsg_PrintPages_Params
* settings
) {
106 if (print_dialog_user_response_
) {
107 printer_
->ScriptedPrint(params
.cookie
,
108 params
.expected_pages_count
,
109 params
.has_selection
,
114 void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
115 int cookie
, int number_pages
) {
116 printer_
->SetPrintedPagesCount(cookie
, number_pages
);
119 void ChromeMockRenderThread::OnDidPrintPage(
120 const PrintHostMsg_DidPrintPage_Params
& params
) {
121 printer_
->PrintPage(params
);
124 void ChromeMockRenderThread::OnDidGetPreviewPageCount(
125 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
) {
126 print_preview_pages_remaining_
= params
.page_count
;
129 void ChromeMockRenderThread::OnDidPreviewPage(
130 const PrintHostMsg_DidPreviewPage_Params
& params
) {
131 DCHECK_GE(params
.page_number
, printing::FIRST_PAGE_INDEX
);
132 print_preview_pages_remaining_
--;
135 void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id
,
136 int preview_request_id
,
139 (print_preview_pages_remaining_
== print_preview_cancel_page_number_
);
142 void ChromeMockRenderThread::OnUpdatePrintSettings(
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 int margins_type
= 0;
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
) ||
158 !job_settings
.GetInteger(printing::kPreviewUIID
, NULL
) ||
159 !job_settings
.GetInteger(printing::kPreviewRequestID
, NULL
) ||
160 !job_settings
.GetInteger(printing::kSettingMarginsType
, &margins_type
)) {
164 // Just return the default settings.
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
))
172 printing::PageRange range
;
173 if (!dict
->GetInteger(printing::kSettingPageRangeFrom
, &range
.from
) ||
174 !dict
->GetInteger(printing::kSettingPageRangeTo
, &range
.to
)) {
177 // Page numbers are 1-based in the dictionary.
178 // Page numbers are 0-based for the printing context.
181 new_ranges
.push_back(range
);
184 std::vector
<int> pages(printing::PageRange::GetPages(new_ranges
));
185 printer_
->UpdateSettings(document_cookie
, params
, pages
, margins_type
);
187 job_settings
.GetBoolean(printing::kSettingShouldPrintSelectionOnly
,
188 ¶ms
->params
.selection_only
);
189 job_settings
.GetBoolean(printing::kSettingShouldPrintBackgrounds
,
190 ¶ms
->params
.should_print_backgrounds
);
193 MockPrinter
* ChromeMockRenderThread::printer() {
194 return printer_
.get();
197 void ChromeMockRenderThread::set_print_dialog_user_response(bool response
) {
198 print_dialog_user_response_
= response
;
201 void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page
) {
202 print_preview_cancel_page_number_
= page
;
205 int ChromeMockRenderThread::print_preview_pages_remaining() const {
206 return print_preview_pages_remaining_
;