Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/printing/pdf_to_emf_converter.cc

Issue 255543006: Printing on Windows via PDF (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: almost working; SafePlayback failing in final print Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 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/local_discovery/pwg_raster_converter.h" 5 #include "chrome/browser/printing/pdf_to_emf_converter.h"
6 6
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/cancelable_callback.h" 8 #include "base/cancelable_callback.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "chrome/common/chrome_utility_messages.h" 13 #include "chrome/common/chrome_utility_messages.h"
14 #include "content/public/browser/browser_thread.h" 14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/child_process_data.h" 15 #include "content/public/browser/child_process_data.h"
16 #include "content/public/browser/utility_process_host.h" 16 #include "content/public/browser/utility_process_host.h"
17 #include "content/public/browser/utility_process_host_client.h" 17 #include "content/public/browser/utility_process_host_client.h"
18 18
19 namespace local_discovery { 19 namespace printing {
20 20
21 namespace { 21 namespace {
22 22
23 using content::BrowserThread; 23 using content::BrowserThread;
24 24
25 class FileHandlers { 25 class FileHandlers {
26 public: 26 public:
27 FileHandlers() {} 27 FileHandlers() {}
28 28
29 ~FileHandlers() { 29 ~FileHandlers() {
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
31 } 31 }
32 32
33 void Init(base::RefCountedMemory* data); 33 void Init(base::RefCountedMemory* data);
34 bool IsValid(); 34 bool IsValid();
35 35
36 base::FilePath GetPwgPath() const { 36 base::FilePath GetEmfPath() const {
37 return temp_dir_.path().AppendASCII("output.pwg"); 37 return temp_dir_.path().AppendASCII("output.emf");
38 } 38 }
39 39
40 base::FilePath GetPdfPath() const { 40 base::FilePath GetPdfPath() const {
41 return temp_dir_.path().AppendASCII("input.pdf"); 41 return temp_dir_.path().AppendASCII("input.pdf");
42 } 42 }
43 43
44 IPC::PlatformFileForTransit GetPdfForProcess(base::ProcessHandle process) { 44 IPC::PlatformFileForTransit GetPdfForProcess(base::ProcessHandle process) {
45 DCHECK(pdf_file_.IsValid()); 45 DCHECK(pdf_file_.IsValid());
46 IPC::PlatformFileForTransit transit = 46 IPC::PlatformFileForTransit transit =
47 IPC::TakeFileHandleForProcess(pdf_file_.Pass(), process); 47 IPC::TakeFileHandleForProcess(pdf_file_.Pass(), process);
48 return transit; 48 return transit;
49 } 49 }
50 50
51 IPC::PlatformFileForTransit GetPwgForProcess(base::ProcessHandle process) { 51 IPC::PlatformFileForTransit GetEmfForProcess(base::ProcessHandle process) {
52 DCHECK(pwg_file_.IsValid()); 52 DCHECK(emf_file_.IsValid());
53 IPC::PlatformFileForTransit transit = 53 IPC::PlatformFileForTransit transit =
54 IPC::TakeFileHandleForProcess(pwg_file_.Pass(), process); 54 IPC::TakeFileHandleForProcess(emf_file_.Pass(), process);
55 return transit; 55 return transit;
56 } 56 }
57 57
58 private: 58 private:
59 base::ScopedTempDir temp_dir_; 59 base::ScopedTempDir temp_dir_;
60 base::File pdf_file_; 60 base::File pdf_file_;
61 base::File pwg_file_; 61 base::File emf_file_;
62 }; 62 };
63 63
64 void FileHandlers::Init(base::RefCountedMemory* data) { 64 void FileHandlers::Init(base::RefCountedMemory* data) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
66 66
67 if (!temp_dir_.CreateUniqueTempDir()) { 67 if (!temp_dir_.CreateUniqueTempDir()) {
68 return; 68 return;
69 } 69 }
70 70
71 if (static_cast<int>(data->size()) != 71 if (static_cast<int>(data->size()) !=
72 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) { 72 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) {
73 return; 73 return;
74 } 74 }
75 75
76 // Reopen in read only mode. 76 // Reopen in read only mode.
77 pdf_file_.Initialize(GetPdfPath(), 77 pdf_file_.Initialize(GetPdfPath(),
78 base::File::FLAG_OPEN | base::File::FLAG_READ); 78 base::File::FLAG_OPEN | base::File::FLAG_READ);
79 pwg_file_.Initialize( 79 emf_file_.Initialize(
80 GetPwgPath(), 80 GetEmfPath(),
81 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_APPEND); 81 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_APPEND);
82 } 82 }
83 83
84 bool FileHandlers::IsValid() { 84 bool FileHandlers::IsValid() {
85 return pdf_file_.IsValid() && pwg_file_.IsValid(); 85 return pdf_file_.IsValid() && emf_file_.IsValid();
86 } 86 }
87 87
88 // Converts PDF into PWG raster. 88 // Converts PDF into EMF.
89 // Class uses 3 threads: UI, IO and FILE. 89 // Class uses 3 threads: UI, IO and FILE.
90 // Internal workflow is following: 90 // Internal workflow is following:
91 // 1. Create instance on the UI thread. (files_, settings_,) 91 // 1. Create instance on the UI thread. (files_, settings_,)
92 // 2. Create file on the FILE thread. 92 // 2. Create file on the FILE thread.
93 // 3. Start utility process and start conversion on the IO thread. 93 // 3. Start utility process and start conversion on the IO thread.
94 // 4. Run result callback on the UI thread. 94 // 4. Run result callback on the UI thread.
95 // 5. Instance is destroyed from any thread that has the last reference. 95 // 5. Instance is destroyed from any thread that has the last reference.
96 // 6. FileHandlers destroyed on the FILE thread. 96 // 6. FileHandlers destroyed on the FILE thread.
97 // This step posts |FileHandlers| to be destroyed on the FILE thread. 97 // This step posts |FileHandlers| to be destroyed on the FILE thread.
98 // All these steps work sequentially, so no data should be accessed 98 // All these steps work sequentially, so no data should be accessed
99 // simultaneously by several threads. 99 // simultaneously by several threads.
100 class PwgUtilityProcessHostClient : public content::UtilityProcessHostClient { 100 class PdfToEmfUtilityProcessHostClient
101 : public content::UtilityProcessHostClient {
101 public: 102 public:
102 explicit PwgUtilityProcessHostClient( 103 explicit PdfToEmfUtilityProcessHostClient(
103 const printing::PdfRenderSettings& settings, 104 const printing::PdfRenderSettings& settings);
104 const printing::PwgRasterSettings& bitmap_settings);
105 105
106 void Convert(base::RefCountedMemory* data, 106 void Convert(base::RefCountedMemory* data,
107 const PWGRasterConverter::ResultCallback& callback); 107 const PdfToEmfConverter::ResultCallback& callback);
108 108
109 // UtilityProcessHostClient implementation. 109 // UtilityProcessHostClient implementation.
110 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 110 virtual void OnProcessCrashed(int exit_code) OVERRIDE;
111 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 111 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
112 112
113 private: 113 private:
114 virtual ~PwgUtilityProcessHostClient(); 114 virtual ~PdfToEmfUtilityProcessHostClient();
115 115
116 // Message handlers. 116 // Message handlers.
117 void OnProcessStarted(); 117 void OnProcessStarted();
118 void OnSucceeded(); 118 void OnSucceeded(int highest_rendered_page_number, double scale_factor);
119 void OnFailed(); 119 void OnFailed();
120 120
121 void RunCallback(bool success); 121 void RunCallback(bool success);
122 122
123 void StartProcessOnIOThread(); 123 void StartProcessOnIOThread();
124 124
125 void RunCallbackOnUIThread(bool success); 125 void RunCallbackOnUIThread(bool success);
126 void OnFilesReadyOnUIThread(); 126 void OnFilesReadyOnUIThread();
127 127
128 scoped_ptr<FileHandlers> files_; 128 scoped_ptr<FileHandlers> files_;
129 printing::PdfRenderSettings settings_; 129 printing::PdfRenderSettings settings_;
130 printing::PwgRasterSettings bitmap_settings_; 130 PdfToEmfConverter::ResultCallback callback_;
131 PWGRasterConverter::ResultCallback callback_;
132 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; 131 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
133 132
134 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); 133 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient);
135 }; 134 };
136 135
137 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( 136 PdfToEmfUtilityProcessHostClient::PdfToEmfUtilityProcessHostClient(
138 const printing::PdfRenderSettings& settings, 137 const printing::PdfRenderSettings& settings)
139 const printing::PwgRasterSettings& bitmap_settings) 138 : settings_(settings) {}
140 : settings_(settings), bitmap_settings_(bitmap_settings) {}
141 139
142 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { 140 PdfToEmfUtilityProcessHostClient::~PdfToEmfUtilityProcessHostClient() {
143 // Delete temp directory. 141 // Delete temp directory.
144 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release()); 142 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release());
145 } 143 }
146 144
147 void PwgUtilityProcessHostClient::Convert( 145 void PdfToEmfUtilityProcessHostClient::Convert(
148 base::RefCountedMemory* data, 146 base::RefCountedMemory* data,
149 const PWGRasterConverter::ResultCallback& callback) { 147 const PdfToEmfConverter::ResultCallback& callback) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 callback_ = callback; 149 callback_ = callback;
152 CHECK(!files_); 150 CHECK(!files_);
153 files_.reset(new FileHandlers()); 151 files_.reset(new FileHandlers());
154 BrowserThread::PostTaskAndReply( 152 BrowserThread::PostTaskAndReply(
155 BrowserThread::FILE, FROM_HERE, 153 BrowserThread::FILE,
156 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), 154 FROM_HERE,
155 base::Bind(&FileHandlers::Init,
156 base::Unretained(files_.get()),
157 make_scoped_refptr(data)), 157 make_scoped_refptr(data)),
158 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); 158 base::Bind(&PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread,
159 this));
159 } 160 }
160 161
161 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { 162 void PdfToEmfUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
162 OnFailed(); 163 OnFailed();
163 } 164 }
164 165
165 bool PwgUtilityProcessHostClient::OnMessageReceived( 166 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived(
166 const IPC::Message& message) { 167 const IPC::Message& message) {
167 bool handled = true; 168 bool handled = true;
168 IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message) 169 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message)
169 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) 170 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted)
170 IPC_MESSAGE_HANDLER( 171 IPC_MESSAGE_HANDLER(
171 ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded) 172 ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Succeeded, OnSucceeded)
172 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed, 173 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed,
173 OnFailed) 174 OnFailed)
174 IPC_MESSAGE_UNHANDLED(handled = false) 175 IPC_MESSAGE_UNHANDLED(handled = false)
175 IPC_END_MESSAGE_MAP() 176 IPC_END_MESSAGE_MAP()
176 return handled; 177 return handled;
177 } 178 }
178 179
179 void PwgUtilityProcessHostClient::OnProcessStarted() { 180 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 if (!utility_process_host_) { 182 if (!utility_process_host_) {
182 RunCallbackOnUIThread(false); 183 RunCallbackOnUIThread(false);
183 return; 184 return;
184 } 185 }
185 186
186 base::ProcessHandle process = utility_process_host_->GetData().handle; 187 base::ProcessHandle process = utility_process_host_->GetData().handle;
187 utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( 188 // Only converting one page at a time.
188 files_->GetPdfForProcess(process), 189 printing::PageRange page_range;
189 settings_, 190 page_range.from = 0;
190 bitmap_settings_, 191 page_range.to = 0;
191 files_->GetPwgForProcess(process))); 192 std::vector<printing::PageRange> range;
193 range.push_back(page_range);
194 utility_process_host_->Send(
195 new ChromeUtilityMsg_RenderPDFPagesToMetafileHandles(
196 files_->GetPdfForProcess(process),
197 files_->GetEmfForProcess(process),
198 settings_,
199 range));
192 utility_process_host_.reset(); 200 utility_process_host_.reset();
193 } 201 }
194 202
195 void PwgUtilityProcessHostClient::OnSucceeded() { 203 void PdfToEmfUtilityProcessHostClient::OnSucceeded(
204 int highest_rendered_page_number,
205 double scale_factor) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
197 RunCallback(true); 207 RunCallback(true);
198 } 208 }
199 209
200 void PwgUtilityProcessHostClient::OnFailed() { 210 void PdfToEmfUtilityProcessHostClient::OnFailed() {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
202 RunCallback(false); 212 RunCallback(false);
203 } 213 }
204 214
205 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { 215 void PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread() {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 if (!files_->IsValid()) { 217 if (!files_->IsValid()) {
208 RunCallbackOnUIThread(false); 218 RunCallbackOnUIThread(false);
209 return; 219 return;
210 } 220 }
211 BrowserThread::PostTask( 221 BrowserThread::PostTask(
212 BrowserThread::IO, FROM_HERE, 222 BrowserThread::IO,
213 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); 223 FROM_HERE,
224 base::Bind(&PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread,
225 this));
214 } 226 }
215 227
216 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { 228 void PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread() {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
218 utility_process_host_ = 230 utility_process_host_ =
219 content::UtilityProcessHost::Create( 231 content::UtilityProcessHost::Create(
220 this, 232 this,
221 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr(); 233 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr();
222 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); 234 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
223 } 235 }
224 236
225 void PwgUtilityProcessHostClient::RunCallback(bool success) { 237 void PdfToEmfUtilityProcessHostClient::RunCallback(bool success) {
226 BrowserThread::PostTask( 238 BrowserThread::PostTask(
227 BrowserThread::UI, FROM_HERE, 239 BrowserThread::UI,
228 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, 240 FROM_HERE,
241 base::Bind(&PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread,
242 this,
229 success)); 243 success));
230 } 244 }
231 245
232 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { 246 void PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread(bool success) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
234 if (!callback_.is_null()) { 248 if (!callback_.is_null()) {
235 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 249 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
236 base::Bind(callback_, success, 250 base::Bind(callback_, success,
237 files_->GetPwgPath())); 251 files_->GetEmfPath()));
238 callback_.Reset(); 252 callback_.Reset();
239 } 253 }
240 } 254 }
241 255
242 class PWGRasterConverterImpl : public PWGRasterConverter { 256 class PdfToEmfConverterImpl : public PdfToEmfConverter {
243 public: 257 public:
244 PWGRasterConverterImpl(); 258 PdfToEmfConverterImpl();
245 259
246 virtual ~PWGRasterConverterImpl(); 260 virtual ~PdfToEmfConverterImpl();
247 261
248 virtual void Start(base::RefCountedMemory* data, 262 virtual void Start(base::RefCountedMemory* data,
249 const printing::PdfRenderSettings& conversion_settings, 263 const printing::PdfRenderSettings& conversion_settings,
250 const printing::PwgRasterSettings& bitmap_settings,
251 const ResultCallback& callback) OVERRIDE; 264 const ResultCallback& callback) OVERRIDE;
252 265
253 private: 266 private:
254 scoped_refptr<PwgUtilityProcessHostClient> utility_client_; 267 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_;
255 base::CancelableCallback<ResultCallback::RunType> callback_; 268 base::CancelableCallback<ResultCallback::RunType> callback_;
256 269
257 DISALLOW_COPY_AND_ASSIGN(PWGRasterConverterImpl); 270 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl);
258 }; 271 };
259 272
260 PWGRasterConverterImpl::PWGRasterConverterImpl() { 273 PdfToEmfConverterImpl::PdfToEmfConverterImpl() {
261 } 274 }
262 275
263 PWGRasterConverterImpl::~PWGRasterConverterImpl() { 276 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() {
264 } 277 }
265 278
266 void PWGRasterConverterImpl::Start( 279 void PdfToEmfConverterImpl::Start(
267 base::RefCountedMemory* data, 280 base::RefCountedMemory* data,
268 const printing::PdfRenderSettings& conversion_settings, 281 const printing::PdfRenderSettings& conversion_settings,
269 const printing::PwgRasterSettings& bitmap_settings,
270 const ResultCallback& callback) { 282 const ResultCallback& callback) {
271 // Rebind cancelable callback to avoid calling callback if 283 // Rebind cancelable callback to avoid calling callback if
272 // PWGRasterConverterImpl is destroyed. 284 // PdfToEmfConverterImpl is destroyed.
273 callback_.Reset(callback); 285 callback_.Reset(callback);
274 utility_client_ = 286 utility_client_ = new PdfToEmfUtilityProcessHostClient(conversion_settings);
275 new PwgUtilityProcessHostClient(conversion_settings, bitmap_settings);
276 utility_client_->Convert(data, callback_.callback()); 287 utility_client_->Convert(data, callback_.callback());
277 } 288 }
278 289
279 } // namespace 290 } // namespace
280 291
281 // static 292 // static
282 scoped_ptr<PWGRasterConverter> PWGRasterConverter::CreateDefault() { 293 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() {
283 return scoped_ptr<PWGRasterConverter>(new PWGRasterConverterImpl()); 294 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl());
284 } 295 }
285 296
286 } // namespace local_discovery 297 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698