Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/pdf_to_emf_converter.h" | 5 #include "chrome/browser/printing/pdf_to_emf_converter.h" |
| 6 | 6 |
| 7 #include "base/bind_helpers.h" | |
| 8 #include "base/cancelable_callback.h" | |
| 9 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_temp_dir.h" | |
| 12 #include "base/logging.h" | 9 #include "base/logging.h" |
| 13 #include "chrome/common/chrome_utility_messages.h" | 10 #include "chrome/common/chrome_utility_messages.h" |
| 14 #include "chrome/common/chrome_utility_printing_messages.h" | 11 #include "chrome/common/chrome_utility_printing_messages.h" |
| 15 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/child_process_data.h" | 13 #include "content/public/browser/child_process_data.h" |
| 17 #include "content/public/browser/utility_process_host.h" | 14 #include "content/public/browser/utility_process_host.h" |
| 18 #include "content/public/browser/utility_process_host_client.h" | 15 #include "content/public/browser/utility_process_host_client.h" |
| 19 #include "printing/emf_win.h" | 16 #include "printing/emf_win.h" |
| 20 #include "printing/page_range.h" | |
| 21 #include "printing/pdf_render_settings.h" | 17 #include "printing/pdf_render_settings.h" |
| 22 | 18 |
| 23 namespace printing { | 19 namespace printing { |
| 24 | 20 |
| 25 namespace { | 21 namespace { |
| 26 | 22 |
| 27 using content::BrowserThread; | 23 using content::BrowserThread; |
| 28 | 24 |
| 29 class FileHandlers | 25 class PdfToEmfConverterImpl; |
| 30 : public base::RefCountedThreadSafe<FileHandlers, | 26 |
| 31 BrowserThread::DeleteOnFileThread> { | 27 const int kMaxNumberOfTempFilesPerDocument = 3; |
| 28 | |
| 29 typedef scoped_ptr<base::File, BrowserThread::DeleteOnFileThread> TempFilePtr; | |
| 30 | |
| 31 // Wrapper for Emf to keep only file handle in memory, and load actual data only | |
| 32 // on playback. |InitFromFile| can play metafile directly from disk, but it | |
| 33 // can't open file handles. We need file handles to reliably delete temporary | |
| 34 // files, and to efficiently interact with sandbox process. | |
| 35 class LazyEmf : public MetafilePlayer { | |
| 32 public: | 36 public: |
| 33 FileHandlers() {} | 37 explicit LazyEmf(TempFilePtr file) : file_(file.Pass()) {} |
| 34 | 38 virtual ~LazyEmf() { Close(); } |
| 35 void Init(base::RefCountedMemory* data); | 39 |
| 36 bool IsValid(); | 40 virtual bool SafePlayback(HDC hdc) const OVERRIDE; |
| 37 | 41 virtual bool SaveTo(base::File* file) const OVERRIDE; |
| 38 base::FilePath GetEmfPath() const { | |
| 39 return temp_dir_.path().AppendASCII("output.emf"); | |
| 40 } | |
| 41 | |
| 42 base::FilePath GetEmfPagePath(int page_number) const { | |
| 43 return GetEmfPath().InsertBeforeExtensionASCII( | |
| 44 base::StringPrintf(".%d", page_number)); | |
| 45 } | |
| 46 | |
| 47 base::FilePath GetPdfPath() const { | |
| 48 return temp_dir_.path().AppendASCII("input.pdf"); | |
| 49 } | |
| 50 | |
| 51 IPC::PlatformFileForTransit GetPdfForProcess(base::ProcessHandle process) { | |
| 52 DCHECK(pdf_file_.IsValid()); | |
| 53 IPC::PlatformFileForTransit transit = | |
| 54 IPC::TakeFileHandleForProcess(pdf_file_.Pass(), process); | |
| 55 return transit; | |
| 56 } | |
| 57 | |
| 58 const base::FilePath& GetBasePath() const { | |
| 59 return temp_dir_.path(); | |
| 60 } | |
| 61 | 42 |
| 62 private: | 43 private: |
| 63 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 44 void Close() const; |
| 64 friend class base::DeleteHelper<FileHandlers>; | 45 bool LoadEmf(Emf* emf) const; |
| 65 | 46 |
| 66 ~FileHandlers() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); } | 47 mutable TempFilePtr file_; // Mutable because of consts in base class. |
| 67 | 48 DISALLOW_COPY_AND_ASSIGN(LazyEmf); |
| 68 base::ScopedTempDir temp_dir_; | |
| 69 base::File pdf_file_; | |
| 70 }; | |
| 71 | |
| 72 void FileHandlers::Init(base::RefCountedMemory* data) { | |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 74 | |
| 75 if (!temp_dir_.CreateUniqueTempDir()) { | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 pdf_file_.Initialize(GetPdfPath(), | |
| 80 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | | |
| 81 base::File::FLAG_READ | | |
| 82 base::File::FLAG_DELETE_ON_CLOSE); | |
| 83 if (static_cast<int>(data->size()) != | |
| 84 pdf_file_.WriteAtCurrentPos(data->front_as<char>(), data->size())) { | |
| 85 pdf_file_.Close(); | |
| 86 return; | |
| 87 } | |
| 88 pdf_file_.Seek(base::File::FROM_BEGIN, 0); | |
| 89 pdf_file_.Flush(); | |
| 90 } | |
| 91 | |
| 92 bool FileHandlers::IsValid() { | |
| 93 return pdf_file_.IsValid(); | |
| 94 } | |
| 95 | |
| 96 // Modification of Emf to keep references to |FileHandlers|. | |
| 97 // |FileHandlers| must be deleted after the last metafile is closed because | |
| 98 // Emf holds files locked. | |
| 99 // Ideally we want to use FLAG_DELETE_ON_CLOSE, but it requires large changes. | |
| 100 // It's going to be done for crbug.com/408184 | |
| 101 class TempEmf : public Emf { | |
| 102 public: | |
| 103 explicit TempEmf(const scoped_refptr<FileHandlers>& files) : files_(files) {} | |
| 104 virtual ~TempEmf() {} | |
| 105 | |
| 106 virtual bool SafePlayback(HDC hdc) const OVERRIDE { | |
| 107 bool result = Emf::SafePlayback(hdc); | |
| 108 TempEmf* this_mutable = const_cast<TempEmf*>(this); | |
| 109 // TODO(vitalybuka): Fix destruction of metafiles. For some reasons | |
| 110 // instances of Emf are not deleted. crbug.com/411683 | |
| 111 // |files_| must be released as soon as possible to guarantee deletion. | |
| 112 // It's know that this Emf file is going to be played just once to | |
| 113 // a printer. So just release files here. | |
| 114 this_mutable->Close(); | |
| 115 this_mutable->files_ = NULL; | |
| 116 return result; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 scoped_refptr<FileHandlers> files_; | |
| 121 DISALLOW_COPY_AND_ASSIGN(TempEmf); | |
| 122 }; | 49 }; |
| 123 | 50 |
| 124 // Converts PDF into EMF. | 51 // Converts PDF into EMF. |
| 125 // Class uses 3 threads: UI, IO and FILE. | 52 // Class uses 3 threads: UI, IO and FILE. |
| 126 // Internal workflow is following: | 53 // Internal workflow is following: |
| 127 // 1. Create instance on the UI thread. (files_, settings_,) | 54 // 1. Create instance on the UI thread. (files_, settings_,) |
| 128 // 2. Create file on the FILE thread. | 55 // 2. Create pdf file on the FILE thread. |
| 129 // 3. Start utility process and start conversion on the IO thread. | 56 // 3. Start utility process and start conversion on the IO thread. |
| 130 // 4. Run result callback on the UI thread. | 57 // 4. For each page: |
| 131 // 5. Instance is destroyed from any thread that has the last reference. | 58 // 1. Utility is requesting open file. |
| 132 // 6. FileHandlers destroyed on the FILE thread. | 59 // 2. Utility converts the page and save to the file. |
| 133 // This step posts |FileHandlers| to be destroyed on the FILE thread. | 60 // 3. Utility sends notification on page conversion result. |
| 61 // 4. ProcessHostClient calls a page callback. | |
| 62 // 5. Utility sends notification if about document completion. | |
| 63 // 6. ProcessHostClient calls a document callback. | |
| 134 // All these steps work sequentially, so no data should be accessed | 64 // All these steps work sequentially, so no data should be accessed |
| 135 // simultaneously by several threads. | 65 // simultaneously by several threads. |
| 136 class PdfToEmfUtilityProcessHostClient | 66 class PdfToEmfUtilityProcessHostClient |
| 137 : public content::UtilityProcessHostClient { | 67 : public content::UtilityProcessHostClient { |
| 138 public: | 68 public: |
| 139 explicit PdfToEmfUtilityProcessHostClient( | 69 PdfToEmfUtilityProcessHostClient( |
| 70 base::WeakPtr<PdfToEmfConverterImpl> converter, | |
| 140 const printing::PdfRenderSettings& settings); | 71 const printing::PdfRenderSettings& settings); |
| 141 | 72 |
| 142 void Convert(base::RefCountedMemory* data, | 73 void Convert(const scoped_refptr<base::RefCountedMemory>& data, |
| 143 const PdfToEmfConverter::ResultCallback& callback); | 74 const PdfToEmfConverter::PdfLoadedCallback& document_callback); |
| 75 | |
| 76 void GetPage(int page_number, | |
| 77 const PdfToEmfConverter::PageCallback& page_callback); | |
| 144 | 78 |
| 145 // UtilityProcessHostClient implementation. | 79 // UtilityProcessHostClient implementation. |
| 146 virtual void OnProcessCrashed(int exit_code) OVERRIDE; | 80 virtual void OnProcessCrashed(int exit_code) OVERRIDE; |
| 81 virtual void OnProcessLaunchFailed() OVERRIDE; | |
| 147 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 82 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 148 | 83 |
| 84 bool Send(IPC::Message* msg); | |
| 85 | |
| 149 private: | 86 private: |
| 150 virtual ~PdfToEmfUtilityProcessHostClient(); | 87 virtual ~PdfToEmfUtilityProcessHostClient(); |
| 151 | 88 |
| 152 // Message handlers. | 89 // Message handlers. |
| 153 void OnProcessStarted(); | 90 void OnProcessStarted(); |
| 154 void OnSucceeded(const std::vector<printing::PageRange>& page_ranges, | 91 void OnPageCount(int page_count); |
| 155 double scale_factor); | 92 void OnPageDone(bool success, double scale_factor); |
| 93 | |
| 156 void OnFailed(); | 94 void OnFailed(); |
| 157 | 95 void OnTempPdfReady(TempFilePtr pdf); |
| 158 void RunCallback(const std::vector<printing::PageRange>& page_ranges, | 96 void OnTempEmfReady(int page_number, |
| 159 double scale_factor); | 97 const PdfToEmfConverter::PageCallback& page_callback, |
| 160 | 98 TempFilePtr emf); |
| 161 void StartProcessOnIOThread(); | 99 |
| 162 | 100 // Used to suppress callbacks after PdfToEmfConverterImpl is deleted. |
| 163 void RunCallbackOnUIThread( | 101 base::WeakPtr<PdfToEmfConverterImpl> converter_; |
| 164 const std::vector<printing::PageRange>& page_ranges, | |
| 165 double scale_factor); | |
| 166 void OnFilesReadyOnUIThread(); | |
| 167 | |
| 168 scoped_refptr<FileHandlers> files_; | |
| 169 printing::PdfRenderSettings settings_; | 102 printing::PdfRenderSettings settings_; |
| 170 PdfToEmfConverter::ResultCallback callback_; | 103 scoped_refptr<base::RefCountedMemory> data_; |
| 104 | |
| 105 // Final document callback. | |
| 106 PdfToEmfConverter::PdfLoadedCallback document_callback_; | |
| 107 | |
| 108 // Process host for IPC. | |
| 171 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 109 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 172 | 110 |
| 111 typedef base::Callback< | |
| 112 void(double scale_factor, scoped_ptr<MetafilePlayer> emf)> | |
| 113 PageCallbackInternal; | |
| 114 std::deque<std::pair<PageCallbackInternal, TempFilePtr> > page_callbacks_; | |
| 115 | |
| 173 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); | 116 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); |
| 174 }; | 117 }; |
| 175 | 118 |
| 119 class PdfToEmfConverterImpl : public PdfToEmfConverter { | |
| 120 public: | |
| 121 PdfToEmfConverterImpl(); | |
| 122 | |
| 123 virtual ~PdfToEmfConverterImpl(); | |
| 124 | |
| 125 virtual void Start(const scoped_refptr<base::RefCountedMemory>& data, | |
| 126 const printing::PdfRenderSettings& conversion_settings, | |
| 127 const PdfLoadedCallback& document_callback) OVERRIDE; | |
| 128 | |
| 129 virtual void GetPage(int page_number, | |
| 130 const PageCallback& page_callback) OVERRIDE; | |
| 131 | |
| 132 // Helps to cancel callbacks if this object is destroyed. | |
| 133 void RunCallback(const base::Closure& callback); | |
| 134 | |
| 135 private: | |
| 136 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_; | |
| 137 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | |
| 140 }; | |
| 141 | |
| 142 TempFilePtr CreateTempFile() { | |
| 143 TempFilePtr file; | |
| 144 base::FilePath path; | |
| 145 if (!base::CreateTemporaryFile(&path)) | |
| 146 return file.Pass(); | |
| 147 file.reset(new base::File(path, | |
| 148 base::File::FLAG_CREATE_ALWAYS | | |
| 149 base::File::FLAG_WRITE | base::File::FLAG_READ | | |
| 150 base::File::FLAG_DELETE_ON_CLOSE | | |
| 151 base::File::FLAG_TEMPORARY)); | |
| 152 if (!file->IsValid()) | |
| 153 file.reset(); | |
| 154 return file.Pass(); | |
| 155 } | |
| 156 | |
| 157 TempFilePtr CreateTempPdfFile( | |
| 158 const scoped_refptr<base::RefCountedMemory>& data) { | |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
|
Lei Zhang
2014/09/15 21:49:53
nit: DCHECK_CURRENTLY_ON()
Vitaly Buka (NO REVIEWS)
2014/09/15 22:33:49
Done.
| |
| 160 | |
| 161 TempFilePtr pdf_file = CreateTempFile(); | |
| 162 if (!pdf_file || | |
| 163 static_cast<int>(data->size()) != | |
| 164 pdf_file->WriteAtCurrentPos(data->front_as<char>(), data->size())) { | |
| 165 pdf_file.reset(); | |
| 166 } | |
| 167 pdf_file->Seek(base::File::FROM_BEGIN, 0); | |
| 168 return pdf_file.Pass(); | |
| 169 } | |
| 170 | |
| 171 bool LazyEmf::SafePlayback(HDC hdc) const { | |
| 172 Emf emf; | |
| 173 bool result = LoadEmf(&emf) && emf.SafePlayback(hdc); | |
| 174 // TODO(vitalybuka): Fix destruction of metafiles. For some reasons | |
| 175 // instances of Emf are not deleted. crbug.com/411683 | |
| 176 // It's known that the Emf going to be played just once to a printer. So just | |
| 177 // release file here. | |
| 178 Close(); | |
| 179 return result; | |
| 180 } | |
| 181 | |
| 182 bool LazyEmf::SaveTo(base::File* file) const { | |
| 183 Emf emf; | |
| 184 return LoadEmf(&emf) && emf.SaveTo(file); | |
| 185 } | |
| 186 | |
| 187 void LazyEmf::Close() const { | |
| 188 file_.reset(); | |
| 189 } | |
| 190 | |
| 191 bool LazyEmf::LoadEmf(Emf* emf) const { | |
| 192 file_->Seek(base::File::FROM_BEGIN, 0); | |
| 193 int64 size = file_->GetLength(); | |
| 194 if (size <= 0) | |
| 195 return false; | |
| 196 std::vector<char> data(size); | |
| 197 if (file_->ReadAtCurrentPos(&data[0], data.size()) != size) | |
|
Lei Zhang
2014/09/15 21:49:53
nit: &data[0] -> data.front()
Vitaly Buka (NO REVIEWS)
2014/09/15 22:33:49
std::vector::data()?
Done.
| |
| 198 return false; | |
| 199 return emf->InitFromData(&data[0], data.size()); | |
| 200 } | |
| 201 | |
| 176 PdfToEmfUtilityProcessHostClient::PdfToEmfUtilityProcessHostClient( | 202 PdfToEmfUtilityProcessHostClient::PdfToEmfUtilityProcessHostClient( |
| 203 base::WeakPtr<PdfToEmfConverterImpl> converter, | |
| 177 const printing::PdfRenderSettings& settings) | 204 const printing::PdfRenderSettings& settings) |
| 178 : settings_(settings) {} | 205 : converter_(converter), settings_(settings) { |
| 206 } | |
| 179 | 207 |
| 180 PdfToEmfUtilityProcessHostClient::~PdfToEmfUtilityProcessHostClient() { | 208 PdfToEmfUtilityProcessHostClient::~PdfToEmfUtilityProcessHostClient() { |
| 181 } | 209 } |
| 182 | 210 |
| 183 void PdfToEmfUtilityProcessHostClient::Convert( | 211 void PdfToEmfUtilityProcessHostClient::Convert( |
| 184 base::RefCountedMemory* data, | 212 const scoped_refptr<base::RefCountedMemory>& data, |
| 185 const PdfToEmfConverter::ResultCallback& callback) { | 213 const PdfToEmfConverter::PdfLoadedCallback& document_callback) { |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 214 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 187 callback_ = callback; | 215 BrowserThread::PostTask( |
|
Lei Zhang
2014/09/15 21:49:53
Can we just call Convert() on the IO thread from a
Vitaly Buka (NO REVIEWS)
2014/09/15 22:33:49
I realy like this way. We need to Post anyway, but
Lei Zhang
2014/09/16 03:36:53
But don't you only have 1 caller? Just make the ca
| |
| 188 CHECK(!files_.get()); | 216 BrowserThread::IO, |
| 189 files_ = new FileHandlers(); | 217 FROM_HERE, |
| 190 BrowserThread::PostTaskAndReply( | 218 base::Bind(&PdfToEmfUtilityProcessHostClient::Convert, |
| 219 this, | |
| 220 data, | |
| 221 document_callback)); | |
| 222 return; | |
| 223 } | |
| 224 data_ = data; | |
| 225 document_callback_ = document_callback; | |
| 226 | |
| 227 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load | |
| 228 // gdiplus.dll, change how rendering happens, and not be able to correctly | |
| 229 // generate when sent to a metafile DC. | |
| 230 utility_process_host_ = | |
| 231 content::UtilityProcessHost::Create( | |
| 232 this, base::MessageLoop::current()->message_loop_proxy()) | |
| 233 ->AsWeakPtr(); | |
| 234 if (!utility_process_host_) | |
| 235 return OnFailed(); | |
| 236 Send(new ChromeUtilityMsg_StartupPing); | |
| 237 } | |
| 238 | |
| 239 void PdfToEmfUtilityProcessHostClient::GetPage( | |
| 240 int page_number, | |
| 241 const PdfToEmfConverter::PageCallback& page_callback) { | |
| 242 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | |
| 243 BrowserThread::PostTask( | |
| 244 BrowserThread::IO, | |
| 245 FROM_HERE, | |
| 246 base::Bind(&PdfToEmfUtilityProcessHostClient::GetPage, | |
| 247 this, | |
| 248 page_number, | |
| 249 page_callback)); | |
| 250 return; | |
| 251 } | |
| 252 | |
| 253 BrowserThread::PostTaskAndReplyWithResult( | |
| 191 BrowserThread::FILE, | 254 BrowserThread::FILE, |
| 192 FROM_HERE, | 255 FROM_HERE, |
| 193 base::Bind(&FileHandlers::Init, files_, make_scoped_refptr(data)), | 256 base::Bind(&CreateTempFile), |
| 194 base::Bind(&PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread, | 257 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempEmfReady, |
| 195 this)); | 258 this, |
| 259 page_number, | |
| 260 page_callback)); | |
| 196 } | 261 } |
| 197 | 262 |
| 198 void PdfToEmfUtilityProcessHostClient::OnProcessCrashed(int exit_code) { | 263 void PdfToEmfUtilityProcessHostClient::OnProcessCrashed(int exit_code) { |
| 199 OnFailed(); | 264 OnFailed(); |
| 200 } | 265 } |
| 201 | 266 |
| 267 void PdfToEmfUtilityProcessHostClient::OnProcessLaunchFailed() { | |
| 268 OnFailed(); | |
| 269 } | |
| 270 | |
| 202 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( | 271 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( |
| 203 const IPC::Message& message) { | 272 const IPC::Message& message) { |
| 204 bool handled = true; | 273 bool handled = true; |
| 205 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) | 274 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) |
| 206 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) | 275 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) |
| 207 IPC_MESSAGE_HANDLER( | 276 IPC_MESSAGE_HANDLER( |
| 208 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, OnSucceeded) | 277 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount) |
| 209 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | 278 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, |
| 210 OnFailed) | 279 OnPageDone) |
| 211 IPC_MESSAGE_UNHANDLED(handled = false) | 280 IPC_MESSAGE_UNHANDLED(handled = false) |
| 212 IPC_END_MESSAGE_MAP() | 281 IPC_END_MESSAGE_MAP() |
| 213 return handled; | 282 return handled; |
| 214 } | 283 } |
| 215 | 284 |
| 285 bool PdfToEmfUtilityProcessHostClient::Send(IPC::Message* msg) { | |
| 286 if (utility_process_host_) | |
| 287 return utility_process_host_->Send(msg); | |
| 288 delete msg; | |
| 289 return false; | |
| 290 } | |
| 291 | |
| 216 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() { | 292 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() { |
| 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 293 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 218 if (!utility_process_host_) { | 294 if (!utility_process_host_) |
| 219 RunCallbackOnUIThread(std::vector<printing::PageRange>(), 0.0); | 295 return OnFailed(); |
| 220 return; | |
| 221 } | |
| 222 | 296 |
| 223 base::ProcessHandle process = utility_process_host_->GetData().handle; | 297 base::ProcessHandle process = utility_process_host_->GetData().handle; |
| 224 utility_process_host_->Send( | 298 scoped_refptr<base::RefCountedMemory> data = data_; |
| 225 new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | 299 data_ = NULL; |
| 226 files_->GetPdfForProcess(process), | 300 BrowserThread::PostTaskAndReplyWithResult( |
| 227 files_->GetEmfPath(), | 301 BrowserThread::FILE, |
| 228 settings_, | |
| 229 std::vector<printing::PageRange>())); | |
| 230 utility_process_host_.reset(); | |
| 231 } | |
| 232 | |
| 233 void PdfToEmfUtilityProcessHostClient::OnSucceeded( | |
| 234 const std::vector<printing::PageRange>& page_ranges, | |
| 235 double scale_factor) { | |
| 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 237 RunCallback(page_ranges, scale_factor); | |
| 238 } | |
| 239 | |
| 240 void PdfToEmfUtilityProcessHostClient::OnFailed() { | |
| 241 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 242 RunCallback(std::vector<printing::PageRange>(), 0.0); | |
| 243 } | |
| 244 | |
| 245 void PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread() { | |
| 246 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 247 if (!files_->IsValid()) { | |
| 248 RunCallbackOnUIThread(std::vector<printing::PageRange>(), 0.0); | |
| 249 return; | |
| 250 } | |
| 251 BrowserThread::PostTask( | |
| 252 BrowserThread::IO, | |
| 253 FROM_HERE, | 302 FROM_HERE, |
| 254 base::Bind(&PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread, | 303 base::Bind(&CreateTempPdfFile, data), |
| 255 this)); | 304 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this)); |
| 256 } | 305 } |
| 257 | 306 |
| 258 void PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread() { | 307 void PdfToEmfUtilityProcessHostClient::OnPageCount(int page_count) { |
| 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 260 utility_process_host_ = | 309 if (document_callback_.is_null()) |
| 261 content::UtilityProcessHost::Create( | 310 return OnFailed(); |
| 262 this, | |
| 263 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr(); | |
| 264 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load | |
| 265 // gdiplus.dll, change how rendering happens, and not be able to correctly | |
| 266 // generate when sent to a metafile DC. | |
| 267 utility_process_host_->SetExposedDir(files_->GetBasePath()); | |
| 268 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); | |
| 269 } | |
| 270 | |
| 271 void PdfToEmfUtilityProcessHostClient::RunCallback( | |
| 272 const std::vector<printing::PageRange>& page_ranges, | |
| 273 double scale_factor) { | |
| 274 BrowserThread::PostTask( | 311 BrowserThread::PostTask( |
| 275 BrowserThread::UI, | 312 BrowserThread::UI, |
| 276 FROM_HERE, | 313 FROM_HERE, |
| 277 base::Bind(&PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread, | 314 base::Bind(&PdfToEmfConverterImpl::RunCallback, |
| 278 this, | 315 converter_, |
| 279 page_ranges, | 316 base::Bind(document_callback_, page_count))); |
| 280 scale_factor)); | 317 document_callback_.Reset(); |
| 281 } | 318 } |
| 282 | 319 |
| 283 void PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread( | 320 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, |
| 284 const std::vector<printing::PageRange>& page_ranges, | 321 double scale_factor) { |
| 285 double scale_factor) { | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 323 if (page_callbacks_.empty()) | |
| 324 return OnFailed(); | |
| 325 scoped_ptr<LazyEmf> emf; | |
| 326 if (success) | |
| 327 emf.reset(new LazyEmf(page_callbacks_.front().second.Pass())); | |
| 328 BrowserThread::PostTask(BrowserThread::UI, | |
| 329 FROM_HERE, | |
| 330 base::Bind(&PdfToEmfConverterImpl::RunCallback, | |
| 331 converter_, | |
| 332 base::Bind(page_callbacks_.front().first, | |
| 333 scale_factor, | |
| 334 base::Passed(&emf)))); | |
| 335 page_callbacks_.pop_front(); | |
| 336 } | |
| 337 | |
| 338 void PdfToEmfUtilityProcessHostClient::OnFailed() { | |
| 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 340 if (!document_callback_.is_null()) | |
| 341 OnPageCount(0); | |
| 342 while (!page_callbacks_.empty()) | |
| 343 OnPageDone(false, 0.0); | |
| 344 } | |
| 345 | |
| 346 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(TempFilePtr pdf) { | |
| 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 348 if (!utility_process_host_) | |
| 349 return OnFailed(); | |
| 350 base::ProcessHandle process = utility_process_host_->GetData().handle; | |
| 351 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | |
| 352 IPC::GetFileHandleForProcess(pdf->GetPlatformFile(), process, false), | |
| 353 settings_)); | |
| 354 } | |
| 355 | |
| 356 void PdfToEmfUtilityProcessHostClient::OnTempEmfReady( | |
| 357 int page_number, | |
| 358 const PdfToEmfConverter::PageCallback& page_callback, | |
| 359 TempFilePtr emf) { | |
| 360 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 361 if (!utility_process_host_) | |
| 362 return OnFailed(); | |
| 363 base::ProcessHandle process = utility_process_host_->GetData().handle; | |
| 364 IPC::PlatformFileForTransit transit = | |
| 365 IPC::GetFileHandleForProcess(emf->GetPlatformFile(), process, false); | |
| 366 page_callbacks_.push_back( | |
| 367 std::make_pair(base::Bind(page_callback, page_number), emf.Pass())); | |
| 368 Send(new ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_GetPage(page_number, | |
| 369 transit)); | |
| 370 } | |
| 371 | |
| 372 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) { | |
| 373 } | |
| 374 | |
| 375 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | |
| 376 } | |
| 377 | |
| 378 void PdfToEmfConverterImpl::Start( | |
| 379 const scoped_refptr<base::RefCountedMemory>& data, | |
| 380 const printing::PdfRenderSettings& conversion_settings, | |
| 381 const PdfLoadedCallback& document_callback) { | |
| 382 DCHECK(!utility_client_); | |
| 383 utility_client_ = new PdfToEmfUtilityProcessHostClient( | |
| 384 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | |
| 385 utility_client_->Convert(data, document_callback); | |
| 386 } | |
| 387 | |
| 388 void PdfToEmfConverterImpl::GetPage(int page_number, | |
| 389 const PageCallback& page_callback) { | |
| 390 utility_client_->GetPage(page_number, page_callback); | |
| 391 } | |
| 392 | |
| 393 void PdfToEmfConverterImpl::RunCallback(const base::Closure& callback) { | |
| 286 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 394 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 287 ScopedVector<MetafilePlayer> pages; | 395 callback.Run(); |
| 288 std::vector<printing::PageRange>::const_iterator iter; | |
| 289 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) { | |
| 290 for (int page_number = iter->from; page_number <= iter->to; ++page_number) { | |
| 291 scoped_ptr<TempEmf> metafile(new TempEmf(files_)); | |
| 292 if (!metafile->InitFromFile(files_->GetEmfPagePath(page_number))) { | |
| 293 NOTREACHED() << "Invalid metafile"; | |
| 294 metafile.reset(); | |
| 295 } | |
| 296 pages.push_back(metafile.release()); | |
| 297 } | |
| 298 } | |
| 299 files_ = NULL; | |
| 300 if (!callback_.is_null()) { | |
| 301 callback_.Run(scale_factor, &pages); | |
| 302 callback_.Reset(); | |
| 303 } | |
| 304 } | |
| 305 | |
| 306 class PdfToEmfConverterImpl : public PdfToEmfConverter { | |
| 307 public: | |
| 308 PdfToEmfConverterImpl(); | |
| 309 | |
| 310 virtual ~PdfToEmfConverterImpl(); | |
| 311 | |
| 312 virtual void Start(base::RefCountedMemory* data, | |
| 313 const printing::PdfRenderSettings& conversion_settings, | |
| 314 const ResultCallback& callback) OVERRIDE; | |
| 315 | |
| 316 private: | |
| 317 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_; | |
| 318 base::CancelableCallback<ResultCallback::RunType> callback_; | |
| 319 | |
| 320 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | |
| 321 }; | |
| 322 | |
| 323 PdfToEmfConverterImpl::PdfToEmfConverterImpl() { | |
| 324 } | |
| 325 | |
| 326 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | |
| 327 } | |
| 328 | |
| 329 void PdfToEmfConverterImpl::Start( | |
| 330 base::RefCountedMemory* data, | |
| 331 const printing::PdfRenderSettings& conversion_settings, | |
| 332 const ResultCallback& callback) { | |
| 333 // Rebind cancelable callback to avoid calling callback if | |
| 334 // PdfToEmfConverterImpl is destroyed. | |
| 335 callback_.Reset(callback); | |
| 336 utility_client_ = new PdfToEmfUtilityProcessHostClient(conversion_settings); | |
| 337 utility_client_->Convert(data, callback_.callback()); | |
| 338 } | 396 } |
| 339 | 397 |
| 340 } // namespace | 398 } // namespace |
| 341 | 399 |
| 342 // static | 400 // static |
| 343 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { | 401 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { |
| 344 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); | 402 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); |
| 345 } | 403 } |
| 346 | 404 |
| 347 } // namespace printing | 405 } // namespace printing |
| OLD | NEW |