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_SERVICE_H_
6 #define CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_
11 #include "base/basictypes.h"
12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/files/file_path.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/strings/string16.h"
21 #include "base/task/cancelable_task_tracker.h"
22 #include "base/threading/thread_checker.h"
23 #include "base/time/time.h"
24 #include "chrome/browser/common/cancelable_request.h"
25 #include "chrome/browser/favicon/favicon_service.h"
26 #include "chrome/browser/history/delete_directive_handler.h"
27 #include "chrome/browser/history/history_types.h"
28 #include "chrome/browser/history/typed_url_syncable_service.h"
29 #include "chrome/browser/search_engines/template_url_id.h"
30 #include "chrome/common/ref_counted_util.h"
31 #include "components/keyed_service/core/keyed_service.h"
32 #include "components/visitedlink/browser/visitedlink_delegate.h"
33 #include "content/public/browser/download_manager_delegate.h"
34 #include "content/public/browser/notification_observer.h"
35 #include "content/public/browser/notification_registrar.h"
36 #include "content/public/common/page_transition_types.h"
37 #include "sql/init_status.h"
38 #include "sync/api/syncable_service.h"
39 #include "ui/base/layout.h"
41 #if defined(OS_ANDROID)
42 #include "chrome/browser/history/android/android_history_provider_service.h"
45 class BookmarkService;
47 class HistoryURLProvider;
49 class PageUsageRequest;
51 struct HistoryURLProviderParams;
58 namespace visitedlink {
59 class VisitedLinkMaster;
65 class HistoryDatabase;
67 class HistoryQueryTest;
68 class InMemoryHistoryBackend;
69 class InMemoryURLIndex;
70 class InMemoryURLIndexTest;
72 class VisitDatabaseObserver;
75 struct HistoryAddPageArgs;
76 struct HistoryDetails;
78 } // namespace history
80 // The history service records page titles, and visit times, as well as
81 // (eventually) information about autocomplete.
83 // This service is thread safe. Each request callback is invoked in the
84 // thread that made the request.
85 class HistoryService : public CancelableRequestProvider,
86 public content::NotificationObserver,
87 public syncer::SyncableService,
89 public visitedlink::VisitedLinkDelegate {
91 // Miscellaneous commonly-used types.
92 typedef std::vector<PageUsageData*> PageUsageDataList;
94 // Must call Init after construction.
95 explicit HistoryService(Profile* profile);
96 // The empty constructor is provided only for testing.
99 virtual ~HistoryService();
101 // Initializes the history service, returning true on success. On false, do
102 // not call any other functions. The given directory will be used for storing
103 // the history files. The BookmarkService is used when deleting URLs to
104 // test if a URL is bookmarked; it may be NULL during testing.
105 bool Init(const base::FilePath& history_dir, BookmarkService* bookmark_service) {
106 return Init(history_dir, bookmark_service, false);
109 // Triggers the backend to load if it hasn't already, and then returns whether
110 // it's finished loading.
111 // Note: Virtual needed for mocking.
112 virtual bool BackendLoaded();
114 // Returns true if the backend has finished loading.
115 bool backend_loaded() const { return backend_loaded_; }
117 // Called on shutdown, this will tell the history backend to complete and
118 // will release pointers to it. No other functions should be called once
119 // cleanup has happened that may dispatch to the history thread (because it
122 // In practice, this will be called by the service manager (BrowserProcess)
123 // when it is being destroyed. Because that reference is being destroyed, it
124 // should be impossible for anybody else to call the service, even if it is
125 // still in memory (pending requests may be holding a reference to us).
128 // RenderProcessHost pointers are used to scope page IDs (see AddPage). These
129 // objects must tell us when they are being destroyed so that we can clear
130 // out any cached data associated with that scope.
132 // The given pointer will not be dereferenced, it is only used for
133 // identification purposes, hence it is a void*.
134 void NotifyRenderProcessHostDestruction(const void* host);
136 // Triggers the backend to load if it hasn't already, and then returns the
137 // in-memory URL database. The returned pointer MAY BE NULL if the in-memory
138 // database has not been loaded yet. This pointer is owned by the history
139 // system. Callers should not store or cache this value.
141 // TODO(brettw) this should return the InMemoryHistoryBackend.
142 history::URLDatabase* InMemoryDatabase();
144 // Following functions get URL information from in-memory database.
145 // They return false if database is not available (e.g. not loaded yet) or the
146 // URL does not exist.
148 // Reads the number of times the user has typed the given URL.
149 bool GetTypedCountForURL(const GURL& url, int* typed_count);
151 // Reads the last visit time for the given URL.
152 bool GetLastVisitTimeForURL(const GURL& url, base::Time* last_visit);
154 // Reads the number of times this URL has been visited.
155 bool GetVisitCountForURL(const GURL& url, int* visit_count);
157 // Returns a pointer to the TypedUrlSyncableService owned by HistoryBackend.
158 // This method should only be called from the history thread, because the
159 // returned service is intended to be accessed only via the history thread.
160 history::TypedUrlSyncableService* GetTypedUrlSyncableService() const;
162 // Return the quick history index.
163 history::InMemoryURLIndex* InMemoryIndex() const {
164 return in_memory_url_index_.get();
168 virtual void Shutdown() OVERRIDE;
170 // Navigation ----------------------------------------------------------------
172 // Adds the given canonical URL to history with the given time as the visit
173 // time. Referrer may be the empty string.
175 // The supplied render process host is used to scope the given page ID. Page
176 // IDs are only unique inside a given render process, so we need that to
177 // differentiate them. This pointer should not be dereferenced by the history
180 // The scope/ids can be NULL if there is no meaningful tracking information
181 // that can be performed on the given URL. The 'page_id' should be the ID of
182 // the current session history entry in the given process.
184 // 'redirects' is an array of redirect URLs leading to this page, with the
185 // page itself as the last item (so when there is no redirect, it will have
186 // one entry). If there are no redirects, this array may also be empty for
187 // the convenience of callers.
189 // 'did_replace_entry' is true when the navigation entry for this page has
190 // replaced the existing entry. A non-user initiated redirect causes such
193 // All "Add Page" functions will update the visited link database.
194 void AddPage(const GURL& url,
196 const void* id_scope,
198 const GURL& referrer,
199 const history::RedirectList& redirects,
200 content::PageTransition transition,
201 history::VisitSource visit_source,
202 bool did_replace_entry);
204 // For adding pages to history where no tracking information can be done.
205 void AddPage(const GURL& url,
207 history::VisitSource visit_source);
209 // All AddPage variants end up here.
210 void AddPage(const history::HistoryAddPageArgs& add_page_args);
212 // Adds an entry for the specified url without creating a visit. This should
213 // only be used when bookmarking a page, otherwise the row leaks in the
214 // history db (it never gets cleaned).
215 void AddPageNoVisitForBookmark(const GURL& url, const base::string16& title);
217 // Sets the title for the given page. The page should be in history. If it
218 // is not, this operation is ignored.
219 void SetPageTitle(const GURL& url, const base::string16& title);
221 // Updates the history database with a page's ending time stamp information.
222 // The page can be identified by the combination of the pointer to
223 // a RenderProcessHost, the page id and the url.
225 // The given pointer will not be dereferenced, it is only used for
226 // identification purposes, hence it is a void*.
227 void UpdateWithPageEndTime(const void* host,
232 // Querying ------------------------------------------------------------------
234 // Returns the information about the requested URL. If the URL is found,
235 // success will be true and the information will be in the URLRow parameter.
236 // On success, the visits, if requested, will be sorted by date. If they have
237 // not been requested, the pointer will be valid, but the vector will be
240 // If success is false, neither the row nor the vector will be valid.
241 typedef base::Callback<void(
243 bool, // Success flag, when false, nothing else is valid.
244 const history::URLRow*,
245 history::VisitVector*)> QueryURLCallback;
247 // Queries the basic information about the URL in the history database. If
248 // the caller is interested in the visits (each time the URL is visited),
249 // set |want_visits| to true. If these are not needed, the function will be
250 // faster by setting this to false.
251 Handle QueryURL(const GURL& url,
253 CancelableRequestConsumerBase* consumer,
254 const QueryURLCallback& callback);
256 // Provides the result of a query. See QueryResults in history_types.h.
257 // The common use will be to use QueryResults.Swap to suck the contents of
258 // the results out of the passed in parameter and take ownership of them.
259 typedef base::Callback<void(Handle, history::QueryResults*)>
260 QueryHistoryCallback;
262 // Queries all history with the given options (see QueryOptions in
263 // history_types.h). If empty, all results matching the given options
265 Handle QueryHistory(const base::string16& text_query,
266 const history::QueryOptions& options,
267 CancelableRequestConsumerBase* consumer,
268 const QueryHistoryCallback& callback);
270 // Called when the results of QueryRedirectsFrom are available.
271 // The given vector will contain a list of all redirects, not counting
272 // the original page. If A redirects to B which redirects to C, the vector
273 // will contain [B, C], and A will be in 'from_url'.
275 // For QueryRedirectsTo, the order is reversed. For A->B->C, the vector will
276 // contain [B, A] and C will be in 'to_url'.
278 // If there is no such URL in the database or the most recent visit has no
279 // redirect, the vector will be empty. If the history system failed for
280 // some reason, success will additionally be false. If the given page
281 // has redirected to multiple destinations, this will pick a random one.
282 typedef base::Callback<void(Handle,
283 GURL, // from_url / to_url
285 history::RedirectList*)> QueryRedirectsCallback;
287 // Schedules a query for the most recent redirect coming out of the given
288 // URL. See the RedirectQuerySource above, which is guaranteed to be called
289 // if the request is not canceled.
290 Handle QueryRedirectsFrom(const GURL& from_url,
291 CancelableRequestConsumerBase* consumer,
292 const QueryRedirectsCallback& callback);
294 // Schedules a query to get the most recent redirects ending at the given
296 Handle QueryRedirectsTo(const GURL& to_url,
297 CancelableRequestConsumerBase* consumer,
298 const QueryRedirectsCallback& callback);
300 typedef base::Callback<
302 bool, // Were we able to determine the # of visits?
303 int, // Number of visits.
304 base::Time)> // Time of first visit. Only set if bool
305 // is true and int is > 0.
306 GetVisibleVisitCountToHostCallback;
308 // Requests the number of user-visible visits (i.e. no redirects or subframes)
309 // to all urls on the same scheme/host/port as |url|. This is only valid for
310 // HTTP and HTTPS URLs.
311 Handle GetVisibleVisitCountToHost(
313 CancelableRequestConsumerBase* consumer,
314 const GetVisibleVisitCountToHostCallback& callback);
316 // Called when QueryTopURLsAndRedirects completes. The vector contains a list
317 // of the top |result_count| URLs. For each of these URLs, there is an entry
318 // in the map containing redirects from the URL. For example, if we have the
319 // redirect chain A -> B -> C and A is a top visited URL, then A will be in
320 // the vector and "A => {B -> C}" will be in the map.
321 typedef base::Callback<
323 bool, // Did we get the top urls and redirects?
324 std::vector<GURL>*, // List of top URLs.
325 history::RedirectMap*)> // Redirects for top URLs.
326 QueryTopURLsAndRedirectsCallback;
328 // Request the top |result_count| most visited URLs and the chain of redirects
329 // leading to each of these URLs.
330 // TODO(Nik): remove this. Use QueryMostVisitedURLs instead.
331 Handle QueryTopURLsAndRedirects(
333 CancelableRequestConsumerBase* consumer,
334 const QueryTopURLsAndRedirectsCallback& callback);
336 typedef base::Callback<void(Handle, history::MostVisitedURLList)>
337 QueryMostVisitedURLsCallback;
339 typedef base::Callback<void(Handle, const history::FilteredURLList&)>
340 QueryFilteredURLsCallback;
342 // Request the |result_count| most visited URLs and the chain of
343 // redirects leading to each of these URLs. |days_back| is the
344 // number of days of history to use. Used by TopSites.
345 Handle QueryMostVisitedURLs(int result_count, int days_back,
346 CancelableRequestConsumerBase* consumer,
347 const QueryMostVisitedURLsCallback& callback);
349 // Request the |result_count| URLs filtered and sorted based on the |filter|.
350 // If |extended_info| is true, additional data will be provided in the
351 // results. Computing this additional data is expensive, likely to become
352 // more expensive as additional data points are added in future changes, and
353 // not useful in most cases. Set |extended_info| to true only if you
354 // explicitly require the additional data.
355 Handle QueryFilteredURLs(
357 const history::VisitFilter& filter,
359 CancelableRequestConsumerBase* consumer,
360 const QueryFilteredURLsCallback& callback);
362 // Database management operations --------------------------------------------
364 // Delete all the information related to a single url.
365 void DeleteURL(const GURL& url);
367 // Delete all the information related to a list of urls. (Deleting
368 // URLs one by one is slow as it has to flush to disk each time.)
369 void DeleteURLsForTest(const std::vector<GURL>& urls);
371 // Removes all visits in the selected time range (including the
372 // start time), updating the URLs accordingly. This deletes any
373 // associated data. This function also deletes the associated
374 // favicons, if they are no longer referenced. |callback| runs when
375 // the expiration is complete. You may use null Time values to do an
376 // unbounded delete in either direction.
377 // If |restrict_urls| is not empty, only visits to the URLs in this set are
379 void ExpireHistoryBetween(const std::set<GURL>& restrict_urls,
380 base::Time begin_time,
382 const base::Closure& callback,
383 base::CancelableTaskTracker* tracker);
385 // Removes all visits to specified URLs in specific time ranges.
386 // This is the equivalent ExpireHistoryBetween() once for each element in the
387 // vector. The fields of |ExpireHistoryArgs| map directly to the arguments of
388 // of ExpireHistoryBetween().
389 void ExpireHistory(const std::vector<history::ExpireHistoryArgs>& expire_list,
390 const base::Closure& callback,
391 base::CancelableTaskTracker* tracker);
393 // Removes all visits to the given URLs in the specified time range. Calls
394 // ExpireHistoryBetween() to delete local visits, and handles deletion of
395 // synced visits if appropriate.
396 void ExpireLocalAndRemoteHistoryBetween(const std::set<GURL>& restrict_urls,
397 base::Time begin_time,
399 const base::Closure& callback,
400 base::CancelableTaskTracker* tracker);
402 // Processes the given |delete_directive| and sends it to the
403 // SyncChangeProcessor (if it exists). Returns any error resulting
404 // from sending the delete directive to sync.
405 syncer::SyncError ProcessLocalDeleteDirective(
406 const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive);
408 // Downloads -----------------------------------------------------------------
410 // Implemented by the caller of 'CreateDownload' below, and is called when the
411 // history service has created a new entry for a download in the history db.
412 typedef base::Callback<void(bool)> DownloadCreateCallback;
414 // Begins a history request to create a new row for a download. 'info'
415 // contains all the download's creation state, and 'callback' runs when the
416 // history service request is complete. The callback is called on the thread
417 // that calls CreateDownload().
419 const history::DownloadRow& info,
420 const DownloadCreateCallback& callback);
422 // Responds on the calling thread with the maximum id of all downloads records
423 // in the database plus 1.
424 void GetNextDownloadId(const content::DownloadIdCallback& callback);
426 // Implemented by the caller of 'QueryDownloads' below, and is called when the
427 // history service has retrieved a list of all download state. The call
428 typedef base::Callback<void(
429 scoped_ptr<std::vector<history::DownloadRow> >)>
430 DownloadQueryCallback;
432 // Begins a history request to retrieve the state of all downloads in the
433 // history db. 'callback' runs when the history service request is complete,
434 // at which point 'info' contains an array of history::DownloadRow, one per
435 // download. The callback is called on the thread that calls QueryDownloads().
436 void QueryDownloads(const DownloadQueryCallback& callback);
438 // Called to update the history service about the current state of a download.
439 // This is a 'fire and forget' query, so just pass the relevant state info to
440 // the database with no need for a callback.
441 void UpdateDownload(const history::DownloadRow& data);
443 // Permanently remove some downloads from the history system. This is a 'fire
444 // and forget' operation.
445 void RemoveDownloads(const std::set<uint32>& ids);
447 // Visit Segments ------------------------------------------------------------
449 typedef base::Callback<void(Handle, std::vector<PageUsageData*>*)>
450 SegmentQueryCallback;
452 // Query usage data for all visit segments since the provided time.
454 // The request is performed asynchronously and can be cancelled by using the
457 // The vector provided to the callback and its contents is owned by the
458 // history system. It will be deeply deleted after the callback is invoked.
459 // If you want to preserve any PageUsageData instance, simply remove them
462 // The vector contains a list of PageUsageData. Each PageUsageData ID is set
463 // to the segment ID. The URL and all the other information is set to the page
464 // representing the segment.
465 Handle QuerySegmentUsageSince(CancelableRequestConsumerBase* consumer,
466 const base::Time from_time,
467 int max_result_count,
468 const SegmentQueryCallback& callback);
470 // Keyword search terms -----------------------------------------------------
472 // Sets the search terms for the specified url and keyword. url_id gives the
473 // id of the url, keyword_id the id of the keyword and term the search term.
474 void SetKeywordSearchTermsForURL(const GURL& url,
475 TemplateURLID keyword_id,
476 const base::string16& term);
478 // Deletes all search terms for the specified keyword.
479 void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id);
481 typedef base::Callback<
482 void(Handle, std::vector<history::KeywordSearchTermVisit>*)>
483 GetMostRecentKeywordSearchTermsCallback;
485 // Returns up to max_count of the most recent search terms starting with the
486 // specified text. The matching is case insensitive. The results are ordered
487 // in descending order up to |max_count| with the most recent search term
489 Handle GetMostRecentKeywordSearchTerms(
490 TemplateURLID keyword_id,
491 const base::string16& prefix,
493 CancelableRequestConsumerBase* consumer,
494 const GetMostRecentKeywordSearchTermsCallback& callback);
496 // Deletes any search term corresponding to |url|.
497 void DeleteKeywordSearchTermForURL(const GURL& url);
499 // Deletes all URL and search term entries matching the given |term| and
501 void DeleteMatchingURLsForKeyword(TemplateURLID keyword_id,
502 const base::string16& term);
504 // Bookmarks -----------------------------------------------------------------
506 // Notification that a URL is no longer bookmarked.
507 void URLsNoLongerBookmarked(const std::set<GURL>& urls);
509 // Generic Stuff -------------------------------------------------------------
511 // Schedules a HistoryDBTask for running on the history backend thread. See
512 // HistoryDBTask for details on what this does.
513 virtual void ScheduleDBTask(history::HistoryDBTask* task,
514 CancelableRequestConsumerBase* consumer);
516 // Adds or removes observers for the VisitDatabase.
517 void AddVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
518 void RemoveVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
520 void NotifyVisitDBObserversOnAddVisit(const history::BriefVisitInfo& info);
522 // Testing -------------------------------------------------------------------
524 // Runs |flushed| after bouncing off the history thread.
525 void FlushForTest(const base::Closure& flushed);
527 // Designed for unit tests, this passes the given task on to the history
528 // backend to be called once the history backend has terminated. This allows
529 // callers to know when the history thread is complete and the database files
530 // can be deleted and the next test run. Otherwise, the history thread may
531 // still be running, causing problems in subsequent tests.
533 // There can be only one closing task, so this will override any previously
534 // set task. We will take ownership of the pointer and delete it when done.
535 // The task will be run on the calling thread (this function is threadsafe).
536 void SetOnBackendDestroyTask(const base::Closure& task);
538 // Used for unit testing and potentially importing to get known information
539 // into the database. This assumes the URL doesn't exist in the database
541 // Calling this function many times may be slow because each call will
542 // dispatch to the history thread and will be a separate database
543 // transaction. If this functionality is needed for importing many URLs,
544 // callers should use AddPagesWithDetails() instead.
546 // Note that this routine (and AddPageWithDetails()) always adds a single
547 // visit using the |last_visit| timestamp, and a PageTransition type of LINK,
548 // if |visit_source| != SYNCED.
549 void AddPageWithDetails(const GURL& url,
550 const base::string16& title,
553 base::Time last_visit,
555 history::VisitSource visit_source);
557 // The same as AddPageWithDetails() but takes a vector.
558 void AddPagesWithDetails(const history::URLRows& info,
559 history::VisitSource visit_source);
561 // Returns true if this looks like the type of URL we want to add to the
562 // history. We filter out some URLs such as JavaScript.
563 static bool CanAddURL(const GURL& url);
565 base::WeakPtr<HistoryService> AsWeakPtr();
567 // syncer::SyncableService implementation.
568 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
569 syncer::ModelType type,
570 const syncer::SyncDataList& initial_sync_data,
571 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
572 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
573 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
574 virtual syncer::SyncDataList GetAllSyncData(
575 syncer::ModelType type) const OVERRIDE;
576 virtual syncer::SyncError ProcessSyncChanges(
577 const tracked_objects::Location& from_here,
578 const syncer::SyncChangeList& change_list) OVERRIDE;
581 // These are not currently used, hopefully we can do something in the future
582 // to ensure that the most important things happen first.
583 enum SchedulePriority {
584 PRIORITY_UI, // The highest priority (must respond to UI events).
585 PRIORITY_NORMAL, // Normal stuff like adding a page.
586 PRIORITY_LOW, // Low priority things like indexing or expiration.
590 class BackendDelegate;
591 #if defined(OS_ANDROID)
592 friend class AndroidHistoryProviderService;
594 friend class base::RefCountedThreadSafe<HistoryService>;
595 friend class BackendDelegate;
596 friend class FaviconService;
597 friend class history::HistoryBackend;
598 friend class history::HistoryQueryTest;
599 friend class HistoryOperation;
600 friend class HistoryQuickProviderTest;
601 friend class HistoryURLProvider;
602 friend class HistoryURLProviderTest;
603 friend class history::InMemoryURLIndexTest;
604 template<typename Info, typename Callback> friend class DownloadRequest;
605 friend class PageUsageRequest;
606 friend class RedirectRequest;
607 friend class TestingProfile;
609 // Implementation of content::NotificationObserver.
610 virtual void Observe(int type,
611 const content::NotificationSource& source,
612 const content::NotificationDetails& details) OVERRIDE;
614 // Implementation of visitedlink::VisitedLinkDelegate.
615 virtual void RebuildTable(
616 const scoped_refptr<URLEnumerator>& enumerator) OVERRIDE;
618 // Low-level Init(). Same as the public version, but adds a |no_db| parameter
619 // that is only set by unittests which causes the backend to not init its DB.
620 bool Init(const base::FilePath& history_dir,
621 BookmarkService* bookmark_service,
624 // Called by the HistoryURLProvider class to schedule an autocomplete, it
625 // will be called back on the internal history thread with the history
626 // database so it can query. See history_autocomplete.cc for a diagram.
627 void ScheduleAutocomplete(HistoryURLProvider* provider,
628 HistoryURLProviderParams* params);
630 // Broadcasts the given notification. This is called by the backend so that
631 // the notification will be broadcast on the main thread.
632 void BroadcastNotificationsHelper(
634 scoped_ptr<history::HistoryDetails> details);
636 // Notification from the backend that it has finished loading. Sends
637 // notification (NOTIFY_HISTORY_LOADED) and sets backend_loaded_ to true.
640 // Helper function for getting URL information.
641 // Reads a URLRow from in-memory database. Returns false if database is not
642 // available or the URL does not exist.
643 bool GetRowForURL(const GURL& url, history::URLRow* url_row);
645 // Favicon -------------------------------------------------------------------
647 // These favicon methods are exposed to the FaviconService. Instead of calling
648 // these methods directly you should call the respective method on the
651 // Used by FaviconService to get the favicon bitmaps from the history backend
652 // which most closely match |desired_size_in_dip| x |desired_size_in_dip| and
653 // |desired_scale_factors| for |icon_types|. If |desired_size_in_dip| is 0,
654 // the largest favicon bitmap for |icon_types| is returned. The returned
655 // FaviconBitmapResults will have at most one result for each of
656 // |desired_scale_factors|. If a favicon bitmap is determined to be the best
657 // candidate for multiple scale factors there will be less results.
658 // If |icon_types| has several types, results for only a single type will be
659 // returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and
661 base::CancelableTaskTracker::TaskId GetFavicons(
662 const std::vector<GURL>& icon_urls,
664 int desired_size_in_dip,
665 const std::vector<ui::ScaleFactor>& desired_scale_factors,
666 const FaviconService::FaviconResultsCallback& callback,
667 base::CancelableTaskTracker* tracker);
669 // Used by the FaviconService to get favicons mapped to |page_url| for
670 // |icon_types| which most closely match |desired_size_in_dip| and
671 // |desired_scale_factors|. If |desired_size_in_dip| is 0, the largest favicon
672 // bitmap for |icon_types| is returned. The returned FaviconBitmapResults will
673 // have at most one result for each of |desired_scale_factors|. If a favicon
674 // bitmap is determined to be the best candidate for multiple scale factors
675 // there will be less results. If |icon_types| has several types, results for
676 // only a single type will be returned in the priority of
677 // TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and FAVICON.
678 base::CancelableTaskTracker::TaskId GetFaviconsForURL(
679 const GURL& page_url,
681 int desired_size_in_dip,
682 const std::vector<ui::ScaleFactor>& desired_scale_factors,
683 const FaviconService::FaviconResultsCallback& callback,
684 base::CancelableTaskTracker* tracker);
686 // Used by FaviconService to find the first favicon bitmap whose width and
687 // height are greater than that of |minimum_size_in_pixels|. This searches
688 // for icons by IconType. Each element of |icon_types| is a bitmask of
689 // IconTypes indicating the types to search for.
690 // If the largest icon of |icon_types[0]| is not larger than
691 // |minimum_size_in_pixel|, the next icon types of
692 // |icon_types| will be searched and so on.
693 // If no icon is larger than |minimum_size_in_pixel|, the largest one of all
694 // icon types in |icon_types| is returned.
695 // This feature is especially useful when some types of icon is perfered as
696 // long as its size is larger than a specific value.
697 base::CancelableTaskTracker::TaskId GetLargestFaviconForURL(
698 const GURL& page_url,
699 const std::vector<int>& icon_types,
700 int minimum_size_in_pixels,
701 const FaviconService::FaviconRawCallback& callback,
702 base::CancelableTaskTracker* tracker);
704 // Used by the FaviconService to get the favicon bitmap which most closely
705 // matches |desired_size_in_dip| and |desired_scale_factor| from the favicon
706 // with |favicon_id| from the history backend. If |desired_size_in_dip| is 0,
707 // the largest favicon bitmap for |favicon_id| is returned.
708 base::CancelableTaskTracker::TaskId GetFaviconForID(
709 favicon_base::FaviconID favicon_id,
710 int desired_size_in_dip,
711 ui::ScaleFactor desired_scale_factor,
712 const FaviconService::FaviconResultsCallback& callback,
713 base::CancelableTaskTracker* tracker);
715 // Used by the FaviconService to replace the favicon mappings to |page_url|
716 // for |icon_types| on the history backend.
717 // Sample |icon_urls|:
718 // { ICON_URL1 -> TOUCH_ICON, known to the database,
719 // ICON_URL2 -> TOUCH_ICON, not known to the database,
720 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
721 // The new mappings are computed from |icon_urls| with these rules:
722 // 1) Any urls in |icon_urls| which are not already known to the database are
724 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
725 // 2) If |icon_types| has multiple types, the mappings are only set for the
726 // largest icon type.
727 // Sample new mappings to |page_url|: { ICON_URL3 }
728 // |icon_types| can only have multiple IconTypes if
729 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
730 // The favicon bitmaps which most closely match |desired_size_in_dip|
731 // and |desired_scale_factors| from the favicons which were just mapped
732 // to |page_url| are returned. If |desired_size_in_dip| is 0, the
733 // largest favicon bitmap is returned.
734 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
735 const GURL& page_url,
736 const std::vector<GURL>& icon_urls,
738 int desired_size_in_dip,
739 const std::vector<ui::ScaleFactor>& desired_scale_factors,
740 const FaviconService::FaviconResultsCallback& callback,
741 base::CancelableTaskTracker* tracker);
743 // Used by FaviconService to set a favicon for |page_url| and |icon_url| with
746 // |page_url|: www.google.com
747 // 2 favicons in history for |page_url|:
748 // www.google.com/a.ico 16x16
749 // www.google.com/b.ico 32x32
750 // MergeFavicon(|page_url|, www.google.com/a.ico, ..., ..., 16x16)
752 // Merging occurs in the following manner:
753 // 1) |page_url| is set to map to only to |icon_url|. In order to not lose
754 // data, favicon bitmaps mapped to |page_url| but not to |icon_url| are
755 // copied to the favicon at |icon_url|.
756 // For the example above, |page_url| will only be mapped to a.ico.
757 // The 32x32 favicon bitmap at b.ico is copied to a.ico
758 // 2) |bitmap_data| is added to the favicon at |icon_url|, overwriting any
759 // favicon bitmaps of |pixel_size|.
760 // For the example above, |bitmap_data| overwrites the 16x16 favicon
762 // TODO(pkotwicz): Remove once no longer required by sync.
763 void MergeFavicon(const GURL& page_url,
764 const GURL& icon_url,
765 favicon_base::IconType icon_type,
766 scoped_refptr<base::RefCountedMemory> bitmap_data,
767 const gfx::Size& pixel_size);
769 // Used by the FaviconService to set the favicons for a page on the history
771 // |favicon_bitmap_data| replaces all the favicon bitmaps mapped to
773 // |expired| and |icon_type| fields in FaviconBitmapData are ignored.
774 // Use MergeFavicon() if |favicon_bitmap_data| is incomplete, and favicon
775 // bitmaps in the database should be preserved if possible. For instance,
776 // favicon bitmaps from sync are 1x only. MergeFavicon() is used to avoid
777 // deleting the 2x favicon bitmap if it is present in the history backend.
778 // See HistoryBackend::ValidateSetFaviconsParams() for more details on the
779 // criteria for |favicon_bitmap_data| to be valid.
781 const GURL& page_url,
782 favicon_base::IconType icon_type,
783 const std::vector<favicon_base::FaviconBitmapData>& favicon_bitmap_data);
785 // Used by the FaviconService to mark the favicon for the page as being out
787 void SetFaviconsOutOfDateForPage(const GURL& page_url);
789 // Used by the FaviconService to clone favicons from one page to another,
790 // provided that other page does not already have favicons.
791 void CloneFavicons(const GURL& old_page_url, const GURL& new_page_url);
793 // Used by the FaviconService for importing many favicons for many pages at
794 // once. The pages must exist, any favicon sets for unknown pages will be
795 // discarded. Existing favicons will not be overwritten.
796 void SetImportedFavicons(
797 const std::vector<ImportedFaviconUsage>& favicon_usage);
799 // Sets the in-memory URL database. This is called by the backend once the
800 // database is loaded to make it available.
801 void SetInMemoryBackend(
802 scoped_ptr<history::InMemoryHistoryBackend> mem_backend);
804 // Called by our BackendDelegate when there is a problem reading the database.
805 void NotifyProfileError(sql::InitStatus init_status);
807 // Call to schedule a given task for running on the history thread with the
808 // specified priority. The task will have ownership taken.
809 void ScheduleTask(SchedulePriority priority, const base::Closure& task);
811 // Schedule ------------------------------------------------------------------
813 // Functions for scheduling operations on the history thread that have a
814 // handle and may be cancelable. For fire-and-forget operations, see
815 // ScheduleAndForget below.
817 template<typename BackendFunc, class RequestType>
818 Handle Schedule(SchedulePriority priority,
819 BackendFunc func, // Function to call on the HistoryBackend.
820 CancelableRequestConsumerBase* consumer,
821 RequestType* request) {
822 DCHECK(thread_) << "History service being called after cleanup";
823 DCHECK(thread_checker_.CalledOnValidThread());
825 AddRequest(request, consumer);
826 ScheduleTask(priority,
827 base::Bind(func, history_backend_.get(),
828 scoped_refptr<RequestType>(request)));
829 return request->handle();
832 template<typename BackendFunc, class RequestType, typename ArgA>
833 Handle Schedule(SchedulePriority priority,
834 BackendFunc func, // Function to call on the HistoryBackend.
835 CancelableRequestConsumerBase* consumer,
836 RequestType* request,
838 DCHECK(thread_) << "History service being called after cleanup";
839 DCHECK(thread_checker_.CalledOnValidThread());
841 AddRequest(request, consumer);
842 ScheduleTask(priority,
843 base::Bind(func, history_backend_.get(),
844 scoped_refptr<RequestType>(request), a));
845 return request->handle();
848 template<typename BackendFunc,
849 class RequestType, // Descendant of CancelableRequestBase.
852 Handle Schedule(SchedulePriority priority,
853 BackendFunc func, // Function to call on the HistoryBackend.
854 CancelableRequestConsumerBase* consumer,
855 RequestType* request,
858 DCHECK(thread_) << "History service being called after cleanup";
859 DCHECK(thread_checker_.CalledOnValidThread());
861 AddRequest(request, consumer);
862 ScheduleTask(priority,
863 base::Bind(func, history_backend_.get(),
864 scoped_refptr<RequestType>(request), a, b));
865 return request->handle();
868 template<typename BackendFunc,
869 class RequestType, // Descendant of CancelableRequestBase.
873 Handle Schedule(SchedulePriority priority,
874 BackendFunc func, // Function to call on the HistoryBackend.
875 CancelableRequestConsumerBase* consumer,
876 RequestType* request,
880 DCHECK(thread_) << "History service being called after cleanup";
881 DCHECK(thread_checker_.CalledOnValidThread());
883 AddRequest(request, consumer);
884 ScheduleTask(priority,
885 base::Bind(func, history_backend_.get(),
886 scoped_refptr<RequestType>(request), a, b, c));
887 return request->handle();
890 template<typename BackendFunc,
891 class RequestType, // Descendant of CancelableRequestBase.
896 Handle Schedule(SchedulePriority priority,
897 BackendFunc func, // Function to call on the HistoryBackend.
898 CancelableRequestConsumerBase* consumer,
899 RequestType* request,
904 DCHECK(thread_) << "History service being called after cleanup";
905 DCHECK(thread_checker_.CalledOnValidThread());
907 AddRequest(request, consumer);
908 ScheduleTask(priority,
909 base::Bind(func, history_backend_.get(),
910 scoped_refptr<RequestType>(request), a, b, c, d));
911 return request->handle();
914 // ScheduleAndForget ---------------------------------------------------------
916 // Functions for scheduling operations on the history thread that do not need
917 // any callbacks and are not cancelable.
919 template<typename BackendFunc>
920 void ScheduleAndForget(SchedulePriority priority,
921 BackendFunc func) { // Function to call on backend.
922 DCHECK(thread_) << "History service being called after cleanup";
923 DCHECK(thread_checker_.CalledOnValidThread());
924 ScheduleTask(priority, base::Bind(func, history_backend_.get()));
927 template<typename BackendFunc, typename ArgA>
928 void ScheduleAndForget(SchedulePriority priority,
929 BackendFunc func, // Function to call on backend.
931 DCHECK(thread_) << "History service being called after cleanup";
932 DCHECK(thread_checker_.CalledOnValidThread());
933 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a));
936 template<typename BackendFunc, typename ArgA, typename ArgB>
937 void ScheduleAndForget(SchedulePriority priority,
938 BackendFunc func, // Function to call on backend.
941 DCHECK(thread_) << "History service being called after cleanup";
942 DCHECK(thread_checker_.CalledOnValidThread());
943 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b));
946 template<typename BackendFunc, typename ArgA, typename ArgB, typename ArgC>
947 void ScheduleAndForget(SchedulePriority priority,
948 BackendFunc func, // Function to call on backend.
952 DCHECK(thread_) << "History service being called after cleanup";
953 DCHECK(thread_checker_.CalledOnValidThread());
954 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b, c));
957 template<typename BackendFunc,
962 void ScheduleAndForget(SchedulePriority priority,
963 BackendFunc func, // Function to call on backend.
968 DCHECK(thread_) << "History service being called after cleanup";
969 DCHECK(thread_checker_.CalledOnValidThread());
970 ScheduleTask(priority, base::Bind(func, history_backend_.get(),
974 template<typename BackendFunc,
980 void ScheduleAndForget(SchedulePriority priority,
981 BackendFunc func, // Function to call on backend.
987 DCHECK(thread_) << "History service being called after cleanup";
988 DCHECK(thread_checker_.CalledOnValidThread());
989 ScheduleTask(priority, base::Bind(func, history_backend_.get(),
993 // All vended weak pointers are invalidated in Cleanup().
994 base::WeakPtrFactory<HistoryService> weak_ptr_factory_;
996 base::ThreadChecker thread_checker_;
998 content::NotificationRegistrar registrar_;
1000 // Some void primitives require some internal processing in the main thread
1001 // when done. We use this internal consumer for this purpose.
1002 CancelableRequestConsumer internal_consumer_;
1004 // The thread used by the history service to run complicated operations.
1005 // |thread_| is NULL once |Cleanup| is NULL.
1006 base::Thread* thread_;
1008 // This class has most of the implementation and runs on the 'thread_'.
1009 // You MUST communicate with this class ONLY through the thread_'s
1012 // This pointer will be NULL once Cleanup() has been called, meaning no
1013 // more calls should be made to the history thread.
1014 scoped_refptr<history::HistoryBackend> history_backend_;
1016 // A cache of the user-typed URLs kept in memory that is used by the
1017 // autocomplete system. This will be NULL until the database has been created
1018 // on the background thread.
1019 // TODO(mrossetti): Consider changing ownership. See http://crbug.com/138321
1020 scoped_ptr<history::InMemoryHistoryBackend> in_memory_backend_;
1022 // The profile, may be null when testing.
1025 // Used for propagating link highlighting data across renderers. May be null
1027 scoped_ptr<visitedlink::VisitedLinkMaster> visitedlink_master_;
1029 // Has the backend finished loading? The backend is loaded once Init has
1031 bool backend_loaded_;
1033 // Cached values from Init(), used whenever we need to reload the backend.
1034 base::FilePath history_dir_;
1035 BookmarkService* bookmark_service_;
1038 // The index used for quick history lookups.
1039 // TODO(mrossetti): Move in_memory_url_index out of history_service.
1040 // See http://crbug.com/138321
1041 scoped_ptr<history::InMemoryURLIndex> in_memory_url_index_;
1043 ObserverList<history::VisitDatabaseObserver> visit_database_observers_;
1045 history::DeleteDirectiveHandler delete_directive_handler_;
1047 DISALLOW_COPY_AND_ASSIGN(HistoryService);
1050 #endif // CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_