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/dom_ui/most_visited_handler.h" | 5 #include "chrome/browser/dom_ui/most_visited_handler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 SendPagesValue(); | 125 SendPagesValue(); |
126 got_first_most_visited_request_ = true; | 126 got_first_most_visited_request_ = true; |
127 } else { | 127 } else { |
128 StartQueryForMostVisited(); | 128 StartQueryForMostVisited(); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 void MostVisitedHandler::SendPagesValue() { | 132 void MostVisitedHandler::SendPagesValue() { |
133 if (pages_value_.get()) { | 133 if (pages_value_.get()) { |
134 bool has_blacklisted_urls = !url_blacklist_->empty(); | 134 bool has_blacklisted_urls = !url_blacklist_->empty(); |
135 if (history::TopSites::IsEnabled()) { | 135 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
136 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 136 if (ts) |
137 if (ts) | 137 has_blacklisted_urls = ts->HasBlacklistedItems(); |
138 has_blacklisted_urls = ts->HasBlacklistedItems(); | |
139 } | |
140 FundamentalValue first_run(IsFirstRun()); | 138 FundamentalValue first_run(IsFirstRun()); |
141 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 139 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
142 dom_ui_->CallJavascriptFunction(L"mostVisitedPages", | 140 dom_ui_->CallJavascriptFunction(L"mostVisitedPages", |
143 *(pages_value_.get()), | 141 *(pages_value_.get()), |
144 first_run, | 142 first_run, |
145 has_blacklisted_urls_value); | 143 has_blacklisted_urls_value); |
146 pages_value_.reset(); | 144 pages_value_.reset(); |
147 } | 145 } |
148 } | 146 } |
149 | 147 |
150 void MostVisitedHandler::StartQueryForMostVisited() { | 148 void MostVisitedHandler::StartQueryForMostVisited() { |
151 if (history::TopSites::IsEnabled()) { | 149 // Use TopSites. |
152 // Use TopSites. | 150 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
153 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 151 if (ts) { |
154 if (ts) { | 152 ts->GetMostVisitedURLs( |
155 ts->GetMostVisitedURLs( | 153 &topsites_consumer_, |
156 &topsites_consumer_, | 154 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); |
157 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); | |
158 } | |
159 return; | |
160 } | |
161 | |
162 const int page_count = kMostVisitedPages; | |
163 // Let's query for the number of items we want plus the blacklist size as | |
164 // we'll be filtering-out the returned list with the blacklist URLs. | |
165 // We do not subtract the number of pinned URLs we have because the | |
166 // HistoryService does not know about those. | |
167 const int result_count = page_count + url_blacklist_->size(); | |
168 HistoryService* hs = | |
169 dom_ui_->GetProfile()->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
170 // |hs| may be null during unit tests. | |
171 if (hs) { | |
172 hs->QuerySegmentUsageSince( | |
173 &cancelable_consumer_, | |
174 base::Time::Now() - base::TimeDelta::FromDays(kMostVisitedScope), | |
175 result_count, | |
176 NewCallback(this, &MostVisitedHandler::OnSegmentUsageAvailable)); | |
177 } | 155 } |
178 } | 156 } |
179 | 157 |
180 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { | 158 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { |
181 std::string url = WideToUTF8(ExtractStringValue(args)); | 159 std::string url = WideToUTF8(ExtractStringValue(args)); |
182 BlacklistURL(GURL(url)); | 160 BlacklistURL(GURL(url)); |
183 } | 161 } |
184 | 162 |
185 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { | 163 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { |
186 DCHECK(args->GetSize() != 0); | 164 DCHECK(args->GetSize() != 0); |
187 | 165 |
188 for (ListValue::const_iterator iter = args->begin(); | 166 for (ListValue::const_iterator iter = args->begin(); |
189 iter != args->end(); ++iter) { | 167 iter != args->end(); ++iter) { |
190 std::string url; | 168 std::string url; |
191 bool r = (*iter)->GetAsString(&url); | 169 bool r = (*iter)->GetAsString(&url); |
192 if (!r) { | 170 if (!r) { |
193 NOTREACHED(); | 171 NOTREACHED(); |
194 return; | 172 return; |
195 } | 173 } |
196 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved"), | 174 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved"), |
197 dom_ui_->GetProfile()); | 175 dom_ui_->GetProfile()); |
198 if (history::TopSites::IsEnabled()) { | 176 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
199 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 177 if (ts) |
200 if (ts) | 178 ts->RemoveBlacklistedURL(GURL(url)); |
201 ts->RemoveBlacklistedURL(GURL(url)); | |
202 return; | |
203 } | |
204 | |
205 r = url_blacklist_->Remove(GetDictionaryKeyForURL(url), NULL); | |
206 DCHECK(r) << "Unknown URL removed from the NTP Most Visited blacklist."; | |
207 } | 179 } |
208 } | 180 } |
209 | 181 |
210 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 182 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { |
211 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared"), | 183 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared"), |
212 dom_ui_->GetProfile()); | 184 dom_ui_->GetProfile()); |
213 | 185 |
214 if (history::TopSites::IsEnabled()) { | 186 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
215 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 187 if (ts) |
216 if (ts) | 188 ts->ClearBlacklistedURLs(); |
217 ts->ClearBlacklistedURLs(); | |
218 return; | |
219 } | |
220 | |
221 url_blacklist_->Clear(); | |
222 } | 189 } |
223 | 190 |
224 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { | 191 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { |
225 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; | 192 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; |
226 MostVisitedPage mvp; | 193 MostVisitedPage mvp; |
227 std::string tmp_string; | 194 std::string tmp_string; |
228 string16 tmp_string16; | 195 string16 tmp_string16; |
229 int index; | 196 int index; |
230 | 197 |
231 bool r = args->GetString(0, &tmp_string); | 198 bool r = args->GetString(0, &tmp_string); |
(...skipping 17 matching lines...) Expand all Loading... |
249 mvp.thumbnail_url = GURL(tmp_string); | 216 mvp.thumbnail_url = GURL(tmp_string); |
250 | 217 |
251 r = args->GetString(4, &tmp_string); | 218 r = args->GetString(4, &tmp_string); |
252 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; | 219 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; |
253 base::StringToInt(tmp_string, &index); | 220 base::StringToInt(tmp_string, &index); |
254 | 221 |
255 AddPinnedURL(mvp, index); | 222 AddPinnedURL(mvp, index); |
256 } | 223 } |
257 | 224 |
258 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { | 225 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { |
259 if (history::TopSites::IsEnabled()) { | 226 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
260 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 227 if (ts) |
261 if (ts) | 228 ts->AddPinnedURL(page.url, index); |
262 ts->AddPinnedURL(page.url, index); | |
263 return; | |
264 } | |
265 | |
266 // Remove any pinned URL at the given index. | |
267 MostVisitedPage old_page; | |
268 if (GetPinnedURLAtIndex(index, &old_page)) { | |
269 RemovePinnedURL(old_page.url); | |
270 } | |
271 | |
272 DictionaryValue* new_value = new DictionaryValue(); | |
273 SetMostVisistedPage(new_value, page); | |
274 | |
275 new_value->SetInteger("index", index); | |
276 pinned_urls_->Set(GetDictionaryKeyForURL(page.url.spec()), new_value); | |
277 | |
278 // TODO(arv): Notify observers? | |
279 | |
280 // Don't call HandleGetMostVisited. Let the client call this as needed. | |
281 } | 229 } |
282 | 230 |
283 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { | 231 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { |
284 std::string url = WideToUTF8(ExtractStringValue(args)); | 232 std::string url = WideToUTF8(ExtractStringValue(args)); |
285 RemovePinnedURL(GURL(url)); | 233 RemovePinnedURL(GURL(url)); |
286 } | 234 } |
287 | 235 |
288 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { | 236 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { |
289 if (history::TopSites::IsEnabled()) { | 237 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
290 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 238 if (ts) |
291 if (ts) | 239 ts->RemovePinnedURL(url); |
292 ts->RemovePinnedURL(url); | |
293 return; | |
294 } | |
295 | |
296 const std::string key = GetDictionaryKeyForURL(url.spec()); | |
297 if (pinned_urls_->HasKey(key)) | |
298 pinned_urls_->Remove(key, NULL); | |
299 | |
300 // TODO(arv): Notify observers? | |
301 | |
302 // Don't call HandleGetMostVisited. Let the client call this as needed. | |
303 } | 240 } |
304 | 241 |
305 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, | 242 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, |
306 MostVisitedPage* page) { | 243 MostVisitedPage* page) { |
307 // This iterates over all the pinned URLs. It might seem like it is worth | 244 // This iterates over all the pinned URLs. It might seem like it is worth |
308 // having a map from the index to the item but the number of items is limited | 245 // having a map from the index to the item but the number of items is limited |
309 // to the number of items the most visited section is showing on the NTP so | 246 // to the number of items the most visited section is showing on the NTP so |
310 // this will be fast enough for now. | 247 // this will be fast enough for now. |
311 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); | 248 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); |
312 it != pinned_urls_->end_keys(); ++it) { | 249 it != pinned_urls_->end_keys(); ++it) { |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 pages_value_->Append(page_value); | 350 pages_value_->Append(page_value); |
414 most_visited_urls_.push_back(mvp.url); | 351 most_visited_urls_.push_back(mvp.url); |
415 seen_urls.insert(mvp.url); | 352 seen_urls.insert(mvp.url); |
416 } | 353 } |
417 output_index++; | 354 output_index++; |
418 } | 355 } |
419 } | 356 } |
420 | 357 |
421 void MostVisitedHandler::SetPagesValueFromTopSites( | 358 void MostVisitedHandler::SetPagesValueFromTopSites( |
422 const history::MostVisitedURLList& data) { | 359 const history::MostVisitedURLList& data) { |
423 DCHECK(history::TopSites::IsEnabled()); | |
424 pages_value_.reset(new ListValue); | 360 pages_value_.reset(new ListValue); |
425 for (size_t i = 0; i < data.size(); i++) { | 361 for (size_t i = 0; i < data.size(); i++) { |
426 const history::MostVisitedURL& url = data[i]; | 362 const history::MostVisitedURL& url = data[i]; |
427 DictionaryValue* page_value = new DictionaryValue(); | 363 DictionaryValue* page_value = new DictionaryValue(); |
428 if (url.url.is_empty()) { | 364 if (url.url.is_empty()) { |
429 page_value->SetBoolean("filler", true); | 365 page_value->SetBoolean("filler", true); |
430 pages_value_->Append(page_value); | 366 pages_value_->Append(page_value); |
431 continue; | 367 continue; |
432 } | 368 } |
433 | 369 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
515 if (type != NotificationType::HISTORY_URLS_DELETED) { | 451 if (type != NotificationType::HISTORY_URLS_DELETED) { |
516 NOTREACHED(); | 452 NOTREACHED(); |
517 return; | 453 return; |
518 } | 454 } |
519 | 455 |
520 // Some URLs were deleted from history. Reload the most visited list. | 456 // Some URLs were deleted from history. Reload the most visited list. |
521 HandleGetMostVisited(NULL); | 457 HandleGetMostVisited(NULL); |
522 } | 458 } |
523 | 459 |
524 void MostVisitedHandler::BlacklistURL(const GURL& url) { | 460 void MostVisitedHandler::BlacklistURL(const GURL& url) { |
525 if (history::TopSites::IsEnabled()) { | 461 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); |
526 history::TopSites* ts = dom_ui_->GetProfile()->GetTopSites(); | 462 if (ts) |
527 if (ts) | 463 ts->AddBlacklistedURL(url); |
528 ts->AddBlacklistedURL(url); | |
529 return; | |
530 } | |
531 | |
532 RemovePinnedURL(url); | |
533 | |
534 std::string key = GetDictionaryKeyForURL(url.spec()); | |
535 if (url_blacklist_->HasKey(key)) | |
536 return; | |
537 url_blacklist_->SetBoolean(key, true); | |
538 } | 464 } |
539 | 465 |
540 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 466 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { |
541 return MD5String(url); | 467 return MD5String(url); |
542 } | 468 } |
543 | 469 |
544 // static | 470 // static |
545 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 471 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
546 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); | 472 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); |
547 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); | 473 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); |
548 } | 474 } |
549 | 475 |
550 // static | 476 // static |
551 std::vector<GURL> MostVisitedHandler::GetPrePopulatedUrls() { | 477 std::vector<GURL> MostVisitedHandler::GetPrePopulatedUrls() { |
552 const std::vector<MostVisitedPage> pages = | 478 const std::vector<MostVisitedPage> pages = |
553 MostVisitedHandler::GetPrePopulatedPages(); | 479 MostVisitedHandler::GetPrePopulatedPages(); |
554 std::vector<GURL> page_urls; | 480 std::vector<GURL> page_urls; |
555 for (size_t i = 0; i < pages.size(); ++i) | 481 for (size_t i = 0; i < pages.size(); ++i) |
556 page_urls.push_back(pages[i].url); | 482 page_urls.push_back(pages[i].url); |
557 return page_urls; | 483 return page_urls; |
558 } | 484 } |
OLD | NEW |