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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_HISTORY_HISTORY_BACKEND_H_
6 #define CHROME_BROWSER_HISTORY_HISTORY_BACKEND_H_
13 #include "base/containers/mru_cache.h"
14 #include "base/files/file_path.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/memory/memory_pressure_listener.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/single_thread_task_runner.h"
20 #include "base/task/cancelable_task_tracker.h"
21 #include "chrome/browser/history/expire_history_backend.h"
22 #include "chrome/browser/history/history_database.h"
23 #include "chrome/browser/history/thumbnail_database.h"
24 #include "chrome/browser/history/visit_tracker.h"
25 #include "components/history/core/browser/history_types.h"
26 #include "components/history/core/browser/keyword_id.h"
27 #include "components/visitedlink/browser/visitedlink_delegate.h"
28 #include "sql/init_status.h"
30 #if defined(OS_ANDROID)
31 #include "components/history/core/android/android_history_types.h"
34 class HistoryURLProvider;
35 struct HistoryURLProviderParams;
36 struct ImportedFaviconUsage;
39 struct ThumbnailScore;
43 class SingleThreadTaskRunner;
47 #if defined(OS_ANDROID)
48 class AndroidProviderBackend;
51 class CommitLaterTask;
53 class HistoryBackendObserver;
55 struct HistoryDetails;
57 class InMemoryHistoryBackend;
58 class TypedUrlSyncableService;
61 // The maximum number of icons URLs per page which can be stored in the
62 // thumbnail database.
63 static const size_t kMaxFaviconsPerPage = 8;
65 // The maximum number of bitmaps for a single icon URL which can be stored in
66 // the thumbnail database.
67 static const size_t kMaxFaviconBitmapsPerIconURL = 8;
69 // Keeps track of a queued HistoryDBTask. This class lives solely on the
71 class QueuedHistoryDBTask {
74 scoped_ptr<HistoryDBTask> task,
75 scoped_refptr<base::SingleThreadTaskRunner> origin_loop,
76 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled);
77 ~QueuedHistoryDBTask();
80 bool Run(HistoryBackend* backend, HistoryDatabase* db);
84 scoped_ptr<HistoryDBTask> task_;
85 scoped_refptr<base::SingleThreadTaskRunner> origin_loop_;
86 base::CancelableTaskTracker::IsCanceledCallback is_canceled_;
88 DISALLOW_COPY_AND_ASSIGN(QueuedHistoryDBTask);
91 // *See the .cc file for more information on the design.*
93 // Internal history implementation which does most of the work of the history
94 // system. This runs on a background thread (to not block the browser when we
95 // do expensive operations) and is NOT threadsafe, so it must only be called
96 // from message handlers on the background thread. Invoking on another thread
97 // requires threadsafe refcounting.
99 // Most functions here are just the implementations of the corresponding
100 // functions in the history service. These functions are not documented
101 // here, see the history service for behavior.
102 class HistoryBackend : public base::RefCountedThreadSafe<HistoryBackend>,
103 public ExpireHistoryBackendDelegate {
105 // Interface implemented by the owner of the HistoryBackend object. Normally,
106 // the history service implements this to send stuff back to the main thread.
107 // The unit tests can provide a different implementation if they don't have
108 // a history service object.
111 virtual ~Delegate() {}
113 // Called when the database cannot be read correctly for some reason.
114 virtual void NotifyProfileError(sql::InitStatus init_status) = 0;
116 // Sets the in-memory history backend. The in-memory backend is created by
117 // the main backend. For non-unit tests, this happens on the background
118 // thread. It is to be used on the main thread, so this would transfer
119 // it to the history service. Unit tests can override this behavior.
121 // This function is NOT guaranteed to be called. If there is an error,
122 // there may be no in-memory database.
123 virtual void SetInMemoryBackend(
124 scoped_ptr<InMemoryHistoryBackend> backend) = 0;
126 // Notify HistoryService that VisitDatabase was changed. The event will be
127 // forwarded to the history::HistoryServiceObservers in the UI thread.
128 virtual void NotifyAddVisit(const BriefVisitInfo& info) = 0;
130 // Notify HistoryService that some URLs favicon changed that will forward
131 // the events to the FaviconChangedObservers in the correct thread.
132 virtual void NotifyFaviconChanged(const std::set<GURL>& urls) = 0;
134 // Notify HistoryService that the user is visiting an URL. The event will
135 // be forwarded to the HistoryServiceObservers in the correct thread.
136 virtual void NotifyURLVisited(ui::PageTransition transition,
138 const RedirectList& redirects,
139 base::Time visit_time) = 0;
141 // Broadcasts the specified notification to the notification service.
142 // This is implemented here because notifications must only be sent from
143 // the main thread. This is the only method that doesn't identify the
144 // caller because notifications must always be sent.
145 virtual void BroadcastNotifications(int type,
146 scoped_ptr<HistoryDetails> details) = 0;
148 // Invoked when the backend has finished loading the db.
149 virtual void DBLoaded() = 0;
152 // Init must be called to complete object creation. This object can be
153 // constructed on any thread, but all other functions including Init() must
154 // be called on the history thread.
156 // |history_dir| is the directory where the history files will be placed.
157 // See the definition of BroadcastNotificationsCallback above. This function
158 // takes ownership of the callback pointer.
160 // |history_client| is used to determine bookmarked URLs when deleting and
163 // This constructor is fast and does no I/O, so can be called at any time.
164 HistoryBackend(const base::FilePath& history_dir,
166 HistoryClient* history_client);
168 // Must be called after creation but before any objects are created. If this
169 // fails, all other functions will fail as well. (Since this runs on another
170 // thread, we don't bother returning failure.)
172 // |languages| gives a list of language encodings with which the history
173 // URLs and omnibox searches are interpreted.
174 // |force_fail| can be set during unittests to unconditionally fail to init.
175 void Init(const std::string& languages, bool force_fail);
177 // Notification that the history system is shutting down. This will break
178 // the refs owned by the delegate and any pending transaction so it will
179 // actually be deleted.
182 void ClearCachedDataForContextID(ContextID context_id);
184 // Navigation ----------------------------------------------------------------
186 // |request.time| must be unique with high probability.
187 void AddPage(const HistoryAddPageArgs& request);
188 virtual void SetPageTitle(const GURL& url, const base::string16& title);
189 void AddPageNoVisitForBookmark(const GURL& url, const base::string16& title);
190 void UpdateWithPageEndTime(ContextID context_id,
195 // Querying ------------------------------------------------------------------
197 // Run the |callback| on the History thread.
198 // history_url_provider.h has the temporal ordering for
199 // the call sequence.
200 // |callback| should handle the NULL database case.
201 void ScheduleAutocomplete(const base::Callback<
202 void(history::HistoryBackend*, history::URLDatabase*)>& callback);
205 const scoped_refptr<visitedlink::VisitedLinkDelegate::URLEnumerator>&
207 void QueryURL(const GURL& url,
209 QueryURLResult* query_url_result);
210 void QueryHistory(const base::string16& text_query,
211 const QueryOptions& options,
212 QueryResults* query_results);
214 // Computes the most recent URL(s) that the given canonical URL has
215 // redirected to. There may be more than one redirect in a row, so this
216 // function will fill the given array with the entire chain. If there are
217 // no redirects for the most recent visit of the URL, or the URL is not
218 // in history, the array will be empty.
219 void QueryRedirectsFrom(const GURL& url, RedirectList* redirects);
221 // Similar to above function except computes a chain of redirects to the
222 // given URL. Stores the most recent list of redirects ending at |url| in the
223 // given RedirectList. For example, if we have the redirect list A -> B -> C,
224 // then calling this function with url=C would fill redirects with {B, A}.
225 void QueryRedirectsTo(const GURL& url, RedirectList* redirects);
227 void GetVisibleVisitCountToHost(const GURL& url,
228 VisibleVisitCountToHostResult* result);
230 // Request the |result_count| most visited URLs and the chain of
231 // redirects leading to each of these URLs. |days_back| is the
232 // number of days of history to use. Used by TopSites.
233 void QueryMostVisitedURLs(int result_count,
235 MostVisitedURLList* result);
237 // Request the |result_count| URLs and the chain of redirects
238 // leading to each of these URLs, filterd and sorted based on the |filter|.
239 // If |debug| is enabled, additional data will be computed and provided.
240 void QueryFilteredURLs(int result_count,
241 const history::VisitFilter& filter,
243 history::FilteredURLList* result);
245 // Favicon -------------------------------------------------------------------
248 const std::vector<GURL>& icon_urls,
250 const std::vector<int>& desired_sizes,
251 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results);
253 void GetLargestFaviconForURL(
254 const GURL& page_url,
255 const std::vector<int>& icon_types,
256 int minimum_size_in_pixels,
257 favicon_base::FaviconRawBitmapResult* bitmap_result);
259 void GetFaviconsForURL(
260 const GURL& page_url,
262 const std::vector<int>& desired_sizes,
263 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results);
265 void GetFaviconForID(
266 favicon_base::FaviconID favicon_id,
268 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results);
270 void UpdateFaviconMappingsAndFetch(
271 const GURL& page_url,
272 const std::vector<GURL>& icon_urls,
274 const std::vector<int>& desired_sizes,
275 std::vector<favicon_base::FaviconRawBitmapResult>* bitmap_results);
277 void MergeFavicon(const GURL& page_url,
278 const GURL& icon_url,
279 favicon_base::IconType icon_type,
280 scoped_refptr<base::RefCountedMemory> bitmap_data,
281 const gfx::Size& pixel_size);
283 void SetFavicons(const GURL& page_url,
284 favicon_base::IconType icon_type,
285 const GURL& icon_url,
286 const std::vector<SkBitmap>& bitmaps);
288 void SetFaviconsOutOfDateForPage(const GURL& page_url);
290 void CloneFavicons(const GURL& old_page_url, const GURL& new_page_url);
292 void SetImportedFavicons(
293 const std::vector<ImportedFaviconUsage>& favicon_usage);
295 // Downloads -----------------------------------------------------------------
297 uint32 GetNextDownloadId();
298 void QueryDownloads(std::vector<DownloadRow>* rows);
299 void UpdateDownload(const DownloadRow& data);
300 bool CreateDownload(const history::DownloadRow& history_info);
301 void RemoveDownloads(const std::set<uint32>& ids);
303 // Keyword search terms ------------------------------------------------------
305 void SetKeywordSearchTermsForURL(const GURL& url,
306 KeywordID keyword_id,
307 const base::string16& term);
309 void DeleteAllSearchTermsForKeyword(KeywordID keyword_id);
311 void DeleteKeywordSearchTermForURL(const GURL& url);
313 void DeleteMatchingURLsForKeyword(KeywordID keyword_id,
314 const base::string16& term);
316 // Observers -----------------------------------------------------------------
318 void AddObserver(HistoryBackendObserver* observer);
319 void RemoveObserver(HistoryBackendObserver* observer);
321 #if defined(OS_ANDROID)
322 // Android Provider ---------------------------------------------------------
324 // History and bookmarks ----------------------------------------------------
325 // Inserts the given values into history backend.
326 AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& row);
328 // Runs the given query on history backend and returns the result.
330 // |projections| is the vector of the result columns.
331 // |selection| is the SQL WHERE clause without 'WHERE'.
332 // |selection_args| is the arguments for WHERE clause.
333 // |sort_order| is the SQL ORDER clause.
334 history::AndroidStatement* QueryHistoryAndBookmarks(
335 const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
336 const std::string& selection,
337 const std::vector<base::string16>& selection_args,
338 const std::string& sort_order);
340 // Returns the number of row updated by the update query.
342 // |row| is the value to update.
343 // |selection| is the SQL WHERE clause without 'WHERE'.
344 // |selection_args| is the arguments for the WHERE clause.
345 int UpdateHistoryAndBookmarks(
346 const HistoryAndBookmarkRow& row,
347 const std::string& selection,
348 const std::vector<base::string16>& selection_args);
350 // Deletes the specified rows and returns the number of rows deleted.
352 // |selection| is the SQL WHERE clause without 'WHERE'.
353 // |selection_args| is the arguments for the WHERE clause.
355 // If |selection| is empty all history and bookmarks are deleted.
356 int DeleteHistoryAndBookmarks(
357 const std::string& selection,
358 const std::vector<base::string16>& selection_args);
360 // Deletes the matched history and returns the number of rows deleted.
361 int DeleteHistory(const std::string& selection,
362 const std::vector<base::string16>& selection_args);
364 // Statement ----------------------------------------------------------------
365 // Move the statement's current position.
366 int MoveStatement(history::AndroidStatement* statement,
370 // Close the given statement. The ownership is transfered.
371 void CloseStatement(AndroidStatement* statement);
373 // Search terms -------------------------------------------------------------
374 // Inserts the given values and returns the SearchTermID of the inserted row.
375 SearchTermID InsertSearchTerm(const SearchRow& row);
377 // Returns the number of row updated by the update query.
379 // |row| is the value to update.
380 // |selection| is the SQL WHERE clause without 'WHERE'.
381 // |selection_args| is the arguments for the WHERE clause.
382 int UpdateSearchTerms(const SearchRow& row,
383 const std::string& selection,
384 const std::vector<base::string16> selection_args);
386 // Deletes the matched rows and returns the number of deleted rows.
388 // |selection| is the SQL WHERE clause without 'WHERE'.
389 // |selection_args| is the arguments for WHERE clause.
391 // If |selection| is empty all search terms will be deleted.
392 int DeleteSearchTerms(const std::string& selection,
393 const std::vector<base::string16> selection_args);
395 // Returns the result of the given query.
397 // |projections| specifies the result columns.
398 // |selection| is the SQL WHERE clause without 'WHERE'.
399 // |selection_args| is the arguments for WHERE clause.
400 // |sort_order| is the SQL ORDER clause.
401 history::AndroidStatement* QuerySearchTerms(
402 const std::vector<SearchRow::ColumnID>& projections,
403 const std::string& selection,
404 const std::vector<base::string16>& selection_args,
405 const std::string& sort_order);
407 #endif // defined(OS_ANDROID)
409 // Generic operations --------------------------------------------------------
412 scoped_ptr<HistoryDBTask> task,
413 scoped_refptr<base::SingleThreadTaskRunner> origin_loop,
414 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled);
416 virtual bool GetAllTypedURLs(URLRows* urls);
418 virtual bool GetVisitsForURL(URLID id, VisitVector* visits);
420 // Fetches up to |max_visits| most recent visits for the passed URL.
421 virtual bool GetMostRecentVisitsForURL(URLID id,
423 VisitVector* visits);
425 // For each element in |urls|, updates the pre-existing URLRow in the database
426 // with the same ID; or ignores the element if no such row exists. Returns the
427 // number of records successfully updated.
428 virtual size_t UpdateURLs(const history::URLRows& urls);
430 // While adding visits in batch, the source needs to be provided.
431 virtual bool AddVisits(const GURL& url,
432 const std::vector<history::VisitInfo>& visits,
433 VisitSource visit_source);
435 virtual bool RemoveVisits(const VisitVector& visits);
437 // Returns the VisitSource associated with each one of the passed visits.
438 // If there is no entry in the map for a given visit, that means the visit
439 // was SOURCE_BROWSED. Returns false if there is no HistoryDatabase..
440 bool GetVisitsSource(const VisitVector& visits, VisitSourceMap* sources);
442 virtual bool GetURL(const GURL& url, history::URLRow* url_row);
444 // Returns the syncable service for syncing typed urls. The returned service
445 // is owned by |this| object.
446 virtual TypedUrlSyncableService* GetTypedUrlSyncableService() const;
448 // Deleting ------------------------------------------------------------------
450 virtual void DeleteURLs(const std::vector<GURL>& urls);
452 virtual void DeleteURL(const GURL& url);
454 // Calls ExpireHistoryBackend::ExpireHistoryBetween and commits the change.
455 void ExpireHistoryBetween(
456 const std::set<GURL>& restrict_urls,
457 base::Time begin_time,
458 base::Time end_time);
460 // Finds the URLs visited at |times| and expires all their visits within
461 // [|begin_time|, |end_time|). All times in |times| should be in
462 // [|begin_time|, |end_time|). This is used when expiration request is from
463 // server side, i.e. web history deletes, where only visit times (possibly
464 // incomplete) are transmitted to protect user's privacy.
465 void ExpireHistoryForTimes(const std::set<base::Time>& times,
466 base::Time begin_time, base::Time end_time);
468 // Calls ExpireHistoryBetween() once for each element in the vector.
469 // The fields of |ExpireHistoryArgs| map directly to the arguments of
470 // of ExpireHistoryBetween().
471 void ExpireHistory(const std::vector<ExpireHistoryArgs>& expire_list);
473 // Bookmarks -----------------------------------------------------------------
475 // Notification that a URL is no longer bookmarked. If there are no visits
476 // for the specified url, it is deleted.
477 void URLsNoLongerBookmarked(const std::set<GURL>& urls);
479 // Callbacks To Kill Database When It Gets Corrupted -------------------------
481 // Called by the database to report errors. Schedules one call to
482 // KillHistoryDatabase() in case of corruption.
483 void DatabaseErrorCallback(int error, sql::Statement* stmt);
485 // Raze the history database. It will be recreated in a future run. Hopefully
486 // things go better then. Continue running but without reading or storing any
487 // state into the HistoryBackend databases. Close all of the databases managed
488 // HistoryBackend as there are no provisions for accessing the other databases
489 // managed by HistoryBackend when the history database cannot be accessed.
490 void KillHistoryDatabase();
492 // Testing -------------------------------------------------------------------
494 // Sets the task to run and the message loop to run it on when this object
495 // is destroyed. See HistoryService::SetOnBackendDestroyTask for a more
496 // complete description.
497 void SetOnBackendDestroyTask(base::MessageLoop* message_loop,
498 const base::Closure& task);
500 // Adds the given rows to the database if it doesn't exist. A visit will be
501 // added for each given URL at the last visit time in the URLRow if the
502 // passed visit type != SOURCE_SYNCED (the sync code manages visits itself).
503 // Each visit will have the visit_source type set.
504 void AddPagesWithDetails(const URLRows& info, VisitSource visit_source);
506 #if defined(UNIT_TEST)
507 HistoryDatabase* db() const { return db_.get(); }
509 ExpireHistoryBackend* expire_backend() { return &expirer_; }
512 // Returns true if the passed visit time is already expired (used by the sync
513 // code to avoid syncing visits that would immediately be expired).
514 virtual bool IsExpiredVisitTime(const base::Time& time);
516 base::Time GetFirstRecordedTimeForTest() {
517 return first_recorded_time_;
521 ~HistoryBackend() override;
523 // Notify HistoryBackendObserver that |transition| to |row| occurred at
524 // |visit_time| following |redirects| (empty if there is no redirects).
525 void NotifyURLVisited(ui::PageTransition transition,
527 const RedirectList& redirects,
528 base::Time visit_time);
531 friend class base::RefCountedThreadSafe<HistoryBackend>;
532 friend class CommitLaterTask; // The commit task needs to call Commit().
533 friend class HistoryBackendTest;
534 friend class HistoryBackendDBTest; // So the unit tests can poke our innards.
535 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAll);
536 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteAllThenAddData);
537 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, AddPagesWithDetails);
538 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, UpdateURLs);
539 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, ImportedFaviconsTest);
540 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, URLsNoLongerBookmarked);
541 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, StripUsernamePasswordTest);
542 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteThumbnailsDatabaseTest);
543 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, AddPageVisitSource);
544 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, AddPageVisitNotLastVisit);
545 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
546 AddPageVisitFiresNotificationWithCorrectDetails);
547 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, AddPageArgsSource);
548 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, AddVisitsSource);
549 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, GetMostRecentVisits);
550 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, RemoveVisitsSource);
551 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, RemoveVisitsTransitions);
552 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, MigrationVisitSource);
553 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
554 SetFaviconMappingsForPageAndRedirects);
555 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
556 SetFaviconMappingsForPageDuplicates);
557 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, SetFaviconsDeleteBitmaps);
558 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, SetFaviconsReplaceBitmapData);
559 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
560 SetFaviconsSameFaviconURLForTwoPages);
561 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
562 UpdateFaviconMappingsAndFetchNoChange);
563 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, MergeFaviconPageURLNotInDB);
564 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, MergeFaviconPageURLInDB);
565 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, MergeFaviconMaxFaviconsPerPage);
566 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
567 MergeFaviconIconURLMappedToDifferentPageURL);
568 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
569 MergeFaviconMaxFaviconBitmapsPerIconURL);
570 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
571 UpdateFaviconMappingsAndFetchMultipleIconTypes);
572 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, GetFaviconsFromDBEmpty);
573 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
574 GetFaviconsFromDBNoFaviconBitmaps);
575 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
576 GetFaviconsFromDBSelectClosestMatch);
577 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, GetFaviconsFromDBIconType);
578 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, GetFaviconsFromDBExpired);
579 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
580 UpdateFaviconMappingsAndFetchNoDB);
581 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest,
582 CloneFaviconIsRestrictedToSameDomain);
583 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, QueryFilteredURLs);
584 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, UpdateVisitDuration);
585 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, ExpireHistoryForTimes);
586 FRIEND_TEST_ALL_PREFIXES(HistoryBackendTest, DeleteFTSIndexDatabases);
588 friend class ::TestingProfile;
590 // Computes the name of the specified database on disk.
591 base::FilePath GetArchivedFileName() const;
592 base::FilePath GetThumbnailFileName() const;
594 // Returns the name of the Favicons database. This is the new name
595 // of the Thumbnails database.
596 base::FilePath GetFaviconsFileName() const;
598 #if defined(OS_ANDROID)
599 // Returns the name of android cache database.
600 base::FilePath GetAndroidCacheFileName() const;
602 // Populate a map from a |MostVisitedURLList|. The map assigns a rank to each
603 // top URL and its redirects. This should only be done once at backend
605 // This can be removed for M31. (See issue 248761.)
607 void PopulateMostVisitedURLMap();
608 // Record counts of page visits by rank. If a url is not ranked, record the
609 // page visit in a slot corresponding to |max_top_url_count|, which should
610 // be one greater than the largest rank of any url in |top_urls|.
611 // This can be removed for M31. (See issue 248761.)
612 void RecordTopPageVisitStats(const GURL& url);
616 friend class URLQuerier;
618 // Does the work of Init.
619 void InitImpl(const std::string& languages);
621 // Called when the system is under memory pressure.
622 void OnMemoryPressure(
623 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
625 // Closes all databases managed by HistoryBackend. Commits any pending
627 void CloseAllDatabases();
629 // Adds a single visit to the database, updating the URL information such
630 // as visit and typed count. The visit ID of the added visit and the URL ID
631 // of the associated URL (whether added or not) is returned. Both values will
634 // This does not schedule database commits, it is intended to be used as a
635 // subroutine for AddPage only. It also assumes the database is valid.
636 std::pair<URLID, VisitID> AddPageVisit(const GURL& url,
638 VisitID referring_visit,
639 ui::PageTransition transition,
640 VisitSource visit_source);
642 // Returns a redirect chain in |redirects| for the VisitID
643 // |cur_visit|. |cur_visit| is assumed to be valid. Assumes that
644 // this HistoryBackend object has been Init()ed successfully.
645 void GetRedirectsFromSpecificVisit(
646 VisitID cur_visit, history::RedirectList* redirects);
648 // Similar to the above function except returns a redirect list ending
650 void GetRedirectsToSpecificVisit(
651 VisitID cur_visit, history::RedirectList* redirects);
653 // Update the visit_duration information in visits table.
654 void UpdateVisitDuration(VisitID visit_id, const base::Time end_ts);
656 // Querying ------------------------------------------------------------------
658 // Backends for QueryHistory. *Basic() handles queries that are not
659 // text search queries and can just be given directly to the history DB.
660 // The *Text() version performs a brute force query of the history DB to
661 // search for results which match the given text query.
662 // Both functions assume QueryHistory already checked the DB for validity.
663 void QueryHistoryBasic(const QueryOptions& options, QueryResults* result);
664 void QueryHistoryText(const base::string16& text_query,
665 const QueryOptions& options,
666 QueryResults* result);
668 // Committing ----------------------------------------------------------------
670 // We always keep a transaction open on the history database so that multiple
671 // transactions can be batched. Periodically, these are flushed (use
672 // ScheduleCommit). This function does the commit to write any new changes to
673 // disk and opens a new transaction. This will be called automatically by
674 // ScheduleCommit, or it can be called explicitly if a caller really wants
675 // to write something to disk.
678 // Schedules a commit to happen in the future. We do this so that many
679 // operations over a period of time will be batched together. If there is
680 // already a commit scheduled for the future, this will do nothing.
681 void ScheduleCommit();
683 // Cancels the scheduled commit, if any. If there is no scheduled commit,
685 void CancelScheduledCommit();
687 // Segments ------------------------------------------------------------------
689 // Walks back a segment chain to find the last visit with a non null segment
690 // id and returns it. If there is none found, returns 0.
691 SegmentID GetLastSegmentID(VisitID from_visit);
693 // Update the segment information. This is called internally when a page is
694 // added. Return the segment id of the segment that has been updated.
695 SegmentID UpdateSegments(const GURL& url,
698 ui::PageTransition transition_type,
699 const base::Time ts);
701 // Favicons ------------------------------------------------------------------
703 // Used by both UpdateFaviconMappingsAndFetch and GetFavicons.
704 // If |page_url| is non-null, the icon urls for |page_url| (and all
705 // redirects) are set to the subset of |icon_urls| for which icons are
706 // already stored in the database.
707 // If |page_url| is non-null, |icon_types| can be multiple icon types
708 // only if |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
709 // If multiple icon types are specified, |page_url| will be mapped to the
710 // icon URLs of the largest type available in the database.
711 void UpdateFaviconMappingsAndFetchImpl(
712 const GURL* page_url,
713 const std::vector<GURL>& icon_urls,
715 const std::vector<int>& desired_sizes,
716 std::vector<favicon_base::FaviconRawBitmapResult>* results);
718 // Set the favicon bitmaps for |icon_id|.
719 // For each entry in |bitmaps|, if a favicon bitmap already exists at the
720 // entry's pixel size, replace the favicon bitmap's data with the entry's
721 // bitmap data. Otherwise add a new favicon bitmap.
722 // Any favicon bitmaps already mapped to |icon_id| whose pixel size does not
723 // match the pixel size of one of |bitmaps| is deleted.
724 // Returns true if any of the bitmap data at |icon_id| is changed as a result
725 // of calling this method.
726 bool SetFaviconBitmaps(favicon_base::FaviconID icon_id,
727 const std::vector<SkBitmap>& bitmaps);
729 // Returns true if the bitmap data at |bitmap_id| equals |new_bitmap_data|.
730 bool IsFaviconBitmapDataEqual(
731 FaviconBitmapID bitmap_id,
732 const scoped_refptr<base::RefCountedMemory>& new_bitmap_data);
734 // Returns true if there are favicons for |page_url| and one of the types in
736 // |favicon_bitmap_results| is set to the favicon bitmaps whose edge sizes
737 // most closely match |desired_sizes|. If |desired_sizes| has a '0' entry, the
738 // largest favicon bitmap with one of the icon types in |icon_types| is
739 // returned. If |icon_types| contains multiple icon types and there are
740 // several matched icon types in the database, results will only be returned
741 // for a single icon type in the priority of TOUCH_PRECOMPOSED_ICON,
742 // TOUCH_ICON, and FAVICON. See the comment for
743 // GetFaviconResultsForBestMatch() for more details on how
744 // |favicon_bitmap_results| is constructed.
745 bool GetFaviconsFromDB(
746 const GURL& page_url,
748 const std::vector<int>& desired_sizes,
749 std::vector<favicon_base::FaviconRawBitmapResult>*
750 favicon_bitmap_results);
752 // Returns the favicon bitmaps whose edge sizes most closely match
753 // |desired_sizes| in |favicon_bitmap_results|. If |desired_sizes| has a '0'
754 // entry, only the largest favicon bitmap is returned. Goodness is computed
755 // via SelectFaviconFrameIndices(). It is computed on a per FaviconID basis,
756 // thus all |favicon_bitmap_results| are guaranteed to be for the same
757 // FaviconID. |favicon_bitmap_results| will have at most one entry for each
758 // desired edge size. There will be fewer entries if the same favicon bitmap
759 // is the best result for multiple edge sizes.
760 // Returns true if there were no errors.
761 bool GetFaviconBitmapResultsForBestMatch(
762 const std::vector<favicon_base::FaviconID>& candidate_favicon_ids,
763 const std::vector<int>& desired_sizes,
764 std::vector<favicon_base::FaviconRawBitmapResult>*
765 favicon_bitmap_results);
767 // Maps the favicon ids in |icon_ids| to |page_url| (and all redirects)
769 // Returns true if the mappings for the page or any of its redirects were
771 bool SetFaviconMappingsForPageAndRedirects(
772 const GURL& page_url,
773 favicon_base::IconType icon_type,
774 const std::vector<favicon_base::FaviconID>& icon_ids);
776 // Maps the favicon ids in |icon_ids| to |page_url| for |icon_type|.
777 // Returns true if the function changed some of |page_url|'s mappings.
778 bool SetFaviconMappingsForPage(
779 const GURL& page_url,
780 favicon_base::IconType icon_type,
781 const std::vector<favicon_base::FaviconID>& icon_ids);
783 // Returns all the page URLs in the redirect chain for |page_url|. If there
784 // are no known redirects for |page_url|, returns a vector with |page_url|.
785 void GetCachedRecentRedirects(const GURL& page_url,
786 history::RedirectList* redirect_list);
788 // Send notification that the favicon has changed for |page_url| and all its
790 void SendFaviconChangedNotificationForPageAndRedirects(
791 const GURL& page_url);
793 // Generic stuff -------------------------------------------------------------
795 // Processes the next scheduled HistoryDBTask, scheduling this method
796 // to be invoked again if there are more tasks that need to run.
797 void ProcessDBTaskImpl();
799 // Broadcasts the specified notification to the notification service on both
800 // the main thread and the history thread if a notification service is
802 void BroadcastNotifications(int type, scoped_ptr<HistoryDetails> details);
804 // ExpireHistoryBackendDelegate:
805 void NotifyURLsModified(const URLRows& rows) override;
806 void NotifyURLsDeleted(bool all_history,
809 const std::set<GURL>& favicon_urls) override;
811 // Deleting all history ------------------------------------------------------
813 // Deletes all history. This is a special case of deleting that is separated
814 // from our normal dependency-following method for performance reasons. The
815 // logic lives here instead of ExpireHistoryBackend since it will cause
816 // re-initialization of some databases (e.g. Thumbnails) that could fail.
817 // When these databases are not valid, our pointers must be NULL, so we need
818 // to handle this type of operation to keep the pointers in sync.
819 void DeleteAllHistory();
821 // Given a vector of all URLs that we will keep, removes all thumbnails
822 // referenced by any URL, and also all favicons that aren't used by those
824 bool ClearAllThumbnailHistory(const URLRows& kept_urls);
826 // Deletes all information in the history database, except for the supplied
827 // set of URLs in the URL table (these should correspond to the bookmarked
830 // The IDs of the URLs may change.
831 bool ClearAllMainHistory(const URLRows& kept_urls);
833 // Deletes the FTS index database files, which are no longer used.
834 void DeleteFTSIndexDatabases();
836 // Returns the HistoryClient, blocking until the bookmarks are loaded. This
837 // may return NULL during testing.
838 HistoryClient* GetHistoryClient();
840 // Notify any observers of an addition to the visit database.
841 void NotifyVisitObservers(const VisitRow& visit);
843 // Data ----------------------------------------------------------------------
845 // Delegate. See the class definition above for more information. This will
846 // be NULL before Init is called and after Cleanup, but is guaranteed
847 // non-NULL in between.
848 scoped_ptr<Delegate> delegate_;
850 // Directory where database files will be stored.
851 base::FilePath history_dir_;
853 // The history/thumbnail databases. Either MAY BE NULL if the database could
854 // not be opened, all users must first check for NULL and return immediately
855 // if it is. The thumbnail DB may be NULL when the history one isn't, but not
857 scoped_ptr<HistoryDatabase> db_;
858 bool scheduled_kill_db_; // Database is being killed due to error.
859 scoped_ptr<ThumbnailDatabase> thumbnail_db_;
861 // Manages expiration between the various databases.
862 ExpireHistoryBackend expirer_;
864 // A commit has been scheduled to occur sometime in the future. We can check
865 // non-null-ness to see if there is a commit scheduled in the future, and we
866 // can use the pointer to cancel the scheduled commit. There can be only one
867 // scheduled commit at a time (see ScheduleCommit).
868 scoped_refptr<CommitLaterTask> scheduled_commit_;
870 // Maps recent redirect destination pages to the chain of redirects that
871 // brought us to there. Pages that did not have redirects or were not the
872 // final redirect in a chain will not be in this list, as well as pages that
873 // redirected "too long" ago (as determined by ExpireOldRedirects above).
874 // It is used to set titles & favicons for redirects to that of the
877 // As with AddPage, the last item in the redirect chain will be the
878 // destination of the redirect (i.e., the key into recent_redirects_);
879 typedef base::MRUCache<GURL, history::RedirectList> RedirectCache;
880 RedirectCache recent_redirects_;
882 // Timestamp of the first entry in our database.
883 base::Time first_recorded_time_;
885 // When set, this is the task that should be invoked on destruction.
886 base::MessageLoop* backend_destroy_message_loop_;
887 base::Closure backend_destroy_task_;
889 // Tracks page transition types.
890 VisitTracker tracker_;
892 // A boolean variable to track whether we have already purged obsolete segment
894 bool segment_queried_;
896 // List of QueuedHistoryDBTasks to run;
897 std::list<QueuedHistoryDBTask*> queued_history_db_tasks_;
899 // Used to determine if a URL is bookmarked; may be NULL.
901 // Use GetHistoryClient to access this, which makes sure the bookmarks are
902 // loaded before returning.
903 HistoryClient* history_client_;
905 #if defined(OS_ANDROID)
906 // Used to provide the Android ContentProvider APIs.
907 scoped_ptr<AndroidProviderBackend> android_provider_backend_;
909 // Used to provide UMA on the number of page visits that are to the most
910 // visited URLs. This is here because the backend both has access to this
911 // information and is notified of page visits. The top sites service should
912 // be used instead whenever possible.
913 std::map<GURL, int> most_visited_urls_map_;
916 // Used to manage syncing of the typed urls datatype. This will be NULL
917 // before Init is called.
918 // TODO(sdefresne): turn TypedUrlSyncableService into a HistoryBackendObserver
919 // and remove this field since it is only used for forwarding notifications
920 // about URL visited, modified, deleted.
921 scoped_ptr<TypedUrlSyncableService> typed_url_syncable_service_;
923 // Listens for the system being under memory pressure.
924 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
927 ObserverList<HistoryBackendObserver> observers_;
929 DISALLOW_COPY_AND_ASSIGN(HistoryBackend);
932 } // namespace history
934 #endif // CHROME_BROWSER_HISTORY_HISTORY_BACKEND_H_