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/ui/app_list/search/common/url_icon_source.h" | 5 #include "chrome/browser/ui/app_list/search/common/url_icon_source.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
11 #include "net/url_request/url_fetcher.h" | 11 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_request_status.h" | 12 #include "net/url_request/url_request_status.h" |
13 #include "ui/base/resource/resource_bundle.h" | 13 #include "ui/base/resource/resource_bundle.h" |
14 #include "ui/gfx/image/image_skia_operations.h" | 14 #include "ui/gfx/image/image_skia_operations.h" |
15 | 15 |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 namespace app_list { | 18 namespace app_list { |
19 | 19 |
20 UrlIconSource::UrlIconSource( | 20 UrlIconSource::UrlIconSource(const IconLoadedCallback& icon_loaded_callback, |
21 const IconLoadedCallback& icon_loaded_callback, | 21 net::URLRequestContextGetter* context_getter, |
22 net::URLRequestContextGetter* context_getter, | 22 const GURL& icon_url, |
23 const GURL& icon_url, | 23 int icon_size, |
24 int icon_size, | 24 int default_icon_resource_id) |
25 int default_icon_resource_id) | 25 : ImageRequest( |
26 : icon_loaded_callback_(icon_loaded_callback), | 26 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), |
| 27 icon_loaded_callback_(icon_loaded_callback), |
27 context_getter_(context_getter), | 28 context_getter_(context_getter), |
28 icon_url_(icon_url), | 29 icon_url_(icon_url), |
29 icon_size_(icon_size), | 30 icon_size_(icon_size), |
30 default_icon_resource_id_(default_icon_resource_id), | 31 default_icon_resource_id_(default_icon_resource_id), |
31 icon_fetch_attempted_(false) { | 32 icon_fetch_attempted_(false) { |
32 DCHECK(!icon_loaded_callback_.is_null()); | 33 DCHECK(!icon_loaded_callback_.is_null()); |
33 } | 34 } |
34 | 35 |
35 UrlIconSource::~UrlIconSource() { | 36 UrlIconSource::~UrlIconSource() { |
36 if (image_decoder_.get()) | |
37 image_decoder_->set_delegate(NULL); | |
38 } | 37 } |
39 | 38 |
40 void UrlIconSource::StartIconFetch() { | 39 void UrlIconSource::StartIconFetch() { |
41 icon_fetch_attempted_ = true; | 40 icon_fetch_attempted_ = true; |
42 | 41 |
43 icon_fetcher_.reset( | 42 icon_fetcher_.reset( |
44 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this)); | 43 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this)); |
45 icon_fetcher_->SetRequestContext(context_getter_); | 44 icon_fetcher_->SetRequestContext(context_getter_); |
46 icon_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 45 icon_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
47 icon_fetcher_->Start(); | 46 icon_fetcher_->Start(); |
(...skipping 17 matching lines...) Expand all Loading... |
65 scoped_ptr<net::URLFetcher> fetcher(icon_fetcher_.Pass()); | 64 scoped_ptr<net::URLFetcher> fetcher(icon_fetcher_.Pass()); |
66 | 65 |
67 if (!fetcher->GetStatus().is_success() || | 66 if (!fetcher->GetStatus().is_success() || |
68 fetcher->GetResponseCode() != 200) { | 67 fetcher->GetResponseCode() != 200) { |
69 return; | 68 return; |
70 } | 69 } |
71 | 70 |
72 std::string unsafe_icon_data; | 71 std::string unsafe_icon_data; |
73 fetcher->GetResponseAsString(&unsafe_icon_data); | 72 fetcher->GetResponseAsString(&unsafe_icon_data); |
74 | 73 |
75 image_decoder_ = | 74 ImageDecoder::Start(this, unsafe_icon_data); |
76 new ImageDecoder(this, unsafe_icon_data, ImageDecoder::DEFAULT_CODEC); | |
77 image_decoder_->Start( | |
78 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)); | |
79 } | 75 } |
80 | 76 |
81 void UrlIconSource::OnImageDecoded(const ImageDecoder* decoder, | 77 void UrlIconSource::OnImageDecoded(const SkBitmap& decoded_image) { |
82 const SkBitmap& decoded_image) { | |
83 icon_ = gfx::ImageSkiaOperations::CreateResizedImage( | 78 icon_ = gfx::ImageSkiaOperations::CreateResizedImage( |
84 gfx::ImageSkia::CreateFrom1xBitmap(decoded_image), | 79 gfx::ImageSkia::CreateFrom1xBitmap(decoded_image), |
85 skia::ImageOperations::RESIZE_BEST, | 80 skia::ImageOperations::RESIZE_BEST, |
86 gfx::Size(icon_size_, icon_size_)); | 81 gfx::Size(icon_size_, icon_size_)); |
87 | 82 |
88 icon_loaded_callback_.Run(); | 83 icon_loaded_callback_.Run(); |
89 } | 84 } |
90 | 85 |
91 void UrlIconSource::OnDecodeImageFailed( | 86 void UrlIconSource::OnDecodeImageFailed() { |
92 const ImageDecoder* decoder) { | |
93 // Failed to decode image. Do nothing and just use the default icon. | 87 // Failed to decode image. Do nothing and just use the default icon. |
94 } | 88 } |
95 | 89 |
96 } // namespace app_list | 90 } // namespace app_list |
OLD | NEW |