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 "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
11 #include "chrome/common/image_decoder.mojom.h" | 11 #include "chrome/common/image_decoder.mojom.h" |
12 #include "chrome/grit/generated_resources.h" | 12 #include "chrome/grit/generated_resources.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/utility_process_host.h" | 14 #include "content/public/browser/utility_process_host.h" |
15 #include "content/public/common/service_registry.h" | 15 #include "content/public/common/service_registry.h" |
16 #include "skia/public/type_converters.h" | |
17 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
18 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
19 | 18 |
20 using content::BrowserThread; | 19 using content::BrowserThread; |
21 using content::UtilityProcessHost; | 20 using content::UtilityProcessHost; |
22 | 21 |
23 namespace { | 22 namespace { |
24 | 23 |
25 // static, Leaky to allow access from any thread. | 24 // static, Leaky to allow access from any thread. |
26 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; | 25 base::LazyInstance<ImageDecoder>::Leaky g_decoder = LAZY_INSTANCE_INITIALIZER; |
27 | 26 |
28 // How long to wait after the last request has been received before ending | 27 // How long to wait after the last request has been received before ending |
29 // batch mode. | 28 // batch mode. |
30 const int kBatchModeTimeoutSeconds = 5; | 29 const int kBatchModeTimeoutSeconds = 5; |
31 | 30 |
32 void OnDecodeImageDone( | 31 void OnDecodeImageDone( |
33 base::Callback<void(int)> fail_callback, | 32 base::Callback<void(int)> fail_callback, |
34 base::Callback<void(const SkBitmap&, int)> success_callback, | 33 base::Callback<void(const SkBitmap&, int)> success_callback, |
35 int request_id, skia::mojom::BitmapPtr image) { | 34 int request_id, |
| 35 const SkBitmap& image) { |
36 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 36 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
37 if (image) { | 37 if (!image.isNull() && !image.empty()) |
38 SkBitmap bitmap = image.To<SkBitmap>(); | 38 success_callback.Run(image, request_id); |
39 if (!bitmap.empty()) { | 39 else |
40 success_callback.Run(bitmap, request_id); | 40 fail_callback.Run(request_id); |
41 return; | |
42 } | |
43 } | |
44 fail_callback.Run(request_id); | |
45 } | 41 } |
46 | 42 |
47 } // namespace | 43 } // namespace |
48 | 44 |
49 ImageDecoder::ImageDecoder() | 45 ImageDecoder::ImageDecoder() |
50 : image_request_id_counter_(0) { | 46 : image_request_id_counter_(0) { |
51 // A single ImageDecoder instance should live for the life of the program. | 47 // A single ImageDecoder instance should live for the life of the program. |
52 // Explicitly add a reference so the object isn't deleted. | 48 // Explicitly add a reference so the object isn't deleted. |
53 AddRef(); | 49 AddRef(); |
54 } | 50 } |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 auto it = image_request_id_map_.find(request_id); | 296 auto it = image_request_id_map_.find(request_id); |
301 if (it == image_request_id_map_.end()) | 297 if (it == image_request_id_map_.end()) |
302 return; | 298 return; |
303 image_request = it->second; | 299 image_request = it->second; |
304 image_request_id_map_.erase(it); | 300 image_request_id_map_.erase(it); |
305 } | 301 } |
306 | 302 |
307 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); | 303 DCHECK(image_request->task_runner()->RunsTasksOnCurrentThread()); |
308 image_request->OnDecodeImageFailed(); | 304 image_request->OnDecodeImageFailed(); |
309 } | 305 } |
OLD | NEW |