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/printing/mock_printer.h"
11 #include "ipc/ipc_sync_message.h"
12 #include "printing/page_range.h"
13 #include "printing/print_job_constants.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 #if defined(OS_CHROMEOS)
19 #include "base/files/file_util.h"
22 #if defined(ENABLE_EXTENSIONS)
23 #include "extensions/common/extension_messages.h"
26 #if defined(ENABLE_PRINTING)
27 #include "chrome/common/print_messages.h"
30 ChromeMockRenderThread::ChromeMockRenderThread()
31 #if defined(ENABLE_PRINTING)
32 : printer_(new MockPrinter
),
33 print_dialog_user_response_(true),
34 print_preview_cancel_page_number_(-1),
35 print_preview_pages_remaining_(0)
40 ChromeMockRenderThread::~ChromeMockRenderThread() {
43 scoped_refptr
<base::MessageLoopProxy
>
44 ChromeMockRenderThread::GetIOMessageLoopProxy() {
45 return io_message_loop_proxy_
;
48 void ChromeMockRenderThread::set_io_message_loop_proxy(
49 const scoped_refptr
<base::MessageLoopProxy
>& proxy
) {
50 io_message_loop_proxy_
= proxy
;
53 bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message
& msg
) {
54 if (content::MockRenderThread::OnMessageReceived(msg
))
57 // Some messages we do special handling.
59 IPC_BEGIN_MESSAGE_MAP(ChromeMockRenderThread
, msg
)
60 #if defined(ENABLE_EXTENSIONS)
61 IPC_MESSAGE_HANDLER(ExtensionHostMsg_OpenChannelToExtension
,
62 OnOpenChannelToExtension
)
64 #if defined(ENABLE_PRINTING)
65 IPC_MESSAGE_HANDLER(PrintHostMsg_GetDefaultPrintSettings
,
66 OnGetDefaultPrintSettings
)
67 IPC_MESSAGE_HANDLER(PrintHostMsg_ScriptedPrint
, OnScriptedPrint
)
68 IPC_MESSAGE_HANDLER(PrintHostMsg_UpdatePrintSettings
, OnUpdatePrintSettings
)
69 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPrintedPagesCount
,
70 OnDidGetPrintedPagesCount
)
71 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage
, OnDidPrintPage
)
72 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount
,
73 OnDidGetPreviewPageCount
)
74 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage
, OnDidPreviewPage
)
75 IPC_MESSAGE_HANDLER(PrintHostMsg_CheckForCancel
, OnCheckForCancel
)
77 IPC_MESSAGE_HANDLER(PrintHostMsg_DuplicateSection
, OnDuplicateSection
)
79 #if defined(OS_CHROMEOS)
80 IPC_MESSAGE_HANDLER(PrintHostMsg_AllocateTempFileForPrinting
,
81 OnAllocateTempFileForPrinting
)
82 IPC_MESSAGE_HANDLER(PrintHostMsg_TempFileForPrintingWritten
,
83 OnTempFileForPrintingWritten
)
84 #endif // defined(OS_CHROMEOS)
85 #endif // defined(ENABLE_PRINTING)
86 IPC_MESSAGE_UNHANDLED(handled
= false)
91 #if defined(ENABLE_EXTENSIONS)
92 void ChromeMockRenderThread::OnOpenChannelToExtension(
94 const ExtensionMsg_ExternalConnectionInfo
& info
,
95 const std::string
& channel_name
,
96 bool include_tls_channel_id
,
102 #if defined(ENABLE_PRINTING)
103 #if defined(OS_CHROMEOS)
104 void ChromeMockRenderThread::OnAllocateTempFileForPrinting(
106 base::FileDescriptor
* renderer_fd
,
108 renderer_fd
->fd
= *browser_fd
= -1;
109 renderer_fd
->auto_close
= false;
112 if (base::CreateTemporaryFile(&path
)) {
113 int fd
= open(path
.value().c_str(), O_WRONLY
);
115 renderer_fd
->fd
= *browser_fd
= fd
;
119 void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id
,
123 #endif // defined(OS_CHROMEOS)
125 void ChromeMockRenderThread::OnGetDefaultPrintSettings(
126 PrintMsg_Print_Params
* params
) {
127 printer_
->GetDefaultPrintSettings(params
);
130 void ChromeMockRenderThread::OnScriptedPrint(
131 const PrintHostMsg_ScriptedPrint_Params
& params
,
132 PrintMsg_PrintPages_Params
* settings
) {
133 if (print_dialog_user_response_
) {
134 printer_
->ScriptedPrint(params
.cookie
,
135 params
.expected_pages_count
,
136 params
.has_selection
,
141 void ChromeMockRenderThread::OnDidGetPrintedPagesCount(
142 int cookie
, int number_pages
) {
143 printer_
->SetPrintedPagesCount(cookie
, number_pages
);
146 void ChromeMockRenderThread::OnDidPrintPage(
147 const PrintHostMsg_DidPrintPage_Params
& params
) {
148 printer_
->PrintPage(params
);
151 void ChromeMockRenderThread::OnDidGetPreviewPageCount(
152 const PrintHostMsg_DidGetPreviewPageCount_Params
& params
) {
153 print_preview_pages_remaining_
= params
.page_count
;
156 void ChromeMockRenderThread::OnDidPreviewPage(
157 const PrintHostMsg_DidPreviewPage_Params
& params
) {
158 DCHECK_GE(params
.page_number
, printing::FIRST_PAGE_INDEX
);
159 print_preview_pages_remaining_
--;
162 void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id
,
163 int preview_request_id
,
166 (print_preview_pages_remaining_
== print_preview_cancel_page_number_
);
169 void ChromeMockRenderThread::OnUpdatePrintSettings(
171 const base::DictionaryValue
& job_settings
,
172 PrintMsg_PrintPages_Params
* params
,
176 // Check and make sure the required settings are all there.
177 // We don't actually care about the values.
178 std::string dummy_string
;
179 int margins_type
= 0;
180 if (!job_settings
.GetBoolean(printing::kSettingLandscape
, NULL
) ||
181 !job_settings
.GetBoolean(printing::kSettingCollate
, NULL
) ||
182 !job_settings
.GetInteger(printing::kSettingColor
, NULL
) ||
183 !job_settings
.GetBoolean(printing::kSettingPrintToPDF
, NULL
) ||
184 !job_settings
.GetBoolean(printing::kIsFirstRequest
, NULL
) ||
185 !job_settings
.GetString(printing::kSettingDeviceName
, &dummy_string
) ||
186 !job_settings
.GetInteger(printing::kSettingDuplexMode
, NULL
) ||
187 !job_settings
.GetInteger(printing::kSettingCopies
, NULL
) ||
188 !job_settings
.GetInteger(printing::kPreviewUIID
, NULL
) ||
189 !job_settings
.GetInteger(printing::kPreviewRequestID
, NULL
) ||
190 !job_settings
.GetInteger(printing::kSettingMarginsType
, &margins_type
)) {
194 // Just return the default settings.
195 const base::ListValue
* page_range_array
;
196 printing::PageRanges new_ranges
;
197 if (job_settings
.GetList(printing::kSettingPageRange
, &page_range_array
)) {
198 for (size_t index
= 0; index
< page_range_array
->GetSize(); ++index
) {
199 const base::DictionaryValue
* dict
;
200 if (!page_range_array
->GetDictionary(index
, &dict
))
202 printing::PageRange range
;
203 if (!dict
->GetInteger(printing::kSettingPageRangeFrom
, &range
.from
) ||
204 !dict
->GetInteger(printing::kSettingPageRangeTo
, &range
.to
)) {
207 // Page numbers are 1-based in the dictionary.
208 // Page numbers are 0-based for the printing context.
211 new_ranges
.push_back(range
);
214 std::vector
<int> pages(printing::PageRange::GetPages(new_ranges
));
215 printer_
->UpdateSettings(document_cookie
, params
, pages
, margins_type
);
217 job_settings
.GetBoolean(printing::kSettingShouldPrintSelectionOnly
,
218 ¶ms
->params
.selection_only
);
219 job_settings
.GetBoolean(printing::kSettingShouldPrintBackgrounds
,
220 ¶ms
->params
.should_print_backgrounds
);
223 MockPrinter
* ChromeMockRenderThread::printer() {
224 return printer_
.get();
227 void ChromeMockRenderThread::set_print_dialog_user_response(bool response
) {
228 print_dialog_user_response_
= response
;
231 void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page
) {
232 print_preview_cancel_page_number_
= page
;
235 int ChromeMockRenderThread::print_preview_pages_remaining() const {
236 return print_preview_pages_remaining_
;
238 #endif // defined(ENABLE_PRINTING)