OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/search_engines/template_url_table_model.h" | 5 #include "chrome/browser/ui/search_engines/template_url_table_model.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 } | 45 } |
46 } | 46 } |
47 | 47 |
48 const TemplateURL& template_url() { | 48 const TemplateURL& template_url() { |
49 return template_url_; | 49 return template_url_; |
50 } | 50 } |
51 | 51 |
52 SkBitmap GetIcon() { | 52 SkBitmap GetIcon() { |
53 if (load_state_ == NOT_LOADED) | 53 if (load_state_ == NOT_LOADED) |
54 LoadFavIcon(); | 54 LoadFavIcon(); |
55 if (!fav_icon_.isNull()) | 55 if (!favicon_.isNull()) |
56 return fav_icon_; | 56 return favicon_; |
57 return *default_icon; | 57 return *default_icon; |
58 } | 58 } |
59 | 59 |
60 // Resets internal status so that the next time the icon is asked for its | 60 // Resets internal status so that the next time the icon is asked for its |
61 // fetched again. This should be invoked if the url is modified. | 61 // fetched again. This should be invoked if the url is modified. |
62 void ResetIcon() { | 62 void ResetIcon() { |
63 load_state_ = NOT_LOADED; | 63 load_state_ = NOT_LOADED; |
64 fav_icon_ = SkBitmap(); | 64 favicon_ = SkBitmap(); |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 // State of the favicon. | 68 // State of the favicon. |
69 enum LoadState { | 69 enum LoadState { |
70 NOT_LOADED, | 70 NOT_LOADED, |
71 LOADING, | 71 LOADING, |
72 LOADED | 72 LOADED |
73 }; | 73 }; |
74 | 74 |
75 void LoadFavIcon() { | 75 void LoadFavIcon() { |
76 load_state_ = LOADED; | 76 load_state_ = LOADED; |
77 FaviconService* favicon_service = | 77 FaviconService* favicon_service = |
78 model_->template_url_model()->profile()->GetFaviconService( | 78 model_->template_url_model()->profile()->GetFaviconService( |
79 Profile::EXPLICIT_ACCESS); | 79 Profile::EXPLICIT_ACCESS); |
80 if (!favicon_service) | 80 if (!favicon_service) |
81 return; | 81 return; |
82 GURL fav_icon_url = template_url().GetFavIconURL(); | 82 GURL favicon_url = template_url().GetFavIconURL(); |
83 if (!fav_icon_url.is_valid()) { | 83 if (!favicon_url.is_valid()) { |
84 // The favicon url isn't always set. Guess at one here. | 84 // The favicon url isn't always set. Guess at one here. |
85 if (template_url_.url() && template_url_.url()->IsValid()) { | 85 if (template_url_.url() && template_url_.url()->IsValid()) { |
86 GURL url = GURL(template_url_.url()->url()); | 86 GURL url = GURL(template_url_.url()->url()); |
87 if (url.is_valid()) | 87 if (url.is_valid()) |
88 fav_icon_url = TemplateURL::GenerateFaviconURL(url); | 88 favicon_url = TemplateURL::GenerateFaviconURL(url); |
89 } | 89 } |
90 if (!fav_icon_url.is_valid()) | 90 if (!favicon_url.is_valid()) |
91 return; | 91 return; |
92 } | 92 } |
93 load_state_ = LOADING; | 93 load_state_ = LOADING; |
94 favicon_service->GetFavicon(fav_icon_url, | 94 favicon_service->GetFavicon(favicon_url, |
95 &request_consumer_, | 95 &request_consumer_, |
96 NewCallback(this, &ModelEntry::OnFavIconDataAvailable)); | 96 NewCallback(this, &ModelEntry::OnFavIconDataAvailable)); |
97 } | 97 } |
98 | 98 |
99 void OnFavIconDataAvailable( | 99 void OnFavIconDataAvailable( |
100 FaviconService::Handle handle, | 100 FaviconService::Handle handle, |
101 bool know_favicon, | 101 bool know_favicon, |
102 scoped_refptr<RefCountedMemory> data, | 102 scoped_refptr<RefCountedMemory> data, |
103 bool expired, | 103 bool expired, |
104 GURL icon_url) { | 104 GURL icon_url) { |
105 load_state_ = LOADED; | 105 load_state_ = LOADED; |
106 if (know_favicon && data.get() && | 106 if (know_favicon && data.get() && |
107 gfx::PNGCodec::Decode(data->front(), data->size(), &fav_icon_)) { | 107 gfx::PNGCodec::Decode(data->front(), data->size(), &favicon_)) { |
108 model_->FavIconAvailable(this); | 108 model_->FavIconAvailable(this); |
109 } | 109 } |
110 } | 110 } |
111 | 111 |
112 const TemplateURL& template_url_; | 112 const TemplateURL& template_url_; |
113 SkBitmap fav_icon_; | 113 SkBitmap favicon_; |
114 LoadState load_state_; | 114 LoadState load_state_; |
115 TemplateURLTableModel* model_; | 115 TemplateURLTableModel* model_; |
116 CancelableRequestConsumer request_consumer_; | 116 CancelableRequestConsumer request_consumer_; |
117 | 117 |
118 DISALLOW_COPY_AND_ASSIGN(ModelEntry); | 118 DISALLOW_COPY_AND_ASSIGN(ModelEntry); |
119 }; | 119 }; |
120 | 120 |
121 // TemplateURLTableModel ----------------------------------------- | 121 // TemplateURLTableModel ----------------------------------------- |
122 | 122 |
123 TemplateURLTableModel::TemplateURLTableModel( | 123 TemplateURLTableModel::TemplateURLTableModel( |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 void TemplateURLTableModel::FavIconAvailable(ModelEntry* entry) { | 372 void TemplateURLTableModel::FavIconAvailable(ModelEntry* entry) { |
373 std::vector<ModelEntry*>::iterator i = | 373 std::vector<ModelEntry*>::iterator i = |
374 find(entries_.begin(), entries_.end(), entry); | 374 find(entries_.begin(), entries_.end(), entry); |
375 DCHECK(i != entries_.end()); | 375 DCHECK(i != entries_.end()); |
376 NotifyChanged(static_cast<int>(i - entries_.begin())); | 376 NotifyChanged(static_cast<int>(i - entries_.begin())); |
377 } | 377 } |
378 | 378 |
379 void TemplateURLTableModel::OnTemplateURLModelChanged() { | 379 void TemplateURLTableModel::OnTemplateURLModelChanged() { |
380 Reload(); | 380 Reload(); |
381 } | 381 } |
OLD | NEW |