| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "headless/lib/browser/headless_print_manager.h" | 5 #include "headless/lib/browser/headless_print_manager.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 166 |
| 167 auto print_params = base::MakeUnique<PrintMsg_PrintPages_Params>(); | 167 auto print_params = base::MakeUnique<PrintMsg_PrintPages_Params>(); |
| 168 RenderParamsFromPrintSettings(print_settings, &print_params->params); | 168 RenderParamsFromPrintSettings(print_settings, &print_params->params); |
| 169 print_params->params.document_cookie = PrintSettings::NewCookie(); | 169 print_params->params.document_cookie = PrintSettings::NewCookie(); |
| 170 return print_params; | 170 return print_params; |
| 171 } | 171 } |
| 172 | 172 |
| 173 bool HeadlessPrintManager::OnMessageReceived( | 173 bool HeadlessPrintManager::OnMessageReceived( |
| 174 const IPC::Message& message, | 174 const IPC::Message& message, |
| 175 content::RenderFrameHost* render_frame_host) { | 175 content::RenderFrameHost* render_frame_host) { |
| 176 if (!printing_rfh_ && |
| 177 (message.type() == PrintHostMsg_GetDefaultPrintSettings::ID || |
| 178 message.type() == PrintHostMsg_ScriptedPrint::ID)) { |
| 179 std::string type; |
| 180 switch (message.type()) { |
| 181 case PrintHostMsg_GetDefaultPrintSettings::ID: |
| 182 type = "GetDefaultPrintSettings"; |
| 183 break; |
| 184 case PrintHostMsg_ScriptedPrint::ID: |
| 185 type = "ScriptedPrint"; |
| 186 break; |
| 187 default: |
| 188 type = "Unknown"; |
| 189 break; |
| 190 } |
| 191 DLOG(ERROR) |
| 192 << "Unexpected message received before GetPDFContents is called: " |
| 193 << type; |
| 194 |
| 195 // TODO: consider propagating the error back to the caller, rather than |
| 196 // effectively dropping the request. |
| 197 render_frame_host->Send(IPC::SyncMessage::GenerateReply(&message)); |
| 198 return true; |
| 199 } |
| 200 |
| 176 bool handled = true; | 201 bool handled = true; |
| 177 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) | 202 IPC_BEGIN_MESSAGE_MAP(HeadlessPrintManager, message) |
| 178 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, | 203 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowInvalidPrinterSettingsError, |
| 179 OnShowInvalidPrinterSettingsError) | 204 OnShowInvalidPrinterSettingsError) |
| 180 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) | 205 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPrintPage, OnDidPrintPage) |
| 181 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, | 206 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_GetDefaultPrintSettings, |
| 182 OnGetDefaultPrintSettings) | 207 OnGetDefaultPrintSettings) |
| 183 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) | 208 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_ScriptedPrint, OnScriptedPrint) |
| 184 IPC_MESSAGE_UNHANDLED(handled = false) | 209 IPC_MESSAGE_UNHANDLED(handled = false) |
| 185 IPC_END_MESSAGE_MAP() | 210 IPC_END_MESSAGE_MAP() |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 251 |
| 227 void HeadlessPrintManager::OnDidGetPrintedPagesCount(int cookie, | 252 void HeadlessPrintManager::OnDidGetPrintedPagesCount(int cookie, |
| 228 int number_pages) { | 253 int number_pages) { |
| 229 PrintManager::OnDidGetPrintedPagesCount(cookie, number_pages); | 254 PrintManager::OnDidGetPrintedPagesCount(cookie, number_pages); |
| 230 if (!print_params_->pages.empty()) | 255 if (!print_params_->pages.empty()) |
| 231 number_pages_ = print_params_->pages.size(); | 256 number_pages_ = print_params_->pages.size(); |
| 232 } | 257 } |
| 233 | 258 |
| 234 void HeadlessPrintManager::OnDidPrintPage( | 259 void HeadlessPrintManager::OnDidPrintPage( |
| 235 const PrintHostMsg_DidPrintPage_Params& params) { | 260 const PrintHostMsg_DidPrintPage_Params& params) { |
| 236 if (!callback_) { | |
| 237 DLOG(ERROR) | |
| 238 << "Unexpected PrintHostMsg_DidPrintPage message from the renderer"; | |
| 239 return; | |
| 240 } | |
| 241 | |
| 242 const bool metafile_must_be_valid = expecting_first_page_; | 261 const bool metafile_must_be_valid = expecting_first_page_; |
| 243 expecting_first_page_ = false; | 262 expecting_first_page_ = false; |
| 244 | 263 |
| 245 if (metafile_must_be_valid) { | 264 if (metafile_must_be_valid) { |
| 246 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { | 265 if (!base::SharedMemory::IsHandleValid(params.metafile_data_handle)) { |
| 247 ReleaseJob(INVALID_MEMORY_HANDLE); | 266 ReleaseJob(INVALID_MEMORY_HANDLE); |
| 248 return; | 267 return; |
| 249 } | 268 } |
| 250 auto shared_buf = | 269 auto shared_buf = |
| 251 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); | 270 base::MakeUnique<base::SharedMemory>(params.metafile_data_handle, true); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 if (result == PRINT_SUCCESS) | 315 if (result == PRINT_SUCCESS) |
| 297 callback_.Run(result, std::move(data_)); | 316 callback_.Run(result, std::move(data_)); |
| 298 else | 317 else |
| 299 callback_.Run(result, std::string()); | 318 callback_.Run(result, std::string()); |
| 300 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), | 319 printing_rfh_->Send(new PrintMsg_PrintingDone(printing_rfh_->GetRoutingID(), |
| 301 result == PRINT_SUCCESS)); | 320 result == PRINT_SUCCESS)); |
| 302 Reset(); | 321 Reset(); |
| 303 } | 322 } |
| 304 | 323 |
| 305 } // namespace printing | 324 } // namespace printing |
| OLD | NEW |