OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/pwg_raster_converter.h" | 5 #include "chrome/browser/printing/pwg_raster_converter.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/cancelable_callback.h" | 12 #include "base/cancelable_callback.h" |
13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
16 #include "base/location.h" | 16 #include "base/location.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/memory/ptr_util.h" | 19 #include "base/memory/ptr_util.h" |
20 #include "base/single_thread_task_runner.h" | 20 #include "base/single_thread_task_runner.h" |
21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
22 #include "chrome/common/chrome_utility_messages.h" | 22 #include "chrome/common/chrome_utility_messages.h" |
23 #include "chrome/common/chrome_utility_printing_messages.h" | |
24 #include "chrome/grit/generated_resources.h" | 23 #include "chrome/grit/generated_resources.h" |
25 #include "components/cloud_devices/common/cloud_device_description.h" | 24 #include "components/cloud_devices/common/cloud_device_description.h" |
26 #include "components/cloud_devices/common/printer_description.h" | 25 #include "components/cloud_devices/common/printer_description.h" |
26 #include "components/printing/common/printing.mojom.h" | |
27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
28 #include "content/public/browser/child_process_data.h" | 28 #include "content/public/browser/child_process_data.h" |
29 #include "content/public/browser/utility_process_host.h" | 29 #include "content/public/browser/utility_process_host.h" |
30 #include "content/public/browser/utility_process_host_client.h" | 30 #include "content/public/browser/utility_process_host_client.h" |
31 #include "mojo/public/cpp/system/platform_handle.h" | |
Sam McNally
2017/01/05 06:23:15
Remove.
tibell
2017/01/12 03:30:14
Done.
| |
31 #include "printing/pdf_render_settings.h" | 32 #include "printing/pdf_render_settings.h" |
32 #include "printing/pwg_raster_settings.h" | 33 #include "printing/pwg_raster_settings.h" |
33 #include "printing/units.h" | 34 #include "printing/units.h" |
35 #include "services/service_manager/public/cpp/interface_provider.h" | |
34 #include "ui/base/l10n/l10n_util.h" | 36 #include "ui/base/l10n/l10n_util.h" |
35 #include "ui/gfx/geometry/rect.h" | 37 #include "ui/gfx/geometry/rect.h" |
36 #include "ui/gfx/geometry/size.h" | 38 #include "ui/gfx/geometry/size.h" |
37 | 39 |
38 namespace printing { | 40 namespace printing { |
39 | 41 |
40 namespace { | 42 namespace { |
41 | 43 |
42 using content::BrowserThread; | 44 using content::BrowserThread; |
43 | 45 |
44 class FileHandlers { | 46 class FileHandlers { |
45 public: | 47 public: |
46 FileHandlers() {} | 48 FileHandlers() {} |
47 | 49 |
48 ~FileHandlers() { | 50 ~FileHandlers() { |
49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 51 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
50 } | 52 } |
51 | 53 |
52 void Init(base::RefCountedMemory* data); | 54 void Init(base::RefCountedMemory* data); |
53 bool IsValid(); | 55 bool IsValid(); |
54 | 56 |
55 base::FilePath GetPwgPath() const { | 57 base::FilePath GetPwgPath() const { |
56 return temp_dir_.GetPath().AppendASCII("output.pwg"); | 58 return temp_dir_.GetPath().AppendASCII("output.pwg"); |
57 } | 59 } |
58 | 60 |
59 base::FilePath GetPdfPath() const { | 61 base::FilePath GetPdfPath() const { |
60 return temp_dir_.GetPath().AppendASCII("input.pdf"); | 62 return temp_dir_.GetPath().AppendASCII("input.pdf"); |
61 } | 63 } |
62 | 64 |
63 IPC::PlatformFileForTransit GetPdfForProcess() { | 65 base::File GetPdfForProcess() { |
64 DCHECK(pdf_file_.IsValid()); | 66 DCHECK(pdf_file_.IsValid()); |
65 IPC::PlatformFileForTransit transit = | 67 return pdf_file_.Duplicate(); |
66 IPC::TakePlatformFileForTransit(std::move(pdf_file_)); | |
67 return transit; | |
68 } | 68 } |
69 | 69 |
70 IPC::PlatformFileForTransit GetPwgForProcess() { | 70 base::File GetPwgForProcess() { |
71 DCHECK(pwg_file_.IsValid()); | 71 DCHECK(pwg_file_.IsValid()); |
72 IPC::PlatformFileForTransit transit = | 72 return pwg_file_.Duplicate(); |
73 IPC::TakePlatformFileForTransit(std::move(pwg_file_)); | |
74 return transit; | |
75 } | 73 } |
76 | 74 |
77 private: | 75 private: |
78 base::ScopedTempDir temp_dir_; | 76 base::ScopedTempDir temp_dir_; |
79 base::File pdf_file_; | 77 base::File pdf_file_; |
80 base::File pwg_file_; | 78 base::File pwg_file_; |
81 | 79 |
82 DISALLOW_COPY_AND_ASSIGN(FileHandlers); | 80 DISALLOW_COPY_AND_ASSIGN(FileHandlers); |
83 }; | 81 }; |
84 | 82 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 void Convert(base::RefCountedMemory* data, | 123 void Convert(base::RefCountedMemory* data, |
126 const PWGRasterConverter::ResultCallback& callback); | 124 const PWGRasterConverter::ResultCallback& callback); |
127 | 125 |
128 // UtilityProcessHostClient implementation. | 126 // UtilityProcessHostClient implementation. |
129 void OnProcessCrashed(int exit_code) override; | 127 void OnProcessCrashed(int exit_code) override; |
130 bool OnMessageReceived(const IPC::Message& message) override; | 128 bool OnMessageReceived(const IPC::Message& message) override; |
131 | 129 |
132 private: | 130 private: |
133 ~PwgUtilityProcessHostClient() override; | 131 ~PwgUtilityProcessHostClient() override; |
134 | 132 |
135 // Message handlers. | |
136 void OnSucceeded(); | |
137 void OnFailed(); | |
138 | |
139 void RunCallback(bool success); | 133 void RunCallback(bool success); |
140 | 134 |
141 void StartProcessOnIOThread(); | 135 void StartProcessOnIOThread(); |
142 | 136 |
143 void RunCallbackOnUIThread(bool success); | 137 void RunCallbackOnUIThread(bool success); |
144 void OnFilesReadyOnUIThread(); | 138 void OnFilesReadyOnUIThread(); |
145 | 139 |
146 std::unique_ptr<FileHandlers, BrowserThread::DeleteOnFileThread> files_; | 140 std::unique_ptr<FileHandlers, BrowserThread::DeleteOnFileThread> files_; |
147 PdfRenderSettings settings_; | 141 PdfRenderSettings settings_; |
148 PwgRasterSettings bitmap_settings_; | 142 PwgRasterSettings bitmap_settings_; |
149 PWGRasterConverter::ResultCallback callback_; | 143 PWGRasterConverter::ResultCallback callback_; |
144 mojom::PrintingPtr printing_; | |
150 | 145 |
151 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); | 146 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); |
152 }; | 147 }; |
153 | 148 |
154 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( | 149 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( |
155 const PdfRenderSettings& settings, | 150 const PdfRenderSettings& settings, |
156 const PwgRasterSettings& bitmap_settings) | 151 const PwgRasterSettings& bitmap_settings) |
157 : settings_(settings), bitmap_settings_(bitmap_settings) {} | 152 : settings_(settings), bitmap_settings_(bitmap_settings) {} |
158 | 153 |
159 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { | 154 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { |
160 } | 155 } |
161 | 156 |
162 void PwgUtilityProcessHostClient::Convert( | 157 void PwgUtilityProcessHostClient::Convert( |
163 base::RefCountedMemory* data, | 158 base::RefCountedMemory* data, |
164 const PWGRasterConverter::ResultCallback& callback) { | 159 const PWGRasterConverter::ResultCallback& callback) { |
165 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 160 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
166 | 161 |
167 callback_ = callback; | 162 callback_ = callback; |
168 CHECK(!files_); | 163 CHECK(!files_); |
169 files_.reset(new FileHandlers()); | 164 files_.reset(new FileHandlers()); |
170 | 165 |
171 BrowserThread::PostTaskAndReply( | 166 BrowserThread::PostTaskAndReply( |
172 BrowserThread::FILE, FROM_HERE, | 167 BrowserThread::FILE, FROM_HERE, |
173 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), | 168 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), |
174 base::RetainedRef(data)), | 169 base::RetainedRef(data)), |
175 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); | 170 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); |
176 } | 171 } |
177 | 172 |
178 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { | 173 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { |
179 OnFailed(); | 174 RunCallback(false); |
180 } | 175 } |
181 | 176 |
182 bool PwgUtilityProcessHostClient::OnMessageReceived( | 177 bool PwgUtilityProcessHostClient::OnMessageReceived( |
183 const IPC::Message& message) { | 178 const IPC::Message& message) { |
184 bool handled = true; | 179 return false; |
185 IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message) | |
186 IPC_MESSAGE_HANDLER( | |
187 ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded) | |
188 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed, | |
189 OnFailed) | |
190 IPC_MESSAGE_UNHANDLED(handled = false) | |
191 IPC_END_MESSAGE_MAP() | |
192 return handled; | |
193 } | |
194 | |
195 void PwgUtilityProcessHostClient::OnSucceeded() { | |
196 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
197 RunCallback(true); | |
198 } | |
199 | |
200 void PwgUtilityProcessHostClient::OnFailed() { | |
201 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
202 RunCallback(false); | |
203 } | 180 } |
204 | 181 |
205 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { | 182 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { |
206 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 183 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
207 | 184 |
208 if (!files_->IsValid()) { | 185 if (!files_->IsValid()) { |
209 RunCallbackOnUIThread(false); | 186 RunCallbackOnUIThread(false); |
210 return; | 187 return; |
211 } | 188 } |
212 BrowserThread::PostTask( | 189 BrowserThread::PostTask( |
213 BrowserThread::IO, FROM_HERE, | 190 BrowserThread::IO, FROM_HERE, |
214 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); | 191 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); |
215 } | 192 } |
216 | 193 |
217 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { | 194 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { |
218 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
219 | 196 |
220 content::UtilityProcessHost* utility_process_host = | 197 content::UtilityProcessHost* utility_process_host = |
221 content::UtilityProcessHost::Create(this, | 198 content::UtilityProcessHost::Create(this, |
222 base::ThreadTaskRunnerHandle::Get()); | 199 base::ThreadTaskRunnerHandle::Get()); |
223 utility_process_host->SetName(l10n_util::GetStringUTF16( | 200 utility_process_host->SetName( |
Sam McNally
2017/01/05 06:23:15
I suspect this could also be simplified by using U
tibell
2017/01/12 03:30:14
Added TODO.
| |
224 IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME)); | 201 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME)); |
225 utility_process_host->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( | 202 utility_process_host->Start(); |
203 | |
204 mojom::PrintingFactoryPtr printing_factory; | |
205 utility_process_host->GetRemoteInterfaces()->GetInterface(&printing_factory); | |
206 // We don't care about calls to FontPreCaching so we pass a null pointer for | |
207 // the second argument. | |
208 printing_factory->MakePrinting(mojo::MakeRequest(&printing_), | |
209 mojom::FontPreCachingPtr()); | |
210 printing_->RenderPDFPagesToPWGRaster( | |
226 files_->GetPdfForProcess(), settings_, bitmap_settings_, | 211 files_->GetPdfForProcess(), settings_, bitmap_settings_, |
227 files_->GetPwgForProcess())); | 212 files_->GetPwgForProcess(), |
213 base::Bind(&PwgUtilityProcessHostClient::RunCallback, this)); | |
228 } | 214 } |
229 | 215 |
230 void PwgUtilityProcessHostClient::RunCallback(bool success) { | 216 void PwgUtilityProcessHostClient::RunCallback(bool success) { |
217 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
231 BrowserThread::PostTask( | 218 BrowserThread::PostTask( |
232 BrowserThread::UI, FROM_HERE, | 219 BrowserThread::UI, FROM_HERE, |
233 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, | 220 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, |
234 success)); | 221 success)); |
235 } | 222 } |
236 | 223 |
237 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { | 224 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { |
238 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 225 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
239 if (callback_.is_null()) | 226 if (callback_.is_null()) |
240 return; | 227 return; |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
339 result.odd_page_transform = TRANSFORM_FLIP_HORIZONTAL; | 326 result.odd_page_transform = TRANSFORM_FLIP_HORIZONTAL; |
340 break; | 327 break; |
341 } | 328 } |
342 | 329 |
343 result.rotate_all_pages = raster_capability.value().rotate_all_pages; | 330 result.rotate_all_pages = raster_capability.value().rotate_all_pages; |
344 result.reverse_page_order = raster_capability.value().reverse_order_streaming; | 331 result.reverse_page_order = raster_capability.value().reverse_order_streaming; |
345 return result; | 332 return result; |
346 } | 333 } |
347 | 334 |
348 } // namespace printing | 335 } // namespace printing |
OLD | NEW |