OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/possible_url_model.h" | 5 #include "chrome/browser/possible_url_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/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
11 #include "chrome/browser/favicon_service.h" | 11 #include "chrome/browser/favicon_service.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "content/browser/cancelable_request.h" | 15 #include "content/browser/cancelable_request.h" |
16 #include "grit/app_resources.h" | 16 #include "grit/app_resources.h" |
17 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
18 #include "third_party/skia/include/core/SkBitmap.h" | 18 #include "third_party/skia/include/core/SkBitmap.h" |
19 #include "ui/base/models/table_model_observer.h" | 19 #include "ui/base/models/table_model_observer.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "ui/base/text/text_elider.h" | 21 #include "ui/base/text/text_elider.h" |
22 #include "ui/gfx/codec/png_codec.h" | 22 #include "ui/gfx/codec/png_codec.h" |
23 | 23 |
24 using base::Time; | 24 using base::Time; |
25 using base::TimeDelta; | 25 using base::TimeDelta; |
26 | 26 |
27 namespace { | 27 namespace { |
28 | 28 |
29 // The default favicon. | 29 // The default favicon. |
30 SkBitmap* default_fav_icon = NULL; | 30 SkBitmap* default_favicon = NULL; |
31 | 31 |
32 // How long we query entry points for. | 32 // How long we query entry points for. |
33 const int kPossibleURLTimeScope = 30; | 33 const int kPossibleURLTimeScope = 30; |
34 | 34 |
35 } // anonymous namespace | 35 } // anonymous namespace |
36 | 36 |
37 // Contains the data needed to show a result. | 37 // Contains the data needed to show a result. |
38 struct PossibleURLModel::Result { | 38 struct PossibleURLModel::Result { |
39 Result() : index(0) {} | 39 Result() : index(0) {} |
40 | 40 |
41 GURL url; | 41 GURL url; |
42 // Index of this Result in results_. This is used as the key into | 42 // Index of this Result in results_. This is used as the key into |
43 // fav_icon_map_ to lookup the favicon for the url, as well as the index | 43 // favicon_map_ to lookup the favicon for the url, as well as the index |
44 // into results_ when the favicon is received. | 44 // into results_ when the favicon is received. |
45 size_t index; | 45 size_t index; |
46 ui::SortedDisplayURL display_url; | 46 ui::SortedDisplayURL display_url; |
47 std::wstring title; | 47 std::wstring title; |
48 }; | 48 }; |
49 | 49 |
50 | 50 |
51 PossibleURLModel::PossibleURLModel() | 51 PossibleURLModel::PossibleURLModel() |
52 : profile_(NULL), | 52 : profile_(NULL), |
53 observer_(NULL) { | 53 observer_(NULL) { |
54 if (!default_fav_icon) { | 54 if (!default_favicon) { |
55 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 55 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
56 default_fav_icon = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); | 56 default_favicon = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); |
57 } | 57 } |
58 } | 58 } |
59 | 59 |
60 PossibleURLModel::~PossibleURLModel() { | 60 PossibleURLModel::~PossibleURLModel() { |
61 } | 61 } |
62 | 62 |
63 void PossibleURLModel::Reload(Profile *profile) { | 63 void PossibleURLModel::Reload(Profile *profile) { |
64 profile_ = profile; | 64 profile_ = profile; |
65 consumer_.CancelAllRequests(); | 65 consumer_.CancelAllRequests(); |
66 HistoryService* hs = | 66 HistoryService* hs = |
(...skipping 25 matching lines...) Expand all Loading... |
92 | 92 |
93 // The old version of this code would filter out all but the most recent | 93 // The old version of this code would filter out all but the most recent |
94 // visit to each host, plus all typed URLs and AUTO_BOOKMARK transitions. I | 94 // visit to each host, plus all typed URLs and AUTO_BOOKMARK transitions. I |
95 // think this dialog has a lot of work, and I'm not sure those old | 95 // think this dialog has a lot of work, and I'm not sure those old |
96 // conditions are correct (the results look about equal quality for my | 96 // conditions are correct (the results look about equal quality for my |
97 // history with and without those conditions), so I'm not spending time | 97 // history with and without those conditions), so I'm not spending time |
98 // re-implementing them here. They used to be implemented in the history | 98 // re-implementing them here. They used to be implemented in the history |
99 // service, but I think they should be implemented here because that was | 99 // service, but I think they should be implemented here because that was |
100 // pretty specific behavior that shouldn't be generally exposed. | 100 // pretty specific behavior that shouldn't be generally exposed. |
101 | 101 |
102 fav_icon_map_.clear(); | 102 favicon_map_.clear(); |
103 if (observer_) | 103 if (observer_) |
104 observer_->OnModelChanged(); | 104 observer_->OnModelChanged(); |
105 } | 105 } |
106 | 106 |
107 int PossibleURLModel::RowCount() { | 107 int PossibleURLModel::RowCount() { |
108 return static_cast<int>(results_.size()); | 108 return static_cast<int>(results_.size()); |
109 } | 109 } |
110 | 110 |
111 const GURL& PossibleURLModel::GetURL(int row) { | 111 const GURL& PossibleURLModel::GetURL(int row) { |
112 if (row < 0 || row >= RowCount()) { | 112 if (row < 0 || row >= RowCount()) { |
(...skipping 28 matching lines...) Expand all Loading... |
141 | 141 |
142 // TODO(brettw): this should probably pass the GURL up so the URL elider | 142 // TODO(brettw): this should probably pass the GURL up so the URL elider |
143 // can be used at a higher level when we know the width. | 143 // can be used at a higher level when we know the width. |
144 string16 url = results_[row].display_url.display_url(); | 144 string16 url = results_[row].display_url.display_url(); |
145 return base::i18n::GetDisplayStringInLTRDirectionality(url); | 145 return base::i18n::GetDisplayStringInLTRDirectionality(url); |
146 } | 146 } |
147 | 147 |
148 SkBitmap PossibleURLModel::GetIcon(int row) { | 148 SkBitmap PossibleURLModel::GetIcon(int row) { |
149 if (row < 0 || row >= RowCount()) { | 149 if (row < 0 || row >= RowCount()) { |
150 NOTREACHED(); | 150 NOTREACHED(); |
151 return *default_fav_icon; | 151 return *default_favicon; |
152 } | 152 } |
153 | 153 |
154 Result& result = results_[row]; | 154 Result& result = results_[row]; |
155 FavIconMap::iterator i = fav_icon_map_.find(result.index); | 155 FavIconMap::iterator i = favicon_map_.find(result.index); |
156 if (i != fav_icon_map_.end()) { | 156 if (i != favicon_map_.end()) { |
157 // We already requested the favicon, return it. | 157 // We already requested the favicon, return it. |
158 if (!i->second.isNull()) | 158 if (!i->second.isNull()) |
159 return i->second; | 159 return i->second; |
160 } else if (profile_) { | 160 } else if (profile_) { |
161 FaviconService* favicon_service = | 161 FaviconService* favicon_service = |
162 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 162 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
163 if (favicon_service) { | 163 if (favicon_service) { |
164 CancelableRequestProvider::Handle h = | 164 CancelableRequestProvider::Handle h = |
165 favicon_service->GetFaviconForURL( | 165 favicon_service->GetFaviconForURL( |
166 result.url, &consumer_, | 166 result.url, &consumer_, |
167 NewCallback(this, &PossibleURLModel::OnFavIconAvailable)); | 167 NewCallback(this, &PossibleURLModel::OnFavIconAvailable)); |
168 consumer_.SetClientData(favicon_service, h, result.index); | 168 consumer_.SetClientData(favicon_service, h, result.index); |
169 // Add an entry to the map so that we don't attempt to request the | 169 // Add an entry to the map so that we don't attempt to request the |
170 // favicon again. | 170 // favicon again. |
171 fav_icon_map_[result.index] = SkBitmap(); | 171 favicon_map_[result.index] = SkBitmap(); |
172 } | 172 } |
173 } | 173 } |
174 return *default_fav_icon; | 174 return *default_favicon; |
175 } | 175 } |
176 | 176 |
177 int PossibleURLModel::CompareValues(int row1, int row2, int column_id) { | 177 int PossibleURLModel::CompareValues(int row1, int row2, int column_id) { |
178 if (column_id == IDS_ASI_URL_COLUMN) { | 178 if (column_id == IDS_ASI_URL_COLUMN) { |
179 return results_[row1].display_url.Compare( | 179 return results_[row1].display_url.Compare( |
180 results_[row2].display_url, GetCollator()); | 180 results_[row2].display_url, GetCollator()); |
181 } | 181 } |
182 return ui::TableModel::CompareValues(row1, row2, column_id); | 182 return ui::TableModel::CompareValues(row1, row2, column_id); |
183 } | 183 } |
184 | 184 |
185 void PossibleURLModel::OnFavIconAvailable( | 185 void PossibleURLModel::OnFavIconAvailable( |
186 FaviconService::Handle h, | 186 FaviconService::Handle h, |
187 bool fav_icon_available, | 187 bool favicon_available, |
188 scoped_refptr<RefCountedMemory> data, | 188 scoped_refptr<RefCountedMemory> data, |
189 bool expired, | 189 bool expired, |
190 GURL icon_url) { | 190 GURL icon_url) { |
191 if (profile_) { | 191 if (profile_) { |
192 FaviconService* favicon_service = | 192 FaviconService* favicon_service = |
193 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 193 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
194 size_t index = consumer_.GetClientData(favicon_service, h); | 194 size_t index = consumer_.GetClientData(favicon_service, h); |
195 if (fav_icon_available) { | 195 if (favicon_available) { |
196 // The decoder will leave our bitmap empty on error. | 196 // The decoder will leave our bitmap empty on error. |
197 gfx::PNGCodec::Decode(data->front(), data->size(), | 197 gfx::PNGCodec::Decode(data->front(), data->size(), |
198 &(fav_icon_map_[index])); | 198 &(favicon_map_[index])); |
199 | 199 |
200 // Notify the observer. | 200 // Notify the observer. |
201 if (!fav_icon_map_[index].isNull() && observer_) | 201 if (!favicon_map_[index].isNull() && observer_) |
202 observer_->OnItemsChanged(static_cast<int>(index), 1); | 202 observer_->OnItemsChanged(static_cast<int>(index), 1); |
203 } | 203 } |
204 } | 204 } |
205 } | 205 } |
206 | 206 |
207 void PossibleURLModel::SetObserver(ui::TableModelObserver* observer) { | 207 void PossibleURLModel::SetObserver(ui::TableModelObserver* observer) { |
208 observer_ = observer; | 208 observer_ = observer; |
209 } | 209 } |
OLD | NEW |