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

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

Issue 2477283002: Convert printing IPCs to Mojo
Patch Set: Fix more Windows compile errors Created 3 years, 11 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
OLDNEW
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 "printing/pdf_render_settings.h" 31 #include "printing/pdf_render_settings.h"
32 #include "printing/pwg_raster_settings.h" 32 #include "printing/pwg_raster_settings.h"
33 #include "printing/units.h" 33 #include "printing/units.h"
34 #include "services/service_manager/public/cpp/interface_provider.h"
34 #include "ui/base/l10n/l10n_util.h" 35 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/gfx/geometry/rect.h" 36 #include "ui/gfx/geometry/rect.h"
36 #include "ui/gfx/geometry/size.h" 37 #include "ui/gfx/geometry/size.h"
37 38
38 namespace printing { 39 namespace printing {
39 40
40 namespace { 41 namespace {
41 42
42 using content::BrowserThread; 43 using content::BrowserThread;
43 44
44 class FileHandlers { 45 class FileHandlers {
45 public: 46 public:
46 FileHandlers() {} 47 FileHandlers() {}
47 48
48 ~FileHandlers() { 49 ~FileHandlers() {
49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 50 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
50 } 51 }
51 52
52 void Init(base::RefCountedMemory* data); 53 void Init(base::RefCountedMemory* data);
53 bool IsValid(); 54 bool IsValid();
54 55
55 base::FilePath GetPwgPath() const { 56 base::FilePath GetPwgPath() const {
56 return temp_dir_.GetPath().AppendASCII("output.pwg"); 57 return temp_dir_.GetPath().AppendASCII("output.pwg");
57 } 58 }
58 59
59 base::FilePath GetPdfPath() const { 60 base::FilePath GetPdfPath() const {
60 return temp_dir_.GetPath().AppendASCII("input.pdf"); 61 return temp_dir_.GetPath().AppendASCII("input.pdf");
61 } 62 }
62 63
63 IPC::PlatformFileForTransit GetPdfForProcess() { 64 base::File GetPdfForProcess() {
64 DCHECK(pdf_file_.IsValid()); 65 DCHECK(pdf_file_.IsValid());
65 IPC::PlatformFileForTransit transit = 66 return std::move(pdf_file_);
66 IPC::TakePlatformFileForTransit(std::move(pdf_file_));
67 return transit;
68 } 67 }
69 68
70 IPC::PlatformFileForTransit GetPwgForProcess() { 69 base::File GetPwgForProcess() {
71 DCHECK(pwg_file_.IsValid()); 70 DCHECK(pwg_file_.IsValid());
72 IPC::PlatformFileForTransit transit = 71 return std::move(pwg_file_);
73 IPC::TakePlatformFileForTransit(std::move(pwg_file_));
74 return transit;
75 } 72 }
76 73
77 private: 74 private:
78 base::ScopedTempDir temp_dir_; 75 base::ScopedTempDir temp_dir_;
79 base::File pdf_file_; 76 base::File pdf_file_;
80 base::File pwg_file_; 77 base::File pwg_file_;
81 78
82 DISALLOW_COPY_AND_ASSIGN(FileHandlers); 79 DISALLOW_COPY_AND_ASSIGN(FileHandlers);
83 }; 80 };
84 81
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 void Convert(base::RefCountedMemory* data, 122 void Convert(base::RefCountedMemory* data,
126 const PWGRasterConverter::ResultCallback& callback); 123 const PWGRasterConverter::ResultCallback& callback);
127 124
128 // UtilityProcessHostClient implementation. 125 // UtilityProcessHostClient implementation.
129 void OnProcessCrashed(int exit_code) override; 126 void OnProcessCrashed(int exit_code) override;
130 bool OnMessageReceived(const IPC::Message& message) override; 127 bool OnMessageReceived(const IPC::Message& message) override;
131 128
132 private: 129 private:
133 ~PwgUtilityProcessHostClient() override; 130 ~PwgUtilityProcessHostClient() override;
134 131
135 // Message handlers.
136 void OnSucceeded();
137 void OnFailed();
138
139 void RunCallback(bool success); 132 void RunCallback(bool success);
140 133
141 void StartProcessOnIOThread(); 134 void StartProcessOnIOThread();
142 135
143 void RunCallbackOnUIThread(bool success); 136 void RunCallbackOnUIThread(bool success);
144 void OnFilesReadyOnUIThread(); 137 void OnFilesReadyOnUIThread();
145 138
146 std::unique_ptr<FileHandlers, BrowserThread::DeleteOnFileThread> files_; 139 std::unique_ptr<FileHandlers, BrowserThread::DeleteOnFileThread> files_;
147 PdfRenderSettings settings_; 140 PdfRenderSettings settings_;
148 PwgRasterSettings bitmap_settings_; 141 PwgRasterSettings bitmap_settings_;
149 PWGRasterConverter::ResultCallback callback_; 142 PWGRasterConverter::ResultCallback callback_;
143 mojom::PrintingPtr printing_;
150 144
151 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); 145 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient);
152 }; 146 };
153 147
154 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( 148 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient(
155 const PdfRenderSettings& settings, 149 const PdfRenderSettings& settings,
156 const PwgRasterSettings& bitmap_settings) 150 const PwgRasterSettings& bitmap_settings)
157 : settings_(settings), bitmap_settings_(bitmap_settings) {} 151 : settings_(settings), bitmap_settings_(bitmap_settings) {}
158 152
159 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { 153 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() {
160 } 154 }
161 155
162 void PwgUtilityProcessHostClient::Convert( 156 void PwgUtilityProcessHostClient::Convert(
163 base::RefCountedMemory* data, 157 base::RefCountedMemory* data,
164 const PWGRasterConverter::ResultCallback& callback) { 158 const PWGRasterConverter::ResultCallback& callback) {
165 DCHECK_CURRENTLY_ON(BrowserThread::UI); 159 DCHECK_CURRENTLY_ON(BrowserThread::UI);
166 160
167 callback_ = callback; 161 callback_ = callback;
168 CHECK(!files_); 162 CHECK(!files_);
169 files_.reset(new FileHandlers()); 163 files_.reset(new FileHandlers());
170 164
171 BrowserThread::PostTaskAndReply( 165 BrowserThread::PostTaskAndReply(
172 BrowserThread::FILE, FROM_HERE, 166 BrowserThread::FILE, FROM_HERE,
173 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), 167 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()),
174 base::RetainedRef(data)), 168 base::RetainedRef(data)),
175 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); 169 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this));
176 } 170 }
177 171
178 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { 172 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) {
179 OnFailed(); 173 RunCallback(false);
180 } 174 }
181 175
182 bool PwgUtilityProcessHostClient::OnMessageReceived( 176 bool PwgUtilityProcessHostClient::OnMessageReceived(
183 const IPC::Message& message) { 177 const IPC::Message& message) {
184 bool handled = true; 178 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 } 179 }
204 180
205 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { 181 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() {
206 DCHECK_CURRENTLY_ON(BrowserThread::UI); 182 DCHECK_CURRENTLY_ON(BrowserThread::UI);
207 183
208 if (!files_->IsValid()) { 184 if (!files_->IsValid()) {
209 RunCallbackOnUIThread(false); 185 RunCallbackOnUIThread(false);
210 return; 186 return;
211 } 187 }
212 BrowserThread::PostTask( 188 BrowserThread::PostTask(
213 BrowserThread::IO, FROM_HERE, 189 BrowserThread::IO, FROM_HERE,
214 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); 190 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this));
215 } 191 }
216 192
217 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { 193 void PwgUtilityProcessHostClient::StartProcessOnIOThread() {
218 DCHECK_CURRENTLY_ON(BrowserThread::IO); 194 DCHECK_CURRENTLY_ON(BrowserThread::IO);
219 195
196 // TODO(tibell): Use UtilityProcessMojoClient to manage process lifetime
197 // instead.
220 content::UtilityProcessHost* utility_process_host = 198 content::UtilityProcessHost* utility_process_host =
221 content::UtilityProcessHost::Create(this, 199 content::UtilityProcessHost::Create(this,
222 base::ThreadTaskRunnerHandle::Get()); 200 base::ThreadTaskRunnerHandle::Get());
223 utility_process_host->SetName(l10n_util::GetStringUTF16( 201 utility_process_host->SetName(
224 IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME)); 202 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME));
225 utility_process_host->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( 203 utility_process_host->Start();
204
205 mojom::PrintingFactoryPtr printing_factory;
206 utility_process_host->GetRemoteInterfaces()->GetInterface(&printing_factory);
207 // We don't care about calls to FontPreCaching so we pass a null pointer for
208 // the second argument.
209 printing_factory->MakePrinting(mojo::MakeRequest(&printing_),
210 mojom::FontPreCachingPtr());
211 printing_->RenderPDFPagesToPWGRaster(
226 files_->GetPdfForProcess(), settings_, bitmap_settings_, 212 files_->GetPdfForProcess(), settings_, bitmap_settings_,
227 files_->GetPwgForProcess())); 213 files_->GetPwgForProcess(),
214 base::Bind(&PwgUtilityProcessHostClient::RunCallback, this));
228 } 215 }
229 216
230 void PwgUtilityProcessHostClient::RunCallback(bool success) { 217 void PwgUtilityProcessHostClient::RunCallback(bool success) {
218 DCHECK_CURRENTLY_ON(BrowserThread::IO);
231 BrowserThread::PostTask( 219 BrowserThread::PostTask(
232 BrowserThread::UI, FROM_HERE, 220 BrowserThread::UI, FROM_HERE,
233 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, 221 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this,
234 success)); 222 success));
235 } 223 }
236 224
237 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { 225 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) {
238 DCHECK_CURRENTLY_ON(BrowserThread::UI); 226 DCHECK_CURRENTLY_ON(BrowserThread::UI);
239 if (callback_.is_null()) 227 if (callback_.is_null())
240 return; 228 return;
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 result.odd_page_transform = TRANSFORM_FLIP_HORIZONTAL; 327 result.odd_page_transform = TRANSFORM_FLIP_HORIZONTAL;
340 break; 328 break;
341 } 329 }
342 330
343 result.rotate_all_pages = raster_capability.value().rotate_all_pages; 331 result.rotate_all_pages = raster_capability.value().rotate_all_pages;
344 result.reverse_page_order = raster_capability.value().reverse_order_streaming; 332 result.reverse_page_order = raster_capability.value().reverse_order_streaming;
345 return result; 333 return result;
346 } 334 }
347 335
348 } // namespace printing 336 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698