[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 5 | #include "printing/pdf_metafile_cg_mac.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 6 | |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 10 | #include "base/lazy_instance.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | ba64e2b | 2011-06-14 18:18:38 | [diff] [blame] | 12 | #include "base/mac/mac_util.h" |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 13 | #include "base/mac/scoped_cftyperef.h" |
[email protected] | 13ac5353 | 2013-03-30 00:27:00 | [diff] [blame] | 14 | #include "base/strings/sys_string_conversions.h" |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 15 | #include "base/threading/thread_local.h" |
[email protected] | 08397d5 | 2011-02-05 01:53:38 | [diff] [blame] | 16 | #include "ui/gfx/rect.h" |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 17 | #include "ui/gfx/size.h" |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 18 | |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 19 | using base::mac::ScopedCFTypeRef; |
| 20 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // What is up with this ugly hack? <http://crbug.com/64641>, that's what. |
| 24 | // The bug: Printing certain PDFs crashes. The cause: When printing, the |
| 25 | // renderer process assembles pages one at a time, in PDF format, to send to the |
| 26 | // browser process. When printing a PDF, the PDF plugin returns output in PDF |
| 27 | // format. There is a bug in 10.5 and 10.6 (<rdar://9018916>, |
| 28 | // <http://www.openradar.me/9018916>) where reference counting is broken when |
| 29 | // drawing certain PDFs into PDF contexts. So at the high-level, a PdfMetafileCg |
| 30 | // is used to hold the destination context, and then about five layers down on |
| 31 | // the callstack, a PdfMetafileCg is used to hold the source PDF. If the source |
| 32 | // PDF is drawn into the destination PDF context and then released, accessing |
| 33 | // the destination PDF context will crash. So the outermost instantiation of |
| 34 | // PdfMetafileCg creates a pool for deeper instantiations to dump their used |
| 35 | // PDFs into rather than releasing them. When the top-level PDF is closed, then |
| 36 | // it's safe to clear the pool. A thread local is used to allow this to work in |
| 37 | // single-process mode. TODO(avi): This Apple bug appears fixed in 10.7; when |
| 38 | // 10.7 is the minimum required version for Chromium, remove this hack. |
| 39 | |
[email protected] | 9fc4416 | 2012-01-23 22:56:41 | [diff] [blame] | 40 | base::LazyInstance<base::ThreadLocalPointer<struct __CFSet> >::Leaky |
| 41 | thread_pdf_docs = LAZY_INSTANCE_INITIALIZER; |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 42 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 43 | } // namespace |
| 44 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 45 | namespace printing { |
| 46 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 47 | PdfMetafileCg::PdfMetafileCg() |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 48 | : page_is_open_(false), |
| 49 | thread_pdf_docs_owned_(false) { |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 50 | if (!thread_pdf_docs.Pointer()->Get() && |
[email protected] | 59e82f0 | 2012-08-10 22:47:14 | [diff] [blame] | 51 | base::mac::IsOSSnowLeopard()) { |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 52 | thread_pdf_docs_owned_ = true; |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 53 | thread_pdf_docs.Pointer()->Set( |
| 54 | CFSetCreateMutable(kCFAllocatorDefault, 0, &kCFTypeSetCallBacks)); |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 55 | } |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 56 | } |
| 57 | |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 58 | PdfMetafileCg::~PdfMetafileCg() { |
[email protected] | 69f5b1e6 | 2011-09-01 06:34:04 | [diff] [blame] | 59 | DCHECK(thread_checker_.CalledOnValidThread()); |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 60 | if (pdf_doc_ && thread_pdf_docs.Pointer()->Get()) { |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 61 | // Transfer ownership to the pool. |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 62 | CFSetAddValue(thread_pdf_docs.Pointer()->Get(), pdf_doc_); |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (thread_pdf_docs_owned_) { |
[email protected] | 26430024 | 2011-11-07 06:03:30 | [diff] [blame] | 66 | CFRelease(thread_pdf_docs.Pointer()->Get()); |
| 67 | thread_pdf_docs.Pointer()->Set(NULL); |
[email protected] | df6df68 | 2011-05-02 19:50:50 | [diff] [blame] | 68 | } |
| 69 | } |
[email protected] | 1d20cdf | 2011-02-16 18:09:28 | [diff] [blame] | 70 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 71 | bool PdfMetafileCg::Init() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 72 | // Ensure that Init hasn't already been called. |
| 73 | DCHECK(!context_.get()); |
| 74 | DCHECK(!pdf_data_.get()); |
| 75 | |
| 76 | pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, 0)); |
| 77 | if (!pdf_data_.get()) { |
| 78 | LOG(ERROR) << "Failed to create pdf data for metafile"; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 79 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 80 | } |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 81 | ScopedCFTypeRef<CGDataConsumerRef> pdf_consumer( |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 82 | CGDataConsumerCreateWithCFData(pdf_data_)); |
| 83 | if (!pdf_consumer.get()) { |
| 84 | LOG(ERROR) << "Failed to create data consumer for metafile"; |
| 85 | pdf_data_.reset(NULL); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 86 | return false; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 87 | } |
| 88 | context_.reset(CGPDFContextCreate(pdf_consumer, NULL, NULL)); |
| 89 | if (!context_.get()) { |
| 90 | LOG(ERROR) << "Failed to create pdf context for metafile"; |
| 91 | pdf_data_.reset(NULL); |
| 92 | } |
| 93 | |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 94 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 97 | bool PdfMetafileCg::InitFromData(const void* src_buffer, |
| 98 | uint32 src_buffer_size) { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 99 | DCHECK(!context_.get()); |
| 100 | DCHECK(!pdf_data_.get()); |
| 101 | |
| 102 | if (!src_buffer || src_buffer_size == 0) { |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | pdf_data_.reset(CFDataCreateMutable(kCFAllocatorDefault, src_buffer_size)); |
| 107 | CFDataAppendBytes(pdf_data_, static_cast<const UInt8*>(src_buffer), |
| 108 | src_buffer_size); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 113 | SkDevice* PdfMetafileCg::StartPageForVectorCanvas( |
[email protected] | 534c4fb | 2011-08-02 16:44:20 | [diff] [blame] | 114 | const gfx::Size& page_size, const gfx::Rect& content_area, |
[email protected] | d2fdcf0 | 2011-03-21 22:16:43 | [diff] [blame] | 115 | const float& scale_factor) { |
| 116 | NOTIMPLEMENTED(); |
| 117 | return NULL; |
| 118 | } |
| 119 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 120 | bool PdfMetafileCg::StartPage(const gfx::Size& page_size, |
[email protected] | 39892b9 | 2011-04-30 02:24:44 | [diff] [blame] | 121 | const gfx::Rect& content_area, |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 122 | const float& scale_factor) { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 123 | DCHECK(context_.get()); |
| 124 | DCHECK(!page_is_open_); |
| 125 | |
[email protected] | 15c2bb32 | 2010-12-21 22:30:10 | [diff] [blame] | 126 | double height = page_size.height(); |
| 127 | double width = page_size.width(); |
| 128 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 129 | CGRect bounds = CGRectMake(0, 0, width, height); |
| 130 | CGContextBeginPage(context_, &bounds); |
| 131 | page_is_open_ = true; |
| 132 | CGContextSaveGState(context_); |
| 133 | |
[email protected] | 9d42d072 | 2011-07-26 19:22:29 | [diff] [blame] | 134 | // Move to the context origin. |
| 135 | CGContextTranslateCTM(context_, content_area.x(), -content_area.y()); |
| 136 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 137 | // Flip the context. |
| 138 | CGContextTranslateCTM(context_, 0, height); |
| 139 | CGContextScaleCTM(context_, scale_factor, -scale_factor); |
[email protected] | 15c2bb32 | 2010-12-21 22:30:10 | [diff] [blame] | 140 | |
[email protected] | edc531f9 | 2011-03-18 17:52:23 | [diff] [blame] | 141 | return context_.get() != NULL; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 144 | bool PdfMetafileCg::FinishPage() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 145 | DCHECK(context_.get()); |
| 146 | DCHECK(page_is_open_); |
| 147 | |
| 148 | CGContextRestoreGState(context_); |
| 149 | CGContextEndPage(context_); |
| 150 | page_is_open_ = false; |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 151 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 152 | } |
| 153 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 154 | bool PdfMetafileCg::FinishDocument() { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 155 | DCHECK(context_.get()); |
| 156 | DCHECK(!page_is_open_); |
| 157 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 158 | #ifndef NDEBUG |
| 159 | // Check that the context will be torn down properly; if it's not, pdf_data_ |
| 160 | // will be incomplete and generate invalid PDF files/documents. |
| 161 | if (context_.get()) { |
| 162 | CFIndex extra_retain_count = CFGetRetainCount(context_.get()) - 1; |
| 163 | if (extra_retain_count > 0) { |
| 164 | LOG(ERROR) << "Metafile context has " << extra_retain_count |
| 165 | << " extra retain(s) on Close"; |
| 166 | } |
| 167 | } |
| 168 | #endif |
[email protected] | 1461948 | 2010-04-01 16:46:23 | [diff] [blame] | 169 | CGPDFContextClose(context_.get()); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 170 | context_.reset(NULL); |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 171 | return true; |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 174 | bool PdfMetafileCg::RenderPage(unsigned int page_number, |
| 175 | CGContextRef context, |
| 176 | const CGRect rect, |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 177 | const MacRenderPageParams& params) const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 178 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 179 | if (!pdf_doc) { |
| 180 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 181 | return false; |
| 182 | } |
| 183 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
[email protected] | a09ef7e | 2011-09-28 21:59:33 | [diff] [blame] | 184 | CGRect source_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFCropBox); |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 185 | float scaling_factor = 1.0; |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 186 | const bool source_is_landscape = |
| 187 | (source_rect.size.width > source_rect.size.height); |
| 188 | const bool dest_is_landscape = (rect.size.width > rect.size.height); |
| 189 | const bool rotate = |
| 190 | params.autorotate ? (source_is_landscape != dest_is_landscape) : false; |
| 191 | const float source_width = |
| 192 | rotate ? source_rect.size.height : source_rect.size.width; |
| 193 | const float source_height = |
| 194 | rotate ? source_rect.size.width : source_rect.size.height; |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 195 | |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 196 | // See if we need to scale the output. |
| 197 | const bool scaling_needed = |
| 198 | (params.shrink_to_fit && ((source_width > rect.size.width) || |
| 199 | (source_height > rect.size.height))) || |
| 200 | (params.stretch_to_fit && ((source_width < rect.size.width) && |
| 201 | (source_height < rect.size.height))); |
| 202 | if (scaling_needed) { |
| 203 | float x_scaling_factor = rect.size.width / source_width; |
| 204 | float y_scaling_factor = rect.size.height / source_height; |
| 205 | scaling_factor = std::min(x_scaling_factor, y_scaling_factor); |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 206 | } |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 207 | // Some PDFs have a non-zero origin. Need to take that into account and align |
| 208 | // the PDF to the origin. |
| 209 | const float x_origin_offset = -1 * source_rect.origin.x; |
| 210 | const float y_origin_offset = -1 * source_rect.origin.y; |
| 211 | |
| 212 | // If the PDF needs to be centered, calculate the offsets here. |
| 213 | float x_offset = params.center_horizontally ? |
| 214 | ((rect.size.width - (source_width * scaling_factor)) / 2) : 0; |
| 215 | if (rotate) |
| 216 | x_offset = -x_offset; |
| 217 | |
| 218 | float y_offset = params.center_vertically ? |
| 219 | ((rect.size.height - (source_height * scaling_factor)) / 2) : 0; |
| 220 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 221 | CGContextSaveGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 222 | |
| 223 | // The transform operations specified here gets applied in reverse order. |
| 224 | // i.e. the origin offset translation happens first. |
| 225 | // Origin is at bottom-left. |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 226 | CGContextTranslateCTM(context, x_offset, y_offset); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 227 | if (rotate) { |
| 228 | // After rotating by 90 degrees with the axis at the origin, the page |
| 229 | // content is now "off screen". Shift it right to move it back on screen. |
| 230 | CGContextTranslateCTM(context, rect.size.width, 0); |
| 231 | // Rotates counter-clockwise by 90 degrees. |
| 232 | CGContextRotateCTM(context, M_PI_2); |
| 233 | } |
[email protected] | f3cab62 | 2010-06-28 18:56:30 | [diff] [blame] | 234 | CGContextScaleCTM(context, scaling_factor, scaling_factor); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 235 | CGContextTranslateCTM(context, x_origin_offset, y_origin_offset); |
| 236 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 237 | CGContextDrawPDFPage(context, pdf_page); |
| 238 | CGContextRestoreGState(context); |
[email protected] | b5cf844c | 2012-06-18 21:49:20 | [diff] [blame] | 239 | |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 243 | unsigned int PdfMetafileCg::GetPageCount() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 244 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 245 | return pdf_doc ? CGPDFDocumentGetNumberOfPages(pdf_doc) : 0; |
| 246 | } |
| 247 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 248 | gfx::Rect PdfMetafileCg::GetPageBounds(unsigned int page_number) const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 249 | CGPDFDocumentRef pdf_doc = GetPDFDocument(); |
| 250 | if (!pdf_doc) { |
| 251 | LOG(ERROR) << "Unable to create PDF document from data"; |
| 252 | return gfx::Rect(); |
| 253 | } |
| 254 | if (page_number > GetPageCount()) { |
| 255 | LOG(ERROR) << "Invalid page number: " << page_number; |
| 256 | return gfx::Rect(); |
| 257 | } |
| 258 | CGPDFPageRef pdf_page = CGPDFDocumentGetPage(pdf_doc, page_number); |
| 259 | CGRect page_rect = CGPDFPageGetBoxRect(pdf_page, kCGPDFMediaBox); |
| 260 | return gfx::Rect(page_rect); |
| 261 | } |
| 262 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 263 | uint32 PdfMetafileCg::GetDataSize() const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 264 | // PDF data is only valid/complete once the context is released. |
| 265 | DCHECK(!context_); |
| 266 | |
| 267 | if (!pdf_data_) |
| 268 | return 0; |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 269 | return static_cast<uint32>(CFDataGetLength(pdf_data_)); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 272 | bool PdfMetafileCg::GetData(void* dst_buffer, uint32 dst_buffer_size) const { |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 273 | // PDF data is only valid/complete once the context is released. |
| 274 | DCHECK(!context_); |
| 275 | DCHECK(pdf_data_); |
| 276 | DCHECK(dst_buffer); |
| 277 | DCHECK_GT(dst_buffer_size, 0U); |
| 278 | |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 279 | uint32 data_size = GetDataSize(); |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 280 | if (dst_buffer_size > data_size) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | CFDataGetBytes(pdf_data_, CFRangeMake(0, dst_buffer_size), |
| 285 | static_cast<UInt8*>(dst_buffer)); |
| 286 | return true; |
| 287 | } |
| 288 | |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 289 | bool PdfMetafileCg::SaveTo(const base::FilePath& file_path) const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 290 | DCHECK(pdf_data_.get()); |
| 291 | DCHECK(!context_.get()); |
| 292 | |
| 293 | std::string path_string = file_path.value(); |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 294 | ScopedCFTypeRef<CFURLRef> path_url(CFURLCreateFromFileSystemRepresentation( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 295 | kCFAllocatorDefault, reinterpret_cast<const UInt8*>(path_string.c_str()), |
| 296 | path_string.length(), false)); |
| 297 | SInt32 error_code; |
| 298 | CFURLWriteDataAndPropertiesToResource(path_url, pdf_data_, NULL, &error_code); |
| 299 | return error_code == 0; |
| 300 | } |
| 301 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 302 | CGContextRef PdfMetafileCg::context() const { |
[email protected] | 8f17cd3e | 2011-03-16 01:39:42 | [diff] [blame] | 303 | return context_.get(); |
| 304 | } |
| 305 | |
[email protected] | 9b61753 | 2011-04-04 23:15:06 | [diff] [blame] | 306 | CGPDFDocumentRef PdfMetafileCg::GetPDFDocument() const { |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 307 | // Make sure that we have data, and that it's not being modified any more. |
| 308 | DCHECK(pdf_data_.get()); |
| 309 | DCHECK(!context_.get()); |
| 310 | |
| 311 | if (!pdf_doc_.get()) { |
[email protected] | df0ca6c8 | 2010-10-17 04:09:06 | [diff] [blame] | 312 | ScopedCFTypeRef<CGDataProviderRef> pdf_data_provider( |
[email protected] | 72f966b | 2009-10-14 20:02:27 | [diff] [blame] | 313 | CGDataProviderCreateWithCFData(pdf_data_)); |
| 314 | pdf_doc_.reset(CGPDFDocumentCreateWithProvider(pdf_data_provider)); |
| 315 | } |
| 316 | return pdf_doc_.get(); |
| 317 | } |
| 318 | |
[email protected] | 14b21079 | 2009-10-12 18:45:31 | [diff] [blame] | 319 | } // namespace printing |