OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/printing/print_view_manager_base.h" | 5 #include "chrome/browser/printing/print_view_manager_base.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
35 #include "base/command_line.h" | 35 #include "base/command_line.h" |
36 #include "chrome/common/chrome_switches.h" | 36 #include "chrome/common/chrome_switches.h" |
37 #endif | 37 #endif |
38 | 38 |
39 #if defined(ENABLE_FULL_PRINTING) | 39 #if defined(ENABLE_FULL_PRINTING) |
40 #include "chrome/browser/printing/print_error_dialog.h" | 40 #include "chrome/browser/printing/print_error_dialog.h" |
41 #endif | 41 #endif |
42 | 42 |
| 43 #if defined(WIN_PDF_METAFILE_FOR_PRINTING) |
| 44 #include "base/memory/ref_counted.h" |
| 45 #include "base/memory/ref_counted_memory.h" |
| 46 #include "chrome/browser/printing/pdf_to_emf_converter.h" |
| 47 #include "printing/pdf_render_settings.h" |
| 48 #endif |
| 49 |
43 using base::TimeDelta; | 50 using base::TimeDelta; |
44 using content::BrowserThread; | 51 using content::BrowserThread; |
45 | 52 |
46 #if defined(OS_WIN) | 53 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) |
47 // Limits memory usage by raster to 64 MiB. | 54 // Limits memory usage by raster to 64 MiB. |
48 const int kMaxRasterSizeInPixels = 16*1024*1024; | 55 const int kMaxRasterSizeInPixels = 16*1024*1024; |
49 #endif | 56 #endif |
50 | 57 |
51 namespace printing { | 58 namespace printing { |
52 | 59 |
53 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) | 60 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) |
54 : content::WebContentsObserver(web_contents), | 61 : content::WebContentsObserver(web_contents), |
55 number_pages_(0), | 62 number_pages_(0), |
56 printing_succeeded_(false), | 63 printing_succeeded_(false), |
57 inside_inner_message_loop_(false), | 64 inside_inner_message_loop_(false), |
58 cookie_(0), | 65 cookie_(0), |
59 queue_(g_browser_process->print_job_manager()->queue()) { | 66 queue_(g_browser_process->print_job_manager()->queue()) { |
60 DCHECK(queue_); | 67 DCHECK(queue_); |
61 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 68 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
| 69 defined(WIN_PDF_METAFILE_FOR_PRINTING) |
62 expecting_first_page_ = true; | 70 expecting_first_page_ = true; |
63 #endif | 71 #endif |
64 Profile* profile = | 72 Profile* profile = |
65 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 73 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
66 printing_enabled_.Init( | 74 printing_enabled_.Init( |
67 prefs::kPrintingEnabled, | 75 prefs::kPrintingEnabled, |
68 profile->GetPrefs(), | 76 profile->GetPrefs(), |
69 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, | 77 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, |
70 base::Unretained(this))); | 78 base::Unretained(this))); |
71 } | 79 } |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 DCHECK_GT(cookie, 0); | 125 DCHECK_GT(cookie, 0); |
118 DCHECK_GT(number_pages, 0); | 126 DCHECK_GT(number_pages, 0); |
119 number_pages_ = number_pages; | 127 number_pages_ = number_pages; |
120 OpportunisticallyCreatePrintJob(cookie); | 128 OpportunisticallyCreatePrintJob(cookie); |
121 } | 129 } |
122 | 130 |
123 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { | 131 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { |
124 cookie_ = cookie; | 132 cookie_ = cookie; |
125 } | 133 } |
126 | 134 |
| 135 #if defined(WIN_PDF_METAFILE_FOR_PRINTING) |
| 136 void PrintViewManagerBase::OnPdfToEmfConverted( |
| 137 const PrintHostMsg_DidPrintPage_Params& params, |
| 138 double scale_factor, |
| 139 const std::vector<base::FilePath>& emf_files) { |
| 140 PrintedDocument* document = print_job_->document(); |
| 141 if (!document) |
| 142 return; |
| 143 |
| 144 for (size_t i = 0; i < emf_files.size(); ++i) { |
| 145 scoped_ptr<printing::Emf> metafile(new printing::Emf); |
| 146 if (!metafile->InitFromFile(emf_files[i])) { |
| 147 NOTREACHED() << "Invalid metafile"; |
| 148 web_contents()->Stop(); |
| 149 return; |
| 150 } |
| 151 // Update the rendered document. It will send notifications to the listener. |
| 152 document->SetPage(i, |
| 153 metafile.release(), |
| 154 scale_factor, |
| 155 params.page_size, |
| 156 params.content_area); |
| 157 } |
| 158 |
| 159 ShouldQuitFromInnerMessageLoop(); |
| 160 } |
| 161 #endif // WIN_PDF_METAFILE_FOR_PRINTING |
| 162 |
127 void PrintViewManagerBase::OnDidPrintPage( | 163 void PrintViewManagerBase::OnDidPrintPage( |
128 const PrintHostMsg_DidPrintPage_Params& params) { | 164 const PrintHostMsg_DidPrintPage_Params& params) { |
129 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) | 165 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) |
130 return; | 166 return; |
131 | 167 |
132 PrintedDocument* document = print_job_->document(); | 168 PrintedDocument* document = print_job_->document(); |
133 if (!document || params.document_cookie != document->cookie()) { | 169 if (!document || params.document_cookie != document->cookie()) { |
134 // Out of sync. It may happen since we are completely asynchronous. Old | 170 // Out of sync. It may happen since we are completely asynchronous. Old |
135 // spurious messages can be received if one of the processes is overloaded. | 171 // spurious messages can be received if one of the processes is overloaded. |
136 return; | 172 return; |
137 } | 173 } |
138 | 174 |
139 #if defined(OS_WIN) || defined(OS_MACOSX) | 175 #if (defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING)) || \ |
| 176 defined(OS_MACOSX) |
140 const bool metafile_must_be_valid = true; | 177 const bool metafile_must_be_valid = true; |
141 #elif defined(OS_POSIX) | 178 #elif defined(OS_POSIX) || defined(WIN_PDF_METAFILE_FOR_PRINTING) |
142 const bool metafile_must_be_valid = expecting_first_page_; | 179 const bool metafile_must_be_valid = expecting_first_page_; |
143 expecting_first_page_ = false; | 180 expecting_first_page_ = false; |
144 #endif | 181 #endif |
145 | 182 |
146 base::SharedMemory shared_buf(params.metafile_data_handle, true); | 183 base::SharedMemory shared_buf(params.metafile_data_handle, true); |
147 if (metafile_must_be_valid) { | 184 if (metafile_must_be_valid) { |
148 if (!shared_buf.Map(params.data_size)) { | 185 if (!shared_buf.Map(params.data_size)) { |
149 NOTREACHED() << "couldn't map"; | 186 NOTREACHED() << "couldn't map"; |
150 web_contents()->Stop(); | 187 web_contents()->Stop(); |
151 return; | 188 return; |
152 } | 189 } |
153 } | 190 } |
154 | 191 |
155 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); | 192 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); |
156 if (metafile_must_be_valid) { | 193 if (metafile_must_be_valid) { |
157 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { | 194 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { |
158 NOTREACHED() << "Invalid metafile header"; | 195 NOTREACHED() << "Invalid metafile header"; |
159 web_contents()->Stop(); | 196 web_contents()->Stop(); |
160 return; | 197 return; |
161 } | 198 } |
162 } | 199 } |
163 | 200 |
164 #if defined(OS_WIN) | 201 #if defined(OS_WIN) && !defined(WIN_PDF_METAFILE_FOR_PRINTING) |
165 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); | 202 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); |
166 int raster_size = std::min(params.page_size.GetArea(), | 203 int raster_size = |
167 kMaxRasterSizeInPixels); | 204 std::min(params.page_size.GetArea(), kMaxRasterSizeInPixels); |
168 if (big_emf) { | 205 if (big_emf) { |
169 scoped_ptr<NativeMetafile> raster_metafile( | 206 scoped_ptr<NativeMetafile> raster_metafile( |
170 metafile->RasterizeMetafile(raster_size)); | 207 metafile->RasterizeMetafile(raster_size)); |
171 if (raster_metafile.get()) { | 208 if (raster_metafile.get()) { |
172 metafile.swap(raster_metafile); | 209 metafile.swap(raster_metafile); |
173 } else if (big_emf) { | 210 } else if (big_emf) { |
174 // Don't fall back to emf here. | 211 // Don't fall back to emf here. |
175 NOTREACHED() << "size:" << params.data_size; | 212 NOTREACHED() << "size:" << params.data_size; |
176 TerminatePrintJob(true); | 213 TerminatePrintJob(true); |
177 web_contents()->Stop(); | 214 web_contents()->Stop(); |
178 return; | 215 return; |
179 } | 216 } |
180 } | 217 } |
181 #endif | 218 #endif // OS_WIN && !WIN_PDF_METAFILE_FOR_PRINTING |
182 | 219 |
| 220 #if !defined(WIN_PDF_METAFILE_FOR_PRINTING) |
183 // Update the rendered document. It will send notifications to the listener. | 221 // Update the rendered document. It will send notifications to the listener. |
184 document->SetPage(params.page_number, | 222 document->SetPage(params.page_number, |
185 metafile.release(), | 223 metafile.release(), |
186 params.actual_shrink, | 224 params.actual_shrink, |
187 params.page_size, | 225 params.page_size, |
188 params.content_area); | 226 params.content_area); |
189 | 227 |
190 ShouldQuitFromInnerMessageLoop(); | 228 ShouldQuitFromInnerMessageLoop(); |
| 229 #else |
| 230 if (metafile_must_be_valid) { |
| 231 scoped_refptr<base::RefCountedBytes> bytes = new base::RefCountedBytes( |
| 232 reinterpret_cast<const unsigned char*>(shared_buf.memory()), |
| 233 params.data_size); |
| 234 |
| 235 if (!pdf_to_emf_converter_) |
| 236 pdf_to_emf_converter_ = PdfToEmfConverter::CreateDefault(); |
| 237 |
| 238 const int kPrinterDpi = 600; |
| 239 pdf_to_emf_converter_->Start( |
| 240 bytes, |
| 241 printing::PdfRenderSettings(params.content_area, kPrinterDpi, false), |
| 242 base::Bind(&PrintViewManagerBase::OnPdfToEmfConverted, |
| 243 base::Unretained(this), |
| 244 params)); |
| 245 } |
| 246 #endif // !WIN_PDF_METAFILE_FOR_PRINTING |
191 } | 247 } |
192 | 248 |
193 void PrintViewManagerBase::OnPrintingFailed(int cookie) { | 249 void PrintViewManagerBase::OnPrintingFailed(int cookie) { |
194 if (cookie != cookie_) { | 250 if (cookie != cookie_) { |
195 NOTREACHED(); | 251 NOTREACHED(); |
196 return; | 252 return; |
197 } | 253 } |
198 | 254 |
199 #if defined(ENABLE_FULL_PRINTING) | 255 #if defined(ENABLE_FULL_PRINTING) |
200 chrome::ShowPrintErrorDialog(); | 256 chrome::ShowPrintErrorDialog(); |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 if (print_job_.get() && | 443 if (print_job_.get() && |
388 print_job_->document() && | 444 print_job_->document() && |
389 !print_job_->document()->IsComplete()) { | 445 !print_job_->document()->IsComplete()) { |
390 DCHECK(!result); | 446 DCHECK(!result); |
391 // That failed. | 447 // That failed. |
392 TerminatePrintJob(true); | 448 TerminatePrintJob(true); |
393 } else { | 449 } else { |
394 // DO NOT wait for the job to finish. | 450 // DO NOT wait for the job to finish. |
395 ReleasePrintJob(); | 451 ReleasePrintJob(); |
396 } | 452 } |
397 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 453 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
| 454 defined(WIN_PDF_METAFILE_FOR_PRINTING) |
398 expecting_first_page_ = true; | 455 expecting_first_page_ = true; |
399 #endif | 456 #endif |
400 } | 457 } |
401 | 458 |
402 void PrintViewManagerBase::PrintingDone(bool success) { | 459 void PrintViewManagerBase::PrintingDone(bool success) { |
403 if (!print_job_.get()) | 460 if (!print_job_.get()) |
404 return; | 461 return; |
405 Send(new PrintMsg_PrintingDone(routing_id(), success)); | 462 Send(new PrintMsg_PrintingDone(routing_id(), success)); |
406 } | 463 } |
407 | 464 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 scoped_refptr<printing::PrinterQuery> printer_query; | 588 scoped_refptr<printing::PrinterQuery> printer_query; |
532 printer_query = queue_->PopPrinterQuery(cookie); | 589 printer_query = queue_->PopPrinterQuery(cookie); |
533 if (!printer_query) | 590 if (!printer_query) |
534 return; | 591 return; |
535 BrowserThread::PostTask( | 592 BrowserThread::PostTask( |
536 BrowserThread::IO, FROM_HERE, | 593 BrowserThread::IO, FROM_HERE, |
537 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 594 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
538 } | 595 } |
539 | 596 |
540 } // namespace printing | 597 } // namespace printing |
OLD | NEW |