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

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: remove ref to internal_pdf and rebase Created 6 years, 7 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 const base::FilePath& GetBasePath() const {
52 DCHECK(pwg_file_.IsValid()); 52 return temp_dir_.path();
53 IPC::PlatformFileForTransit transit =
54 IPC::TakeFileHandleForProcess(pwg_file_.Pass(), process);
55 return transit;
56 } 53 }
57 54
58 private: 55 private:
59 base::ScopedTempDir temp_dir_; 56 base::ScopedTempDir temp_dir_;
60 base::File pdf_file_; 57 base::File pdf_file_;
61 base::File pwg_file_;
62 }; 58 };
63 59
64 void FileHandlers::Init(base::RefCountedMemory* data) { 60 void FileHandlers::Init(base::RefCountedMemory* data) {
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
66 62
67 if (!temp_dir_.CreateUniqueTempDir()) { 63 if (!temp_dir_.CreateUniqueTempDir()) {
68 return; 64 return;
69 } 65 }
70 66
71 if (static_cast<int>(data->size()) != 67 if (static_cast<int>(data->size()) !=
72 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) { 68 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) {
73 return; 69 return;
74 } 70 }
75 71
76 // Reopen in read only mode. 72 // Reopen in read only mode.
77 pdf_file_.Initialize(GetPdfPath(), 73 pdf_file_.Initialize(GetPdfPath(),
78 base::File::FLAG_OPEN | base::File::FLAG_READ); 74 base::File::FLAG_OPEN | base::File::FLAG_READ);
79 pwg_file_.Initialize(
80 GetPwgPath(),
81 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_APPEND);
82 } 75 }
83 76
84 bool FileHandlers::IsValid() { 77 bool FileHandlers::IsValid() {
85 return pdf_file_.IsValid() && pwg_file_.IsValid(); 78 return pdf_file_.IsValid();
86 } 79 }
87 80
88 // Converts PDF into PWG raster. 81 // Converts PDF into EMF.
89 // Class uses 3 threads: UI, IO and FILE. 82 // Class uses 3 threads: UI, IO and FILE.
90 // Internal workflow is following: 83 // Internal workflow is following:
91 // 1. Create instance on the UI thread. (files_, settings_,) 84 // 1. Create instance on the UI thread. (files_, settings_,)
92 // 2. Create file on the FILE thread. 85 // 2. Create file on the FILE thread.
93 // 3. Start utility process and start conversion on the IO thread. 86 // 3. Start utility process and start conversion on the IO thread.
94 // 4. Run result callback on the UI thread. 87 // 4. Run result callback on the UI thread.
95 // 5. Instance is destroyed from any thread that has the last reference. 88 // 5. Instance is destroyed from any thread that has the last reference.
96 // 6. FileHandlers destroyed on the FILE thread. 89 // 6. FileHandlers destroyed on the FILE thread.
97 // This step posts |FileHandlers| to be destroyed on the FILE thread. 90 // This step posts |FileHandlers| to be destroyed on the FILE thread.
98 // All these steps work sequentially, so no data should be accessed 91 // All these steps work sequentially, so no data should be accessed
99 // simultaneously by several threads. 92 // simultaneously by several threads.
100 class PwgUtilityProcessHostClient : public content::UtilityProcessHostClient { 93 class PdfToEmfUtilityProcessHostClient
94 : public content::UtilityProcessHostClient {
101 public: 95 public:
102 explicit PwgUtilityProcessHostClient( 96 explicit PdfToEmfUtilityProcessHostClient(
103 const printing::PdfRenderSettings& settings, 97 const printing::PdfRenderSettings& settings);
104 const printing::PwgRasterSettings& bitmap_settings);
105 98
106 void Convert(base::RefCountedMemory* data, 99 void Convert(base::RefCountedMemory* data,
107 const PWGRasterConverter::ResultCallback& callback); 100 const PdfToEmfConverter::ResultCallback& callback);
108 101
109 // UtilityProcessHostClient implementation. 102 // UtilityProcessHostClient implementation.
110 virtual void OnProcessCrashed(int exit_code) OVERRIDE; 103 virtual void OnProcessCrashed(int exit_code) OVERRIDE;
111 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 104 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
112 105
113 private: 106 private:
114 virtual ~PwgUtilityProcessHostClient(); 107 virtual ~PdfToEmfUtilityProcessHostClient();
115 108
116 // Message handlers. 109 // Message handlers.
117 void OnProcessStarted(); 110 void OnProcessStarted();
118 void OnSucceeded(); 111 void OnSucceeded(const std::vector<printing::PageRange>& page_ranges,
112 double scale_factor);
119 void OnFailed(); 113 void OnFailed();
120 114
121 void RunCallback(bool success); 115 void RunCallback(const std::vector<printing::PageRange>& page_ranges,
116 double scale_factor);
122 117
123 void StartProcessOnIOThread(); 118 void StartProcessOnIOThread();
124 119
125 void RunCallbackOnUIThread(bool success); 120 void RunCallbackOnUIThread(
121 const std::vector<printing::PageRange>& page_ranges,
122 double scale_factor);
126 void OnFilesReadyOnUIThread(); 123 void OnFilesReadyOnUIThread();
127 124
128 scoped_ptr<FileHandlers> files_; 125 scoped_ptr<FileHandlers> files_;
129 printing::PdfRenderSettings settings_; 126 printing::PdfRenderSettings settings_;
130 printing::PwgRasterSettings bitmap_settings_; 127 PdfToEmfConverter::ResultCallback callback_;
131 PWGRasterConverter::ResultCallback callback_;
132 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; 128 base::WeakPtr<content::UtilityProcessHost> utility_process_host_;
133 129
134 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); 130 DISALLOW_COPY_AND_ASSIGN(PdfToEmfUtilityProcessHostClient);
135 }; 131 };
136 132
137 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( 133 PdfToEmfUtilityProcessHostClient::PdfToEmfUtilityProcessHostClient(
138 const printing::PdfRenderSettings& settings, 134 const printing::PdfRenderSettings& settings)
139 const printing::PwgRasterSettings& bitmap_settings) 135 : settings_(settings) {}
140 : settings_(settings), bitmap_settings_(bitmap_settings) {} 136
141 137 PdfToEmfUtilityProcessHostClient::~PdfToEmfUtilityProcessHostClient() {
142 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() {
143 // Delete temp directory. 138 // Delete temp directory.
144 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release()); 139 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release());
145 } 140 }
146 141
147 void PwgUtilityProcessHostClient::Convert( 142 void PdfToEmfUtilityProcessHostClient::Convert(
148 base::RefCountedMemory* data, 143 base::RefCountedMemory* data,
149 const PWGRasterConverter::ResultCallback& callback) { 144 const PdfToEmfConverter::ResultCallback& callback) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 145 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
151 callback_ = callback; 146 callback_ = callback;
152 CHECK(!files_); 147 CHECK(!files_);
153 files_.reset(new FileHandlers()); 148 files_.reset(new FileHandlers());
154 BrowserThread::PostTaskAndReply( 149 BrowserThread::PostTaskAndReply(
155 BrowserThread::FILE, FROM_HERE, 150 BrowserThread::FILE,
156 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), 151 FROM_HERE,
152 base::Bind(&FileHandlers::Init,
153 base::Unretained(files_.get()),
157 make_scoped_refptr(data)), 154 make_scoped_refptr(data)),
158 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); 155 base::Bind(&PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread,
159 } 156 this));
160 157 }
161 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { 158
159 void PdfToEmfUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
162 OnFailed(); 160 OnFailed();
163 } 161 }
164 162
165 bool PwgUtilityProcessHostClient::OnMessageReceived( 163 bool PdfToEmfUtilityProcessHostClient::OnMessageReceived(
166 const IPC::Message& message) { 164 const IPC::Message& message) {
167 bool handled = true; 165 bool handled = true;
168 IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message) 166 IPC_BEGIN_MESSAGE_MAP(PdfToEmfUtilityProcessHostClient, message)
169 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) 167 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted)
170 IPC_MESSAGE_HANDLER( 168 IPC_MESSAGE_HANDLER(
171 ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded) 169 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, OnSucceeded)
172 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed, 170 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed,
173 OnFailed) 171 OnFailed)
174 IPC_MESSAGE_UNHANDLED(handled = false) 172 IPC_MESSAGE_UNHANDLED(handled = false)
175 IPC_END_MESSAGE_MAP() 173 IPC_END_MESSAGE_MAP()
176 return handled; 174 return handled;
177 } 175 }
178 176
179 void PwgUtilityProcessHostClient::OnProcessStarted() { 177 void PdfToEmfUtilityProcessHostClient::OnProcessStarted() {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
181 if (!utility_process_host_) { 179 if (!utility_process_host_) {
182 RunCallbackOnUIThread(false); 180 RunCallbackOnUIThread(std::vector<printing::PageRange>(), 0.0);
183 return; 181 return;
184 } 182 }
185 183
186 base::ProcessHandle process = utility_process_host_->GetData().handle; 184 base::ProcessHandle process = utility_process_host_->GetData().handle;
187 utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( 185 utility_process_host_->Send(
188 files_->GetPdfForProcess(process), 186 new ChromeUtilityMsg_RenderPDFPagesToMetafiles(
189 settings_, 187 files_->GetPdfForProcess(process),
190 bitmap_settings_, 188 files_->GetEmfPath(),
191 files_->GetPwgForProcess(process))); 189 settings_,
190 std::vector<printing::PageRange>()));
192 utility_process_host_.reset(); 191 utility_process_host_.reset();
193 } 192 }
194 193
195 void PwgUtilityProcessHostClient::OnSucceeded() { 194 void PdfToEmfUtilityProcessHostClient::OnSucceeded(
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 195 const std::vector<printing::PageRange>& page_ranges,
197 RunCallback(true); 196 double scale_factor) {
198 } 197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
199 198 RunCallback(page_ranges, scale_factor);
200 void PwgUtilityProcessHostClient::OnFailed() { 199 }
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 200
202 RunCallback(false); 201 void PdfToEmfUtilityProcessHostClient::OnFailed() {
203 } 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
204 203 RunCallback(std::vector<printing::PageRange>(), 0.0);
205 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { 204 }
205
206 void PdfToEmfUtilityProcessHostClient::OnFilesReadyOnUIThread() {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 if (!files_->IsValid()) { 208 if (!files_->IsValid()) {
208 RunCallbackOnUIThread(false); 209 RunCallbackOnUIThread(std::vector<printing::PageRange>(), 0.0);
209 return; 210 return;
210 } 211 }
211 BrowserThread::PostTask( 212 BrowserThread::PostTask(
212 BrowserThread::IO, FROM_HERE, 213 BrowserThread::IO,
213 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); 214 FROM_HERE,
214 } 215 base::Bind(&PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread,
215 216 this));
216 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { 217 }
218
219 void PdfToEmfUtilityProcessHostClient::StartProcessOnIOThread() {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
218 utility_process_host_ = 221 utility_process_host_ =
219 content::UtilityProcessHost::Create( 222 content::UtilityProcessHost::Create(
220 this, 223 this,
221 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr(); 224 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr();
225 // NOTE: This process _must_ be sandboxed, otherwise the pdf dll will load
226 // gdiplus.dll, change how rendering happens, and not be able to correctly
227 // generate when sent to a metafile DC.
228 utility_process_host_->SetExposedDir(files_->GetBasePath());
222 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); 229 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing);
223 } 230 }
224 231
225 void PwgUtilityProcessHostClient::RunCallback(bool success) { 232 void PdfToEmfUtilityProcessHostClient::RunCallback(
233 const std::vector<printing::PageRange>& page_ranges,
234 double scale_factor) {
226 BrowserThread::PostTask( 235 BrowserThread::PostTask(
227 BrowserThread::UI, FROM_HERE, 236 BrowserThread::UI,
228 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, 237 FROM_HERE,
229 success)); 238 base::Bind(&PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread,
230 } 239 this,
231 240 page_ranges,
232 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { 241 scale_factor));
242 }
243
244 void PdfToEmfUtilityProcessHostClient::RunCallbackOnUIThread(
245 const std::vector<printing::PageRange>& page_ranges,
246 double scale_factor) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 247 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
248 std::vector<base::FilePath> page_filenames;
249 std::vector<printing::PageRange>::const_iterator iter;
250 for (iter = page_ranges.begin(); iter != page_ranges.end(); ++iter) {
251 for (int page_number = iter->from; page_number <= iter->to; ++page_number) {
252 page_filenames.push_back(files_->GetEmfPath().InsertBeforeExtensionASCII(
253 base::StringPrintf(".%d", page_number)));
254 }
255 }
234 if (!callback_.is_null()) { 256 if (!callback_.is_null()) {
235 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 257 BrowserThread::PostTask(
236 base::Bind(callback_, success, 258 BrowserThread::UI,
237 files_->GetPwgPath())); 259 FROM_HERE,
260 base::Bind(callback_, scale_factor, page_filenames));
238 callback_.Reset(); 261 callback_.Reset();
239 } 262 }
240 } 263 }
241 264
242 class PWGRasterConverterImpl : public PWGRasterConverter { 265 class PdfToEmfConverterImpl : public PdfToEmfConverter {
243 public: 266 public:
244 PWGRasterConverterImpl(); 267 PdfToEmfConverterImpl();
245 268
246 virtual ~PWGRasterConverterImpl(); 269 virtual ~PdfToEmfConverterImpl();
247 270
248 virtual void Start(base::RefCountedMemory* data, 271 virtual void Start(base::RefCountedMemory* data,
249 const printing::PdfRenderSettings& conversion_settings, 272 const printing::PdfRenderSettings& conversion_settings,
250 const printing::PwgRasterSettings& bitmap_settings,
251 const ResultCallback& callback) OVERRIDE; 273 const ResultCallback& callback) OVERRIDE;
252 274
253 private: 275 private:
254 scoped_refptr<PwgUtilityProcessHostClient> utility_client_; 276 scoped_refptr<PdfToEmfUtilityProcessHostClient> utility_client_;
255 base::CancelableCallback<ResultCallback::RunType> callback_; 277 base::CancelableCallback<ResultCallback::RunType> callback_;
256 278
257 DISALLOW_COPY_AND_ASSIGN(PWGRasterConverterImpl); 279 DISALLOW_COPY_AND_ASSIGN(PdfToEmfConverterImpl);
258 }; 280 };
259 281
260 PWGRasterConverterImpl::PWGRasterConverterImpl() { 282 PdfToEmfConverterImpl::PdfToEmfConverterImpl() {
261 } 283 }
262 284
263 PWGRasterConverterImpl::~PWGRasterConverterImpl() { 285 PdfToEmfConverterImpl::~PdfToEmfConverterImpl() {
264 } 286 }
265 287
266 void PWGRasterConverterImpl::Start( 288 void PdfToEmfConverterImpl::Start(
267 base::RefCountedMemory* data, 289 base::RefCountedMemory* data,
268 const printing::PdfRenderSettings& conversion_settings, 290 const printing::PdfRenderSettings& conversion_settings,
269 const printing::PwgRasterSettings& bitmap_settings,
270 const ResultCallback& callback) { 291 const ResultCallback& callback) {
271 // Rebind cancelable callback to avoid calling callback if 292 // Rebind cancelable callback to avoid calling callback if
272 // PWGRasterConverterImpl is destroyed. 293 // PdfToEmfConverterImpl is destroyed.
273 callback_.Reset(callback); 294 callback_.Reset(callback);
274 utility_client_ = 295 utility_client_ = new PdfToEmfUtilityProcessHostClient(conversion_settings);
275 new PwgUtilityProcessHostClient(conversion_settings, bitmap_settings);
276 utility_client_->Convert(data, callback_.callback()); 296 utility_client_->Convert(data, callback_.callback());
277 } 297 }
278 298
279 } // namespace 299 } // namespace
280 300
281 // static 301 // static
282 scoped_ptr<PWGRasterConverter> PWGRasterConverter::CreateDefault() { 302 scoped_ptr<PdfToEmfConverter> PdfToEmfConverter::CreateDefault() {
283 return scoped_ptr<PWGRasterConverter>(new PWGRasterConverterImpl()); 303 return scoped_ptr<PdfToEmfConverter>(new PdfToEmfConverterImpl());
284 } 304 }
285 305
286 } // namespace local_discovery 306 } // namespace printing
OLDNEW
« no previous file with comments | « chrome/browser/printing/pdf_to_emf_converter.h ('k') | chrome/browser/printing/print_view_manager_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698