| 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/custom_home_pages_table_model.h" | 5 #include "chrome/browser/custom_home_pages_table_model.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/browser_list.h" | 9 #include "chrome/browser/browser_list.h" |
| 10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" | 15 #include "content/browser/tab_contents/tab_contents.h" |
| 16 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 17 #include "grit/app_resources.h" | 17 #include "grit/app_resources.h" |
| 18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 19 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 20 #include "third_party/skia/include/core/SkBitmap.h" | 20 #include "third_party/skia/include/core/SkBitmap.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/models/table_model_observer.h" | 22 #include "ui/base/models/table_model_observer.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/gfx/codec/png_codec.h" | 24 #include "ui/gfx/codec/png_codec.h" |
| 25 | 25 |
| 26 struct CustomHomePagesTableModel::Entry { | 26 struct CustomHomePagesTableModel::Entry { |
| 27 Entry() : title_handle(0), fav_icon_handle(0) {} | 27 Entry() : title_handle(0), favicon_handle(0) {} |
| 28 | 28 |
| 29 // URL of the page. | 29 // URL of the page. |
| 30 GURL url; | 30 GURL url; |
| 31 | 31 |
| 32 // Page title. If this is empty, we'll display the URL as the entry. | 32 // Page title. If this is empty, we'll display the URL as the entry. |
| 33 string16 title; | 33 string16 title; |
| 34 | 34 |
| 35 // Icon for the page. | 35 // Icon for the page. |
| 36 SkBitmap icon; | 36 SkBitmap icon; |
| 37 | 37 |
| 38 // If non-zero, indicates we're loading the title for the page. | 38 // If non-zero, indicates we're loading the title for the page. |
| 39 HistoryService::Handle title_handle; | 39 HistoryService::Handle title_handle; |
| 40 | 40 |
| 41 // If non-zero, indicates we're loading the favicon for the page. | 41 // If non-zero, indicates we're loading the favicon for the page. |
| 42 FaviconService::Handle fav_icon_handle; | 42 FaviconService::Handle favicon_handle; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) | 45 CustomHomePagesTableModel::CustomHomePagesTableModel(Profile* profile) |
| 46 : default_favicon_(NULL), | 46 : default_favicon_(NULL), |
| 47 profile_(profile), | 47 profile_(profile), |
| 48 observer_(NULL) { | 48 observer_(NULL) { |
| 49 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 49 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 50 default_favicon_ = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); | 50 default_favicon_ = rb.GetBitmapNamed(IDR_DEFAULT_FAVICON); |
| 51 } | 51 } |
| 52 | 52 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 DCHECK(index >= 0 && index < RowCount()); | 79 DCHECK(index >= 0 && index < RowCount()); |
| 80 Entry* entry = &(entries_[index]); | 80 Entry* entry = &(entries_[index]); |
| 81 // Cancel any pending load requests now so we don't deref a bogus pointer when | 81 // Cancel any pending load requests now so we don't deref a bogus pointer when |
| 82 // we get the loaded notification. | 82 // we get the loaded notification. |
| 83 if (entry->title_handle) { | 83 if (entry->title_handle) { |
| 84 HistoryService* history_service = | 84 HistoryService* history_service = |
| 85 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 85 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 86 if (history_service) | 86 if (history_service) |
| 87 history_service->CancelRequest(entry->title_handle); | 87 history_service->CancelRequest(entry->title_handle); |
| 88 } | 88 } |
| 89 if (entry->fav_icon_handle) { | 89 if (entry->favicon_handle) { |
| 90 FaviconService* favicon_service = | 90 FaviconService* favicon_service = |
| 91 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 91 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 92 if (favicon_service) | 92 if (favicon_service) |
| 93 favicon_service->CancelRequest(entry->fav_icon_handle); | 93 favicon_service->CancelRequest(entry->favicon_handle); |
| 94 } | 94 } |
| 95 entries_.erase(entries_.begin() + static_cast<size_t>(index)); | 95 entries_.erase(entries_.begin() + static_cast<size_t>(index)); |
| 96 if (observer_) | 96 if (observer_) |
| 97 observer_->OnItemsRemoved(index, 1); | 97 observer_->OnItemsRemoved(index, 1); |
| 98 } | 98 } |
| 99 | 99 |
| 100 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { | 100 void CustomHomePagesTableModel::SetToCurrentlyOpenPages() { |
| 101 // Remove the current entries. | 101 // Remove the current entries. |
| 102 while (RowCount()) | 102 while (RowCount()) |
| 103 Remove(0); | 103 Remove(0); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 HistoryService* history_service = | 156 HistoryService* history_service = |
| 157 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 157 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 158 if (history_service) { | 158 if (history_service) { |
| 159 entry->title_handle = history_service->QueryURL(entry->url, false, | 159 entry->title_handle = history_service->QueryURL(entry->url, false, |
| 160 &query_consumer_, | 160 &query_consumer_, |
| 161 NewCallback(this, &CustomHomePagesTableModel::OnGotTitle)); | 161 NewCallback(this, &CustomHomePagesTableModel::OnGotTitle)); |
| 162 } | 162 } |
| 163 FaviconService* favicon_service = | 163 FaviconService* favicon_service = |
| 164 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); | 164 profile_->GetFaviconService(Profile::EXPLICIT_ACCESS); |
| 165 if (favicon_service) { | 165 if (favicon_service) { |
| 166 entry->fav_icon_handle = favicon_service->GetFaviconForURL(entry->url, | 166 entry->favicon_handle = favicon_service->GetFaviconForURL(entry->url, |
| 167 &query_consumer_, | 167 &query_consumer_, |
| 168 NewCallback(this, &CustomHomePagesTableModel::OnGotFavIcon)); | 168 NewCallback(this, &CustomHomePagesTableModel::OnGotFavIcon)); |
| 169 } | 169 } |
| 170 } | 170 } |
| 171 | 171 |
| 172 void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, | 172 void CustomHomePagesTableModel::OnGotTitle(HistoryService::Handle handle, |
| 173 bool found_url, | 173 bool found_url, |
| 174 const history::URLRow* row, | 174 const history::URLRow* row, |
| 175 history::VisitVector* visits) { | 175 history::VisitVector* visits) { |
| 176 int entry_index; | 176 int entry_index; |
| 177 Entry* entry = | 177 Entry* entry = |
| 178 GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); | 178 GetEntryByLoadHandle(&Entry::title_handle, handle, &entry_index); |
| 179 if (!entry) { | 179 if (!entry) { |
| 180 // The URLs changed before we were called back. | 180 // The URLs changed before we were called back. |
| 181 return; | 181 return; |
| 182 } | 182 } |
| 183 entry->title_handle = 0; | 183 entry->title_handle = 0; |
| 184 if (found_url && !row->title().empty()) { | 184 if (found_url && !row->title().empty()) { |
| 185 entry->title = row->title(); | 185 entry->title = row->title(); |
| 186 if (observer_) | 186 if (observer_) |
| 187 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); | 187 observer_->OnItemsChanged(static_cast<int>(entry_index), 1); |
| 188 } | 188 } |
| 189 } | 189 } |
| 190 | 190 |
| 191 void CustomHomePagesTableModel::OnGotFavIcon( | 191 void CustomHomePagesTableModel::OnGotFavIcon( |
| 192 FaviconService::Handle handle, | 192 FaviconService::Handle handle, |
| 193 bool know_fav_icon, | 193 bool know_favicon, |
| 194 scoped_refptr<RefCountedMemory> image_data, | 194 scoped_refptr<RefCountedMemory> image_data, |
| 195 bool is_expired, | 195 bool is_expired, |
| 196 GURL icon_url) { | 196 GURL icon_url) { |
| 197 int entry_index; | 197 int entry_index; |
| 198 Entry* entry = | 198 Entry* entry = |
| 199 GetEntryByLoadHandle(&Entry::fav_icon_handle, handle, &entry_index); | 199 GetEntryByLoadHandle(&Entry::favicon_handle, handle, &entry_index); |
| 200 if (!entry) { | 200 if (!entry) { |
| 201 // The URLs changed before we were called back. | 201 // The URLs changed before we were called back. |
| 202 return; | 202 return; |
| 203 } | 203 } |
| 204 entry->fav_icon_handle = 0; | 204 entry->favicon_handle = 0; |
| 205 if (know_fav_icon && image_data.get() && image_data->size()) { | 205 if (know_favicon && image_data.get() && image_data->size()) { |
| 206 int width, height; | 206 int width, height; |
| 207 std::vector<unsigned char> decoded_data; | 207 std::vector<unsigned char> decoded_data; |
| 208 if (gfx::PNGCodec::Decode(image_data->front(), | 208 if (gfx::PNGCodec::Decode(image_data->front(), |
| 209 image_data->size(), | 209 image_data->size(), |
| 210 gfx::PNGCodec::FORMAT_BGRA, &decoded_data, | 210 gfx::PNGCodec::FORMAT_BGRA, &decoded_data, |
| 211 &width, &height)) { | 211 &width, &height)) { |
| 212 entry->icon.setConfig(SkBitmap::kARGB_8888_Config, width, height); | 212 entry->icon.setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 213 entry->icon.allocPixels(); | 213 entry->icon.allocPixels(); |
| 214 memcpy(entry->icon.getPixels(), &decoded_data.front(), | 214 memcpy(entry->icon.getPixels(), &decoded_data.front(), |
| 215 width * height * 4); | 215 width * height * 4); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 233 return NULL; | 233 return NULL; |
| 234 } | 234 } |
| 235 | 235 |
| 236 string16 CustomHomePagesTableModel::FormattedURL(int row) const { | 236 string16 CustomHomePagesTableModel::FormattedURL(int row) const { |
| 237 std::string languages = | 237 std::string languages = |
| 238 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); | 238 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 239 string16 url = net::FormatUrl(entries_[row].url, languages); | 239 string16 url = net::FormatUrl(entries_[row].url, languages); |
| 240 url = base::i18n::GetDisplayStringInLTRDirectionality(url); | 240 url = base::i18n::GetDisplayStringInLTRDirectionality(url); |
| 241 return url; | 241 return url; |
| 242 } | 242 } |
| OLD | NEW |