OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "printing/pdf_metafile_cg_mac.h" | 5 #include "printing/pdf_metafile_cg_mac.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/mac/mac_util.h" | 10 #include "base/mac/mac_util.h" |
10 #include "base/mac/scoped_cftyperef.h" | 11 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/sys_string_conversions.h" | 12 #include "base/sys_string_conversions.h" |
12 #include "base/threading/thread_local.h" | 13 #include "base/threading/thread_local.h" |
13 #include "ui/gfx/rect.h" | 14 #include "ui/gfx/rect.h" |
14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
15 | 16 |
16 using base::mac::ScopedCFTypeRef; | 17 using base::mac::ScopedCFTypeRef; |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
20 // What is up with this ugly hack? <http://crbug.com/64641>, that's what. | 21 // What is up with this ugly hack? <http://crbug.com/64641>, that's what. |
21 // The bug: Printing certain PDFs crashes. The cause: When printing, the | 22 // The bug: Printing certain PDFs crashes. The cause: When printing, the |
22 // renderer process assembles pages one at a time, in PDF format, to send to the | 23 // renderer process assembles pages one at a time, in PDF format, to send to the |
23 // browser process. When printing a PDF, the PDF plugin returns output in PDF | 24 // browser process. When printing a PDF, the PDF plugin returns output in PDF |
24 // format. There is a bug in 10.5 and 10.6 (<rdar://9018916>, | 25 // format. There is a bug in 10.5 and 10.6 (<rdar://9018916>, |
25 // <http://www.openradar.me/9018916>) where reference counting is broken when | 26 // <http://www.openradar.me/9018916>) where reference counting is broken when |
26 // drawing certain PDFs into PDF contexts. So at the high-level, a PdfMetafileCg | 27 // drawing certain PDFs into PDF contexts. So at the high-level, a PdfMetafileCg |
27 // is used to hold the destination context, and then about five layers down on | 28 // is used to hold the destination context, and then about five layers down on |
28 // the callstack, a PdfMetafileCg is used to hold the source PDF. If the source | 29 // the callstack, a PdfMetafileCg is used to hold the source PDF. If the source |
29 // PDF is drawn into the destination PDF context and then released, accessing | 30 // PDF is drawn into the destination PDF context and then released, accessing |
30 // the destination PDF context will crash. So the outermost instantiation of | 31 // the destination PDF context will crash. So the outermost instantiation of |
31 // PdfMetafileCg creates a pool for deeper instantiations to dump their used | 32 // PdfMetafileCg creates a pool for deeper instantiations to dump their used |
32 // PDFs into rather than releasing them. When the top-level PDF is closed, then | 33 // PDFs into rather than releasing them. When the top-level PDF is closed, then |
33 // it's safe to clear the pool. A thread local is used to allow this to work in | 34 // it's safe to clear the pool. A thread local is used to allow this to work in |
34 // single-process mode. TODO(avi): This Apple bug appears fixed in 10.7; when | 35 // single-process mode. TODO(avi): This Apple bug appears fixed in 10.7; when |
35 // 10.7 is the minimum required version for Chromium, remove this hack. | 36 // 10.7 is the minimum required version for Chromium, remove this hack. |
36 | 37 |
37 base::ThreadLocalPointer<struct __CFSet> thread_pdf_docs; | 38 base::LazyInstance< |
| 39 base::ThreadLocalPointer<struct __CFSet>, |
| 40 base::LeakyLazyInstanceTraits<base::ThreadLocalPointer<struct __CFSet> > > |
| 41 thread_pdf_docs(base::LINKER_INITIALIZED); |
38 | 42 |
39 } // namespace | 43 } // namespace |
40 | 44 |
41 namespace printing { | 45 namespace printing { |
42 | 46 |
43 PdfMetafileCg::PdfMetafileCg() | 47 PdfMetafileCg::PdfMetafileCg() |
44 : page_is_open_(false), | 48 : page_is_open_(false), |
45 thread_pdf_docs_owned_(false) { | 49 thread_pdf_docs_owned_(false) { |
46 if (!thread_pdf_docs.Get() && base::mac::IsOSSnowLeopardOrEarlier()) { | 50 if (!thread_pdf_docs.Pointer()->Get() && |
| 51 base::mac::IsOSSnowLeopardOrEarlier()) { |
47 thread_pdf_docs_owned_ = true; | 52 thread_pdf_docs_owned_ = true; |
48 thread_pdf_docs.Set(CFSetCreateMutable(kCFAllocatorDefault, | 53 thread_pdf_docs.Pointer()->Set( |
49 0, | 54 CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks)); |
50 &kCFTypeSetCallBacks)); | |
51 } | 55 } |
52 } | 56 } |
53 | 57 |
54 PdfMetafileCg::~PdfMetafileCg() { | 58 PdfMetafileCg::~PdfMetafileCg() { |
55 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
56 if (pdf_doc_ && thread_pdf_docs.Get()) { | 60 if (pdf_doc_ && thread_pdf_docs.Pointer()->Get()) { |
57 // Transfer ownership to the pool. | 61 // Transfer ownership to the pool. |
58 CFSetAddValue(thread_pdf_docs.Get(), pdf_doc_); | 62 CFSetAddValue(thread_pdf_docs.Pointer()->Get(), pdf_doc_); |
59 } | 63 } |
60 | 64 |
61 if (thread_pdf_docs_owned_) { | 65 if (thread_pdf_docs_owned_) { |
62 CFRelease(thread_pdf_docs.Get()); | 66 CFRelease(thread_pdf_docs.Pointer()->Get()); |
63 thread_pdf_docs.Set(NULL); | 67 thread_pdf_docs.Pointer()->Set(NULL); |
64 } | 68 } |
65 } | 69 } |
66 | 70 |
67 bool PdfMetafileCg::Init() { | 71 bool PdfMetafileCg::Init() { |
68 // Ensure that Init hasn't already been called. | 72 // Ensure that Init hasn't already been called. |
69 DCHECK(!context_.get()); | 73 DCHECK(!context_.get()); |
70 DCHECK(!pdf_data_.get()); | 74 DCHECK(!pdf_data_.get()); |
71 | 75 |
72 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); | 76 pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); |
73 if (!pdf_data_.get()) { | 77 if (!pdf_data_.get()) { |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 295 |
292 if (!pdf_doc_.get()) { | 296 if (!pdf_doc_.get()) { |
293 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( | 297 ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
294 CGDataProviderCreateWithCFData(pdf_data_)); | 298 CGDataProviderCreateWithCFData(pdf_data_)); |
295 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); | 299 pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
296 } | 300 } |
297 return pdf_doc_.get(); | 301 return pdf_doc_.get(); |
298 } | 302 } |
299 | 303 |
300 } // namespace printing | 304 } // namespace printing |
OLD | NEW |