OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/history/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
20 #include "base/time.h" | 20 #include "base/time.h" |
21 #include "chrome/browser/autocomplete/history_url_provider.h" | 21 #include "chrome/browser/autocomplete/history_url_provider.h" |
22 #include "chrome/browser/bookmarks/bookmark_service.h" | 22 #include "chrome/browser/bookmarks/bookmark_service.h" |
23 #include "chrome/browser/cancelable_request.h" | 23 #include "chrome/browser/cancelable_request.h" |
24 #include "chrome/browser/history/history_notifications.h" | 24 #include "chrome/browser/history/history_notifications.h" |
25 #include "chrome/browser/history/history_publisher.h" | 25 #include "chrome/browser/history/history_publisher.h" |
26 #include "chrome/browser/history/in_memory_history_backend.h" | 26 #include "chrome/browser/history/in_memory_history_backend.h" |
27 #include "chrome/browser/history/page_usage_data.h" | 27 #include "chrome/browser/history/page_usage_data.h" |
| 28 #include "chrome/browser/history/time_filter.h" |
28 #include "chrome/browser/history/top_sites.h" | 29 #include "chrome/browser/history/top_sites.h" |
29 #include "chrome/common/chrome_constants.h" | 30 #include "chrome/common/chrome_constants.h" |
30 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
31 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
32 #include "content/browser/download/download_persistent_store_info.h" | 33 #include "content/browser/download/download_persistent_store_info.h" |
33 #include "googleurl/src/gurl.h" | 34 #include "googleurl/src/gurl.h" |
34 #include "grit/chromium_strings.h" | 35 #include "grit/chromium_strings.h" |
35 #include "grit/generated_resources.h" | 36 #include "grit/generated_resources.h" |
36 #include "net/base/registry_controlled_domain.h" | 37 #include "net/base/registry_controlled_domain.h" |
37 | 38 |
(...skipping 1365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1403 // No History Database - return an empty list. | 1404 // No History Database - return an empty list. |
1404 request->ForwardResult(request->handle(), MostVisitedURLList()); | 1405 request->ForwardResult(request->handle(), MostVisitedURLList()); |
1405 return; | 1406 return; |
1406 } | 1407 } |
1407 | 1408 |
1408 MostVisitedURLList* result = &request->value; | 1409 MostVisitedURLList* result = &request->value; |
1409 QueryMostVisitedURLsImpl(result_count, days_back, result); | 1410 QueryMostVisitedURLsImpl(result_count, days_back, result); |
1410 request->ForwardResult(request->handle(), *result); | 1411 request->ForwardResult(request->handle(), *result); |
1411 } | 1412 } |
1412 | 1413 |
| 1414 void HistoryBackend::QueryMostVisitedURLsDuringTime( |
| 1415 scoped_refptr<QueryMostVisitedURLsRequest> request, |
| 1416 int result_count, |
| 1417 const history::TimeFilter& filter) { |
| 1418 if (request->canceled()) |
| 1419 return; |
| 1420 |
| 1421 if (!db_.get()) { |
| 1422 // No History Database - return an empty list. |
| 1423 request->ForwardResult(request->handle(), MostVisitedURLList()); |
| 1424 return; |
| 1425 } |
| 1426 |
| 1427 VisitVector visits; |
| 1428 db_->GetVisibleVisitsDuringTimes(filter, 0, &visits); |
| 1429 |
| 1430 std::map<URLID, double> score_map; |
| 1431 const double kLn2 = 0.6931471805599453; |
| 1432 base::Time now = base::Time::Now(); |
| 1433 for (size_t i = 0; i < visits.size(); ++i) { |
| 1434 // Decay score by half each week. |
| 1435 base::TimeDelta time_passed = now - visits[i].visit_time; |
| 1436 // Clamp to 0 in case time jumps backwards (e.g. due to DST). |
| 1437 double decay_exponent = std::max(0.0, kLn2 * static_cast<double>( |
| 1438 time_passed.InMicroseconds()) / base::Time::kMicrosecondsPerWeek); |
| 1439 double score = 1.0 / exp(decay_exponent); |
| 1440 std::map<URLID, double>::iterator it = score_map.find(visits[i].url_id); |
| 1441 if (it == score_map.end()) |
| 1442 score_map[visits[i].url_id] = score; |
| 1443 else |
| 1444 it->second += score; |
| 1445 } |
| 1446 |
| 1447 ScopedVector<PageUsageData> data; |
| 1448 data->reserve(score_map.size()); |
| 1449 for (std::map<URLID, double>::iterator it = score_map.begin(); |
| 1450 it != score_map.end(); ++it) { |
| 1451 PageUsageData *one_url = new PageUsageData(it->first); |
| 1452 one_url->SetScore(it->second); |
| 1453 data->push_back(one_url); |
| 1454 } |
| 1455 |
| 1456 // Limit to the top |result_count| results. |
| 1457 std::sort(data.begin(), data.end(), PageUsageData::Predicate); |
| 1458 if (result_count && static_cast<int>(data.size()) > result_count) { |
| 1459 STLDeleteContainerPointers(data.begin() + result_count, data.end()); |
| 1460 data.resize(result_count); |
| 1461 } |
| 1462 |
| 1463 for (size_t i = 0; i < data->size(); ++i) { |
| 1464 PageUsageData* pud = data[i]; |
| 1465 URLRow info; |
| 1466 if (db_->GetURLRow(pud->GetID(), &info)) { |
| 1467 pud->SetURL(info.url()); |
| 1468 pud->SetTitle(info.title()); |
| 1469 } |
| 1470 } |
| 1471 |
| 1472 MostVisitedURLList& result = request->value; |
| 1473 for (size_t i = 0; i < data.size(); ++i) { |
| 1474 PageUsageData* current_data = data[i]; |
| 1475 RedirectList redirects; |
| 1476 GetMostRecentRedirectsFrom(current_data->GetURL(), &redirects); |
| 1477 MostVisitedURL url = MakeMostVisitedURL(*current_data, redirects); |
| 1478 result.push_back(url); |
| 1479 } |
| 1480 |
| 1481 request->ForwardResult(request->handle(), result); |
| 1482 } |
| 1483 |
1413 void HistoryBackend::QueryMostVisitedURLsImpl(int result_count, | 1484 void HistoryBackend::QueryMostVisitedURLsImpl(int result_count, |
1414 int days_back, | 1485 int days_back, |
1415 MostVisitedURLList* result) { | 1486 MostVisitedURLList* result) { |
1416 if (!db_.get()) | 1487 if (!db_.get()) |
1417 return; | 1488 return; |
1418 | 1489 |
1419 ScopedVector<PageUsageData> data; | 1490 ScopedVector<PageUsageData> data; |
1420 db_->QuerySegmentUsage(base::Time::Now() - | 1491 db_->QuerySegmentUsage(base::Time::Now() - |
1421 base::TimeDelta::FromDays(days_back), | 1492 base::TimeDelta::FromDays(days_back), |
1422 result_count, &data.get()); | 1493 result_count, &data.get()); |
(...skipping 886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2309 break; | 2380 break; |
2310 } | 2381 } |
2311 } | 2382 } |
2312 } | 2383 } |
2313 UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB", // historical name | 2384 UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB", // historical name |
2314 TimeTicks::Now() - beginning_time); | 2385 TimeTicks::Now() - beginning_time); |
2315 return success; | 2386 return success; |
2316 } | 2387 } |
2317 | 2388 |
2318 } // namespace history | 2389 } // namespace history |
OLD | NEW |