OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/image_decoder.h" | 5 #include "chrome/browser/image_decoder.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
9 #include "chrome/common/chrome_utility_messages.h" | 9 #include "content/common/image_decoder.mojom.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/utility_process_host.h" | 11 #include "content/public/browser/utility_process_host.h" |
| 12 #include "content/public/browser/utility_process_host_client.h" |
| 13 #include "content/public/common/service_registry.h" |
12 | 14 |
13 using content::BrowserThread; | 15 using content::BrowserThread; |
14 using content::UtilityProcessHost; | 16 using content::UtilityProcessHost; |
15 | 17 |
16 ImageDecoder::ImageDecoder(Delegate* delegate, | 18 ImageDecoder::ImageDecoder(Delegate* delegate, |
17 const std::string& image_data, | 19 const std::string& image_data, |
18 ImageCodec image_codec) | 20 ImageCodec image_codec) |
19 : delegate_(delegate), | 21 : delegate_(delegate), |
20 image_data_(image_data.begin(), image_data.end()), | 22 image_data_(image_data.begin(), image_data.end()), |
21 image_codec_(image_codec), | 23 image_codec_(image_codec), |
22 task_runner_(NULL), | 24 task_runner_(NULL), |
23 shrink_to_fit_(false) { | 25 shrink_to_fit_(false) { |
24 } | 26 } |
25 | 27 |
26 ImageDecoder::ImageDecoder(Delegate* delegate, | 28 ImageDecoder::ImageDecoder(Delegate* delegate, |
27 const std::vector<char>& image_data, | 29 const std::vector<char>& image_data, |
28 ImageCodec image_codec) | 30 ImageCodec image_codec) |
29 : delegate_(delegate), | 31 : delegate_(delegate), |
30 image_data_(image_data.begin(), image_data.end()), | 32 image_data_(image_data.begin(), image_data.end()), |
31 image_codec_(image_codec), | 33 image_codec_(image_codec), |
32 task_runner_(NULL), | 34 task_runner_(NULL), |
33 shrink_to_fit_(false) { | 35 shrink_to_fit_(false) { |
34 } | 36 } |
35 | 37 |
36 ImageDecoder::~ImageDecoder() {} | 38 ImageDecoder::~ImageDecoder() { |
| 39 LOG(INFO) << "Destorying browser-side ImageDecoder"; |
| 40 } |
37 | 41 |
38 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { | 42 void ImageDecoder::Start(scoped_refptr<base::SequencedTaskRunner> task_runner) { |
39 task_runner_ = task_runner; | 43 task_runner_ = task_runner; |
40 BrowserThread::PostTask( | 44 BrowserThread::PostTask( |
41 BrowserThread::IO, FROM_HERE, | 45 BrowserThread::IO, FROM_HERE, |
42 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); | 46 base::Bind(&ImageDecoder::DecodeImageInSandbox, this, image_data_)); |
43 } | 47 } |
44 | 48 |
45 bool ImageDecoder::OnMessageReceived(const IPC::Message& message) { | 49 void ImageDecoder::OnDecodeImageDone(bool success, |
46 bool handled = true; | 50 content::ImageDataPtr image) { |
47 IPC_BEGIN_MESSAGE_MAP(ImageDecoder, message) | 51 LOG(INFO) << "ImageDecoder::OnDecodeImageDone: " << success; |
48 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Succeeded, | 52 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
49 OnDecodeImageSucceeded) | 53 if (delegate_) { |
50 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_DecodeImage_Failed, | 54 if (success && image) { |
51 OnDecodeImageFailed) | 55 SkBitmap bitmap = image.To<SkBitmap>(); |
52 IPC_MESSAGE_UNHANDLED(handled = false) | 56 task_runner_->PostTask(FROM_HERE, |
53 IPC_END_MESSAGE_MAP() | 57 base::Bind(&Delegate::OnImageDecoded, |
54 return handled; | 58 base::Unretained(delegate_), |
55 } | 59 base::Unretained(this), |
56 | 60 bitmap)); |
57 void ImageDecoder::OnDecodeImageSucceeded(const SkBitmap& decoded_image) { | 61 } else { |
58 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 62 task_runner_->PostTask(FROM_HERE, |
59 if (delegate_) | 63 base::Bind(&Delegate::OnDecodeImageFailed, |
60 delegate_->OnImageDecoded(this, decoded_image); | 64 base::Unretained(delegate_), |
61 } | 65 base::Unretained(this))); |
62 | 66 } |
63 void ImageDecoder::OnDecodeImageFailed() { | 67 } |
64 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 68 decoder_.reset(); |
65 if (delegate_) | |
66 delegate_->OnDecodeImageFailed(this); | |
67 } | 69 } |
68 | 70 |
69 void ImageDecoder::DecodeImageInSandbox( | 71 void ImageDecoder::DecodeImageInSandbox( |
70 const std::vector<unsigned char>& image_data) { | 72 const std::vector<unsigned char>& image_data) { |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 73 LOG(INFO) << "ImageDecoder::DecodeImageInSandbox"; |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
72 UtilityProcessHost* utility_process_host; | 75 UtilityProcessHost* utility_process_host; |
73 utility_process_host = UtilityProcessHost::Create(this, task_runner_.get()); | 76 utility_process_host = UtilityProcessHost::Create( |
74 if (image_codec_ == ROBUST_JPEG_CODEC) { | 77 scoped_refptr<content::UtilityProcessHostClient>(), task_runner_.get()); |
75 utility_process_host->Send( | 78 utility_process_host->StartMojoMode(); |
76 new ChromeUtilityMsg_RobustJPEGDecodeImage(image_data)); | 79 content::ServiceRegistry* service_registry = |
77 } else { | 80 utility_process_host->GetServiceRegistry(); |
78 utility_process_host->Send( | 81 service_registry->ConnectToRemoteService(&decoder_); |
79 new ChromeUtilityMsg_DecodeImage(image_data, shrink_to_fit_)); | 82 |
80 } | 83 decoder_->DecodeImage(mojo::Array<uint8_t>::From(image_data), |
| 84 image_codec_ == ROBUST_JPEG_CODEC, |
| 85 shrink_to_fit_, |
| 86 base::Bind(&ImageDecoder::OnDecodeImageDone, this)); |
81 } | 87 } |
OLD | NEW |