| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/favicon/core/large_icon_service.h" | 5 #include "components/favicon/core/large_icon_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/task_runner.h" | 12 #include "base/task_runner.h" |
| 13 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
| 14 #include "components/favicon/core/favicon_service.h" | 14 #include "components/favicon/core/favicon_service.h" |
| 15 #include "components/favicon_base/fallback_icon_style.h" | 15 #include "components/favicon_base/fallback_icon_style.h" |
| 16 #include "components/favicon_base/favicon_types.h" | 16 #include "components/favicon_base/favicon_types.h" |
| 17 #include "skia/ext/image_operations.h" | 17 #include "skia/ext/image_operations.h" |
| 18 #include "ui/gfx/codec/png_codec.h" | 18 #include "ui/gfx/codec/png_codec.h" |
| 19 #include "ui/gfx/geometry/size.h" | 19 #include "ui/gfx/geometry/size.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 // Processes the bitmap data returned from the FaviconService as part of a | 23 // Processes the bitmap data returned from the FaviconService as part of a |
| 24 // LargeIconService request. | 24 // LargeIconService request. |
| 25 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { | 25 class LargeIconWorker : public base::RefCountedThreadSafe<LargeIconWorker> { |
| 26 public: | 26 public: |
| 27 LargeIconWorker(int min_source_size_in_pixel, | 27 LargeIconWorker(int min_source_size_in_pixel, |
| 28 int desired_size_in_pixel, | 28 int desired_size_in_pixel, |
| 29 const favicon_base::FallbackIconStyle& fallback_icon_style, |
| 29 favicon_base::LargeIconCallback callback, | 30 favicon_base::LargeIconCallback callback, |
| 30 scoped_refptr<base::TaskRunner> background_task_runner, | 31 scoped_refptr<base::TaskRunner> background_task_runner, |
| 31 base::CancelableTaskTracker* tracker); | 32 base::CancelableTaskTracker* tracker); |
| 32 | 33 |
| 33 // Must run on the owner (UI) thread in production. | 34 // Must run on the owner (UI) thread in production. |
| 34 // Intermediate callback for GetLargeIconOrFallbackStyle(). Invokes | 35 // Intermediate callback for GetLargeIconOrFallbackStyle(). Invokes |
| 35 // ProcessIconOnBackgroundThread() so we do not perform complex image | 36 // ProcessIconOnBackgroundThread() so we do not perform complex image |
| 36 // operations on the UI thread. | 37 // operations on the UI thread. |
| 37 void OnIconLookupComplete( | 38 void OnIconLookupComplete( |
| 38 const favicon_base::FaviconRawBitmapResult& bitmap_result); | 39 const favicon_base::FaviconRawBitmapResult& bitmap_result); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 56 // into |resized_bitmap_result| and returns true. | 57 // into |resized_bitmap_result| and returns true. |
| 57 bool ResizeLargeIconOnBackgroundThreadIfValid( | 58 bool ResizeLargeIconOnBackgroundThreadIfValid( |
| 58 favicon_base::FaviconRawBitmapResult* resized_bitmap_result); | 59 favicon_base::FaviconRawBitmapResult* resized_bitmap_result); |
| 59 | 60 |
| 60 // Must run on the owner (UI) thread in production. | 61 // Must run on the owner (UI) thread in production. |
| 61 // Invoked when ProcessIconOnBackgroundThread() is done. | 62 // Invoked when ProcessIconOnBackgroundThread() is done. |
| 62 void OnIconProcessingComplete(); | 63 void OnIconProcessingComplete(); |
| 63 | 64 |
| 64 int min_source_size_in_pixel_; | 65 int min_source_size_in_pixel_; |
| 65 int desired_size_in_pixel_; | 66 int desired_size_in_pixel_; |
| 67 favicon_base::FallbackIconStyle fallback_icon_style_; |
| 66 favicon_base::LargeIconCallback callback_; | 68 favicon_base::LargeIconCallback callback_; |
| 67 scoped_refptr<base::TaskRunner> background_task_runner_; | 69 scoped_refptr<base::TaskRunner> background_task_runner_; |
| 68 base::CancelableTaskTracker* tracker_; | 70 base::CancelableTaskTracker* tracker_; |
| 69 favicon_base::FaviconRawBitmapResult bitmap_result_; | 71 favicon_base::FaviconRawBitmapResult bitmap_result_; |
| 70 scoped_ptr<favicon_base::LargeIconResult> result_; | 72 scoped_ptr<favicon_base::LargeIconResult> result_; |
| 71 | 73 |
| 72 DISALLOW_COPY_AND_ASSIGN(LargeIconWorker); | 74 DISALLOW_COPY_AND_ASSIGN(LargeIconWorker); |
| 73 }; | 75 }; |
| 74 | 76 |
| 75 LargeIconWorker::LargeIconWorker( | 77 LargeIconWorker::LargeIconWorker( |
| 76 int min_source_size_in_pixel, | 78 int min_source_size_in_pixel, |
| 77 int desired_size_in_pixel, | 79 int desired_size_in_pixel, |
| 80 const favicon_base::FallbackIconStyle& fallback_icon_style, |
| 78 favicon_base::LargeIconCallback callback, | 81 favicon_base::LargeIconCallback callback, |
| 79 scoped_refptr<base::TaskRunner> background_task_runner, | 82 scoped_refptr<base::TaskRunner> background_task_runner, |
| 80 base::CancelableTaskTracker* tracker) | 83 base::CancelableTaskTracker* tracker) |
| 81 : min_source_size_in_pixel_(min_source_size_in_pixel), | 84 : min_source_size_in_pixel_(min_source_size_in_pixel), |
| 82 desired_size_in_pixel_(desired_size_in_pixel), | 85 desired_size_in_pixel_(desired_size_in_pixel), |
| 86 fallback_icon_style_(fallback_icon_style), |
| 83 callback_(callback), | 87 callback_(callback), |
| 84 background_task_runner_(background_task_runner), | 88 background_task_runner_(background_task_runner), |
| 85 tracker_(tracker) { | 89 tracker_(tracker) {} |
| 86 } | |
| 87 | 90 |
| 88 LargeIconWorker::~LargeIconWorker() { | 91 LargeIconWorker::~LargeIconWorker() { |
| 89 } | 92 } |
| 90 | 93 |
| 91 void LargeIconWorker::OnIconLookupComplete( | 94 void LargeIconWorker::OnIconLookupComplete( |
| 92 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 95 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
| 93 bitmap_result_ = bitmap_result; | 96 bitmap_result_ = bitmap_result; |
| 94 tracker_->PostTaskAndReply( | 97 tracker_->PostTaskAndReply( |
| 95 background_task_runner_.get(), FROM_HERE, | 98 background_task_runner_.get(), FROM_HERE, |
| 96 base::Bind(&LargeIconWorker::ProcessIconOnBackgroundThread, this), | 99 base::Bind(&LargeIconWorker::ProcessIconOnBackgroundThread, this), |
| 97 base::Bind(&LargeIconWorker::OnIconProcessingComplete, this)); | 100 base::Bind(&LargeIconWorker::OnIconProcessingComplete, this)); |
| 98 } | 101 } |
| 99 | 102 |
| 100 void LargeIconWorker::ProcessIconOnBackgroundThread() { | 103 void LargeIconWorker::ProcessIconOnBackgroundThread() { |
| 101 favicon_base::FaviconRawBitmapResult resized_bitmap_result; | 104 favicon_base::FaviconRawBitmapResult resized_bitmap_result; |
| 102 if (ResizeLargeIconOnBackgroundThreadIfValid(&resized_bitmap_result)) { | 105 if (ResizeLargeIconOnBackgroundThreadIfValid(&resized_bitmap_result)) { |
| 103 result_.reset( | 106 result_.reset( |
| 104 new favicon_base::LargeIconResult(resized_bitmap_result)); | 107 new favicon_base::LargeIconResult(resized_bitmap_result)); |
| 105 } else { | 108 } else { |
| 106 // Failed to resize |bitmap_result_|, so compute fallback icon style. | 109 // Failed to resize |bitmap_result_|, so send fallback icon style. |
| 107 scoped_ptr<favicon_base::FallbackIconStyle> fallback_icon_style( | 110 favicon_base::FallbackIconStyle* style = |
| 108 new favicon_base::FallbackIconStyle()); | 111 new favicon_base::FallbackIconStyle(); |
| 112 *style = fallback_icon_style_; |
| 109 if (bitmap_result_.is_valid()) { | 113 if (bitmap_result_.is_valid()) { |
| 110 favicon_base::SetDominantColorAsBackground( | 114 favicon_base::SetDominantColorAsBackground(bitmap_result_.bitmap_data, |
| 111 bitmap_result_.bitmap_data, fallback_icon_style.get()); | 115 style); |
| 112 } | 116 } |
| 113 result_.reset( | 117 result_.reset(new favicon_base::LargeIconResult(style)); |
| 114 new favicon_base::LargeIconResult(fallback_icon_style.release())); | |
| 115 } | 118 } |
| 116 } | 119 } |
| 117 | 120 |
| 118 bool LargeIconWorker::ResizeLargeIconOnBackgroundThreadIfValid( | 121 bool LargeIconWorker::ResizeLargeIconOnBackgroundThreadIfValid( |
| 119 favicon_base::FaviconRawBitmapResult* resized_bitmap_result) { | 122 favicon_base::FaviconRawBitmapResult* resized_bitmap_result) { |
| 120 // Require bitmap to be valid and square. | 123 // Require bitmap to be valid and square. |
| 121 if (!bitmap_result_.is_valid() || | 124 if (!bitmap_result_.is_valid() || |
| 122 bitmap_result_.pixel_size.width() != bitmap_result_.pixel_size.height()) | 125 bitmap_result_.pixel_size.width() != bitmap_result_.pixel_size.height()) |
| 123 return false; | 126 return false; |
| 124 | 127 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 background_task_runner_(background_task_runner) { | 172 background_task_runner_(background_task_runner) { |
| 170 large_icon_types_.push_back(favicon_base::IconType::FAVICON); | 173 large_icon_types_.push_back(favicon_base::IconType::FAVICON); |
| 171 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); | 174 large_icon_types_.push_back(favicon_base::IconType::TOUCH_ICON); |
| 172 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); | 175 large_icon_types_.push_back(favicon_base::IconType::TOUCH_PRECOMPOSED_ICON); |
| 173 } | 176 } |
| 174 | 177 |
| 175 LargeIconService::~LargeIconService() { | 178 LargeIconService::~LargeIconService() { |
| 176 } | 179 } |
| 177 | 180 |
| 178 base::CancelableTaskTracker::TaskId | 181 base::CancelableTaskTracker::TaskId |
| 179 LargeIconService::GetLargeIconOrFallbackStyle( | 182 LargeIconService::GetLargeIconOrFallbackStyle( |
| 180 const GURL& page_url, | 183 const GURL& page_url, |
| 181 int min_source_size_in_pixel, | 184 int min_source_size_in_pixel, |
| 182 int desired_size_in_pixel, | 185 int desired_size_in_pixel, |
| 183 const favicon_base::LargeIconCallback& callback, | 186 const favicon_base::FallbackIconStyle& fallback_icon_style, |
| 184 base::CancelableTaskTracker* tracker) { | 187 const favicon_base::LargeIconCallback& callback, |
| 188 base::CancelableTaskTracker* tracker) { |
| 185 DCHECK_LE(1, min_source_size_in_pixel); | 189 DCHECK_LE(1, min_source_size_in_pixel); |
| 186 DCHECK_LE(0, desired_size_in_pixel); | 190 DCHECK_LE(0, desired_size_in_pixel); |
| 187 | 191 |
| 188 scoped_refptr<LargeIconWorker> worker = | 192 scoped_refptr<LargeIconWorker> worker = new LargeIconWorker( |
| 189 new LargeIconWorker(min_source_size_in_pixel, desired_size_in_pixel, | 193 min_source_size_in_pixel, desired_size_in_pixel, fallback_icon_style, |
| 190 callback, background_task_runner_, tracker); | 194 callback, background_task_runner_, tracker); |
| 191 | 195 |
| 192 // TODO(beaudoin): For now this is just a wrapper around | 196 // TODO(beaudoin): For now this is just a wrapper around |
| 193 // GetLargestRawFaviconForPageURL. Add the logic required to select the best | 197 // GetLargestRawFaviconForPageURL. Add the logic required to select the best |
| 194 // possible large icon. Also add logic to fetch-on-demand when the URL of | 198 // possible large icon. Also add logic to fetch-on-demand when the URL of |
| 195 // a large icon is known but its bitmap is not available. | 199 // a large icon is known but its bitmap is not available. |
| 196 return favicon_service_->GetLargestRawFaviconForPageURL( | 200 return favicon_service_->GetLargestRawFaviconForPageURL( |
| 197 page_url, large_icon_types_, min_source_size_in_pixel, | 201 page_url, large_icon_types_, min_source_size_in_pixel, |
| 198 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), | 202 base::Bind(&LargeIconWorker::OnIconLookupComplete, worker), |
| 199 tracker); | 203 tracker); |
| 200 } | 204 } |
| 201 | 205 |
| 202 } // namespace favicon | 206 } // namespace favicon |
| OLD | NEW |