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 26 matching lines...) Expand all Loading... | |
37 #include "chrome/common/chrome_switches.h" | 37 #include "chrome/common/chrome_switches.h" |
38 #endif | 38 #endif |
39 | 39 |
40 #if defined(ENABLE_FULL_PRINTING) | 40 #if defined(ENABLE_FULL_PRINTING) |
41 #include "chrome/browser/printing/print_error_dialog.h" | 41 #include "chrome/browser/printing/print_error_dialog.h" |
42 #endif | 42 #endif |
43 | 43 |
44 using base::TimeDelta; | 44 using base::TimeDelta; |
45 using content::BrowserThread; | 45 using content::BrowserThread; |
46 | 46 |
47 #if defined(OS_WIN) | 47 #if defined(OS_WIN) && PRINTING_USE_EMF_METAFILE |
48 // Limits memory usage by raster to 64 MiB. | 48 // Limits memory usage by raster to 64 MiB. |
49 const int kMaxRasterSizeInPixels = 16*1024*1024; | 49 const int kMaxRasterSizeInPixels = 16*1024*1024; |
50 #endif | 50 #endif |
51 | 51 |
52 namespace printing { | 52 namespace printing { |
53 | 53 |
54 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) | 54 PrintViewManagerBase::PrintViewManagerBase(content::WebContents* web_contents) |
55 : content::WebContentsObserver(web_contents), | 55 : content::WebContentsObserver(web_contents), |
56 number_pages_(0), | 56 number_pages_(0), |
57 printing_succeeded_(false), | 57 printing_succeeded_(false), |
58 inside_inner_message_loop_(false), | 58 inside_inner_message_loop_(false), |
59 cookie_(0), | 59 cookie_(0), |
60 queue_(g_browser_process->print_job_manager()->queue()) { | 60 queue_(g_browser_process->print_job_manager()->queue()) { |
61 DCHECK(queue_); | 61 DCHECK(queue_); |
62 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 62 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
63 (defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE) | |
63 expecting_first_page_ = true; | 64 expecting_first_page_ = true; |
64 #endif | 65 #endif |
65 Profile* profile = | 66 Profile* profile = |
66 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 67 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
67 printing_enabled_.Init( | 68 printing_enabled_.Init( |
68 prefs::kPrintingEnabled, | 69 prefs::kPrintingEnabled, |
69 profile->GetPrefs(), | 70 profile->GetPrefs(), |
70 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, | 71 base::Bind(&PrintViewManagerBase::UpdateScriptedPrintingBlocked, |
71 base::Unretained(this))); | 72 base::Unretained(this))); |
72 } | 73 } |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 } | 107 } |
107 } | 108 } |
108 | 109 |
109 base::string16 PrintViewManagerBase::RenderSourceName() { | 110 base::string16 PrintViewManagerBase::RenderSourceName() { |
110 base::string16 name(web_contents()->GetTitle()); | 111 base::string16 name(web_contents()->GetTitle()); |
111 if (name.empty()) | 112 if (name.empty()) |
112 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE); | 113 name = l10n_util::GetStringUTF16(IDS_DEFAULT_PRINT_DOCUMENT_TITLE); |
113 return name; | 114 return name; |
114 } | 115 } |
115 | 116 |
117 #if 0 && defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE | |
118 void PrintViewManagerBase::OnRenderPDFPagesToMetafileSucceeded( | |
119 const printing::Emf& metafile, | |
120 int highest_rendered_page_number, | |
121 double scale_factor) { | |
122 CHECK(false); | |
123 } | |
124 #endif // OS_WIN && !PRINTING_USE_EMF_METAFILE | |
125 | |
116 void PrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie, | 126 void PrintViewManagerBase::OnDidGetPrintedPagesCount(int cookie, |
117 int number_pages) { | 127 int number_pages) { |
118 DCHECK_GT(cookie, 0); | 128 DCHECK_GT(cookie, 0); |
119 DCHECK_GT(number_pages, 0); | 129 DCHECK_GT(number_pages, 0); |
120 number_pages_ = number_pages; | 130 number_pages_ = number_pages; |
121 OpportunisticallyCreatePrintJob(cookie); | 131 OpportunisticallyCreatePrintJob(cookie); |
122 } | 132 } |
123 | 133 |
124 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { | 134 void PrintViewManagerBase::OnDidGetDocumentCookie(int cookie) { |
125 cookie_ = cookie; | 135 cookie_ = cookie; |
126 } | 136 } |
127 | 137 |
128 void PrintViewManagerBase::OnDidPrintPage( | 138 void PrintViewManagerBase::OnDidPrintPage( |
129 const PrintHostMsg_DidPrintPage_Params& params) { | 139 const PrintHostMsg_DidPrintPage_Params& params) { |
130 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) | 140 if (!OpportunisticallyCreatePrintJob(params.document_cookie)) |
131 return; | 141 return; |
132 | 142 |
133 PrintedDocument* document = print_job_->document(); | 143 PrintedDocument* document = print_job_->document(); |
134 if (!document || params.document_cookie != document->cookie()) { | 144 if (!document || params.document_cookie != document->cookie()) { |
135 // Out of sync. It may happen since we are completely asynchronous. Old | 145 // Out of sync. It may happen since we are completely asynchronous. Old |
136 // spurious messages can be received if one of the processes is overloaded. | 146 // spurious messages can be received if one of the processes is overloaded. |
137 return; | 147 return; |
138 } | 148 } |
139 | 149 |
140 #if defined(OS_WIN) || defined(OS_MACOSX) | 150 #if (defined(OS_WIN) && PRINTING_USE_EMF_METAFILE) || defined(OS_MACOSX) |
141 const bool metafile_must_be_valid = true; | 151 const bool metafile_must_be_valid = true; |
142 #elif defined(OS_POSIX) | 152 #elif defined(OS_POSIX) || (defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE) |
143 const bool metafile_must_be_valid = expecting_first_page_; | 153 const bool metafile_must_be_valid = expecting_first_page_; |
144 expecting_first_page_ = false; | 154 expecting_first_page_ = false; |
145 #endif | 155 #endif |
146 | 156 |
147 base::SharedMemory shared_buf(params.metafile_data_handle, true); | 157 base::SharedMemory shared_buf(params.metafile_data_handle, true); |
148 if (metafile_must_be_valid) { | 158 if (metafile_must_be_valid) { |
149 if (!shared_buf.Map(params.data_size)) { | 159 if (!shared_buf.Map(params.data_size)) { |
150 NOTREACHED() << "couldn't map"; | 160 NOTREACHED() << "couldn't map"; |
151 web_contents()->Stop(); | 161 web_contents()->Stop(); |
152 return; | 162 return; |
153 } | 163 } |
154 } | 164 } |
155 | 165 |
156 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); | 166 scoped_ptr<NativeMetafile> metafile(new NativeMetafile); |
157 if (metafile_must_be_valid) { | 167 if (metafile_must_be_valid) { |
158 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { | 168 if (!metafile->InitFromData(shared_buf.memory(), params.data_size)) { |
159 NOTREACHED() << "Invalid metafile header"; | 169 NOTREACHED() << "Invalid metafile header"; |
160 web_contents()->Stop(); | 170 web_contents()->Stop(); |
161 return; | 171 return; |
162 } | 172 } |
163 } | 173 } |
164 | 174 |
165 #if defined(OS_WIN) | 175 #if defined(OS_WIN) && PRINTING_USE_EMF_METAFILE |
166 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); | 176 bool big_emf = (params.data_size && params.data_size >= kMetafileMaxSize); |
167 int raster_size = std::min(params.page_size.GetArea(), | 177 int raster_size = std::min(params.page_size.GetArea(), |
168 kMaxRasterSizeInPixels); | 178 kMaxRasterSizeInPixels); |
169 if (big_emf) { | 179 if (big_emf) { |
170 scoped_ptr<NativeMetafile> raster_metafile( | 180 scoped_ptr<NativeMetafile> raster_metafile( |
171 metafile->RasterizeMetafile(raster_size)); | 181 metafile->RasterizeMetafile(raster_size)); |
172 if (raster_metafile.get()) { | 182 if (raster_metafile.get()) { |
173 metafile.swap(raster_metafile); | 183 metafile.swap(raster_metafile); |
174 } else if (big_emf) { | 184 } else if (big_emf) { |
175 // Don't fall back to emf here. | 185 // Don't fall back to emf here. |
176 NOTREACHED() << "size:" << params.data_size; | 186 NOTREACHED() << "size:" << params.data_size; |
177 TerminatePrintJob(true); | 187 TerminatePrintJob(true); |
178 web_contents()->Stop(); | 188 web_contents()->Stop(); |
179 return; | 189 return; |
180 } | 190 } |
181 } | 191 } |
192 #elif 0 && defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE | |
193 ConvertPDFToEMF(metafile.Pass()); | |
182 #endif | 194 #endif |
183 | 195 |
184 // Update the rendered document. It will send notifications to the listener. | 196 // Update the rendered document. It will send notifications to the listener. |
185 document->SetPage(params.page_number, | 197 document->SetPage(params.page_number, |
186 metafile.release(), | 198 metafile.release(), |
187 params.actual_shrink, | 199 params.actual_shrink, |
188 params.page_size, | 200 params.page_size, |
189 params.content_area); | 201 params.content_area); |
190 | 202 |
191 ShouldQuitFromInnerMessageLoop(); | 203 ShouldQuitFromInnerMessageLoop(); |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
388 if (print_job_.get() && | 400 if (print_job_.get() && |
389 print_job_->document() && | 401 print_job_->document() && |
390 !print_job_->document()->IsComplete()) { | 402 !print_job_->document()->IsComplete()) { |
391 DCHECK(!result); | 403 DCHECK(!result); |
392 // That failed. | 404 // That failed. |
393 TerminatePrintJob(true); | 405 TerminatePrintJob(true); |
394 } else { | 406 } else { |
395 // DO NOT wait for the job to finish. | 407 // DO NOT wait for the job to finish. |
396 ReleasePrintJob(); | 408 ReleasePrintJob(); |
397 } | 409 } |
398 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 410 #if (defined(OS_POSIX) && !defined(OS_MACOSX)) || \ |
411 (defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE) | |
399 expecting_first_page_ = true; | 412 expecting_first_page_ = true; |
400 #endif | 413 #endif |
401 } | 414 } |
402 | 415 |
403 void PrintViewManagerBase::PrintingDone(bool success) { | 416 void PrintViewManagerBase::PrintingDone(bool success) { |
404 if (!print_job_.get()) | 417 if (!print_job_.get()) |
405 return; | 418 return; |
406 Send(new PrintMsg_PrintingDone(routing_id(), success)); | 419 Send(new PrintMsg_PrintingDone(routing_id(), success)); |
407 } | 420 } |
408 | 421 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
531 | 544 |
532 scoped_refptr<printing::PrinterQuery> printer_query; | 545 scoped_refptr<printing::PrinterQuery> printer_query; |
533 printer_query = queue_->PopPrinterQuery(cookie); | 546 printer_query = queue_->PopPrinterQuery(cookie); |
534 if (!printer_query) | 547 if (!printer_query) |
535 return; | 548 return; |
536 BrowserThread::PostTask( | 549 BrowserThread::PostTask( |
537 BrowserThread::IO, FROM_HERE, | 550 BrowserThread::IO, FROM_HERE, |
538 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); | 551 base::Bind(&PrinterQuery::StopWorker, printer_query.get())); |
539 } | 552 } |
540 | 553 |
554 #if 0 && defined(OS_WIN) && !PRINTING_USE_EMF_METAFILE | |
555 void PrintViewManagerBase::ConvertPDFToEMF( | |
556 scoped_ptr<NativeMetafile> metafile) { | |
557 scoped_ptr<ServiceUtilityProcessHost> utility_host( | |
Vitaly Buka (NO REVIEWS)
2014/04/24 22:47:58
ServiceUtilityProcessHost is for use in Service pr
scottmg
2014/04/25 20:47:18
Thanks for the pointer, that seems to be mostly wo
| |
558 new ServiceUtilityProcessHost(this, client_message_loop_proxy)); | |
559 if (utility_host->StartRenderPDFPagesToMetafile( | |
560 pdf_path, | |
561 printing::PdfRenderSettings(render_area, render_dpi, false), | |
562 page_ranges)) { | |
Vitaly Buka (NO REVIEWS)
2014/04/24 22:47:58
Message ChromeUtilityMsg_RenderPDFPagesToMetafile
Vitaly Buka (NO REVIEWS)
2014/04/25 05:25:48
It's going to be harder then I thought, Win32 meta
scottmg
2014/04/25 20:47:18
I gave it a shot, basically using GetEnhMetaFileBi
| |
563 // The object will self-destruct when the child process dies. | |
564 utility_host.release(); | |
565 } | |
566 } | |
567 #endif // OS_WIN && !PRINTING_USE_EMF_METAFILE | |
568 | |
541 } // namespace printing | 569 } // namespace printing |
OLD | NEW |