| 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 "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/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 // 3. Start utility process and start conversion on the IO thread. | 101 // 3. Start utility process and start conversion on the IO thread. |
| 102 // 4. Run result callback on the UI thread. | 102 // 4. Run result callback on the UI thread. |
| 103 // 5. Instance is destroyed from any thread that has the last reference. | 103 // 5. Instance is destroyed from any thread that has the last reference. |
| 104 // 6. FileHandlers destroyed on the FILE thread. | 104 // 6. FileHandlers destroyed on the FILE thread. |
| 105 // This step posts |FileHandlers| to be destroyed on the FILE thread. | 105 // This step posts |FileHandlers| to be destroyed on the FILE thread. |
| 106 // All these steps work sequentially, so no data should be accessed | 106 // All these steps work sequentially, so no data should be accessed |
| 107 // simultaneously by several threads. | 107 // simultaneously by several threads. |
| 108 class PwgUtilityProcessHostClient : public content::UtilityProcessHostClient { | 108 class PwgUtilityProcessHostClient : public content::UtilityProcessHostClient { |
| 109 public: | 109 public: |
| 110 explicit PwgUtilityProcessHostClient( | 110 explicit PwgUtilityProcessHostClient( |
| 111 const printing::PdfRenderSettings& settings); | 111 const printing::PdfRenderSettings& settings, |
| 112 const printing::BitmapTransformSettings& bitmap_settings); |
| 112 | 113 |
| 113 void Convert(base::RefCountedMemory* data, | 114 void Convert(base::RefCountedMemory* data, |
| 114 const PWGRasterConverter::ResultCallback& callback); | 115 const PWGRasterConverter::ResultCallback& callback); |
| 115 | 116 |
| 116 // UtilityProcessHostClient implementation. | 117 // UtilityProcessHostClient implementation. |
| 117 virtual void OnProcessCrashed(int exit_code) OVERRIDE; | 118 virtual void OnProcessCrashed(int exit_code) OVERRIDE; |
| 118 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 119 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 119 | 120 |
| 120 private: | 121 private: |
| 121 virtual ~PwgUtilityProcessHostClient(); | 122 virtual ~PwgUtilityProcessHostClient(); |
| 122 | 123 |
| 123 // Message handlers. | 124 // Message handlers. |
| 124 void OnProcessStarted(); | 125 void OnProcessStarted(); |
| 125 void OnSucceeded(); | 126 void OnSucceeded(); |
| 126 void OnFailed(); | 127 void OnFailed(); |
| 127 | 128 |
| 128 void RunCallback(bool success); | 129 void RunCallback(bool success); |
| 129 | 130 |
| 130 void StartProcessOnIOThread(); | 131 void StartProcessOnIOThread(); |
| 131 | 132 |
| 132 void RunCallbackOnUIThread(bool success); | 133 void RunCallbackOnUIThread(bool success); |
| 133 void OnFilesReadyOnUIThread(); | 134 void OnFilesReadyOnUIThread(); |
| 134 | 135 |
| 135 scoped_ptr<FileHandlers> files_; | 136 scoped_ptr<FileHandlers> files_; |
| 136 printing::PdfRenderSettings settings_; | 137 printing::PdfRenderSettings settings_; |
| 138 printing::BitmapTransformSettings bitmap_settings_; |
| 137 PWGRasterConverter::ResultCallback callback_; | 139 PWGRasterConverter::ResultCallback callback_; |
| 138 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; | 140 base::WeakPtr<content::UtilityProcessHost> utility_process_host_; |
| 139 | 141 |
| 140 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); | 142 DISALLOW_COPY_AND_ASSIGN(PwgUtilityProcessHostClient); |
| 141 }; | 143 }; |
| 142 | 144 |
| 143 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( | 145 PwgUtilityProcessHostClient::PwgUtilityProcessHostClient( |
| 144 const printing::PdfRenderSettings& settings) : settings_(settings) { | 146 const printing::PdfRenderSettings& settings, |
| 145 } | 147 const printing::BitmapTransformSettings& bitmap_settings) |
| 148 : settings_(settings), bitmap_settings_(bitmap_settings) {} |
| 146 | 149 |
| 147 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { | 150 PwgUtilityProcessHostClient::~PwgUtilityProcessHostClient() { |
| 148 // Delete temp directory. | 151 // Delete temp directory. |
| 149 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release()); | 152 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, files_.release()); |
| 150 } | 153 } |
| 151 | 154 |
| 152 void PwgUtilityProcessHostClient::Convert( | 155 void PwgUtilityProcessHostClient::Convert( |
| 153 base::RefCountedMemory* data, | 156 base::RefCountedMemory* data, |
| 154 const PWGRasterConverter::ResultCallback& callback) { | 157 const PWGRasterConverter::ResultCallback& callback) { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 182 } | 185 } |
| 183 | 186 |
| 184 void PwgUtilityProcessHostClient::OnProcessStarted() { | 187 void PwgUtilityProcessHostClient::OnProcessStarted() { |
| 185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 186 if (!utility_process_host_) { | 189 if (!utility_process_host_) { |
| 187 RunCallbackOnUIThread(false); | 190 RunCallbackOnUIThread(false); |
| 188 return; | 191 return; |
| 189 } | 192 } |
| 190 | 193 |
| 191 base::ProcessHandle process = utility_process_host_->GetData().handle; | 194 base::ProcessHandle process = utility_process_host_->GetData().handle; |
| 192 utility_process_host_->Send( | 195 utility_process_host_->Send(new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( |
| 193 new ChromeUtilityMsg_RenderPDFPagesToPWGRaster( | 196 files_->GetPdfForProcess(process), |
| 194 files_->GetPdfForProcess(process), | 197 settings_, |
| 195 settings_, | 198 bitmap_settings_, |
| 196 files_->GetPwgForProcess(process))); | 199 files_->GetPwgForProcess(process))); |
| 197 utility_process_host_.reset(); | 200 utility_process_host_.reset(); |
| 198 } | 201 } |
| 199 | 202 |
| 200 void PwgUtilityProcessHostClient::OnSucceeded() { | 203 void PwgUtilityProcessHostClient::OnSucceeded() { |
| 201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 202 RunCallback(true); | 205 RunCallback(true); |
| 203 } | 206 } |
| 204 | 207 |
| 205 void PwgUtilityProcessHostClient::OnFailed() { | 208 void PwgUtilityProcessHostClient::OnFailed() { |
| 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 248 } |
| 246 | 249 |
| 247 class PWGRasterConverterImpl : public PWGRasterConverter { | 250 class PWGRasterConverterImpl : public PWGRasterConverter { |
| 248 public: | 251 public: |
| 249 PWGRasterConverterImpl(); | 252 PWGRasterConverterImpl(); |
| 250 | 253 |
| 251 virtual ~PWGRasterConverterImpl(); | 254 virtual ~PWGRasterConverterImpl(); |
| 252 | 255 |
| 253 virtual void Start(base::RefCountedMemory* data, | 256 virtual void Start(base::RefCountedMemory* data, |
| 254 const printing::PdfRenderSettings& conversion_settings, | 257 const printing::PdfRenderSettings& conversion_settings, |
| 258 const printing::BitmapTransformSettings& bitmap_settings, |
| 255 const ResultCallback& callback) OVERRIDE; | 259 const ResultCallback& callback) OVERRIDE; |
| 260 |
| 256 private: | 261 private: |
| 257 scoped_refptr<PwgUtilityProcessHostClient> utility_client_; | 262 scoped_refptr<PwgUtilityProcessHostClient> utility_client_; |
| 258 base::CancelableCallback<ResultCallback::RunType> callback_; | 263 base::CancelableCallback<ResultCallback::RunType> callback_; |
| 259 | 264 |
| 260 DISALLOW_COPY_AND_ASSIGN(PWGRasterConverterImpl); | 265 DISALLOW_COPY_AND_ASSIGN(PWGRasterConverterImpl); |
| 261 }; | 266 }; |
| 262 | 267 |
| 263 PWGRasterConverterImpl::PWGRasterConverterImpl() { | 268 PWGRasterConverterImpl::PWGRasterConverterImpl() { |
| 264 } | 269 } |
| 265 | 270 |
| 266 PWGRasterConverterImpl::~PWGRasterConverterImpl() { | 271 PWGRasterConverterImpl::~PWGRasterConverterImpl() { |
| 267 } | 272 } |
| 268 | 273 |
| 269 void PWGRasterConverterImpl::Start( | 274 void PWGRasterConverterImpl::Start( |
| 270 base::RefCountedMemory* data, | 275 base::RefCountedMemory* data, |
| 271 const printing::PdfRenderSettings& conversion_settings, | 276 const printing::PdfRenderSettings& conversion_settings, |
| 277 const printing::BitmapTransformSettings& bitmap_settings, |
| 272 const ResultCallback& callback) { | 278 const ResultCallback& callback) { |
| 273 // Rebind cancelable callback to avoid calling callback if | 279 // Rebind cancelable callback to avoid calling callback if |
| 274 // PWGRasterConverterImpl is destroyed. | 280 // PWGRasterConverterImpl is destroyed. |
| 275 callback_.Reset(callback); | 281 callback_.Reset(callback); |
| 276 utility_client_ = new PwgUtilityProcessHostClient(conversion_settings); | 282 utility_client_ = |
| 283 new PwgUtilityProcessHostClient(conversion_settings, bitmap_settings); |
| 277 utility_client_->Convert(data, callback_.callback()); | 284 utility_client_->Convert(data, callback_.callback()); |
| 278 } | 285 } |
| 279 | 286 |
| 280 } // namespace | 287 } // namespace |
| 281 | 288 |
| 282 // static | 289 // static |
| 283 scoped_ptr<PWGRasterConverter> PWGRasterConverter::CreateDefault() { | 290 scoped_ptr<PWGRasterConverter> PWGRasterConverter::CreateDefault() { |
| 284 return scoped_ptr<PWGRasterConverter>(new PWGRasterConverterImpl()); | 291 return scoped_ptr<PWGRasterConverter>(new PWGRasterConverterImpl()); |
| 285 } | 292 } |
| 286 | 293 |
| 287 } // namespace local_discovery | 294 } // namespace local_discovery |
| OLD | NEW |