| 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/local_discovery/pwg_raster_converter.h" | 5 #include "chrome/browser/local_discovery/pwg_raster_converter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/cancelable_callback.h" | 10 #include "base/cancelable_callback.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 namespace { | 33 namespace { |
| 34 | 34 |
| 35 using content::BrowserThread; | 35 using content::BrowserThread; |
| 36 | 36 |
| 37 class FileHandlers { | 37 class FileHandlers { |
| 38 public: | 38 public: |
| 39 FileHandlers() {} | 39 FileHandlers() {} |
| 40 | 40 |
| 41 ~FileHandlers() { | 41 ~FileHandlers() { |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 42 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void Init(base::RefCountedMemory* data); | 45 void Init(base::RefCountedMemory* data); |
| 46 bool IsValid(); | 46 bool IsValid(); |
| 47 | 47 |
| 48 base::FilePath GetPwgPath() const { | 48 base::FilePath GetPwgPath() const { |
| 49 return temp_dir_.path().AppendASCII("output.pwg"); | 49 return temp_dir_.path().AppendASCII("output.pwg"); |
| 50 } | 50 } |
| 51 | 51 |
| 52 base::FilePath GetPdfPath() const { | 52 base::FilePath GetPdfPath() const { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 67 return transit; | 67 return transit; |
| 68 } | 68 } |
| 69 | 69 |
| 70 private: | 70 private: |
| 71 base::ScopedTempDir temp_dir_; | 71 base::ScopedTempDir temp_dir_; |
| 72 base::File pdf_file_; | 72 base::File pdf_file_; |
| 73 base::File pwg_file_; | 73 base::File pwg_file_; |
| 74 }; | 74 }; |
| 75 | 75 |
| 76 void FileHandlers::Init(base::RefCountedMemory* data) { | 76 void FileHandlers::Init(base::RefCountedMemory* data) { |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 77 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 78 | 78 |
| 79 if (!temp_dir_.CreateUniqueTempDir()) { | 79 if (!temp_dir_.CreateUniqueTempDir()) { |
| 80 return; | 80 return; |
| 81 } | 81 } |
| 82 | 82 |
| 83 if (static_cast<int>(data->size()) != | 83 if (static_cast<int>(data->size()) != |
| 84 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) { | 84 base::WriteFile(GetPdfPath(), data->front_as<char>(), data->size())) { |
| 85 return; | 85 return; |
| 86 } | 86 } |
| 87 | 87 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 const printing::PdfRenderSettings& settings, | 149 const printing::PdfRenderSettings& settings, |
| 150 const printing::PwgRasterSettings& bitmap_settings) | 150 const printing::PwgRasterSettings& bitmap_settings) |
| 151 : settings_(settings), bitmap_settings_(bitmap_settings) {} | 151 : settings_(settings), bitmap_settings_(bitmap_settings) {} |
| 152 | 152 |
| 153 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { | 153 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { |
| 154 } | 154 } |
| 155 | 155 |
| 156 void PwgUtilityProcessHostClient::Convert( | 156 void PwgUtilityProcessHostClient::Convert( |
| 157 base::RefCountedMemory* data, | 157 base::RefCountedMemory* data, |
| 158 const PWGRasterConverter::ResultCallback& callback) { | 158 const PWGRasterConverter::ResultCallback& callback) { |
| 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 160 callback_ = callback; | 160 callback_ = callback; |
| 161 CHECK(!files_); | 161 CHECK(!files_); |
| 162 files_.reset(new FileHandlers()); | 162 files_.reset(new FileHandlers()); |
| 163 BrowserThread::PostTaskAndReply( | 163 BrowserThread::PostTaskAndReply( |
| 164 BrowserThread::FILE, FROM_HERE, | 164 BrowserThread::FILE, FROM_HERE, |
| 165 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), | 165 base::Bind(&FileHandlers::Init, base::Unretained(files_.get()), |
| 166 make_scoped_refptr(data)), | 166 make_scoped_refptr(data)), |
| 167 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); | 167 base::Bind(&PwgUtilityProcessHostClient::OnFilesReadyOnUIThread, this)); |
| 168 } | 168 } |
| 169 | 169 |
| 170 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { | 170 void PwgUtilityProcessHostClient::OnProcessCrashed(int exit_code) { |
| 171 OnFailed(); | 171 OnFailed(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 bool PwgUtilityProcessHostClient::OnMessageReceived( | 174 bool PwgUtilityProcessHostClient::OnMessageReceived( |
| 175 const IPC::Message& message) { | 175 const IPC::Message& message) { |
| 176 bool handled = true; | 176 bool handled = true; |
| 177 IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message) | 177 IPC_BEGIN_MESSAGE_MAP(PwgUtilityProcessHostClient, message) |
| 178 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) | 178 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ProcessStarted, OnProcessStarted) |
| 179 IPC_MESSAGE_HANDLER( | 179 IPC_MESSAGE_HANDLER( |
| 180 ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded) | 180 ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Succeeded, OnSucceeded) |
| 181 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed, | 181 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToPWGRaster_Failed, |
| 182 OnFailed) | 182 OnFailed) |
| 183 IPC_MESSAGE_UNHANDLED(handled = false) | 183 IPC_MESSAGE_UNHANDLED(handled = false) |
| 184 IPC_END_MESSAGE_MAP() | 184 IPC_END_MESSAGE_MAP() |
| 185 return handled; | 185 return handled; |
| 186 } | 186 } |
| 187 | 187 |
| 188 void PwgUtilityProcessHostClient::OnProcessStarted() { | 188 void PwgUtilityProcessHostClient::OnProcessStarted() { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 190 if (!utility_process_host_) { | 190 if (!utility_process_host_) { |
| 191 RunCallbackOnUIThread(false); | 191 RunCallbackOnUIThread(false); |
| 192 return; | 192 return; |
| 193 } | 193 } |
| 194 | 194 |
| 195 base::ProcessHandle process = utility_process_host_->GetData().handle; | 195 base::ProcessHandle process = utility_process_host_->GetData().handle; |
| 196 utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( | 196 utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( |
| 197 files_->GetPdfForProcess(process), | 197 files_->GetPdfForProcess(process), |
| 198 settings_, | 198 settings_, |
| 199 bitmap_settings_, | 199 bitmap_settings_, |
| 200 files_->GetPwgForProcess(process))); | 200 files_->GetPwgForProcess(process))); |
| 201 utility_process_host_.reset(); | 201 utility_process_host_.reset(); |
| 202 } | 202 } |
| 203 | 203 |
| 204 void PwgUtilityProcessHostClient::OnSucceeded() { | 204 void PwgUtilityProcessHostClient::OnSucceeded() { |
| 205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 205 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 206 RunCallback(true); | 206 RunCallback(true); |
| 207 } | 207 } |
| 208 | 208 |
| 209 void PwgUtilityProcessHostClient::OnFailed() { | 209 void PwgUtilityProcessHostClient::OnFailed() { |
| 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 211 RunCallback(false); | 211 RunCallback(false); |
| 212 } | 212 } |
| 213 | 213 |
| 214 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { | 214 void PwgUtilityProcessHostClient::OnFilesReadyOnUIThread() { |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 215 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 216 if (!files_->IsValid()) { | 216 if (!files_->IsValid()) { |
| 217 RunCallbackOnUIThread(false); | 217 RunCallbackOnUIThread(false); |
| 218 return; | 218 return; |
| 219 } | 219 } |
| 220 BrowserThread::PostTask( | 220 BrowserThread::PostTask( |
| 221 BrowserThread::IO, FROM_HERE, | 221 BrowserThread::IO, FROM_HERE, |
| 222 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); | 222 base::Bind(&PwgUtilityProcessHostClient::StartProcessOnIOThread, this)); |
| 223 } | 223 } |
| 224 | 224 |
| 225 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { | 225 void PwgUtilityProcessHostClient::StartProcessOnIOThread() { |
| 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 226 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 227 utility_process_host_ = | 227 utility_process_host_ = |
| 228 content::UtilityProcessHost::Create( | 228 content::UtilityProcessHost::Create( |
| 229 this, | 229 this, |
| 230 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr(); | 230 base::MessageLoop::current()->message_loop_proxy())->AsWeakPtr(); |
| 231 utility_process_host_->SetName(l10n_util::GetStringUTF16( | 231 utility_process_host_->SetName(l10n_util::GetStringUTF16( |
| 232 IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME)); | 232 IDS_UTILITY_PROCESS_PWG_RASTER_CONVERTOR_NAME)); |
| 233 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); | 233 utility_process_host_->Send(new ChromeUtilityMsg_StartupPing); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void PwgUtilityProcessHostClient::RunCallback(bool success) { | 236 void PwgUtilityProcessHostClient::RunCallback(bool success) { |
| 237 BrowserThread::PostTask( | 237 BrowserThread::PostTask( |
| 238 BrowserThread::UI, FROM_HERE, | 238 BrowserThread::UI, FROM_HERE, |
| 239 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, | 239 base::Bind(&PwgUtilityProcessHostClient::RunCallbackOnUIThread, this, |
| 240 success)); | 240 success)); |
| 241 } | 241 } |
| 242 | 242 |
| 243 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { | 243 void PwgUtilityProcessHostClient::RunCallbackOnUIThread(bool success) { |
| 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 244 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 245 if (!callback_.is_null()) { | 245 if (!callback_.is_null()) { |
| 246 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 246 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 247 base::Bind(callback_, success, | 247 base::Bind(callback_, success, |
| 248 files_->GetPwgPath())); | 248 files_->GetPwgPath())); |
| 249 callback_.Reset(); | 249 callback_.Reset(); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 class PWGRasterConverterImpl : public PWGRasterConverter { | 253 class PWGRasterConverterImpl : public PWGRasterConverter { |
| 254 public: | 254 public: |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 354 } |
| 355 } | 355 } |
| 356 | 356 |
| 357 result.rotate_all_pages = raster_capability.value().rotate_all_pages; | 357 result.rotate_all_pages = raster_capability.value().rotate_all_pages; |
| 358 | 358 |
| 359 result.reverse_page_order = raster_capability.value().reverse_order_streaming; | 359 result.reverse_page_order = raster_capability.value().reverse_order_streaming; |
| 360 return result; | 360 return result; |
| 361 } | 361 } |
| 362 | 362 |
| 363 } // namespace local_discovery | 363 } // namespace local_discovery |
| OLD | NEW |