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/renderer/mock_printer.h"
11 #include "extensions/common/extension_messages.h"
12 #include "ipc/ipc_sync_message.h"
13 #include "printing/page_range.h"
14 #include "printing/print_job_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 #if defined(OS_CHROMEOS)
20 #include "base/file_util.h"
23 #if defined(ENABLE_PRINTING)
24 #include "chrome/common/print_messages.h"
27 ChromeMockRenderThread::ChromeMockRenderThread()
28 #if defined(ENABLE_PRINTING)
29 : printer_(new MockPrinter
),
30 print_dialog_user_response_(true),
31 print_preview_cancel_page_number_(-1),
32 print_preview_pages_remaining_(0)
37 ChromeMockRenderThread::~ChromeMockRenderThread() {
40 scoped_refptr
<base::MessageLoopProxy
>
41 ChromeMockRenderThread::GetIOMessageLoopProxy() {
42 return io_message_loop_proxy_
;
45 void ChromeMockRenderThread::set_io_message_loop_proxy(
46 const scoped_refptr
<base::MessageLoopProxy
>& proxy
) {
47 io_message_loop_proxy_
= proxy
;
50 bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
51 if (content::MockRenderThread::OnMessageReceived(msg
))
54 // Some messages we do special handling.
56 bool msg_is_ok
= true;
57 IPC_BEGIN_MESSAGE_MAP_EX(ChromeMockRenderThread
, msg
, msg_is_ok
)
58 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension
,
59 OnOpenChannelToExtension
)
60 #if defined(ENABLE_PRINTING)
61 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings
,
62 OnGetDefaultPrintSettings
)
63 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint
, OnScriptedPrint
)
64 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings
, OnUpdatePrintSettings
)
65 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount
,
66 OnDidGetPrintedPagesCount
)
67 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage
, OnDidPrintPage
)
68 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount
,
69 OnDidGetPreviewPageCount
)
70 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage
, OnDidPreviewPage
)
71 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel
, OnCheckForCancel
)
73 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection
, OnDuplicateSection
)
75 #if defined(OS_CHROMEOS)
76 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting
,
77 OnAllocateTempFileForPrinting
)
78 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten
,
79 OnTempFileForPrintingWritten
)
80 #endif // defined(OS_CHROMEOS)
81 #endif // defined(ENABLE_PRINTING)
82 IPC_MESSAGE_UNHANDLED(handled
= false)
83 IPC_END_MESSAGE_MAP_EX()
87 void ChromeMockRenderThread::OnOpenChannelToExtension(
89 const ExtensionMsg_ExternalConnectionInfo
& info
,
90 const std::string
& channel_name
,
91 bool include_tls_channel_id
,
96 #if defined(ENABLE_PRINTING)
97 #if defined(OS_CHROMEOS)
98 void ChromeMockRenderThread::OnAllocateTempFileForPrinting(
100 base::FileDescriptor
* renderer_fd
,
102 renderer_fd
->fd
= *browser_fd
= -1;
103 renderer_fd
->auto_close
= false;
106 if (base::CreateTemporaryFile(&path
)) {
107 int fd
= open(path
.value().c_str(), O_WRONLY
);
109 renderer_fd
->fd
= *browser_fd
= fd
;
113 void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id
,
117 #endif // defined(OS_CHROMEOS)
119 void ChromeMockRenderThread::OnGetDefaultPrintSettings(
120 PrintMsg_Print_Params
* params
) {
121 printer_
->GetDefaultPrintSettings(params
);
124 void ChromeMockRenderThread::OnScriptedPrint(
125 const PrintHostMsg_ScriptedPrint_Params
& params
,
126 PrintMsg_PrintPages_Params
* settings
) {
127 if (print_dialog_user_response_
) {
128 printer_
->ScriptedPrint(params
.cookie
,
129 params
.expected_pages_count
,
130 params
.has_selection
,
135 void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
136 int cookie
, int number_pages
) {
137 printer_
->SetPrintedPagesCount(cookie
, number_pages
);
140 void ChromeMockRenderThread::OnDidPrintPage(
141 const PrintHostMsg_DidPrintPage_Params
& params
) {
142 printer_
->PrintPage(params
);
145 void ChromeMockRenderThread::OnDidGetPreviewPageCount(
146 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
) {
147 print_preview_pages_remaining_
= params
.page_count
;
150 void ChromeMockRenderThread::OnDidPreviewPage(
151 const PrintHostMsg_DidPreviewPage_Params
& params
) {
152 DCHECK_GE(params
.page_number
, printing::FIRST_PAGE_INDEX
);
153 print_preview_pages_remaining_
--;
156 void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id
,
157 int preview_request_id
,
160 (print_preview_pages_remaining_
== print_preview_cancel_page_number_
);
163 void ChromeMockRenderThread::OnUpdatePrintSettings(
165 const base::DictionaryValue
& job_settings
,
166 PrintMsg_PrintPages_Params
* params
) {
167 // Check and make sure the required settings are all there.
168 // We don't actually care about the values.
169 std::string dummy_string
;
170 int margins_type
= 0;
171 if (!job_settings
.GetBoolean(printing::kSettingLandscape
, NULL
) ||
172 !job_settings
.GetBoolean(printing::kSettingCollate
, NULL
) ||
173 !job_settings
.GetInteger(printing::kSettingColor
, NULL
) ||
174 !job_settings
.GetBoolean(printing::kSettingPrintToPDF
, NULL
) ||
175 !job_settings
.GetBoolean(printing::kIsFirstRequest
, NULL
) ||
176 !job_settings
.GetString(printing::kSettingDeviceName
, &dummy_string
) ||
177 !job_settings
.GetInteger(printing::kSettingDuplexMode
, NULL
) ||
178 !job_settings
.GetInteger(printing::kSettingCopies
, NULL
) ||
179 !job_settings
.GetInteger(printing::kPreviewUIID
, NULL
) ||
180 !job_settings
.GetInteger(printing::kPreviewRequestID
, NULL
) ||
181 !job_settings
.GetInteger(printing::kSettingMarginsType
, &margins_type
)) {
185 // Just return the default settings.
186 const base::ListValue
* page_range_array
;
187 printing::PageRanges new_ranges
;
188 if (job_settings
.GetList(printing::kSettingPageRange
, &page_range_array
)) {
189 for (size_t index
= 0; index
< page_range_array
->GetSize(); ++index
) {
190 const base::DictionaryValue
* dict
;
191 if (!page_range_array
->GetDictionary(index
, &dict
))
193 printing::PageRange range
;
194 if (!dict
->GetInteger(printing::kSettingPageRangeFrom
, &range
.from
) ||
195 !dict
->GetInteger(printing::kSettingPageRangeTo
, &range
.to
)) {
198 // Page numbers are 1-based in the dictionary.
199 // Page numbers are 0-based for the printing context.
202 new_ranges
.push_back(range
);
205 std::vector
<int> pages(printing::PageRange::GetPages(new_ranges
));
206 printer_
->UpdateSettings(document_cookie
, params
, pages
, margins_type
);
208 job_settings
.GetBoolean(printing::kSettingShouldPrintSelectionOnly
,
209 ¶ms
->params
.selection_only
);
210 job_settings
.GetBoolean(printing::kSettingShouldPrintBackgrounds
,
211 ¶ms
->params
.should_print_backgrounds
);
214 MockPrinter
* ChromeMockRenderThread::printer() {
215 return printer_
.get();
218 void ChromeMockRenderThread::set_print_dialog_user_response(bool response
) {
219 print_dialog_user_response_
= response
;
222 void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page
) {
223 print_preview_cancel_page_number_
= page
;
226 int ChromeMockRenderThread::print_preview_pages_remaining() const {
227 return print_preview_pages_remaining_
;
229 #endif // defined(ENABLE_PRINTING)