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 <stdint.h> | 7 #include <stdint.h> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 12 matching lines...) Expand all Loading... | |
23 #include "chrome/common/chrome_utility_printing_messages.h" | 23 #include "chrome/common/chrome_utility_printing_messages.h" |
24 #include "chrome/grit/generated_resources.h" | 24 #include "chrome/grit/generated_resources.h" |
25 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
26 #include "content/public/browser/child_process_data.h" | 26 #include "content/public/browser/child_process_data.h" |
27 #include "content/public/browser/utility_process_host.h" | 27 #include "content/public/browser/utility_process_host.h" |
28 #include "content/public/browser/utility_process_host_client.h" | 28 #include "content/public/browser/utility_process_host_client.h" |
29 #include "printing/emf_win.h" | 29 #include "printing/emf_win.h" |
30 #include "printing/pdf_render_settings.h" | 30 #include "printing/pdf_render_settings.h" |
31 #include "ui/base/l10n/l10n_util.h" | 31 #include "ui/base/l10n/l10n_util.h" |
32 | 32 |
33 using content::BrowserThread; | |
34 | |
33 namespace printing { | 35 namespace printing { |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 using content::BrowserThread; | 39 class PdfConverterImpl; |
38 | |
39 class PdfToEmfConverterImpl; | |
40 | 40 |
41 // Allows to delete temporary directory after all temporary files created inside | 41 // Allows to delete temporary directory after all temporary files created inside |
42 // are closed. Windows cannot delete directory with opened files. Directory is | 42 // are closed. Windows cannot delete directory with opened files. Directory is |
43 // used to store PDF and metafiles. PDF should be gone by the time utility | 43 // used to store PDF and metafiles. PDF should be gone by the time utility |
44 // process exits. Metafiles should be gone when all LazyEmf destroyed. | 44 // process exits. Metafiles should be gone when all LazyEmf destroyed. |
45 class RefCountedTempDir | 45 class RefCountedTempDir |
46 : public base::RefCountedThreadSafe<RefCountedTempDir, | 46 : public base::RefCountedThreadSafe<RefCountedTempDir, |
47 BrowserThread::DeleteOnFileThread> { | 47 BrowserThread::DeleteOnFileThread> { |
48 public: | 48 public: |
49 RefCountedTempDir() { ignore_result(temp_dir_.CreateUniqueTempDir()); } | 49 RefCountedTempDir() { ignore_result(temp_dir_.CreateUniqueTempDir()); } |
50 bool IsValid() const { return temp_dir_.IsValid(); } | 50 bool IsValid() const { return temp_dir_.IsValid(); } |
51 const base::FilePath& GetPath() const { return temp_dir_.GetPath(); } | 51 const base::FilePath& GetPath() const { return temp_dir_.GetPath(); } |
52 | 52 |
53 private: | 53 private: |
54 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | 54 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; |
55 friend class base::DeleteHelper<RefCountedTempDir>; | 55 friend class base::DeleteHelper<RefCountedTempDir>; |
56 ~RefCountedTempDir() {} | 56 ~RefCountedTempDir() {} |
57 | 57 |
58 base::ScopedTempDir temp_dir_; | 58 base::ScopedTempDir temp_dir_; |
59 DISALLOW_COPY_AND_ASSIGN(RefCountedTempDir); | 59 DISALLOW_COPY_AND_ASSIGN(RefCountedTempDir); |
60 }; | 60 }; |
61 | 61 |
62 typedef std::unique_ptr<base::File, BrowserThread::DeleteOnFileThread> | 62 using ScopedTempFile = |
63 ScopedTempFile; | 63 std::unique_ptr<base::File, BrowserThread::DeleteOnFileThread>; |
64 | 64 |
65 // Wrapper for Emf to keep only file handle in memory, and load actual data only | 65 // Wrapper for Emf to keep only file handle in memory, and load actual data only |
66 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it | 66 // on playback. Emf::InitFromFile() can play metafile directly from disk, but it |
67 // can't open file handles. We need file handles to reliably delete temporary | 67 // can't open file handles. We need file handles to reliably delete temporary |
68 // files, and to efficiently interact with utility process. | 68 // files, and to efficiently interact with utility process. |
69 class LazyEmf : public MetafilePlayer { | 69 class LazyEmf : public MetafilePlayer { |
70 public: | 70 public: |
71 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file) | 71 LazyEmf(const scoped_refptr<RefCountedTempDir>& temp_dir, ScopedTempFile file) |
72 : temp_dir_(temp_dir), file_(std::move(file)) { | 72 : temp_dir_(temp_dir), file_(std::move(file)) { |
73 CHECK(file_); | 73 CHECK(file_); |
74 } | 74 } |
75 ~LazyEmf() override { Close(); } | 75 ~LazyEmf() override { Close(); } |
76 | 76 |
77 protected: | |
78 // MetafilePlayer: | |
77 bool SafePlayback(HDC hdc) const override; | 79 bool SafePlayback(HDC hdc) const override; |
78 bool GetDataAsVector(std::vector<char>* buffer) const override; | |
79 bool SaveTo(base::File* file) const override; | |
80 | 80 |
81 private: | |
82 void Close() const; | 81 void Close() const; |
83 bool LoadEmf(Emf* emf) const; | 82 bool LoadEmf(Emf* emf) const; |
84 | 83 |
84 private: | |
85 mutable scoped_refptr<RefCountedTempDir> temp_dir_; | 85 mutable scoped_refptr<RefCountedTempDir> temp_dir_; |
86 mutable ScopedTempFile file_; // Mutable because of consts in base class. | 86 mutable ScopedTempFile file_; // Mutable because of consts in base class. |
87 | 87 |
88 bool GetDataAsVector(std::vector<char>* buffer) const override; | |
89 bool SaveTo(base::File* file) const override; | |
90 | |
88 DISALLOW_COPY_AND_ASSIGN(LazyEmf); | 91 DISALLOW_COPY_AND_ASSIGN(LazyEmf); |
89 }; | 92 }; |
90 | 93 |
91 // Converts PDF into EMF. | 94 class PdfConverterUtilityProcessHostClient |
95 : public content::UtilityProcessHostClient { | |
Vitaly Buka (NO REVIEWS)
2017/01/19 00:35:09
Class comments should go before class, not inside.
rbpotter
2017/01/19 02:46:15
Done.
| |
96 // Class for converting PDF to another format for printing (Emf, Postscript). | |
92 // Class uses 3 threads: UI, IO and FILE. | 97 // Class uses 3 threads: UI, IO and FILE. |
93 // Internal workflow is following: | 98 // Internal workflow is following: |
94 // 1. Create instance on the UI thread. (files_, settings_,) | 99 // 1. Create instance on the UI thread. (files_, settings_,) |
95 // 2. Create pdf file on the FILE thread. | 100 // 2. Create pdf file on the FILE thread. |
96 // 3. Start utility process and start conversion on the IO thread. | 101 // 3. Start utility process and start conversion on the IO thread. |
97 // 4. Utility process returns page count. | 102 // 4. Utility process returns page count. |
98 // 5. For each page: | 103 // 5. For each page: |
99 // 1. Clients requests page with file handle to a temp file. | 104 // 1. Clients requests page with file handle to a temp file. |
100 // 2. Utility converts the page, save it to the file and reply. | 105 // 2. Utility converts the page, save it to the file and reply. |
101 // | 106 // |
102 // All these steps work sequentially, so no data should be accessed | 107 // All these steps work sequentially, so no data should be accessed |
103 // simultaneously by several threads. | 108 // simultaneously by several threads. |
104 class PdfToEmfUtilityProcessHostClient | |
105 : public content::UtilityProcessHostClient { | |
106 public: | 109 public: |
Vitaly Buka (NO REVIEWS)
2017/01/19 00:35:09
CL needs "git cl format"
rbpotter
2017/01/19 02:46:15
Done.
| |
107 PdfToEmfUtilityProcessHostClient( | 110 PdfConverterUtilityProcessHostClient( |
108 base::WeakPtr<PdfToEmfConverterImpl> converter, | 111 base::WeakPtr<PdfConverterImpl> converter, |
109 const PdfRenderSettings& settings); | 112 const PdfRenderSettings& settings); |
110 | 113 |
111 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 114 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
112 bool print_text_with_gdi, | 115 bool print_text_with_gdi, |
113 const PdfToEmfConverter::StartCallback& start_callback); | 116 const PdfConverter::StartCallback& start_callback); |
114 | 117 |
115 void GetPage(int page_number, | 118 void GetPage(int page_number, |
116 const PdfToEmfConverter::GetPageCallback& get_page_callback); | 119 const PdfConverter::GetPageCallback& get_page_callback); |
117 | 120 |
118 void Stop(); | 121 void Stop(); |
119 | 122 |
123 // UtilityProcessHostClient implementation. | |
124 void OnProcessCrashed(int exit_code) override; | |
125 void OnProcessLaunchFailed(int exit_code) override; | |
126 | |
120 // Needs to be public to handle ChromeUtilityHostMsg_PreCacheFontCharacters | 127 // Needs to be public to handle ChromeUtilityHostMsg_PreCacheFontCharacters |
121 // sync message replies. | 128 // sync message replies. |
122 bool Send(IPC::Message* msg); | 129 bool Send(IPC::Message* msg); |
123 | 130 protected: |
124 // UtilityProcessHostClient implementation. | |
125 void OnProcessCrashed(int exit_code) override; | |
126 void OnProcessLaunchFailed(int exit_code) override; | |
127 bool OnMessageReceived(const IPC::Message& message) override; | |
128 | |
129 private: | |
130 class GetPageCallbackData { | 131 class GetPageCallbackData { |
131 public: | 132 public: |
132 GetPageCallbackData(int page_number, | 133 GetPageCallbackData(int page_number, |
133 PdfToEmfConverter::GetPageCallback callback) | 134 PdfConverter::GetPageCallback callback) |
134 : page_number_(page_number), callback_(callback) {} | 135 : page_number_(page_number), callback_(callback) {} |
135 | 136 |
136 GetPageCallbackData(GetPageCallbackData&& other) { | 137 GetPageCallbackData(GetPageCallbackData&& other) { |
137 *this = std::move(other); | 138 *this = std::move(other); |
138 } | 139 } |
139 | 140 |
140 GetPageCallbackData& operator=(GetPageCallbackData&& rhs) { | 141 GetPageCallbackData& operator=(GetPageCallbackData&& rhs) { |
141 page_number_ = rhs.page_number_; | 142 page_number_ = rhs.page_number_; |
142 callback_ = rhs.callback_; | 143 callback_ = rhs.callback_; |
143 emf_ = std::move(rhs.emf_); | 144 file_ = std::move(rhs.file_); |
144 return *this; | 145 return *this; |
145 } | 146 } |
146 | 147 |
147 int page_number() const { return page_number_; } | 148 int page_number() const { return page_number_; } |
148 const PdfToEmfConverter::GetPageCallback& callback() const { | 149 const PdfConverter::GetPageCallback& callback() const { |
149 return callback_; | 150 return callback_; |
150 } | 151 } |
151 ScopedTempFile TakeEmf() { return std::move(emf_); } | 152 ScopedTempFile TakeFile() { return std::move(file_); } |
152 void set_emf(ScopedTempFile emf) { emf_ = std::move(emf); } | 153 void set_file(ScopedTempFile file) { file_ = std::move(file); } |
153 | 154 |
154 private: | 155 private: |
155 int page_number_; | 156 int page_number_; |
156 PdfToEmfConverter::GetPageCallback callback_; | 157 |
157 ScopedTempFile emf_; | 158 PdfConverter::GetPageCallback callback_; |
159 ScopedTempFile file_; | |
158 | 160 |
159 DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData); | 161 DISALLOW_COPY_AND_ASSIGN(GetPageCallbackData); |
160 }; | 162 }; |
161 | 163 |
162 ~PdfToEmfUtilityProcessHostClient() override; | 164 ~PdfConverterUtilityProcessHostClient() {} |
163 | 165 |
164 // Message handlers. | 166 // Helper functions: must be overridden by subclasses |
167 // Set the process name | |
168 virtual base::string16 GetName() const = 0; | |
169 // Create a metafileplayer subclass file from a temporary file. | |
170 virtual std::unique_ptr<MetafilePlayer> GetFileFromTemp( | |
171 std::unique_ptr<base::File, | |
172 content::BrowserThread::DeleteOnFileThread> temp_file) = 0; | |
173 // Send the messages to Start, GetPage, and Stop. | |
174 virtual void SendStartMessage(IPC::PlatformFileForTransit transit, | |
175 bool print_text_with_gdi) = 0; | |
176 virtual void SendGetPageMessage(int page_number, | |
177 IPC::PlatformFileForTransit transit) = 0; | |
178 virtual void SendStopMessage() = 0; | |
179 | |
180 // Message handlers: | |
165 void OnPageCount(int page_count); | 181 void OnPageCount(int page_count); |
166 void OnPageDone(bool success, float scale_factor); | 182 void OnPageDone(bool success, float scale_factor); |
167 void OnPreCacheFontCharacters(const LOGFONT& log_font, | |
168 const base::string16& characters); | |
169 | 183 |
170 void OnFailed(); | 184 void OnFailed(); |
185 void OnTempFileReady(GetPageCallbackData* callback_data, | |
186 ScopedTempFile temp_file); | |
171 void OnTempPdfReady(bool print_text_with_gdi, ScopedTempFile pdf); | 187 void OnTempPdfReady(bool print_text_with_gdi, ScopedTempFile pdf); |
172 void OnTempEmfReady(GetPageCallbackData* callback_data, ScopedTempFile emf); | |
173 | 188 |
174 scoped_refptr<RefCountedTempDir> temp_dir_; | 189 scoped_refptr<RefCountedTempDir> temp_dir_; |
175 | 190 |
176 // Used to suppress callbacks after PdfToEmfConverterImpl is deleted. | 191 // Used to suppress callbacks after PdfConverter is deleted. |
177 base::WeakPtr<PdfToEmfConverterImpl> converter_; | 192 base::WeakPtr<PdfConverterImpl> converter_; |
178 PdfRenderSettings settings_; | 193 PdfRenderSettings settings_; |
179 | 194 |
180 // Document loaded callback. | 195 // Document loaded callback. |
181 PdfToEmfConverter::StartCallback start_callback_; | 196 PdfConverter::StartCallback start_callback_; |
182 | 197 |
183 // Process host for IPC. | 198 // Process host for IPC. |
184 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 199 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
185 | 200 |
186 // Queue of callbacks for GetPage() requests. Utility process should reply | 201 // Queue of callbacks for GetPage() requests. Utility process should reply |
187 // with PageDone in the same order as requests were received. | 202 // with PageDone in the same order as requests were received. |
188 // Use containers that keeps element pointers valid after push() and pop(). | 203 // Use containers that keeps element pointers valid after push() and pop(). |
189 typedef std::queue<GetPageCallbackData> GetPageCallbacks; | 204 using GetPageCallbacks = std::queue<GetPageCallbackData>; |
190 GetPageCallbacks get_page_callbacks_; | 205 GetPageCallbacks get_page_callbacks_; |
206 }; | |
207 | |
208 // Converts PDF into Emf. | |
209 class PdfToEmfUtilityProcessHostClient | |
210 : public PdfConverterUtilityProcessHostClient { | |
211 public: | |
212 PdfToEmfUtilityProcessHostClient( | |
213 base::WeakPtr<PdfConverterImpl> converter, | |
214 const PdfRenderSettings& settings) | |
215 : PdfConverterUtilityProcessHostClient(converter, settings) {} | |
216 | |
217 bool OnMessageReceived(const IPC::Message& message) override; | |
218 | |
219 private: | |
220 // Helpers to send messages and set process name | |
221 base::string16 GetName() const override; | |
222 std::unique_ptr<MetafilePlayer> GetFileFromTemp( | |
223 std::unique_ptr<base::File, | |
224 content::BrowserThread::DeleteOnFileThread> temp_file) | |
225 override; | |
226 void SendStartMessage(IPC::PlatformFileForTransit transit, | |
227 bool print_text_with_gdi) override; | |
228 void SendGetPageMessage(int page_number, | |
229 IPC::PlatformFileForTransit transit) override; | |
230 void SendStopMessage() override; | |
231 | |
232 // Additional message handler needed for Pdf to Emf | |
233 void OnPreCacheFontCharacters(const LOGFONT& log_font, | |
234 const base::string16& characters); | |
191 | 235 |
192 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); | 236 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient); |
193 }; | 237 }; |
194 | 238 |
195 class PdfToEmfConverterImpl : public PdfToEmfConverter { | 239 class PdfConverterImpl: public PdfConverter { |
196 public: | 240 public: |
197 PdfToEmfConverterImpl(); | 241 PdfConverterImpl(); |
198 | 242 |
199 ~PdfToEmfConverterImpl() override; | 243 ~PdfConverterImpl() override; |
200 | 244 |
201 void Start(const scoped_refptr<base::RefCountedMemory>& data, | 245 void Start(const scoped_refptr<base::RefCountedMemory>& data, |
202 const PdfRenderSettings& conversion_settings, | 246 const PdfRenderSettings& conversion_settings, |
203 bool print_text_with_gdi, | 247 bool print_text_with_gdi, |
204 const StartCallback& start_callback) override; | 248 const StartCallback& start_callback) override; |
205 | 249 |
206 void GetPage(int page_number, | 250 void GetPage(int page_number, |
207 const GetPageCallback& get_page_callback) override; | 251 const GetPageCallback& get_page_callback) override; |
208 | 252 |
209 // Helps to cancel callbacks if this object is destroyed. | 253 // Helps to cancel callbacks if this object is destroyed. |
210 void RunCallback(const base::Closure& callback); | 254 void RunCallback(const base::Closure& callback); |
211 | 255 |
256 protected: | |
257 scoped_refptr<PdfConverterUtilityProcessHostClient> utility_client_; | |
258 | |
212 private: | 259 private: |
213 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_; | 260 DISALLOW_COPY_AND_ASSIGN(PdfConverterImpl); |
261 }; | |
262 | |
263 class PdfToEmfConverterImpl : public PdfConverterImpl { | |
264 public: | |
265 PdfToEmfConverterImpl(); | |
266 | |
267 ~PdfToEmfConverterImpl() {} | |
268 | |
269 void Start(const scoped_refptr<base::RefCountedMemory>& data, | |
270 const PdfRenderSettings& conversion_settings, | |
271 bool print_text_with_gdi, | |
272 const StartCallback& start_callback) override; | |
273 | |
274 private: | |
214 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; | 275 base::WeakPtrFactory<PdfToEmfConverterImpl> weak_ptr_factory_; |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); | 276 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl); |
217 }; | 277 }; |
218 | 278 |
219 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { | 279 ScopedTempFile CreateTempFile(scoped_refptr<RefCountedTempDir>* temp_dir) { |
220 if (!temp_dir->get()) | 280 if (!temp_dir->get()) |
221 *temp_dir = new RefCountedTempDir(); | 281 *temp_dir = new RefCountedTempDir(); |
222 ScopedTempFile file; | 282 ScopedTempFile file; |
223 if (!(*temp_dir)->IsValid()) | 283 if (!(*temp_dir)->IsValid()) |
224 return file; | 284 return file; |
225 base::FilePath path; | 285 base::FilePath path; |
(...skipping 27 matching lines...) Expand all Loading... | |
253 pdf_file.reset(); | 313 pdf_file.reset(); |
254 return pdf_file; | 314 return pdf_file; |
255 } | 315 } |
256 pdf_file->Seek(base::File::FROM_BEGIN, 0); | 316 pdf_file->Seek(base::File::FROM_BEGIN, 0); |
257 return pdf_file; | 317 return pdf_file; |
258 } | 318 } |
259 | 319 |
260 bool LazyEmf::SafePlayback(HDC hdc) const { | 320 bool LazyEmf::SafePlayback(HDC hdc) const { |
261 Emf emf; | 321 Emf emf; |
262 bool result = LoadEmf(&emf) && emf.SafePlayback(hdc); | 322 bool result = LoadEmf(&emf) && emf.SafePlayback(hdc); |
263 // TODO(vitalybuka): Fix destruction of metafiles. For some reasons | 323 // TODO(thestig): Fix destruction of metafiles. For some reasons |
264 // instances of Emf are not deleted. crbug.com/411683 | 324 // instances of Emf are not deleted. https://crbug.com/260806 |
265 // It's known that the Emf going to be played just once to a printer. So just | 325 // It's known that the Emf going to be played just once to a printer. So just |
266 // release file here. | 326 // release |file_| here. |
267 Close(); | 327 Close(); |
268 return result; | 328 return result; |
269 } | 329 } |
270 | 330 |
271 bool LazyEmf::GetDataAsVector(std::vector<char>* buffer) const { | 331 bool LazyEmf::GetDataAsVector(std::vector<char>* buffer) const { |
272 NOTREACHED(); | 332 NOTREACHED(); |
273 return false; | 333 return false; |
274 } | 334 } |
275 | 335 |
276 bool LazyEmf::SaveTo(base::File* file) const { | 336 bool LazyEmf::SaveTo(base::File* file) const { |
277 Emf emf; | 337 Emf emf; |
278 return LoadEmf(&emf) && emf.SaveTo(file); | 338 return LoadEmf(&emf) && emf.SaveTo(file); |
279 } | 339 } |
280 | 340 |
281 void LazyEmf::Close() const { | 341 void LazyEmf::Close() const { |
282 file_.reset(); | 342 file_.reset(); |
283 temp_dir_ = NULL; | 343 temp_dir_ = nullptr; |
284 } | 344 } |
285 | 345 |
286 bool LazyEmf::LoadEmf(Emf* emf) const { | 346 bool LazyEmf::LoadEmf(Emf* emf) const { |
287 file_->Seek(base::File::FROM_BEGIN, 0); | 347 file_->Seek(base::File::FROM_BEGIN, 0); |
288 int64_t size = file_->GetLength(); | 348 int64_t size = file_->GetLength(); |
289 if (size <= 0) | 349 if (size <= 0) |
290 return false; | 350 return false; |
291 std::vector<char> data(size); | 351 std::vector<char> data(size); |
292 if (file_->ReadAtCurrentPos(data.data(), data.size()) != size) | 352 if (file_->ReadAtCurrentPos(data.data(), data.size()) != size) |
293 return false; | 353 return false; |
294 return emf->InitFromData(data.data(), data.size()); | 354 return emf->InitFromData(data.data(), data.size()); |
295 } | 355 } |
296 | 356 |
297 PdfToEmfUtilityProcessHostClient::PdfToEmfUtilityProcessHostClient( | 357 PdfConverterUtilityProcessHostClient::PdfConverterUtilityProcessHostClient( |
298 base::WeakPtr<PdfToEmfConverterImpl> converter, | 358 base::WeakPtr<PdfConverterImpl> converter, |
299 const PdfRenderSettings& settings) | 359 const PdfRenderSettings& settings) |
300 : converter_(converter), settings_(settings) { | 360 : converter_(converter), settings_(settings) { |
301 } | 361 } |
302 | 362 |
303 PdfToEmfUtilityProcessHostClient::~PdfToEmfUtilityProcessHostClient() { | 363 void PdfConverterUtilityProcessHostClient::OnPageCount(int page_count) { |
364 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
365 if (start_callback_.is_null()) | |
366 return OnFailed(); | |
367 BrowserThread::PostTask(BrowserThread::UI, | |
368 FROM_HERE, | |
369 base::Bind(&PdfConverterImpl::RunCallback, | |
370 converter_, | |
371 base::Bind(start_callback_, page_count))); | |
372 start_callback_.Reset(); | |
304 } | 373 } |
305 | 374 |
306 void PdfToEmfUtilityProcessHostClient::Start( | 375 void PdfConverterUtilityProcessHostClient::OnPageDone(bool success, |
376 float scale_factor) { | |
377 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
378 if (get_page_callbacks_.empty()) | |
379 return OnFailed(); | |
380 GetPageCallbackData& data = get_page_callbacks_.front(); | |
381 std::unique_ptr<MetafilePlayer> file; | |
382 | |
383 if (success) { | |
384 ScopedTempFile temp_file = data.TakeFile(); | |
385 if (!temp_file) // Unexpected message from utility process. | |
386 return OnFailed(); | |
387 file = GetFileFromTemp(std::move(temp_file)); | |
388 } | |
389 | |
390 BrowserThread::PostTask(BrowserThread::UI, | |
391 FROM_HERE, | |
392 base::Bind(&PdfConverterImpl::RunCallback, | |
393 converter_, | |
394 base::Bind(data.callback(), | |
395 data.page_number(), | |
396 scale_factor, | |
397 base::Passed(&file)))); | |
398 get_page_callbacks_.pop(); | |
399 } | |
400 | |
401 void PdfConverterUtilityProcessHostClient::OnProcessCrashed(int exit_code) { | |
402 OnFailed(); | |
403 } | |
404 | |
405 void PdfConverterUtilityProcessHostClient::OnProcessLaunchFailed( | |
406 int exit_code) { | |
407 OnFailed(); | |
408 } | |
409 | |
410 bool PdfConverterUtilityProcessHostClient::Send(IPC::Message* msg) { | |
411 if (utility_process_host_) | |
412 return utility_process_host_->Send(msg); | |
413 delete msg; | |
414 return false; | |
415 } | |
416 | |
417 void PdfConverterUtilityProcessHostClient::Start( | |
307 const scoped_refptr<base::RefCountedMemory>& data, | 418 const scoped_refptr<base::RefCountedMemory>& data, |
308 bool print_text_with_gdi, | 419 bool print_text_with_gdi, |
309 const PdfToEmfConverter::StartCallback& start_callback) { | 420 const PdfConverter::StartCallback& start_callback) { |
310 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 421 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
311 BrowserThread::PostTask( | 422 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
312 BrowserThread::IO, FROM_HERE, | 423 base::Bind(&PdfConverterUtilityProcessHostClient::Start, |
313 base::Bind(&PdfToEmfUtilityProcessHostClient::Start, this, data, | 424 this, data, print_text_with_gdi, start_callback)); |
314 print_text_with_gdi, start_callback)); | |
315 return; | 425 return; |
316 } | 426 } |
317 | 427 |
318 // Store callback before any OnFailed() call to make it called on failure. | 428 // Store callback before any OnFailed() call to make it called on failure. |
319 start_callback_ = start_callback; | 429 start_callback_ = start_callback; |
320 | 430 |
321 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load | 431 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load |
322 // gdiplus.dll, change how rendering happens, and not be able to correctly | 432 // gdiplus.dll, change how rendering happens, and not be able to correctly |
323 // generate when sent to a metafile DC. | 433 // generate when sent to a metafile DC. |
324 utility_process_host_ = content::UtilityProcessHost::Create( | 434 utility_process_host_ = content::UtilityProcessHost::Create( |
325 this, base::ThreadTaskRunnerHandle::Get()) | 435 this, base::ThreadTaskRunnerHandle::Get()) |
326 ->AsWeakPtr(); | 436 ->AsWeakPtr(); |
327 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 437 utility_process_host_->SetName(GetName()); |
328 IDS_UTILITY_PROCESS_EMF_CONVERTOR_NAME)); | |
329 | 438 |
330 BrowserThread::PostTaskAndReplyWithResult( | 439 BrowserThread::PostTaskAndReplyWithResult( |
331 BrowserThread::FILE, FROM_HERE, | 440 BrowserThread::FILE, FROM_HERE, |
332 base::Bind(&CreateTempPdfFile, data, &temp_dir_), | 441 base::Bind(&CreateTempPdfFile, data, &temp_dir_), |
333 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempPdfReady, this, | 442 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempPdfReady, this, |
334 print_text_with_gdi)); | 443 print_text_with_gdi)); |
335 } | 444 } |
336 | 445 |
337 void PdfToEmfUtilityProcessHostClient::OnTempPdfReady(bool print_text_with_gdi, | 446 void PdfConverterUtilityProcessHostClient::GetPage( |
338 ScopedTempFile pdf) { | |
339 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
340 if (!utility_process_host_ || !pdf) | |
341 return OnFailed(); | |
342 // Should reply with OnPageCount(). | |
343 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | |
344 IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), false), settings_, | |
345 print_text_with_gdi)); | |
346 } | |
347 | |
348 void PdfToEmfUtilityProcessHostClient::OnPageCount(int page_count) { | |
349 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
350 if (start_callback_.is_null()) | |
351 return OnFailed(); | |
352 BrowserThread::PostTask(BrowserThread::UI, | |
353 FROM_HERE, | |
354 base::Bind(&PdfToEmfConverterImpl::RunCallback, | |
355 converter_, | |
356 base::Bind(start_callback_, page_count))); | |
357 start_callback_.Reset(); | |
358 } | |
359 | |
360 void PdfToEmfUtilityProcessHostClient::GetPage( | |
361 int page_number, | 447 int page_number, |
362 const PdfToEmfConverter::GetPageCallback& get_page_callback) { | 448 const PdfConverter::GetPageCallback& get_page_callback) { |
363 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 449 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
364 BrowserThread::PostTask( | 450 BrowserThread::PostTask( |
365 BrowserThread::IO, | 451 BrowserThread::IO, |
366 FROM_HERE, | 452 FROM_HERE, |
367 base::Bind(&PdfToEmfUtilityProcessHostClient::GetPage, | 453 base::Bind(&PdfConverterUtilityProcessHostClient::GetPage, |
368 this, | 454 this, |
369 page_number, | 455 page_number, |
370 get_page_callback)); | 456 get_page_callback)); |
371 return; | 457 return; |
372 } | 458 } |
373 | 459 |
374 // Store callback before any OnFailed() call to make it called on failure. | 460 // Store callback before any OnFailed() call to make it called on failure. |
375 get_page_callbacks_.push(GetPageCallbackData(page_number, get_page_callback)); | 461 get_page_callbacks_.push(GetPageCallbackData(page_number, get_page_callback)); |
376 | 462 |
377 if (!utility_process_host_) | 463 if (!utility_process_host_) |
378 return OnFailed(); | 464 return OnFailed(); |
379 | 465 |
380 BrowserThread::PostTaskAndReplyWithResult( | 466 BrowserThread::PostTaskAndReplyWithResult( |
381 BrowserThread::FILE, | 467 BrowserThread::FILE, |
382 FROM_HERE, | 468 FROM_HERE, |
383 base::Bind(&CreateTempFile, &temp_dir_), | 469 base::Bind(&CreateTempFile, &temp_dir_), |
384 base::Bind(&PdfToEmfUtilityProcessHostClient::OnTempEmfReady, | 470 base::Bind(&PdfConverterUtilityProcessHostClient::OnTempFileReady, |
385 this, | 471 this, |
386 &get_page_callbacks_.back())); | 472 &get_page_callbacks_.back())); |
387 } | 473 } |
388 | 474 |
389 void PdfToEmfUtilityProcessHostClient::OnTempEmfReady( | 475 void PdfConverterUtilityProcessHostClient::OnTempFileReady( |
390 GetPageCallbackData* callback_data, | 476 GetPageCallbackData* callback_data, |
391 ScopedTempFile emf) { | 477 ScopedTempFile temp_file) { |
392 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 478 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
393 if (!utility_process_host_ || !emf) | 479 if (!utility_process_host_ || !temp_file) |
394 return OnFailed(); | 480 return OnFailed(); |
395 IPC::PlatformFileForTransit transit = | 481 IPC::PlatformFileForTransit transit = |
396 IPC::GetPlatformFileForTransit(emf->GetPlatformFile(), false); | 482 IPC::GetPlatformFileForTransit(temp_file->GetPlatformFile(), false); |
397 callback_data->set_emf(std::move(emf)); | 483 callback_data->set_file(std::move(temp_file)); |
398 // Should reply with OnPageDone(). | 484 // Should reply with OnPageDone(). |
399 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( | 485 SendGetPageMessage(callback_data->page_number(), transit); |
400 callback_data->page_number(), transit)); | |
401 } | 486 } |
402 | 487 |
403 void PdfToEmfUtilityProcessHostClient::OnPageDone(bool success, | 488 void PdfConverterUtilityProcessHostClient::OnTempPdfReady( |
404 float scale_factor) { | 489 bool print_text_with_gdi, |
490 ScopedTempFile pdf) { | |
405 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 491 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
406 if (get_page_callbacks_.empty()) | 492 if (!utility_process_host_ || !pdf) |
407 return OnFailed(); | 493 return OnFailed(); |
408 GetPageCallbackData& data = get_page_callbacks_.front(); | 494 // Should reply with OnPageCount(). |
409 std::unique_ptr<MetafilePlayer> emf; | 495 SendStartMessage(IPC::GetPlatformFileForTransit(pdf->GetPlatformFile(), |
496 false), print_text_with_gdi); | |
497 } | |
410 | 498 |
411 if (success) { | 499 void PdfConverterUtilityProcessHostClient::Stop() { |
412 ScopedTempFile temp_emf = data.TakeEmf(); | 500 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
413 if (!temp_emf) // Unexpected message from utility process. | 501 BrowserThread::PostTask( |
414 return OnFailed(); | 502 BrowserThread::IO, |
415 emf = base::MakeUnique<LazyEmf>(temp_dir_, std::move(temp_emf)); | 503 FROM_HERE, |
504 base::Bind(&PdfToEmfUtilityProcessHostClient::Stop, this)); | |
505 return; | |
416 } | 506 } |
507 SendStopMessage(); | |
508 } | |
417 | 509 |
418 BrowserThread::PostTask(BrowserThread::UI, | 510 void PdfConverterUtilityProcessHostClient::OnFailed() { |
419 FROM_HERE, | 511 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
420 base::Bind(&PdfToEmfConverterImpl::RunCallback, | 512 if (!start_callback_.is_null()) |
421 converter_, | 513 OnPageCount(0); |
422 base::Bind(data.callback(), | 514 while (!get_page_callbacks_.empty()) |
423 data.page_number(), | 515 OnPageDone(false, 0.0f); |
424 scale_factor, | 516 utility_process_host_.reset(); |
425 base::Passed(&emf)))); | 517 } |
426 get_page_callbacks_.pop(); | 518 |
519 // Pdf to Emf | |
520 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( | |
521 const IPC::Message& message) { | |
522 bool handled = true; | |
523 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) | |
524 IPC_MESSAGE_HANDLER( | |
525 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount) | |
526 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, | |
527 OnPageDone) | |
528 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PreCacheFontCharacters, | |
529 OnPreCacheFontCharacters) | |
530 IPC_MESSAGE_UNHANDLED(handled = false) | |
531 IPC_END_MESSAGE_MAP() | |
532 return handled; | |
533 } | |
534 | |
535 base::string16 PdfToEmfUtilityProcessHostClient::GetName() const { | |
536 return l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_EMF_CONVERTOR_NAME); | |
537 } | |
538 | |
539 std::unique_ptr<MetafilePlayer> | |
540 PdfToEmfUtilityProcessHostClient::GetFileFromTemp( | |
541 std::unique_ptr<base::File, | |
542 content::BrowserThread::DeleteOnFileThread> temp_file) { | |
543 return base::MakeUnique<LazyEmf>(temp_dir_, std::move(temp_file)); | |
544 } | |
545 | |
546 void PdfToEmfUtilityProcessHostClient::SendGetPageMessage( | |
547 int page_number, IPC::PlatformFileForTransit transit) { | |
548 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage(page_number, | |
549 transit)); | |
550 } | |
551 | |
552 void PdfToEmfUtilityProcessHostClient::SendStartMessage( | |
553 IPC::PlatformFileForTransit transit, | |
554 bool print_text_with_gdi) { | |
555 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles(transit, | |
556 settings_, | |
557 print_text_with_gdi)); | |
558 } | |
559 | |
560 void PdfToEmfUtilityProcessHostClient::SendStopMessage() { | |
561 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); | |
427 } | 562 } |
428 | 563 |
429 void PdfToEmfUtilityProcessHostClient::OnPreCacheFontCharacters( | 564 void PdfToEmfUtilityProcessHostClient::OnPreCacheFontCharacters( |
430 const LOGFONT& font, | 565 const LOGFONT& font, |
431 const base::string16& str) { | 566 const base::string16& str) { |
432 // TODO(scottmg): pdf/ppapi still require the renderer to be able to precache | 567 // TODO(scottmg): pdf/ppapi still require the renderer to be able to precache |
433 // GDI fonts (http://crbug.com/383227), even when using DirectWrite. | 568 // GDI fonts (http://crbug.com/383227), even when using DirectWrite. |
434 // Eventually this shouldn't be added and should be moved to | 569 // Eventually this shouldn't be added and should be moved to |
435 // FontCacheDispatcher too. http://crbug.com/356346. | 570 // FontCacheDispatcher too. http://crbug.com/356346. |
436 | 571 |
(...skipping 14 matching lines...) Expand all Loading... | |
451 | 586 |
452 SelectObject(hdc, old_font); | 587 SelectObject(hdc, old_font); |
453 DeleteObject(font_handle); | 588 DeleteObject(font_handle); |
454 | 589 |
455 HENHMETAFILE metafile = CloseEnhMetaFile(hdc); | 590 HENHMETAFILE metafile = CloseEnhMetaFile(hdc); |
456 | 591 |
457 if (metafile) | 592 if (metafile) |
458 DeleteEnhMetaFile(metafile); | 593 DeleteEnhMetaFile(metafile); |
459 } | 594 } |
460 | 595 |
461 void PdfToEmfUtilityProcessHostClient::Stop() { | 596 // Pdf Converter Impl and subclasses |
462 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 597 PdfConverterImpl::PdfConverterImpl() { |
463 BrowserThread::PostTask( | |
464 BrowserThread::IO, | |
465 FROM_HERE, | |
466 base::Bind(&PdfToEmfUtilityProcessHostClient::Stop, this)); | |
467 return; | |
468 } | |
469 Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); | |
470 } | 598 } |
471 | 599 |
472 void PdfToEmfUtilityProcessHostClient::OnProcessCrashed(int exit_code) { | 600 PdfConverterImpl::~PdfConverterImpl() { |
473 OnFailed(); | 601 if (utility_client_.get()) |
602 utility_client_->Stop(); | |
474 } | 603 } |
475 | 604 |
476 void PdfToEmfUtilityProcessHostClient::OnProcessLaunchFailed(int exit_code) { | 605 void PdfConverterImpl::Start( |
477 OnFailed(); | 606 const scoped_refptr<base::RefCountedMemory>& data, |
607 const PdfRenderSettings& conversion_settings, | |
608 bool print_text_with_gdi, | |
609 const StartCallback& start_callback) { | |
610 DCHECK(!utility_client_.get()); | |
478 } | 611 } |
479 | 612 |
480 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived( | 613 void PdfConverterImpl::GetPage(int page_number, |
481 const IPC::Message& message) { | 614 const GetPageCallback& get_page_callback) { |
482 bool handled = true; | 615 utility_client_->GetPage(page_number, get_page_callback); |
483 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message) | |
484 IPC_MESSAGE_HANDLER( | |
485 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, OnPageCount) | |
486 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, | |
487 OnPageDone) | |
488 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_PreCacheFontCharacters, | |
489 OnPreCacheFontCharacters) | |
490 IPC_MESSAGE_UNHANDLED(handled = false) | |
491 IPC_END_MESSAGE_MAP() | |
492 return handled; | |
493 } | 616 } |
494 | 617 |
495 bool PdfToEmfUtilityProcessHostClient::Send(IPC::Message* msg) { | 618 void PdfConverterImpl::RunCallback(const base::Closure& callback) { |
496 if (utility_process_host_) | 619 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
497 return utility_process_host_->Send(msg); | 620 callback.Run(); |
498 delete msg; | |
499 return false; | |
500 } | |
501 | |
502 void PdfToEmfUtilityProcessHostClient::OnFailed() { | |
503 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
504 if (!start_callback_.is_null()) | |
505 OnPageCount(0); | |
506 while (!get_page_callbacks_.empty()) | |
507 OnPageDone(false, 0.0f); | |
508 utility_process_host_.reset(); | |
509 } | 621 } |
510 | 622 |
511 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) { | 623 PdfToEmfConverterImpl::PdfToEmfConverterImpl() : weak_ptr_factory_(this) { |
512 } | 624 } |
513 | 625 |
514 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() { | |
515 if (utility_client_.get()) | |
516 utility_client_->Stop(); | |
517 } | |
518 | |
519 void PdfToEmfConverterImpl::Start( | 626 void PdfToEmfConverterImpl::Start( |
520 const scoped_refptr<base::RefCountedMemory>& data, | 627 const scoped_refptr<base::RefCountedMemory>& data, |
521 const PdfRenderSettings& conversion_settings, | 628 const PdfRenderSettings& conversion_settings, |
522 bool print_text_with_gdi, | 629 bool print_text_with_gdi, |
523 const StartCallback& start_callback) { | 630 const StartCallback& start_callback) { |
524 DCHECK(!utility_client_.get()); | 631 DCHECK(!utility_client_.get()); |
525 utility_client_ = new PdfToEmfUtilityProcessHostClient( | 632 utility_client_ = new PdfToEmfUtilityProcessHostClient( |
526 weak_ptr_factory_.GetWeakPtr(), conversion_settings); | 633 weak_ptr_factory_.GetWeakPtr(), conversion_settings); |
527 utility_client_->Start(data, print_text_with_gdi, start_callback); | 634 utility_client_->Start(data, print_text_with_gdi, start_callback); |
528 } | 635 } |
529 | 636 |
530 void PdfToEmfConverterImpl::GetPage(int page_number, | |
531 const GetPageCallback& get_page_callback) { | |
532 utility_client_->GetPage(page_number, get_page_callback); | |
533 } | |
534 | |
535 void PdfToEmfConverterImpl::RunCallback(const base::Closure& callback) { | |
536 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
537 callback.Run(); | |
538 } | |
539 | |
540 } // namespace | 637 } // namespace |
541 | 638 |
542 PdfToEmfConverter::~PdfToEmfConverter() { | 639 PdfConverter::~PdfConverter() {} |
543 } | |
544 | 640 |
545 // static | 641 // static |
546 std::unique_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() { | 642 std::unique_ptr<PdfConverter> PdfConverter::CreatePdfToEmfConverter() { |
547 return std::unique_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl()); | 643 return base::MakeUnique<PdfToEmfConverterImpl>(); |
548 } | 644 } |
549 | |
550 } // namespace printing | 645 } // namespace printing |
OLD | NEW |