OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/dom_ui/most_visited_handler.h" | 5 #include "chrome/browser/dom_ui/most_visited_handler.h" |
6 | 6 |
7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
8 #include "base/md5.h" | 8 #include "base/md5.h" |
9 #include "base/singleton.h" | 9 #include "base/singleton.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
11 #include "base/thread.h" | 11 #include "base/thread.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 StartQueryForMostVisited(); | 119 StartQueryForMostVisited(); |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 void MostVisitedHandler::StartQueryForMostVisited() { | 123 void MostVisitedHandler::StartQueryForMostVisited() { |
124 const int page_count = kMostVisitedPages; | 124 const int page_count = kMostVisitedPages; |
125 // Let's query for the number of items we want plus the blacklist size as | 125 // Let's query for the number of items we want plus the blacklist size as |
126 // we'll be filtering-out the returned list with the blacklist URLs. | 126 // we'll be filtering-out the returned list with the blacklist URLs. |
127 // We do not subtract the number of pinned URLs we have because the | 127 // We do not subtract the number of pinned URLs we have because the |
128 // HistoryService does not know about those. | 128 // HistoryService does not know about those. |
129 const int result_count = page_count + url_blacklist_->GetSize(); | 129 const int result_count = page_count + url_blacklist_->size(); |
130 HistoryService* hs = | 130 HistoryService* hs = |
131 dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); | 131 dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
132 // |hs| may be null during unit tests. | 132 // |hs| may be null during unit tests. |
133 if (hs) { | 133 if (hs) { |
134 hs->QuerySegmentUsageSince( | 134 hs->QuerySegmentUsageSince( |
135 &cancelable_consumer_, | 135 &cancelable_consumer_, |
136 base::Time::Now() - base::TimeDelta::FromDays(kMostVisitedScope), | 136 base::Time::Now() - base::TimeDelta::FromDays(kMostVisitedScope), |
137 result_count, | 137 result_count, |
138 NewCallback(this, &MostVisitedHandler::OnSegmentUsageAvailable)); | 138 NewCallback(this, &MostVisitedHandler::OnSegmentUsageAvailable)); |
139 } | 139 } |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { | 223 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { |
224 // Remove any pinned URL at the given index. | 224 // Remove any pinned URL at the given index. |
225 MostVisitedPage old_page; | 225 MostVisitedPage old_page; |
226 if (GetPinnedURLAtIndex(index, &old_page)) { | 226 if (GetPinnedURLAtIndex(index, &old_page)) { |
227 RemovePinnedURL(old_page.url); | 227 RemovePinnedURL(old_page.url); |
228 } | 228 } |
229 | 229 |
230 DictionaryValue* new_value = new DictionaryValue(); | 230 DictionaryValue* new_value = new DictionaryValue(); |
231 SetMostVisistedPage(new_value, page); | 231 SetMostVisistedPage(new_value, page); |
232 | 232 |
233 bool r = new_value->SetInteger(L"index", index); | 233 new_value->SetInteger(L"index", index); |
234 DCHECK(r) << "Failed to set the index for a pinned URL from the NTP Most " | 234 pinned_urls_->Set(GetDictionaryKeyForURL(page.url.spec()), new_value); |
235 << "Visited."; | |
236 | |
237 r = pinned_urls_->Set(GetDictionaryKeyForURL(page.url.spec()), new_value); | |
238 DCHECK(r) << "Failed to add pinned URL from the NTP Most Visited."; | |
239 | 235 |
240 // TODO(arv): Notify observers? | 236 // TODO(arv): Notify observers? |
241 | 237 |
242 // Don't call HandleGetMostVisited. Let the client call this as needed. | 238 // Don't call HandleGetMostVisited. Let the client call this as needed. |
243 } | 239 } |
244 | 240 |
245 void MostVisitedHandler::HandleRemovePinnedURL(const Value* value) { | 241 void MostVisitedHandler::HandleRemovePinnedURL(const Value* value) { |
246 if (!value->IsType(Value::TYPE_LIST)) { | 242 if (!value->IsType(Value::TYPE_LIST)) { |
247 NOTREACHED(); | 243 NOTREACHED(); |
248 return; | 244 return; |
(...skipping 20 matching lines...) Expand all Loading... |
269 | 265 |
270 const bool MostVisitedHandler::GetPinnedURLAtIndex(const int index, | 266 const bool MostVisitedHandler::GetPinnedURLAtIndex(const int index, |
271 MostVisitedPage* page) { | 267 MostVisitedPage* page) { |
272 // This iterates over all the pinned URLs. It might seem like it is worth | 268 // This iterates over all the pinned URLs. It might seem like it is worth |
273 // having a map from the index to the item but the number of items is limited | 269 // having a map from the index to the item but the number of items is limited |
274 // to the number of items the most visited section is showing on the NTP so | 270 // to the number of items the most visited section is showing on the NTP so |
275 // this will be fast enough for now. | 271 // this will be fast enough for now. |
276 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); | 272 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); |
277 it != pinned_urls_->end_keys(); ++it) { | 273 it != pinned_urls_->end_keys(); ++it) { |
278 Value* value; | 274 Value* value; |
279 if (pinned_urls_->Get(*it, &value)) { | 275 if (pinned_urls_->GetWithoutPathExpansion(*it, &value)) { |
280 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { | 276 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { |
281 NOTREACHED(); | 277 NOTREACHED(); |
282 return false; | 278 return false; |
283 } | 279 } |
284 | 280 |
285 int dict_index; | 281 int dict_index; |
286 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | 282 DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
287 if (dict->GetInteger(L"index", &dict_index) && dict_index == index) { | 283 if (dict->GetInteger(L"index", &dict_index) && dict_index == index) { |
288 // The favicon and thumbnail URLs may be empty. | 284 // The favicon and thumbnail URLs may be empty. |
289 std::string tmp_string; | 285 std::string tmp_string; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 std::wstring MostVisitedHandler::GetDictionaryKeyForURL( | 438 std::wstring MostVisitedHandler::GetDictionaryKeyForURL( |
443 const std::string& url) { | 439 const std::string& url) { |
444 return ASCIIToWide(MD5String(url)); | 440 return ASCIIToWide(MD5String(url)); |
445 } | 441 } |
446 | 442 |
447 // static | 443 // static |
448 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 444 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
449 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); | 445 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); |
450 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); | 446 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); |
451 } | 447 } |
OLD | NEW |