This source file includes following definitions.
- MakeMostVisitedURL
- Cancel
- RunCommit
- bookmark_service_
- Init
- SetOnBackendDestroyTask
- Closing
- NotifyRenderProcessHostDestruction
- GetThumbnailFileName
- GetFaviconsFileName
- GetArchivedFileName
- GetAndroidCacheFileName
- GetLastSegmentID
- UpdateSegments
- UpdateWithPageEndTime
- UpdateVisitDuration
- AddPage
- InitImpl
- OnMemoryPressure
- CloseAllDatabases
- AddPageVisit
- AddPagesWithDetails
- IsExpiredVisitTime
- SetPageTitle
- AddPageNoVisitForBookmark
- IterateURLs
- GetAllTypedURLs
- GetVisitsForURL
- GetMostRecentVisitsForURL
- UpdateURL
- AddVisits
- RemoveVisits
- GetVisitsSource
- GetURL
- QueryURL
- GetTypedUrlSyncableService
- DeleteOldSegmentData
- QuerySegmentUsage
- SetKeywordSearchTermsForURL
- DeleteAllSearchTermsForKeyword
- GetMostRecentKeywordSearchTerms
- DeleteKeywordSearchTermForURL
- DeleteMatchingURLsForKeyword
- GetNextDownloadId
- QueryDownloads
- UpdateDownload
- CreateDownload
- RemoveDownloads
- QueryHistory
- QueryHistoryBasic
- QueryHistoryText
- QueryRedirectsFrom
- QueryRedirectsTo
- GetVisibleVisitCountToHost
- QueryTopURLsAndRedirects
- QueryMostVisitedURLs
- QueryFilteredURLs
- QueryMostVisitedURLsImpl
- GetRedirectsFromSpecificVisit
- GetRedirectsToSpecificVisit
- GetMostRecentRedirectsFrom
- GetMostRecentRedirectsTo
- ScheduleAutocomplete
- DeleteFTSIndexDatabases
- GetFavicons
- GetLargestFaviconForURL
- GetFaviconsForURL
- GetFaviconForID
- UpdateFaviconMappingsAndFetch
- MergeFavicon
- SetFavicons
- SetFaviconsOutOfDateForPage
- CloneFavicons
- SetImportedFavicons
- UpdateFaviconMappingsAndFetchImpl
- SetFaviconBitmaps
- ValidateSetFaviconsParams
- IsFaviconBitmapDataEqual
- GetFaviconsFromDB
- GetFaviconBitmapResultsForBestMatch
- SetFaviconMappingsForPageAndRedirects
- SetFaviconMappingsForPage
- GetCachedRecentRedirects
- SendFaviconChangedNotificationForPageAndRedirects
- Commit
- ScheduleCommit
- CancelScheduledCommit
- ProcessDBTaskImpl
- ReleaseDBTasks
- DeleteURLs
- DeleteURL
- ExpireHistoryBetween
- ExpireHistoryForTimes
- ExpireHistory
- URLsNoLongerBookmarked
- DatabaseErrorCallback
- KillHistoryDatabase
- ProcessDBTask
- BroadcastNotifications
- NotifySyncURLsDeleted
- DeleteAllHistory
- ClearAllThumbnailHistory
- ClearAllMainHistory
- GetBookmarkService
- NotifyVisitObservers
- PopulateMostVisitedURLMap
- RecordTopPageVisitStats
#include "chrome/browser/history/history_backend.h"
#include <algorithm>
#include <functional>
#include <list>
#include <map>
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/compiler_specific.h"
#include "base/files/file_enumerator.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/autocomplete/history_url_provider.h"
#include "chrome/browser/bookmarks/bookmark_service.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/favicon/favicon_changed_details.h"
#include "chrome/browser/history/download_row.h"
#include "chrome/browser/history/history_db_task.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/in_memory_history_backend.h"
#include "chrome/browser/history/page_usage_data.h"
#include "chrome/browser/history/select_favicon_frames.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/history/typed_url_syncable_service.h"
#include "chrome/browser/history/visit_filter.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/importer/imported_favicon_usage.h"
#include "chrome/common/url_constants.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "sql/error_delegate_util.h"
#include "url/gurl.h"
#if defined(OS_ANDROID)
#include "chrome/browser/history/android/android_provider_backend.h"
#endif
using base::Time;
using base::TimeDelta;
using base::TimeTicks;
namespace history {
const int kSegmentDataRetention = 90;
const int kCommitIntervalSeconds = 10;
const int kFaviconRefetchDays = 7;
const int kMaxRedirectCount = 32;
const int kArchiveDaysThreshold = 90;
#if defined(OS_ANDROID)
const size_t kPageVisitStatsMaxTopSites = 50;
#endif
MostVisitedURL MakeMostVisitedURL(const PageUsageData& page_data,
const RedirectList& redirects) {
MostVisitedURL mv;
mv.url = page_data.GetURL();
mv.title = page_data.GetTitle();
if (redirects.empty()) {
mv.redirects.push_back(mv.url);
} else {
mv.redirects = redirects;
if (mv.redirects[mv.redirects.size() - 1] != mv.url) {
mv.redirects.push_back(mv.url);
}
}
return mv;
}
class CommitLaterTask : public base::RefCounted<CommitLaterTask> {
public:
explicit CommitLaterTask(HistoryBackend* history_backend)
: history_backend_(history_backend) {
}
void Cancel() {
history_backend_ = NULL;
}
void RunCommit() {
if (history_backend_.get())
history_backend_->Commit();
}
private:
friend class base::RefCounted<CommitLaterTask>;
~CommitLaterTask() {}
scoped_refptr<HistoryBackend> history_backend_;
};
HistoryBackend::HistoryBackend(const base::FilePath& history_dir,
Delegate* delegate,
BookmarkService* bookmark_service)
: delegate_(delegate),
history_dir_(history_dir),
scheduled_kill_db_(false),
expirer_(this, bookmark_service),
recent_redirects_(kMaxRedirectCount),
backend_destroy_message_loop_(NULL),
segment_queried_(false),
bookmark_service_(bookmark_service) {
}
HistoryBackend::~HistoryBackend() {
DCHECK(!scheduled_commit_.get()) << "Deleting without cleanup";
ReleaseDBTasks();
#if defined(OS_ANDROID)
android_provider_backend_.reset();
#endif
CloseAllDatabases();
if (!backend_destroy_task_.is_null()) {
DCHECK(backend_destroy_message_loop_);
backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_);
}
#if defined(OS_ANDROID)
sql::Connection::Delete(GetAndroidCacheFileName());
#endif
}
void HistoryBackend::Init(const std::string& languages, bool force_fail) {
if (!force_fail)
InitImpl(languages);
delegate_->DBLoaded();
typed_url_syncable_service_.reset(new TypedUrlSyncableService(this));
memory_pressure_listener_.reset(new base::MemoryPressureListener(
base::Bind(&HistoryBackend::OnMemoryPressure, base::Unretained(this))));
#if defined(OS_ANDROID)
PopulateMostVisitedURLMap();
#endif
}
void HistoryBackend::SetOnBackendDestroyTask(base::MessageLoop* message_loop,
const base::Closure& task) {
if (!backend_destroy_task_.is_null())
DLOG(WARNING) << "Setting more than one destroy task, overriding";
backend_destroy_message_loop_ = message_loop;
backend_destroy_task_ = task;
}
void HistoryBackend::Closing() {
CancelScheduledCommit();
delegate_.reset();
}
void HistoryBackend::NotifyRenderProcessHostDestruction(const void* host) {
tracker_.NotifyRenderProcessHostDestruction(host);
}
base::FilePath HistoryBackend::GetThumbnailFileName() const {
return history_dir_.Append(chrome::kThumbnailsFilename);
}
base::FilePath HistoryBackend::GetFaviconsFileName() const {
return history_dir_.Append(chrome::kFaviconsFilename);
}
base::FilePath HistoryBackend::GetArchivedFileName() const {
return history_dir_.Append(chrome::kArchivedHistoryFilename);
}
#if defined(OS_ANDROID)
base::FilePath HistoryBackend::GetAndroidCacheFileName() const {
return history_dir_.Append(chrome::kAndroidCacheFilename);
}
#endif
SegmentID HistoryBackend::GetLastSegmentID(VisitID from_visit) {
std::set<VisitID> visit_set;
VisitID visit_id = from_visit;
while (visit_id) {
VisitRow row;
if (!db_->GetRowForVisit(visit_id, &row))
return 0;
if (row.segment_id)
return row.segment_id;
visit_id = row.referring_visit;
if (visit_set.find(visit_id) != visit_set.end()) {
NOTREACHED() << "Loop in referer chain, giving up";
break;
}
visit_set.insert(visit_id);
}
return 0;
}
SegmentID HistoryBackend::UpdateSegments(
const GURL& url,
VisitID from_visit,
VisitID visit_id,
content::PageTransition transition_type,
const Time ts) {
if (!db_)
return 0;
if (!content::PageTransitionIsMainFrame(transition_type))
return 0;
SegmentID segment_id = 0;
content::PageTransition t =
content::PageTransitionStripQualifier(transition_type);
if ((t == content::PAGE_TRANSITION_TYPED ||
t == content::PAGE_TRANSITION_AUTO_BOOKMARK) &&
(transition_type & content::PAGE_TRANSITION_FORWARD_BACK) == 0) {
std::string segment_name = db_->ComputeSegmentName(url);
URLID url_id = db_->GetRowForURL(url, NULL);
if (!url_id)
return 0;
if (!(segment_id = db_->GetSegmentNamed(segment_name))) {
if (!(segment_id = db_->CreateSegment(url_id, segment_name))) {
NOTREACHED();
return 0;
}
} else {
db_->UpdateSegmentRepresentationURL(segment_id, url_id);
}
} else {
if (!(segment_id = GetLastSegmentID(from_visit)))
return 0;
}
if (!db_->SetSegmentID(visit_id, segment_id)) {
NOTREACHED();
return 0;
}
if (!db_->IncreaseSegmentVisitCount(segment_id, ts, 1)) {
NOTREACHED();
return 0;
}
return segment_id;
}
void HistoryBackend::UpdateWithPageEndTime(const void* host,
int32 page_id,
const GURL& url,
Time end_ts) {
VisitID visit_id = tracker_.GetLastVisit(host, page_id, url);
UpdateVisitDuration(visit_id, end_ts);
}
void HistoryBackend::UpdateVisitDuration(VisitID visit_id, const Time end_ts) {
if (!db_)
return;
VisitRow visit_row;
if (db_->GetRowForVisit(visit_id, &visit_row)) {
visit_row.visit_duration = end_ts > visit_row.visit_time ?
end_ts - visit_row.visit_time : TimeDelta::FromMicroseconds(0);
db_->UpdateVisitRow(visit_row);
}
}
void HistoryBackend::AddPage(const HistoryAddPageArgs& request) {
if (!db_)
return;
std::pair<URLID, VisitID> last_ids(0, tracker_.GetLastVisit(
request.id_scope, request.page_id, request.referrer));
VisitID from_visit_id = last_ids.second;
DCHECK(request.redirects.empty() ||
request.redirects.back() == request.url);
if (request.time < first_recorded_time_)
first_recorded_time_ = request.time;
content::PageTransition request_transition = request.transition;
content::PageTransition stripped_transition =
content::PageTransitionStripQualifier(request_transition);
bool is_keyword_generated =
(stripped_transition == content::PAGE_TRANSITION_KEYWORD_GENERATED);
bool has_redirects = request.redirects.size() > 1;
if (content::PageTransitionIsMainFrame(request_transition) &&
(stripped_transition != content::PAGE_TRANSITION_TYPED) &&
!is_keyword_generated) {
const GURL& origin_url(has_redirects ?
request.redirects[0] : request.url);
if (origin_url.SchemeIs(content::kHttpScheme) ||
origin_url.SchemeIs(content::kHttpsScheme) ||
origin_url.SchemeIs(content::kFtpScheme)) {
std::string host(origin_url.host());
size_t registry_length =
net::registry_controlled_domains::GetRegistryLength(
host,
net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
if (registry_length == 0 && !db_->IsTypedHost(host)) {
stripped_transition = content::PAGE_TRANSITION_TYPED;
request_transition =
content::PageTransitionFromInt(
stripped_transition |
content::PageTransitionGetQualifier(request_transition));
}
}
}
if (!has_redirects) {
content::PageTransition t = content::PageTransitionFromInt(
request_transition |
content::PAGE_TRANSITION_CHAIN_START |
content::PAGE_TRANSITION_CHAIN_END);
last_ids = AddPageVisit(request.url, request.time,
last_ids.second, t, request.visit_source);
if (!is_keyword_generated) {
UpdateSegments(request.url, from_visit_id, last_ids.second, t,
request.time);
UpdateVisitDuration(from_visit_id, request.time);
}
} else {
content::PageTransition redirect_info =
content::PAGE_TRANSITION_CHAIN_START;
RedirectList redirects = request.redirects;
if (redirects[0].SchemeIs(content::kAboutScheme)) {
redirects.erase(redirects.begin());
} else if (request_transition & content::PAGE_TRANSITION_CLIENT_REDIRECT) {
redirect_info = content::PAGE_TRANSITION_CLIENT_REDIRECT;
if (request.referrer.is_valid()) {
DCHECK(request.referrer == redirects[0]);
redirects.erase(redirects.begin());
VisitRow visit_row;
if (request.did_replace_entry &&
db_->GetRowForVisit(last_ids.second, &visit_row) &&
visit_row.transition & content::PAGE_TRANSITION_CHAIN_END) {
visit_row.transition = content::PageTransitionFromInt(
visit_row.transition & ~content::PAGE_TRANSITION_CHAIN_END);
db_->UpdateVisitRow(visit_row);
}
}
}
for (size_t redirect_index = 0; redirect_index < redirects.size();
redirect_index++) {
content::PageTransition t =
content::PageTransitionFromInt(stripped_transition | redirect_info);
if (redirect_index == (redirects.size() - 1)) {
t = content::PageTransitionFromInt(
t | content::PAGE_TRANSITION_CHAIN_END);
}
last_ids = AddPageVisit(redirects[redirect_index],
request.time, last_ids.second,
t, request.visit_source);
if (t & content::PAGE_TRANSITION_CHAIN_START) {
UpdateSegments(redirects[redirect_index],
from_visit_id, last_ids.second, t, request.time);
UpdateVisitDuration(from_visit_id, request.time);
}
redirect_info = content::PAGE_TRANSITION_SERVER_REDIRECT;
}
recent_redirects_.Put(request.url, redirects);
}
if (stripped_transition != content::PAGE_TRANSITION_AUTO_SUBFRAME &&
stripped_transition != content::PAGE_TRANSITION_MANUAL_SUBFRAME &&
!is_keyword_generated) {
tracker_.AddVisit(request.id_scope, request.page_id, request.url,
last_ids.second);
}
ScheduleCommit();
}
void HistoryBackend::InitImpl(const std::string& languages) {
DCHECK(!db_) << "Initializing HistoryBackend twice";
TimeTicks beginning_time = TimeTicks::Now();
base::FilePath history_name = history_dir_.Append(chrome::kHistoryFilename);
base::FilePath thumbnail_name = GetFaviconsFileName();
base::FilePath archived_name = GetArchivedFileName();
DeleteFTSIndexDatabases();
db_.reset(new HistoryDatabase());
db_->set_error_callback(
base::Bind(&HistoryBackend::DatabaseErrorCallback,
base::Unretained(this)));
sql::InitStatus status = db_->Init(history_name);
switch (status) {
case sql::INIT_OK:
break;
case sql::INIT_FAILURE: {
bool kill_db = scheduled_kill_db_;
if (kill_db)
KillHistoryDatabase();
UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError", kill_db);
delegate_->NotifyProfileError(status);
db_.reset();
return;
}
default:
NOTREACHED();
}
{
scoped_ptr<InMemoryHistoryBackend> mem_backend(new InMemoryHistoryBackend);
if (mem_backend->Init(history_name, db_.get()))
delegate_->SetInMemoryBackend(mem_backend.Pass());
}
db_->BeginExclusiveMode();
thumbnail_db_.reset(new ThumbnailDatabase());
if (thumbnail_db_->Init(thumbnail_name) != sql::INIT_OK) {
LOG(WARNING) << "Could not initialize the thumbnail database.";
thumbnail_db_.reset();
}
if (db_->needs_version_17_migration()) {
sql::Connection::Delete(archived_name);
}
archived_db_.reset(new ArchivedDatabase());
if (!archived_db_->Init(archived_name)) {
LOG(WARNING) << "Could not initialize the archived database.";
archived_db_.reset();
}
if (base::RandInt(1, 100) == 50) {
db_->ComputeDatabaseMetrics(history_name);
if (thumbnail_db_)
thumbnail_db_->ComputeDatabaseMetrics();
}
expirer_.SetDatabases(db_.get(), archived_db_.get(), thumbnail_db_.get());
db_->BeginTransaction();
if (thumbnail_db_)
thumbnail_db_->BeginTransaction();
if (archived_db_)
archived_db_->BeginTransaction();
db_->GetStartDate(&first_recorded_time_);
expirer_.StartArchivingOldStuff(TimeDelta::FromDays(kArchiveDaysThreshold));
#if defined(OS_ANDROID)
if (thumbnail_db_) {
android_provider_backend_.reset(new AndroidProviderBackend(
GetAndroidCacheFileName(), db_.get(), thumbnail_db_.get(),
bookmark_service_, delegate_.get()));
}
#endif
HISTOGRAM_TIMES("History.InitTime",
TimeTicks::Now() - beginning_time);
}
void HistoryBackend::OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
bool trim_aggressively = memory_pressure_level ==
base::MemoryPressureListener::MEMORY_PRESSURE_CRITICAL;
if (db_)
db_->TrimMemory(trim_aggressively);
if (thumbnail_db_)
thumbnail_db_->TrimMemory(trim_aggressively);
if (archived_db_)
archived_db_->TrimMemory(trim_aggressively);
}
void HistoryBackend::CloseAllDatabases() {
if (db_) {
db_->CommitTransaction();
db_.reset();
first_recorded_time_ = base::Time();
}
if (thumbnail_db_) {
thumbnail_db_->CommitTransaction();
thumbnail_db_.reset();
}
if (archived_db_) {
archived_db_->CommitTransaction();
archived_db_.reset();
}
}
std::pair<URLID, VisitID> HistoryBackend::AddPageVisit(
const GURL& url,
Time time,
VisitID referring_visit,
content::PageTransition transition,
VisitSource visit_source) {
bool new_hidden = !content::PageTransitionIsMainFrame(transition);
int typed_increment = 0;
content::PageTransition transition_type =
content::PageTransitionStripQualifier(transition);
if ((transition_type == content::PAGE_TRANSITION_TYPED &&
!content::PageTransitionIsRedirect(transition)) ||
transition_type == content::PAGE_TRANSITION_KEYWORD_GENERATED)
typed_increment = 1;
#if defined(OS_ANDROID)
if (visit_source == SOURCE_BROWSED &&
(transition & content::PAGE_TRANSITION_CHAIN_END) != 0) {
RecordTopPageVisitStats(url);
}
#endif
URLRow url_info(url);
URLID url_id = db_->GetRowForURL(url, &url_info);
if (url_id) {
if (content::PageTransitionStripQualifier(transition) !=
content::PAGE_TRANSITION_RELOAD)
url_info.set_visit_count(url_info.visit_count() + 1);
if (typed_increment)
url_info.set_typed_count(url_info.typed_count() + typed_increment);
if (url_info.last_visit() < time)
url_info.set_last_visit(time);
if (!new_hidden)
url_info.set_hidden(false);
db_->UpdateURLRow(url_id, url_info);
} else {
url_info.set_visit_count(1);
url_info.set_typed_count(typed_increment);
url_info.set_last_visit(time);
url_info.set_hidden(new_hidden);
url_id = db_->AddURL(url_info);
if (!url_id) {
NOTREACHED() << "Adding URL failed.";
return std::make_pair(0, 0);
}
url_info.id_ = url_id;
}
VisitRow visit_info(url_id, time, referring_visit, transition, 0);
VisitID visit_id = db_->AddVisit(&visit_info, visit_source);
NotifyVisitObservers(visit_info);
if (visit_info.visit_time < first_recorded_time_)
first_recorded_time_ = visit_info.visit_time;
if (visit_id) {
if (typed_url_syncable_service_.get())
typed_url_syncable_service_->OnUrlVisited(transition, &url_info);
scoped_ptr<URLVisitedDetails> details(new URLVisitedDetails);
details->transition = transition;
details->row = url_info;
BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URL_VISITED,
details.PassAs<HistoryDetails>());
} else {
VLOG(0) << "Failed to build visit insert statement: "
<< "url_id = " << url_id;
}
return std::make_pair(url_id, visit_id);
}
void HistoryBackend::AddPagesWithDetails(const URLRows& urls,
VisitSource visit_source) {
if (!db_)
return;
scoped_ptr<URLsModifiedDetails> modified(new URLsModifiedDetails);
for (URLRows::const_iterator i = urls.begin(); i != urls.end(); ++i) {
DCHECK(!i->last_visit().is_null());
URLDatabase* url_database;
VisitDatabase* visit_database;
if (IsExpiredVisitTime(i->last_visit())) {
if (!archived_db_)
return;
url_database = archived_db_.get();
visit_database = archived_db_.get();
} else {
url_database = db_.get();
visit_database = db_.get();
}
URLRow existing_url;
URLID url_id = url_database->GetRowForURL(i->url(), &existing_url);
if (!url_id) {
url_id = url_database->AddURL(*i);
if (!url_id) {
NOTREACHED() << "Could not add row to DB";
return;
}
if (i->typed_count() > 0) {
modified->changed_urls.push_back(*i);
modified->changed_urls.back().set_id(url_id);
}
}
if (visit_source != SOURCE_SYNCED) {
VisitRow visit_info(url_id, i->last_visit(), 0,
content::PageTransitionFromInt(
content::PAGE_TRANSITION_LINK |
content::PAGE_TRANSITION_CHAIN_START |
content::PAGE_TRANSITION_CHAIN_END), 0);
if (!visit_database->AddVisit(&visit_info, visit_source)) {
NOTREACHED() << "Adding visit failed.";
return;
}
NotifyVisitObservers(visit_info);
if (visit_info.visit_time < first_recorded_time_)
first_recorded_time_ = visit_info.visit_time;
}
}
if (typed_url_syncable_service_.get())
typed_url_syncable_service_->OnUrlsModified(&modified->changed_urls);
BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
modified.PassAs<HistoryDetails>());
ScheduleCommit();
}
bool HistoryBackend::IsExpiredVisitTime(const base::Time& time) {
return time < expirer_.GetCurrentArchiveTime();
}
void HistoryBackend::SetPageTitle(const GURL& url,
const base::string16& title) {
if (!db_)
return;
history::RedirectList dummy_list;
history::RedirectList* redirects;
RedirectCache::iterator iter = recent_redirects_.Get(url);
if (iter != recent_redirects_.end()) {
redirects = &iter->second;
DCHECK(!redirects->empty());
DCHECK(redirects->back() == url);
} else {
dummy_list.push_back(url);
redirects = &dummy_list;
}
scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails);
for (size_t i = 0; i < redirects->size(); i++) {
URLRow row;
URLID row_id = db_->GetRowForURL(redirects->at(i), &row);
if (row_id && row.title() != title) {
row.set_title(title);
db_->UpdateURLRow(row_id, row);
details->changed_urls.push_back(row);
}
}
if (!details->changed_urls.empty()) {
if (typed_url_syncable_service_.get())
typed_url_syncable_service_->OnUrlsModified(&details->changed_urls);
BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
details.PassAs<HistoryDetails>());
ScheduleCommit();
}
}
void HistoryBackend::AddPageNoVisitForBookmark(const GURL& url,
const base::string16& title) {
if (!db_)
return;
URLRow url_info(url);
URLID url_id = db_->GetRowForURL(url, &url_info);
if (url_id) {
return;
}
if (!title.empty()) {
url_info.set_title(title);
} else {
url_info.set_title(base::UTF8ToUTF16(url.spec()));
}
url_info.set_last_visit(Time::Now());
url_info.set_hidden(true);
db_->AddURL(url_info);
}
void HistoryBackend::IterateURLs(
const scoped_refptr<visitedlink::VisitedLinkDelegate::URLEnumerator>&
iterator) {
if (db_) {
HistoryDatabase::URLEnumerator e;
if (db_->InitURLEnumeratorForEverything(&e)) {
URLRow info;
while (e.GetNextURL(&info)) {
iterator->OnURL(info.url());
}
iterator->OnComplete(true);
return;
}
}
iterator->OnComplete(false);
}
bool HistoryBackend::GetAllTypedURLs(URLRows* urls) {
if (db_)
return db_->GetAllTypedUrls(urls);
return false;
}
bool HistoryBackend::GetVisitsForURL(URLID id, VisitVector* visits) {
if (db_)
return db_->GetVisitsForURL(id, visits);
return false;
}
bool HistoryBackend::GetMostRecentVisitsForURL(URLID id,
int max_visits,
VisitVector* visits) {
if (db_)
return db_->GetMostRecentVisitsForURL(id, max_visits, visits);
return false;
}
bool HistoryBackend::UpdateURL(URLID id, const history::URLRow& url) {
if (db_)
return db_->UpdateURLRow(id, url);
return false;
}
bool HistoryBackend::AddVisits(const GURL& url,
const std::vector<VisitInfo>& visits,
VisitSource visit_source) {
if (db_) {
for (std::vector<VisitInfo>::const_iterator visit = visits.begin();
visit != visits.end(); ++visit) {
if (!AddPageVisit(
url, visit->first, 0, visit->second, visit_source).first) {
return false;
}
}
ScheduleCommit();
return true;
}
return false;
}
bool HistoryBackend::RemoveVisits(const VisitVector& visits) {
if (!db_)
return false;
expirer_.ExpireVisits(visits);
ScheduleCommit();
return true;
}
bool HistoryBackend::GetVisitsSource(const VisitVector& visits,
VisitSourceMap* sources) {
if (!db_)
return false;
db_->GetVisitsSource(visits, sources);
return true;
}
bool HistoryBackend::GetURL(const GURL& url, history::URLRow* url_row) {
if (db_)
return db_->GetRowForURL(url, url_row) != 0;
return false;
}
void HistoryBackend::QueryURL(scoped_refptr<QueryURLRequest> request,
const GURL& url,
bool want_visits) {
if (request->canceled())
return;
bool success = false;
URLRow* row = &request->value.a;
VisitVector* visits = &request->value.b;
if (db_) {
if (db_->GetRowForURL(url, row)) {
success = true;
if (want_visits)
db_->GetVisitsForURL(row->id(), visits);
}
}
request->ForwardResult(request->handle(), success, row, visits);
}
TypedUrlSyncableService* HistoryBackend::GetTypedUrlSyncableService() const {
return typed_url_syncable_service_.get();
}
void HistoryBackend::DeleteOldSegmentData() {
if (db_)
db_->DeleteSegmentData(Time::Now() -
TimeDelta::FromDays(kSegmentDataRetention));
}
void HistoryBackend::QuerySegmentUsage(
scoped_refptr<QuerySegmentUsageRequest> request,
const Time from_time,
int max_result_count) {
if (request->canceled())
return;
if (db_) {
db_->QuerySegmentUsage(from_time, max_result_count, &request->value.get());
if (!segment_queried_) {
segment_queried_ = true;
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&HistoryBackend::DeleteOldSegmentData, this));
}
}
request->ForwardResult(request->handle(), &request->value.get());
}
void HistoryBackend::SetKeywordSearchTermsForURL(const GURL& url,
TemplateURLID keyword_id,
const base::string16& term) {
if (!db_)
return;
URLID url_id = db_->GetRowForURL(url, NULL);
if (!url_id) {
return;
}
db_->SetKeywordSearchTermsForURL(url_id, keyword_id, term);
BroadcastNotifications(
chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED,
scoped_ptr<HistoryDetails>(
new KeywordSearchUpdatedDetails(url, keyword_id, term)));
ScheduleCommit();
}
void HistoryBackend::DeleteAllSearchTermsForKeyword(
TemplateURLID keyword_id) {
if (!db_)
return;
db_->DeleteAllSearchTermsForKeyword(keyword_id);
ScheduleCommit();
}
void HistoryBackend::GetMostRecentKeywordSearchTerms(
scoped_refptr<GetMostRecentKeywordSearchTermsRequest> request,
TemplateURLID keyword_id,
const base::string16& prefix,
int max_count) {
if (request->canceled())
return;
if (db_) {
db_->GetMostRecentKeywordSearchTerms(keyword_id, prefix, max_count,
&(request->value));
}
request->ForwardResult(request->handle(), &request->value);
}
void HistoryBackend::DeleteKeywordSearchTermForURL(const GURL& url) {
if (!db_)
return;
URLID url_id = db_->GetRowForURL(url, NULL);
if (!url_id)
return;
db_->DeleteKeywordSearchTermForURL(url_id);
BroadcastNotifications(
chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_DELETED,
scoped_ptr<HistoryDetails>(new KeywordSearchDeletedDetails(url)));
ScheduleCommit();
}
void HistoryBackend::DeleteMatchingURLsForKeyword(TemplateURLID keyword_id,
const base::string16& term) {
if (!db_)
return;
std::vector<KeywordSearchTermRow> rows;
if (db_->GetKeywordSearchTermRows(term, &rows)) {
std::vector<GURL> items_to_delete;
URLRow row;
for (std::vector<KeywordSearchTermRow>::iterator it = rows.begin();
it != rows.end(); ++it) {
if ((it->keyword_id == keyword_id) && db_->GetURLRow(it->url_id, &row))
items_to_delete.push_back(row.url());
}
DeleteURLs(items_to_delete);
}
}
uint32 HistoryBackend::GetNextDownloadId() {
return db_ ? db_->GetNextDownloadId() : content::DownloadItem::kInvalidId;
}
void HistoryBackend::QueryDownloads(std::vector<DownloadRow>* rows) {
if (db_)
db_->QueryDownloads(rows);
}
void HistoryBackend::UpdateDownload(const history::DownloadRow& data) {
if (!db_)
return;
db_->UpdateDownload(data);
ScheduleCommit();
}
bool HistoryBackend::CreateDownload(const history::DownloadRow& history_info) {
if (!db_)
return false;
bool success = db_->CreateDownload(history_info);
ScheduleCommit();
return success;
}
void HistoryBackend::RemoveDownloads(const std::set<uint32>& ids) {
if (!db_)
return;
size_t downloads_count_before = db_->CountDownloads();
base::TimeTicks started_removing = base::TimeTicks::Now();
for (std::set<uint32>::const_iterator it = ids.begin();
it != ids.end(); ++it) {
db_->RemoveDownload(*it);
}
ScheduleCommit();
base::TimeTicks finished_removing = base::TimeTicks::Now();
size_t downloads_count_after = db_->CountDownloads();
DCHECK_LE(downloads_count_after, downloads_count_before);
if (downloads_count_after > downloads_count_before)
return;
size_t num_downloads_deleted = downloads_count_before - downloads_count_after;
UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount",
num_downloads_deleted);
base::TimeDelta micros = (1000 * (finished_removing - started_removing));
UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros);
if (num_downloads_deleted > 0) {
UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord",
(1000 * micros) / num_downloads_deleted);
}
DCHECK_GE(ids.size(), num_downloads_deleted);
if (ids.size() < num_downloads_deleted)
return;
UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved",
ids.size() - num_downloads_deleted);
}
void HistoryBackend::QueryHistory(scoped_refptr<QueryHistoryRequest> request,
const base::string16& text_query,
const QueryOptions& options) {
if (request->canceled())
return;
TimeTicks beginning_time = TimeTicks::Now();
if (db_) {
if (text_query.empty()) {
QueryHistoryBasic(db_.get(), db_.get(), options, &request->value);
} else {
QueryHistoryText(db_.get(), db_.get(), text_query, options,
&request->value);
if (archived_db_.get() &&
expirer_.GetCurrentArchiveTime() >= options.begin_time) {
QueryHistoryText(archived_db_.get(), archived_db_.get(), text_query,
options, &request->value);
}
}
}
request->ForwardResult(request->handle(), &request->value);
UMA_HISTOGRAM_TIMES("History.QueryHistory",
TimeTicks::Now() - beginning_time);
}
void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db,
VisitDatabase* visit_db,
const QueryOptions& options,
QueryResults* result) {
VisitVector visits;
bool has_more_results = visit_db->GetVisibleVisitsInRange(options, &visits);
DCHECK(static_cast<int>(visits.size()) <= options.EffectiveMaxCount());
URLResult url_result;
for (size_t i = 0; i < visits.size(); i++) {
const VisitRow visit = visits[i];
if (!url_db->GetURLRow(visit.url_id, &url_result)) {
VLOG(0) << "Failed to get id " << visit.url_id
<< " from history.urls.";
continue;
}
if (!url_result.url().is_valid()) {
VLOG(0) << "Got invalid URL from history.urls with id "
<< visit.url_id << ": "
<< url_result.url().possibly_invalid_spec();
continue;
}
if (url_db == db_.get()) {
db_->GetRowForURL(url_result.url(), &url_result);
}
url_result.set_visit_time(visit.visit_time);
url_result.set_blocked_visit(
(visit.transition & content::PAGE_TRANSITION_BLOCKED) != 0);
result->AppendURLBySwapping(&url_result);
}
if (!has_more_results && options.begin_time <= first_recorded_time_)
result->set_reached_beginning(true);
}
void HistoryBackend::QueryHistoryText(URLDatabase* url_db,
VisitDatabase* visit_db,
const base::string16& text_query,
const QueryOptions& options,
QueryResults* result) {
URLRows text_matches;
url_db->GetTextMatches(text_query, &text_matches);
std::vector<URLResult> matching_visits;
VisitVector visits;
for (size_t i = 0; i < text_matches.size(); i++) {
const URLRow& text_match = text_matches[i];
visit_db->GetVisibleVisitsForURL(text_match.id(), options, &visits);
for (size_t j = 0; j < visits.size(); j++) {
URLResult url_result(text_match);
url_result.set_visit_time(visits[j].visit_time);
matching_visits.push_back(url_result);
}
}
std::sort(matching_visits.begin(), matching_visits.end(),
URLResult::CompareVisitTime);
size_t max_results = options.max_count == 0 ?
std::numeric_limits<size_t>::max() : static_cast<int>(options.max_count);
for (std::vector<URLResult>::iterator it = matching_visits.begin();
it != matching_visits.end() && result->size() < max_results; ++it) {
result->AppendURLBySwapping(&(*it));
}
if (matching_visits.size() == result->size() &&
options.begin_time <= first_recorded_time_)
result->set_reached_beginning(true);
}
void HistoryBackend::QueryRedirectsFrom(
scoped_refptr<QueryRedirectsRequest> request,
const GURL& url) {
if (request->canceled())
return;
bool success = GetMostRecentRedirectsFrom(url, &request->value);
request->ForwardResult(request->handle(), url, success, &request->value);
}
void HistoryBackend::QueryRedirectsTo(
scoped_refptr<QueryRedirectsRequest> request,
const GURL& url) {
if (request->canceled())
return;
bool success = GetMostRecentRedirectsTo(url, &request->value);
request->ForwardResult(request->handle(), url, success, &request->value);
}
void HistoryBackend::GetVisibleVisitCountToHost(
scoped_refptr<GetVisibleVisitCountToHostRequest> request,
const GURL& url) {
if (request->canceled())
return;
int count = 0;
Time first_visit;
const bool success = db_.get() &&
db_->GetVisibleVisitCountToHost(url, &count, &first_visit);
request->ForwardResult(request->handle(), success, count, first_visit);
}
void HistoryBackend::QueryTopURLsAndRedirects(
scoped_refptr<QueryTopURLsAndRedirectsRequest> request,
int result_count) {
if (request->canceled())
return;
if (!db_) {
request->ForwardResult(request->handle(), false, NULL, NULL);
return;
}
std::vector<GURL>* top_urls = &request->value.a;
history::RedirectMap* redirects = &request->value.b;
ScopedVector<PageUsageData> data;
db_->QuerySegmentUsage(base::Time::Now() - base::TimeDelta::FromDays(90),
result_count, &data.get());
for (size_t i = 0; i < data.size(); ++i) {
top_urls->push_back(data[i]->GetURL());
RefCountedVector<GURL>* list = new RefCountedVector<GURL>;
GetMostRecentRedirectsFrom(top_urls->back(), &list->data);
(*redirects)[top_urls->back()] = list;
}
request->ForwardResult(request->handle(), true, top_urls, redirects);
}
void HistoryBackend::QueryMostVisitedURLs(
scoped_refptr<QueryMostVisitedURLsRequest> request,
int result_count,
int days_back) {
if (request->canceled())
return;
if (!db_) {
request->ForwardResult(request->handle(), MostVisitedURLList());
return;
}
MostVisitedURLList* result = &request->value;
QueryMostVisitedURLsImpl(result_count, days_back, result);
request->ForwardResult(request->handle(), *result);
}
void HistoryBackend::QueryFilteredURLs(
scoped_refptr<QueryFilteredURLsRequest> request,
int result_count,
const history::VisitFilter& filter,
bool extended_info) {
if (request->canceled())
return;
base::Time request_start = base::Time::Now();
if (!db_) {
request->ForwardResult(request->handle(), FilteredURLList());
return;
}
VisitVector visits;
db_->GetDirectVisitsDuringTimes(filter, 0, &visits);
std::map<URLID, double> score_map;
for (size_t i = 0; i < visits.size(); ++i) {
score_map[visits[i].url_id] += filter.GetVisitScore(visits[i]);
}
ScopedVector<PageUsageData> data;
data.reserve(score_map.size());
for (std::map<URLID, double>::iterator it = score_map.begin();
it != score_map.end(); ++it) {
PageUsageData* pud = new PageUsageData(it->first);
pud->SetScore(it->second);
data.push_back(pud);
}
std::sort(data.begin(), data.end(), PageUsageData::Predicate);
if (result_count && implicit_cast<int>(data.size()) > result_count)
data.resize(result_count);
for (size_t i = 0; i < data.size(); ++i) {
URLRow info;
if (db_->GetURLRow(data[i]->GetID(), &info)) {
data[i]->SetURL(info.url());
data[i]->SetTitle(info.title());
}
}
FilteredURLList& result = request->value;
for (size_t i = 0; i < data.size(); ++i) {
PageUsageData* current_data = data[i];
FilteredURL url(*current_data);
if (extended_info) {
VisitVector visits;
db_->GetVisitsForURL(current_data->GetID(), &visits);
if (visits.size() > 0) {
url.extended_info.total_visits = visits.size();
for (size_t i = 0; i < visits.size(); ++i) {
url.extended_info.duration_opened +=
visits[i].visit_duration.InSeconds();
if (visits[i].visit_time > url.extended_info.last_visit_time) {
url.extended_info.last_visit_time = visits[i].visit_time;
}
}
}
}
result.push_back(url);
}
int delta_time = std::max(1, std::min(999,
static_cast<int>((base::Time::Now() - request_start).InMilliseconds())));
STATIC_HISTOGRAM_POINTER_BLOCK(
"NewTabPage.SuggestedSitesLoadTime",
Add(delta_time),
base::LinearHistogram::FactoryGet("NewTabPage.SuggestedSitesLoadTime",
1, 1000, 100, base::Histogram::kUmaTargetedHistogramFlag));
request->ForwardResult(request->handle(), result);
}
void HistoryBackend::QueryMostVisitedURLsImpl(int result_count,
int days_back,
MostVisitedURLList* result) {
if (!db_)
return;
ScopedVector<PageUsageData> data;
db_->QuerySegmentUsage(base::Time::Now() -
base::TimeDelta::FromDays(days_back),
result_count, &data.get());
for (size_t i = 0; i < data.size(); ++i) {
PageUsageData* current_data = data[i];
RedirectList redirects;
GetMostRecentRedirectsFrom(current_data->GetURL(), &redirects);
MostVisitedURL url = MakeMostVisitedURL(*current_data, redirects);
result->push_back(url);
}
}
void HistoryBackend::GetRedirectsFromSpecificVisit(
VisitID cur_visit, history::RedirectList* redirects) {
GURL cur_url;
std::set<VisitID> visit_set;
visit_set.insert(cur_visit);
while (db_->GetRedirectFromVisit(cur_visit, &cur_visit, &cur_url)) {
if (visit_set.find(cur_visit) != visit_set.end()) {
NOTREACHED() << "Loop in visit chain, giving up";
return;
}
visit_set.insert(cur_visit);
redirects->push_back(cur_url);
}
}
void HistoryBackend::GetRedirectsToSpecificVisit(
VisitID cur_visit,
history::RedirectList* redirects) {
if (!db_)
return;
GURL cur_url;
std::set<VisitID> visit_set;
visit_set.insert(cur_visit);
while (db_->GetRedirectToVisit(cur_visit, &cur_visit, &cur_url)) {
if (visit_set.find(cur_visit) != visit_set.end()) {
NOTREACHED() << "Loop in visit chain, giving up";
return;
}
visit_set.insert(cur_visit);
redirects->push_back(cur_url);
}
}
bool HistoryBackend::GetMostRecentRedirectsFrom(
const GURL& from_url,
history::RedirectList* redirects) {
redirects->clear();
if (!db_)
return false;
URLID from_url_id = db_->GetRowForURL(from_url, NULL);
VisitID cur_visit = db_->GetMostRecentVisitForURL(from_url_id, NULL);
if (!cur_visit)
return false;
GetRedirectsFromSpecificVisit(cur_visit, redirects);
return true;
}
bool HistoryBackend::GetMostRecentRedirectsTo(
const GURL& to_url,
history::RedirectList* redirects) {
redirects->clear();
if (!db_)
return false;
URLID to_url_id = db_->GetRowForURL(to_url, NULL);
VisitID cur_visit = db_->GetMostRecentVisitForURL(to_url_id, NULL);
if (!cur_visit)
return false;
GetRedirectsToSpecificVisit(cur_visit, redirects);
return true;
}
void HistoryBackend::ScheduleAutocomplete(HistoryURLProvider* provider,
HistoryURLProviderParams* params) {
provider->ExecuteWithDB(this, db_.get(), params);
}
void HistoryBackend::DeleteFTSIndexDatabases() {
base::FilePath::StringType filepattern =
FILE_PATH_LITERAL("History Index *");
base::FileEnumerator enumerator(
history_dir_, false, base::FileEnumerator::FILES, filepattern);
int num_databases_deleted = 0;
base::FilePath current_file;
while (!(current_file = enumerator.Next()).empty()) {
if (sql::Connection::Delete(current_file))
num_databases_deleted++;
}
UMA_HISTOGRAM_COUNTS("History.DeleteFTSIndexDatabases",
num_databases_deleted);
}
void HistoryBackend::GetFavicons(
const std::vector<GURL>& icon_urls,
int icon_types,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
UpdateFaviconMappingsAndFetchImpl(NULL, icon_urls, icon_types,
desired_size_in_dip, desired_scale_factors,
bitmap_results);
}
void HistoryBackend::GetLargestFaviconForURL(
const GURL& page_url,
const std::vector<int>& icon_types,
int minimum_size_in_pixels,
chrome::FaviconBitmapResult* favicon_bitmap_result) {
DCHECK(favicon_bitmap_result);
if (!db_ || !thumbnail_db_)
return;
TimeTicks beginning_time = TimeTicks::Now();
std::vector<IconMapping> icon_mappings;
if (!thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings) ||
icon_mappings.empty())
return;
int required_icon_types = 0;
for (std::vector<int>::const_iterator i = icon_types.begin();
i != icon_types.end(); ++i) {
required_icon_types |= *i;
}
std::map<chrome::IconType, FaviconBitmap> largest_favicon_bitmaps;
for (std::vector<IconMapping>::const_iterator i = icon_mappings.begin();
i != icon_mappings.end(); ++i) {
if (!(i->icon_type & required_icon_types))
continue;
std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
thumbnail_db_->GetFaviconBitmapIDSizes(i->icon_id, &bitmap_id_sizes);
FaviconBitmap& largest = largest_favicon_bitmaps[i->icon_type];
for (std::vector<FaviconBitmapIDSize>::const_iterator j =
bitmap_id_sizes.begin(); j != bitmap_id_sizes.end(); ++j) {
if (largest.bitmap_id == 0 ||
(largest.pixel_size.width() < j->pixel_size.width() &&
largest.pixel_size.height() < j->pixel_size.height())) {
largest.icon_id = i->icon_id;
largest.bitmap_id = j->bitmap_id;
largest.pixel_size = j->pixel_size;
}
}
}
if (largest_favicon_bitmaps.empty())
return;
FaviconBitmap largest_icon;
for (std::vector<int>::const_iterator t = icon_types.begin();
t != icon_types.end(); ++t) {
for (std::map<chrome::IconType, FaviconBitmap>::const_iterator f =
largest_favicon_bitmaps.begin(); f != largest_favicon_bitmaps.end();
++f) {
if (f->first & *t &&
(largest_icon.bitmap_id == 0 ||
(largest_icon.pixel_size.height() < f->second.pixel_size.height() &&
largest_icon.pixel_size.width() < f->second.pixel_size.width()))) {
largest_icon = f->second;
}
}
if (largest_icon.pixel_size.width() > minimum_size_in_pixels &&
largest_icon.pixel_size.height() > minimum_size_in_pixels)
break;
}
GURL icon_url;
chrome::IconType icon_type;
if (!thumbnail_db_->GetFaviconHeader(largest_icon.icon_id, &icon_url,
&icon_type)) {
return;
}
base::Time last_updated;
chrome::FaviconBitmapResult bitmap_result;
bitmap_result.icon_url = icon_url;
bitmap_result.icon_type = icon_type;
if (!thumbnail_db_->GetFaviconBitmap(largest_icon.bitmap_id,
&last_updated,
&bitmap_result.bitmap_data,
&bitmap_result.pixel_size)) {
return;
}
bitmap_result.expired = (Time::Now() - last_updated) >
TimeDelta::FromDays(kFaviconRefetchDays);
if (bitmap_result.is_valid())
*favicon_bitmap_result = bitmap_result;
HISTOGRAM_TIMES("History.GetLargestFaviconForURL",
TimeTicks::Now() - beginning_time);
}
void HistoryBackend::GetFaviconsForURL(
const GURL& page_url,
int icon_types,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
DCHECK(bitmap_results);
GetFaviconsFromDB(page_url, icon_types, desired_size_in_dip,
desired_scale_factors, bitmap_results);
}
void HistoryBackend::GetFaviconForID(
chrome::FaviconID favicon_id,
int desired_size_in_dip,
ui::ScaleFactor desired_scale_factor,
std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
std::vector<chrome::FaviconID> favicon_ids;
favicon_ids.push_back(favicon_id);
std::vector<ui::ScaleFactor> desired_scale_factors;
desired_scale_factors.push_back(desired_scale_factor);
GetFaviconBitmapResultsForBestMatch(favicon_ids,
desired_size_in_dip,
desired_scale_factors,
bitmap_results);
}
void HistoryBackend::UpdateFaviconMappingsAndFetch(
const GURL& page_url,
const std::vector<GURL>& icon_urls,
int icon_types,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
UpdateFaviconMappingsAndFetchImpl(&page_url, icon_urls, icon_types,
desired_size_in_dip, desired_scale_factors,
bitmap_results);
}
void HistoryBackend::MergeFavicon(
const GURL& page_url,
const GURL& icon_url,
chrome::IconType icon_type,
scoped_refptr<base::RefCountedMemory> bitmap_data,
const gfx::Size& pixel_size) {
if (!thumbnail_db_ || !db_)
return;
chrome::FaviconID favicon_id =
thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL);
if (!favicon_id) {
favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
}
std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes);
bool bitmap_identical = false;
bool replaced_bitmap = false;
for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
if (bitmap_id_sizes[i].pixel_size == pixel_size) {
if (IsFaviconBitmapDataEqual(bitmap_id_sizes[i].bitmap_id, bitmap_data)) {
thumbnail_db_->SetFaviconBitmapLastUpdateTime(
bitmap_id_sizes[i].bitmap_id, base::Time::Now());
bitmap_identical = true;
} else {
thumbnail_db_->SetFaviconBitmap(bitmap_id_sizes[i].bitmap_id,
bitmap_data, base::Time::Now());
replaced_bitmap = true;
}
break;
}
}
std::vector<gfx::Size> favicon_sizes;
for (size_t i = 0; i < bitmap_id_sizes.size(); ++i)
favicon_sizes.push_back(bitmap_id_sizes[i].pixel_size);
if (!replaced_bitmap && !bitmap_identical) {
thumbnail_db_->SetFaviconOutOfDate(favicon_id);
if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) {
thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id);
favicon_sizes.erase(favicon_sizes.begin());
}
thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data, base::Time::Now(),
pixel_size);
favicon_sizes.push_back(pixel_size);
}
std::vector<IconMapping> icon_mappings;
thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings);
for (size_t i = 0; i < icon_mappings.size(); ++i) {
if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
break;
if (icon_mappings[i].icon_url == icon_url)
continue;
std::vector<FaviconBitmap> bitmaps_to_copy;
thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id,
&bitmaps_to_copy);
for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) {
std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(),
favicon_sizes.end(), bitmaps_to_copy[j].pixel_size);
if (it != favicon_sizes.end())
continue;
thumbnail_db_->AddFaviconBitmap(favicon_id,
bitmaps_to_copy[j].bitmap_data, base::Time(),
bitmaps_to_copy[j].pixel_size);
favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size);
if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
break;
}
}
bool mapping_changed = false;
if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) {
std::vector<chrome::FaviconID> favicon_ids;
favicon_ids.push_back(favicon_id);
SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids);
mapping_changed = true;
}
if (mapping_changed || !bitmap_identical)
SendFaviconChangedNotificationForPageAndRedirects(page_url);
ScheduleCommit();
}
void HistoryBackend::SetFavicons(
const GURL& page_url,
chrome::IconType icon_type,
const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data) {
if (!thumbnail_db_ || !db_)
return;
DCHECK(ValidateSetFaviconsParams(favicon_bitmap_data));
typedef std::map<GURL, std::vector<chrome::FaviconBitmapData> >
BitmapDataByIconURL;
BitmapDataByIconURL grouped_by_icon_url;
for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
const GURL& icon_url = favicon_bitmap_data[i].icon_url;
grouped_by_icon_url[icon_url].push_back(favicon_bitmap_data[i]);
}
bool data_modified = false;
std::vector<chrome::FaviconID> icon_ids;
for (BitmapDataByIconURL::const_iterator it = grouped_by_icon_url.begin();
it != grouped_by_icon_url.end(); ++it) {
const GURL& icon_url = it->first;
chrome::FaviconID icon_id =
thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL);
if (!icon_id) {
icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
data_modified = true;
}
icon_ids.push_back(icon_id);
if (!data_modified)
SetFaviconBitmaps(icon_id, it->second, &data_modified);
else
SetFaviconBitmaps(icon_id, it->second, NULL);
}
data_modified |=
SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids);
if (data_modified) {
SendFaviconChangedNotificationForPageAndRedirects(page_url);
}
ScheduleCommit();
}
void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) {
std::vector<IconMapping> icon_mappings;
if (!thumbnail_db_ ||
!thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings))
return;
for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
m != icon_mappings.end(); ++m) {
thumbnail_db_->SetFaviconOutOfDate(m->icon_id);
}
ScheduleCommit();
}
void HistoryBackend::CloneFavicons(const GURL& old_page_url,
const GURL& new_page_url) {
if (!thumbnail_db_)
return;
if (old_page_url.GetOrigin() != new_page_url.GetOrigin())
return;
thumbnail_db_->CloneIconMappings(old_page_url, new_page_url);
ScheduleCommit();
}
void HistoryBackend::SetImportedFavicons(
const std::vector<ImportedFaviconUsage>& favicon_usage) {
if (!db_ || !thumbnail_db_)
return;
Time now = Time::Now();
std::set<GURL> favicons_changed;
for (size_t i = 0; i < favicon_usage.size(); i++) {
chrome::FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_usage[i].favicon_url, chrome::FAVICON, NULL);
if (!favicon_id) {
favicon_id = thumbnail_db_->AddFavicon(
favicon_usage[i].favicon_url,
chrome::FAVICON,
new base::RefCountedBytes(favicon_usage[i].png_data),
now,
gfx::Size());
}
BookmarkService* bookmark_service = GetBookmarkService();
for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin();
url != favicon_usage[i].urls.end(); ++url) {
URLRow url_row;
if (!db_->GetRowForURL(*url, &url_row)) {
if (bookmark_service && bookmark_service_->IsBookmarked(*url)) {
URLRow url_info(*url);
url_info.set_visit_count(0);
url_info.set_typed_count(0);
url_info.set_last_visit(base::Time());
url_info.set_hidden(false);
db_->AddURL(url_info);
thumbnail_db_->AddIconMapping(*url, favicon_id);
favicons_changed.insert(*url);
}
} else {
if (!thumbnail_db_->GetIconMappingsForPageURL(
*url, chrome::FAVICON, NULL)) {
thumbnail_db_->AddIconMapping(*url, favicon_id);
favicons_changed.insert(*url);
}
}
}
}
if (!favicons_changed.empty()) {
scoped_ptr<FaviconChangedDetails> changed_details(
new FaviconChangedDetails);
changed_details->urls.swap(favicons_changed);
BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
changed_details.PassAs<HistoryDetails>());
}
}
void HistoryBackend::UpdateFaviconMappingsAndFetchImpl(
const GURL* page_url,
const std::vector<GURL>& icon_urls,
int icon_types,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
DCHECK(!page_url ||
icon_types == chrome::FAVICON ||
icon_types == chrome::TOUCH_ICON ||
icon_types == chrome::TOUCH_PRECOMPOSED_ICON ||
icon_types == (chrome::TOUCH_ICON | chrome::TOUCH_PRECOMPOSED_ICON));
bitmap_results->clear();
if (!thumbnail_db_) {
return;
}
std::vector<chrome::FaviconID> favicon_ids;
chrome::IconType selected_icon_type = chrome::INVALID_ICON;
for (size_t i = 0; i < icon_urls.size(); ++i) {
const GURL& icon_url = icon_urls[i];
chrome::IconType icon_type_out;
const chrome::FaviconID favicon_id =
thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, icon_types, &icon_type_out);
if (favicon_id) {
if (icon_type_out > selected_icon_type) {
selected_icon_type = icon_type_out;
favicon_ids.clear();
}
if (icon_type_out == selected_icon_type)
favicon_ids.push_back(favicon_id);
}
}
if (page_url && !favicon_ids.empty()) {
bool mappings_updated =
SetFaviconMappingsForPageAndRedirects(*page_url, selected_icon_type,
favicon_ids);
if (mappings_updated) {
SendFaviconChangedNotificationForPageAndRedirects(*page_url);
ScheduleCommit();
}
}
GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_size_in_dip,
desired_scale_factors, bitmap_results);
}
void HistoryBackend::SetFaviconBitmaps(
chrome::FaviconID icon_id,
const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data,
bool* favicon_bitmaps_changed) {
if (favicon_bitmaps_changed)
*favicon_bitmaps_changed = false;
std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes);
std::vector<chrome::FaviconBitmapData> to_add = favicon_bitmap_data;
for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size;
std::vector<chrome::FaviconBitmapData>::iterator match_it = to_add.end();
for (std::vector<chrome::FaviconBitmapData>::iterator it = to_add.begin();
it != to_add.end(); ++it) {
if (it->pixel_size == pixel_size) {
match_it = it;
break;
}
}
FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id;
if (match_it == to_add.end()) {
thumbnail_db_->DeleteFaviconBitmap(bitmap_id);
if (favicon_bitmaps_changed)
*favicon_bitmaps_changed = true;
} else {
if (favicon_bitmaps_changed &&
!*favicon_bitmaps_changed &&
IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) {
thumbnail_db_->SetFaviconBitmapLastUpdateTime(
bitmap_id, base::Time::Now());
} else {
thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data,
base::Time::Now());
if (favicon_bitmaps_changed)
*favicon_bitmaps_changed = true;
}
to_add.erase(match_it);
}
}
for (size_t i = 0; i < to_add.size(); ++i) {
thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data,
base::Time::Now(), to_add[i].pixel_size);
if (favicon_bitmaps_changed)
*favicon_bitmaps_changed = true;
}
}
bool HistoryBackend::ValidateSetFaviconsParams(
const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data) const {
typedef std::map<GURL, size_t> BitmapsPerIconURL;
BitmapsPerIconURL num_bitmaps_per_icon_url;
for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
if (!favicon_bitmap_data[i].bitmap_data.get())
return false;
const GURL& icon_url = favicon_bitmap_data[i].icon_url;
if (!num_bitmaps_per_icon_url.count(icon_url))
num_bitmaps_per_icon_url[icon_url] = 1u;
else
++num_bitmaps_per_icon_url[icon_url];
}
if (num_bitmaps_per_icon_url.size() > kMaxFaviconsPerPage)
return false;
for (BitmapsPerIconURL::const_iterator it = num_bitmaps_per_icon_url.begin();
it != num_bitmaps_per_icon_url.end(); ++it) {
if (it->second > kMaxFaviconBitmapsPerIconURL)
return false;
}
return true;
}
bool HistoryBackend::IsFaviconBitmapDataEqual(
FaviconBitmapID bitmap_id,
const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) {
if (!new_bitmap_data.get())
return false;
scoped_refptr<base::RefCountedMemory> original_bitmap_data;
thumbnail_db_->GetFaviconBitmap(bitmap_id,
NULL,
&original_bitmap_data,
NULL);
return new_bitmap_data->Equals(original_bitmap_data);
}
bool HistoryBackend::GetFaviconsFromDB(
const GURL& page_url,
int icon_types,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* favicon_bitmap_results) {
DCHECK(favicon_bitmap_results);
favicon_bitmap_results->clear();
if (!db_ || !thumbnail_db_)
return false;
TimeTicks beginning_time = TimeTicks::Now();
std::vector<IconMapping> icon_mappings;
thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_types,
&icon_mappings);
std::vector<chrome::FaviconID> favicon_ids;
for (size_t i = 0; i < icon_mappings.size(); ++i)
favicon_ids.push_back(icon_mappings[i].icon_id);
bool success = GetFaviconBitmapResultsForBestMatch(favicon_ids,
desired_size_in_dip, desired_scale_factors, favicon_bitmap_results);
UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB",
TimeTicks::Now() - beginning_time);
return success && !favicon_bitmap_results->empty();
}
bool HistoryBackend::GetFaviconBitmapResultsForBestMatch(
const std::vector<chrome::FaviconID>& candidate_favicon_ids,
int desired_size_in_dip,
const std::vector<ui::ScaleFactor>& desired_scale_factors,
std::vector<chrome::FaviconBitmapResult>* favicon_bitmap_results) {
favicon_bitmap_results->clear();
if (candidate_favicon_ids.empty())
return true;
chrome::FaviconID best_favicon_id = 0;
std::vector<FaviconBitmapID> best_bitmap_ids;
float highest_score = kSelectFaviconFramesInvalidScore;
for (size_t i = 0; i < candidate_favicon_ids.size(); ++i) {
std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
thumbnail_db_->GetFaviconBitmapIDSizes(candidate_favicon_ids[i],
&bitmap_id_sizes);
std::vector<gfx::Size> sizes;
for (size_t j = 0; j < bitmap_id_sizes.size(); ++j)
sizes.push_back(bitmap_id_sizes[j].pixel_size);
std::vector<size_t> candidate_bitmap_indices;
float score = 0;
SelectFaviconFrameIndices(sizes,
desired_scale_factors,
desired_size_in_dip,
&candidate_bitmap_indices,
&score);
if (score > highest_score) {
highest_score = score;
best_favicon_id = candidate_favicon_ids[i],
best_bitmap_ids.clear();
for (size_t j = 0; j < candidate_bitmap_indices.size(); ++j) {
size_t candidate_index = candidate_bitmap_indices[j];
best_bitmap_ids.push_back(
bitmap_id_sizes[candidate_index].bitmap_id);
}
}
}
GURL icon_url;
chrome::IconType icon_type;
if (!thumbnail_db_->GetFaviconHeader(best_favicon_id, &icon_url,
&icon_type)) {
return false;
}
for (size_t i = 0; i < best_bitmap_ids.size(); ++i) {
base::Time last_updated;
chrome::FaviconBitmapResult bitmap_result;
bitmap_result.icon_url = icon_url;
bitmap_result.icon_type = icon_type;
if (!thumbnail_db_->GetFaviconBitmap(best_bitmap_ids[i],
&last_updated,
&bitmap_result.bitmap_data,
&bitmap_result.pixel_size)) {
return false;
}
bitmap_result.expired = (Time::Now() - last_updated) >
TimeDelta::FromDays(kFaviconRefetchDays);
if (bitmap_result.is_valid())
favicon_bitmap_results->push_back(bitmap_result);
}
return true;
}
bool HistoryBackend::SetFaviconMappingsForPageAndRedirects(
const GURL& page_url,
chrome::IconType icon_type,
const std::vector<chrome::FaviconID>& icon_ids) {
if (!thumbnail_db_)
return false;
history::RedirectList redirects;
GetCachedRecentRedirects(page_url, &redirects);
bool mappings_changed = false;
for (history::RedirectList::const_iterator i(redirects.begin());
i != redirects.end(); ++i) {
mappings_changed |= SetFaviconMappingsForPage(*i, icon_type, icon_ids);
}
return mappings_changed;
}
bool HistoryBackend::SetFaviconMappingsForPage(
const GURL& page_url,
chrome::IconType icon_type,
const std::vector<chrome::FaviconID>& icon_ids) {
DCHECK_LE(icon_ids.size(), kMaxFaviconsPerPage);
bool mappings_changed = false;
std::vector<chrome::FaviconID> unmapped_icon_ids = icon_ids;
std::vector<IconMapping> icon_mappings;
thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings);
for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
m != icon_mappings.end(); ++m) {
std::vector<chrome::FaviconID>::iterator icon_id_it = std::find(
unmapped_icon_ids.begin(), unmapped_icon_ids.end(), m->icon_id);
if (icon_id_it != unmapped_icon_ids.end()) {
unmapped_icon_ids.erase(icon_id_it);
continue;
}
if ((icon_type == chrome::TOUCH_ICON &&
m->icon_type == chrome::TOUCH_PRECOMPOSED_ICON) ||
(icon_type == chrome::TOUCH_PRECOMPOSED_ICON &&
m->icon_type == chrome::TOUCH_ICON) || (icon_type == m->icon_type)) {
thumbnail_db_->DeleteIconMapping(m->mapping_id);
if (!thumbnail_db_->HasMappingFor(m->icon_id))
thumbnail_db_->DeleteFavicon(m->icon_id);
mappings_changed = true;
}
}
for (size_t i = 0; i < unmapped_icon_ids.size(); ++i) {
thumbnail_db_->AddIconMapping(page_url, unmapped_icon_ids[i]);
mappings_changed = true;
}
return mappings_changed;
}
void HistoryBackend::GetCachedRecentRedirects(
const GURL& page_url,
history::RedirectList* redirect_list) {
RedirectCache::iterator iter = recent_redirects_.Get(page_url);
if (iter != recent_redirects_.end()) {
*redirect_list = iter->second;
DCHECK(!redirect_list->empty());
DCHECK(redirect_list->back() == page_url);
} else {
redirect_list->push_back(page_url);
}
}
void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects(
const GURL& page_url) {
history::RedirectList redirect_list;
GetCachedRecentRedirects(page_url, &redirect_list);
scoped_ptr<FaviconChangedDetails> changed_details(new FaviconChangedDetails);
for (size_t i = 0; i < redirect_list.size(); ++i)
changed_details->urls.insert(redirect_list[i]);
BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
changed_details.PassAs<HistoryDetails>());
}
void HistoryBackend::Commit() {
if (!db_)
return;
CancelScheduledCommit();
db_->CommitTransaction();
DCHECK(db_->transaction_nesting() == 0) << "Somebody left a transaction open";
db_->BeginTransaction();
if (thumbnail_db_) {
thumbnail_db_->CommitTransaction();
DCHECK(thumbnail_db_->transaction_nesting() == 0) <<
"Somebody left a transaction open";
thumbnail_db_->BeginTransaction();
}
if (archived_db_) {
archived_db_->CommitTransaction();
archived_db_->BeginTransaction();
}
}
void HistoryBackend::ScheduleCommit() {
if (scheduled_commit_.get())
return;
scheduled_commit_ = new CommitLaterTask(this);
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&CommitLaterTask::RunCommit, scheduled_commit_.get()),
base::TimeDelta::FromSeconds(kCommitIntervalSeconds));
}
void HistoryBackend::CancelScheduledCommit() {
if (scheduled_commit_.get()) {
scheduled_commit_->Cancel();
scheduled_commit_ = NULL;
}
}
void HistoryBackend::ProcessDBTaskImpl() {
if (!db_) {
ReleaseDBTasks();
return;
}
while (!db_task_requests_.empty() && db_task_requests_.front()->canceled()) {
db_task_requests_.front()->Release();
db_task_requests_.pop_front();
}
if (db_task_requests_.empty())
return;
HistoryDBTaskRequest* request = db_task_requests_.front();
db_task_requests_.pop_front();
if (request->value->RunOnDBThread(this, db_.get())) {
request->ForwardResult();
request->Release();
} else {
db_task_requests_.push_back(request);
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(&HistoryBackend::ProcessDBTaskImpl, this));
}
}
void HistoryBackend::ReleaseDBTasks() {
for (std::list<HistoryDBTaskRequest*>::iterator i =
db_task_requests_.begin(); i != db_task_requests_.end(); ++i) {
(*i)->Release();
}
db_task_requests_.clear();
}
void HistoryBackend::DeleteURLs(const std::vector<GURL>& urls) {
expirer_.DeleteURLs(urls);
db_->GetStartDate(&first_recorded_time_);
Commit();
}
void HistoryBackend::DeleteURL(const GURL& url) {
expirer_.DeleteURL(url);
db_->GetStartDate(&first_recorded_time_);
Commit();
}
void HistoryBackend::ExpireHistoryBetween(
const std::set<GURL>& restrict_urls,
Time begin_time,
Time end_time) {
if (!db_)
return;
if (begin_time.is_null() && (end_time.is_null() || end_time.is_max()) &&
restrict_urls.empty()) {
DeleteAllHistory();
} else {
expirer_.ExpireHistoryBetween(restrict_urls, begin_time, end_time);
Commit();
}
if (begin_time <= first_recorded_time_)
db_->GetStartDate(&first_recorded_time_);
}
void HistoryBackend::ExpireHistoryForTimes(
const std::set<base::Time>& times,
base::Time begin_time, base::Time end_time) {
if (times.empty() || !db_)
return;
DCHECK(*times.begin() >= begin_time)
<< "Min time is before begin time: "
<< times.begin()->ToJsTime() << " v.s. " << begin_time.ToJsTime();
DCHECK(*times.rbegin() < end_time)
<< "Max time is after end time: "
<< times.rbegin()->ToJsTime() << " v.s. " << end_time.ToJsTime();
history::QueryOptions options;
options.begin_time = begin_time;
options.end_time = end_time;
options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES;
QueryResults results;
QueryHistoryBasic(db_.get(), db_.get(), options, &results);
std::set<GURL> urls;
for (size_t i = 0; i < results.size(); ++i) {
if (times.count(results[i].visit_time()) > 0)
urls.insert(results[i].url());
}
if (urls.empty())
return;
std::vector<base::Time> times_to_expire;
for (size_t i = 0; i < results.size(); ++i) {
if (urls.count(results[i].url()))
times_to_expire.push_back(results[i].visit_time());
}
std::sort(times_to_expire.begin(), times_to_expire.end(),
std::greater<base::Time>());
times_to_expire.erase(
std::unique(times_to_expire.begin(), times_to_expire.end()),
times_to_expire.end());
DCHECK(!times_to_expire.empty());
expirer_.ExpireHistoryForTimes(times_to_expire);
Commit();
DCHECK(times_to_expire.back() >= first_recorded_time_);
if (times_to_expire.back() == first_recorded_time_)
db_->GetStartDate(&first_recorded_time_);
}
void HistoryBackend::ExpireHistory(
const std::vector<history::ExpireHistoryArgs>& expire_list) {
if (db_) {
bool update_first_recorded_time = false;
for (std::vector<history::ExpireHistoryArgs>::const_iterator it =
expire_list.begin(); it != expire_list.end(); ++it) {
expirer_.ExpireHistoryBetween(it->urls, it->begin_time, it->end_time);
if (it->begin_time < first_recorded_time_)
update_first_recorded_time = true;
}
Commit();
if (update_first_recorded_time)
db_->GetStartDate(&first_recorded_time_);
}
}
void HistoryBackend::URLsNoLongerBookmarked(const std::set<GURL>& urls) {
if (!db_)
return;
for (std::set<GURL>::const_iterator i = urls.begin(); i != urls.end(); ++i) {
URLRow url_row;
if (!db_->GetRowForURL(*i, &url_row))
continue;
VisitVector visits;
db_->GetVisitsForURL(url_row.id(), &visits);
if (visits.empty())
expirer_.DeleteURL(*i);
}
}
void HistoryBackend::DatabaseErrorCallback(int error, sql::Statement* stmt) {
if (!scheduled_kill_db_ && sql::IsErrorCatastrophic(error)) {
scheduled_kill_db_ = true;
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&HistoryBackend::KillHistoryDatabase, this));
}
}
void HistoryBackend::KillHistoryDatabase() {
scheduled_kill_db_ = false;
if (!db_)
return;
db_->RollbackTransaction();
bool success = db_->Raze();
UMA_HISTOGRAM_BOOLEAN("History.KillHistoryDatabaseResult", success);
#if defined(OS_ANDROID)
android_provider_backend_.reset();
#endif
expirer_.SetDatabases(NULL, NULL, NULL);
db_->BeginTransaction();
CloseAllDatabases();
}
void HistoryBackend::ProcessDBTask(
scoped_refptr<HistoryDBTaskRequest> request) {
DCHECK(request.get());
if (request->canceled())
return;
bool task_scheduled = !db_task_requests_.empty();
request->AddRef();
db_task_requests_.push_back(request.get());
if (!task_scheduled) {
ProcessDBTaskImpl();
}
}
void HistoryBackend::BroadcastNotifications(
int type,
scoped_ptr<HistoryDetails> details) {
if (delegate_)
delegate_->BroadcastNotifications(type, details.Pass());
}
void HistoryBackend::NotifySyncURLsDeleted(bool all_history,
bool archived,
URLRows* rows) {
if (typed_url_syncable_service_.get())
typed_url_syncable_service_->OnUrlsDeleted(all_history, archived, rows);
}
void HistoryBackend::DeleteAllHistory() {
std::vector<BookmarkService::URLAndTitle> starred_urls;
BookmarkService* bookmark_service = GetBookmarkService();
if (bookmark_service)
bookmark_service_->GetBookmarks(&starred_urls);
URLRows kept_urls;
for (size_t i = 0; i < starred_urls.size(); i++) {
URLRow row;
if (!db_->GetRowForURL(starred_urls[i].url, &row))
continue;
row.set_last_visit(Time());
row.set_visit_count(0);
row.set_typed_count(0);
kept_urls.push_back(row);
}
if (!ClearAllThumbnailHistory(kept_urls)) {
LOG(ERROR) << "Thumbnail history could not be cleared";
}
if (!ClearAllMainHistory(kept_urls))
LOG(ERROR) << "Main history could not be cleared";
kept_urls.clear();
if (archived_db_) {
archived_db_.reset();
base::FilePath archived_file_name = GetArchivedFileName();
sql::Connection::Delete(archived_file_name);
archived_db_.reset(new ArchivedDatabase());
if (!archived_db_->Init(archived_file_name)) {
LOG(WARNING) << "Could not initialize the archived database.";
archived_db_.reset();
} else {
archived_db_->BeginTransaction();
}
}
db_->GetStartDate(&first_recorded_time_);
scoped_ptr<URLsDeletedDetails> details(new URLsDeletedDetails);
details->all_history = true;
NotifySyncURLsDeleted(true, false, NULL);
BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_DELETED,
details.PassAs<HistoryDetails>());
}
bool HistoryBackend::ClearAllThumbnailHistory(const URLRows& kept_urls) {
if (!thumbnail_db_) {
sql::Connection::Delete(GetFaviconsFileName());
sql::Connection::Delete(GetThumbnailFileName());
return true;
}
std::vector<GURL> urls_to_keep;
for (URLRows::const_iterator i = kept_urls.begin();
i != kept_urls.end(); ++i) {
urls_to_keep.push_back(i->url());
}
thumbnail_db_->CommitTransaction();
thumbnail_db_->BeginTransaction();
if (!thumbnail_db_->RetainDataForPageUrls(urls_to_keep)) {
thumbnail_db_->RollbackTransaction();
thumbnail_db_->BeginTransaction();
return false;
}
#if defined(OS_ANDROID)
db_->ClearAndroidURLRows();
#endif
DCHECK(thumbnail_db_->transaction_nesting() == 1);
thumbnail_db_->CommitTransaction();
thumbnail_db_->Vacuum();
thumbnail_db_->BeginTransaction();
return true;
}
bool HistoryBackend::ClearAllMainHistory(const URLRows& kept_urls) {
if (!db_->CreateTemporaryURLTable())
return false;
for (URLRows::const_iterator i = kept_urls.begin(); i != kept_urls.end();
++i) {
db_->AddTemporaryURL(*i);
}
if (!db_->CommitTemporaryURLTable())
return false;
db_->RecreateAllTablesButURL();
db_->CommitTransaction();
db_->Vacuum();
db_->BeginTransaction();
db_->GetStartDate(&first_recorded_time_);
return true;
}
BookmarkService* HistoryBackend::GetBookmarkService() {
if (bookmark_service_)
bookmark_service_->BlockTillLoaded();
return bookmark_service_;
}
void HistoryBackend::NotifyVisitObservers(const VisitRow& visit) {
BriefVisitInfo info;
info.url_id = visit.url_id;
info.time = visit.visit_time;
info.transition = visit.transition;
if (delegate_)
delegate_->NotifyVisitDBObserversOnAddVisit(info);
}
#if defined(OS_ANDROID)
void HistoryBackend::PopulateMostVisitedURLMap() {
MostVisitedURLList most_visited_urls;
QueryMostVisitedURLsImpl(kPageVisitStatsMaxTopSites, kSegmentDataRetention,
&most_visited_urls);
DCHECK_LE(most_visited_urls.size(), kPageVisitStatsMaxTopSites);
for (size_t i = 0; i < most_visited_urls.size(); ++i) {
most_visited_urls_map_[most_visited_urls[i].url] = i;
for (size_t j = 0; j < most_visited_urls[i].redirects.size(); ++j)
most_visited_urls_map_[most_visited_urls[i].redirects[j]] = i;
}
}
void HistoryBackend::RecordTopPageVisitStats(const GURL& url) {
int rank = kPageVisitStatsMaxTopSites;
std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url);
if (it != most_visited_urls_map_.end())
rank = (*it).second;
UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank",
rank, kPageVisitStatsMaxTopSites + 1);
}
#endif
}