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/browser_context_keyed_service/browser_context_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,
88 public BrowserContextKeyedService,
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 // Unloads the backend without actually shutting down the history service.
118 // This can be used to temporarily reduce the browser process' memory
120 void UnloadBackend();
122 // Called on shutdown, this will tell the history backend to complete and
123 // will release pointers to it. No other functions should be called once
124 // cleanup has happened that may dispatch to the history thread (because it
127 // In practice, this will be called by the service manager (BrowserProcess)
128 // when it is being destroyed. Because that reference is being destroyed, it
129 // should be impossible for anybody else to call the service, even if it is
130 // still in memory (pending requests may be holding a reference to us).
133 // RenderProcessHost pointers are used to scope page IDs (see AddPage). These
134 // objects must tell us when they are being destroyed so that we can clear
135 // out any cached data associated with that scope.
137 // The given pointer will not be dereferenced, it is only used for
138 // identification purposes, hence it is a void*.
139 void NotifyRenderProcessHostDestruction(const void* host);
141 // Triggers the backend to load if it hasn't already, and then returns the
142 // in-memory URL database. The returned pointer MAY BE NULL if the in-memory
143 // database has not been loaded yet. This pointer is owned by the history
144 // system. Callers should not store or cache this value.
146 // TODO(brettw) this should return the InMemoryHistoryBackend.
147 history::URLDatabase* InMemoryDatabase();
149 // Following functions get URL information from in-memory database.
150 // They return false if database is not available (e.g. not loaded yet) or the
151 // URL does not exist.
153 // Reads the number of times the user has typed the given URL.
154 bool GetTypedCountForURL(const GURL& url, int* typed_count);
156 // Reads the last visit time for the given URL.
157 bool GetLastVisitTimeForURL(const GURL& url, base::Time* last_visit);
159 // Reads the number of times this URL has been visited.
160 bool GetVisitCountForURL(const GURL& url, int* visit_count);
162 // Returns a pointer to the TypedUrlSyncableService owned by HistoryBackend.
163 // This method should only be called from the history thread, because the
164 // returned service is intended to be accessed only via the history thread.
165 history::TypedUrlSyncableService* GetTypedUrlSyncableService() const;
167 // Return the quick history index.
168 history::InMemoryURLIndex* InMemoryIndex() const {
169 return in_memory_url_index_.get();
172 // BrowserContextKeyedService:
173 virtual void Shutdown() OVERRIDE;
175 // Navigation ----------------------------------------------------------------
177 // Adds the given canonical URL to history with the given time as the visit
178 // time. Referrer may be the empty string.
180 // The supplied render process host is used to scope the given page ID. Page
181 // IDs are only unique inside a given render process, so we need that to
182 // differentiate them. This pointer should not be dereferenced by the history
185 // The scope/ids can be NULL if there is no meaningful tracking information
186 // that can be performed on the given URL. The 'page_id' should be the ID of
187 // the current session history entry in the given process.
189 // 'redirects' is an array of redirect URLs leading to this page, with the
190 // page itself as the last item (so when there is no redirect, it will have
191 // one entry). If there are no redirects, this array may also be empty for
192 // the convenience of callers.
194 // 'did_replace_entry' is true when the navigation entry for this page has
195 // replaced the existing entry. A non-user initiated redirect causes such
198 // All "Add Page" functions will update the visited link database.
199 void AddPage(const GURL& url,
201 const void* id_scope,
203 const GURL& referrer,
204 const history::RedirectList& redirects,
205 content::PageTransition transition,
206 history::VisitSource visit_source,
207 bool did_replace_entry);
209 // For adding pages to history where no tracking information can be done.
210 void AddPage(const GURL& url,
212 history::VisitSource visit_source);
214 // All AddPage variants end up here.
215 void AddPage(const history::HistoryAddPageArgs& add_page_args);
217 // Adds an entry for the specified url without creating a visit. This should
218 // only be used when bookmarking a page, otherwise the row leaks in the
219 // history db (it never gets cleaned).
220 void AddPageNoVisitForBookmark(const GURL& url, const base::string16& title);
222 // Sets the title for the given page. The page should be in history. If it
223 // is not, this operation is ignored.
224 void SetPageTitle(const GURL& url, const base::string16& title);
226 // Updates the history database with a page's ending time stamp information.
227 // The page can be identified by the combination of the pointer to
228 // a RenderProcessHost, the page id and the url.
230 // The given pointer will not be dereferenced, it is only used for
231 // identification purposes, hence it is a void*.
232 void UpdateWithPageEndTime(const void* host,
237 // Querying ------------------------------------------------------------------
239 // Returns the information about the requested URL. If the URL is found,
240 // success will be true and the information will be in the URLRow parameter.
241 // On success, the visits, if requested, will be sorted by date. If they have
242 // not been requested, the pointer will be valid, but the vector will be
245 // If success is false, neither the row nor the vector will be valid.
246 typedef base::Callback<void(
248 bool, // Success flag, when false, nothing else is valid.
249 const history::URLRow*,
250 history::VisitVector*)> QueryURLCallback;
252 // Queries the basic information about the URL in the history database. If
253 // the caller is interested in the visits (each time the URL is visited),
254 // set |want_visits| to true. If these are not needed, the function will be
255 // faster by setting this to false.
256 Handle QueryURL(const GURL& url,
258 CancelableRequestConsumerBase* consumer,
259 const QueryURLCallback& callback);
261 // Provides the result of a query. See QueryResults in history_types.h.
262 // The common use will be to use QueryResults.Swap to suck the contents of
263 // the results out of the passed in parameter and take ownership of them.
264 typedef base::Callback<void(Handle, history::QueryResults*)>
265 QueryHistoryCallback;
267 // Queries all history with the given options (see QueryOptions in
268 // history_types.h). If empty, all results matching the given options
270 Handle QueryHistory(const base::string16& text_query,
271 const history::QueryOptions& options,
272 CancelableRequestConsumerBase* consumer,
273 const QueryHistoryCallback& callback);
275 // Called when the results of QueryRedirectsFrom are available.
276 // The given vector will contain a list of all redirects, not counting
277 // the original page. If A redirects to B, the vector will contain only B,
278 // and A will be in 'source_url'.
280 // If there is no such URL in the database or the most recent visit has no
281 // redirect, the vector will be empty. If the history system failed for
282 // some reason, success will additionally be false. If the given page
283 // has redirected to multiple destinations, this will pick a random one.
284 typedef base::Callback<void(Handle,
287 history::RedirectList*)> QueryRedirectsCallback;
289 // Schedules a query for the most recent redirect coming out of the given
290 // URL. See the RedirectQuerySource above, which is guaranteed to be called
291 // if the request is not canceled.
292 Handle QueryRedirectsFrom(const GURL& from_url,
293 CancelableRequestConsumerBase* consumer,
294 const QueryRedirectsCallback& callback);
296 // Schedules a query to get the most recent redirects ending at the given
298 Handle QueryRedirectsTo(const GURL& to_url,
299 CancelableRequestConsumerBase* consumer,
300 const QueryRedirectsCallback& callback);
302 typedef base::Callback<
304 bool, // Were we able to determine the # of visits?
305 int, // Number of visits.
306 base::Time)> // Time of first visit. Only set if bool
307 // is true and int is > 0.
308 GetVisibleVisitCountToHostCallback;
310 // Requests the number of user-visible visits (i.e. no redirects or subframes)
311 // to all urls on the same scheme/host/port as |url|. This is only valid for
312 // HTTP and HTTPS URLs.
313 Handle GetVisibleVisitCountToHost(
315 CancelableRequestConsumerBase* consumer,
316 const GetVisibleVisitCountToHostCallback& callback);
318 // Called when QueryTopURLsAndRedirects completes. The vector contains a list
319 // of the top |result_count| URLs. For each of these URLs, there is an entry
320 // in the map containing redirects from the URL. For example, if we have the
321 // redirect chain A -> B -> C and A is a top visited URL, then A will be in
322 // the vector and "A => {B -> C}" will be in the map.
323 typedef base::Callback<
325 bool, // Did we get the top urls and redirects?
326 std::vector<GURL>*, // List of top URLs.
327 history::RedirectMap*)> // Redirects for top URLs.
328 QueryTopURLsAndRedirectsCallback;
330 // Request the top |result_count| most visited URLs and the chain of redirects
331 // leading to each of these URLs.
332 // TODO(Nik): remove this. Use QueryMostVisitedURLs instead.
333 Handle QueryTopURLsAndRedirects(
335 CancelableRequestConsumerBase* consumer,
336 const QueryTopURLsAndRedirectsCallback& callback);
338 typedef base::Callback<void(Handle, history::MostVisitedURLList)>
339 QueryMostVisitedURLsCallback;
341 typedef base::Callback<void(Handle, const history::FilteredURLList&)>
342 QueryFilteredURLsCallback;
344 // Request the |result_count| most visited URLs and the chain of
345 // redirects leading to each of these URLs. |days_back| is the
346 // number of days of history to use. Used by TopSites.
347 Handle QueryMostVisitedURLs(int result_count, int days_back,
348 CancelableRequestConsumerBase* consumer,
349 const QueryMostVisitedURLsCallback& callback);
351 // Request the |result_count| URLs filtered and sorted based on the |filter|.
352 // If |extended_info| is true, additional data will be provided in the
353 // results. Computing this additional data is expensive, likely to become
354 // more expensive as additional data points are added in future changes, and
355 // not useful in most cases. Set |extended_info| to true only if you
356 // explicitly require the additional data.
357 Handle QueryFilteredURLs(
359 const history::VisitFilter& filter,
361 CancelableRequestConsumerBase* consumer,
362 const QueryFilteredURLsCallback& callback);
364 // Database management operations --------------------------------------------
366 // Delete all the information related to a single url.
367 void DeleteURL(const GURL& url);
369 // Delete all the information related to a list of urls. (Deleting
370 // URLs one by one is slow as it has to flush to disk each time.)
371 void DeleteURLsForTest(const std::vector<GURL>& urls);
373 // Removes all visits in the selected time range (including the
374 // start time), updating the URLs accordingly. This deletes any
375 // associated data. This function also deletes the associated
376 // favicons, if they are no longer referenced. |callback| runs when
377 // the expiration is complete. You may use null Time values to do an
378 // unbounded delete in either direction.
379 // If |restrict_urls| is not empty, only visits to the URLs in this set are
381 void ExpireHistoryBetween(const std::set<GURL>& restrict_urls,
382 base::Time begin_time,
384 const base::Closure& callback,
385 base::CancelableTaskTracker* tracker);
387 // Removes all visits to specified URLs in specific time ranges.
388 // This is the equivalent ExpireHistoryBetween() once for each element in the
389 // vector. The fields of |ExpireHistoryArgs| map directly to the arguments of
390 // of ExpireHistoryBetween().
391 void ExpireHistory(const std::vector<history::ExpireHistoryArgs>& expire_list,
392 const base::Closure& callback,
393 base::CancelableTaskTracker* tracker);
395 // Removes all visits to the given URLs in the specified time range. Calls
396 // ExpireHistoryBetween() to delete local visits, and handles deletion of
397 // synced visits if appropriate.
398 void ExpireLocalAndRemoteHistoryBetween(const std::set<GURL>& restrict_urls,
399 base::Time begin_time,
401 const base::Closure& callback,
402 base::CancelableTaskTracker* tracker);
404 // Processes the given |delete_directive| and sends it to the
405 // SyncChangeProcessor (if it exists). Returns any error resulting
406 // from sending the delete directive to sync.
407 syncer::SyncError ProcessLocalDeleteDirective(
408 const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive);
410 // Downloads -----------------------------------------------------------------
412 // Implemented by the caller of 'CreateDownload' below, and is called when the
413 // history service has created a new entry for a download in the history db.
414 typedef base::Callback<void(bool)> DownloadCreateCallback;
416 // Begins a history request to create a new row for a download. 'info'
417 // contains all the download's creation state, and 'callback' runs when the
418 // history service request is complete. The callback is called on the thread
419 // that calls CreateDownload().
421 const history::DownloadRow& info,
422 const DownloadCreateCallback& callback);
424 // Responds on the calling thread with the maximum id of all downloads records
425 // in the database plus 1.
426 void GetNextDownloadId(const content::DownloadIdCallback& callback);
428 // Implemented by the caller of 'QueryDownloads' below, and is called when the
429 // history service has retrieved a list of all download state. The call
430 typedef base::Callback<void(
431 scoped_ptr<std::vector<history::DownloadRow> >)>
432 DownloadQueryCallback;
434 // Begins a history request to retrieve the state of all downloads in the
435 // history db. 'callback' runs when the history service request is complete,
436 // at which point 'info' contains an array of history::DownloadRow, one per
437 // download. The callback is called on the thread that calls QueryDownloads().
438 void QueryDownloads(const DownloadQueryCallback& callback);
440 // Called to update the history service about the current state of a download.
441 // This is a 'fire and forget' query, so just pass the relevant state info to
442 // the database with no need for a callback.
443 void UpdateDownload(const history::DownloadRow& data);
445 // Permanently remove some downloads from the history system. This is a 'fire
446 // and forget' operation.
447 void RemoveDownloads(const std::set<uint32>& ids);
449 // Visit Segments ------------------------------------------------------------
451 typedef base::Callback<void(Handle, std::vector<PageUsageData*>*)>
452 SegmentQueryCallback;
454 // Query usage data for all visit segments since the provided time.
456 // The request is performed asynchronously and can be cancelled by using the
459 // The vector provided to the callback and its contents is owned by the
460 // history system. It will be deeply deleted after the callback is invoked.
461 // If you want to preserve any PageUsageData instance, simply remove them
464 // The vector contains a list of PageUsageData. Each PageUsageData ID is set
465 // to the segment ID. The URL and all the other information is set to the page
466 // representing the segment.
467 Handle QuerySegmentUsageSince(CancelableRequestConsumerBase* consumer,
468 const base::Time from_time,
469 int max_result_count,
470 const SegmentQueryCallback& callback);
472 // Keyword search terms -----------------------------------------------------
474 // Sets the search terms for the specified url and keyword. url_id gives the
475 // id of the url, keyword_id the id of the keyword and term the search term.
476 void SetKeywordSearchTermsForURL(const GURL& url,
477 TemplateURLID keyword_id,
478 const base::string16& term);
480 // Deletes all search terms for the specified keyword.
481 void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id);
483 typedef base::Callback<
484 void(Handle, std::vector<history::KeywordSearchTermVisit>*)>
485 GetMostRecentKeywordSearchTermsCallback;
487 // Returns up to max_count of the most recent search terms starting with the
488 // specified text. The matching is case insensitive. The results are ordered
489 // in descending order up to |max_count| with the most recent search term
491 Handle GetMostRecentKeywordSearchTerms(
492 TemplateURLID keyword_id,
493 const base::string16& prefix,
495 CancelableRequestConsumerBase* consumer,
496 const GetMostRecentKeywordSearchTermsCallback& callback);
498 // Deletes any search term corresponding to |url|.
499 void DeleteKeywordSearchTermForURL(const GURL& url);
501 // Deletes all URL and search term entries matching the given |term| and
503 void DeleteMatchingURLsForKeyword(TemplateURLID keyword_id,
504 const base::string16& term);
506 // Bookmarks -----------------------------------------------------------------
508 // Notification that a URL is no longer bookmarked.
509 void URLsNoLongerBookmarked(const std::set<GURL>& urls);
511 // Generic Stuff -------------------------------------------------------------
513 // Schedules a HistoryDBTask for running on the history backend thread. See
514 // HistoryDBTask for details on what this does.
515 virtual void ScheduleDBTask(history::HistoryDBTask* task,
516 CancelableRequestConsumerBase* consumer);
518 // Adds or removes observers for the VisitDatabase.
519 void AddVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
520 void RemoveVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
522 void NotifyVisitDBObserversOnAddVisit(const history::BriefVisitInfo& info);
524 // Testing -------------------------------------------------------------------
526 // Runs |flushed| after bouncing off the history thread.
527 void FlushForTest(const base::Closure& flushed);
529 // Designed for unit tests, this passes the given task on to the history
530 // backend to be called once the history backend has terminated. This allows
531 // callers to know when the history thread is complete and the database files
532 // can be deleted and the next test run. Otherwise, the history thread may
533 // still be running, causing problems in subsequent tests.
535 // There can be only one closing task, so this will override any previously
536 // set task. We will take ownership of the pointer and delete it when done.
537 // The task will be run on the calling thread (this function is threadsafe).
538 void SetOnBackendDestroyTask(const base::Closure& task);
540 // Used for unit testing and potentially importing to get known information
541 // into the database. This assumes the URL doesn't exist in the database
543 // Calling this function many times may be slow because each call will
544 // dispatch to the history thread and will be a separate database
545 // transaction. If this functionality is needed for importing many URLs,
546 // callers should use AddPagesWithDetails() instead.
548 // Note that this routine (and AddPageWithDetails()) always adds a single
549 // visit using the |last_visit| timestamp, and a PageTransition type of LINK,
550 // if |visit_source| != SYNCED.
551 void AddPageWithDetails(const GURL& url,
552 const base::string16& title,
555 base::Time last_visit,
557 history::VisitSource visit_source);
559 // The same as AddPageWithDetails() but takes a vector.
560 void AddPagesWithDetails(const history::URLRows& info,
561 history::VisitSource visit_source);
563 // Returns true if this looks like the type of URL we want to add to the
564 // history. We filter out some URLs such as JavaScript.
565 static bool CanAddURL(const GURL& url);
567 base::WeakPtr<HistoryService> AsWeakPtr();
569 // syncer::SyncableService implementation.
570 virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
571 syncer::ModelType type,
572 const syncer::SyncDataList& initial_sync_data,
573 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
574 scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
575 virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
576 virtual syncer::SyncDataList GetAllSyncData(
577 syncer::ModelType type) const OVERRIDE;
578 virtual syncer::SyncError ProcessSyncChanges(
579 const tracked_objects::Location& from_here,
580 const syncer::SyncChangeList& change_list) OVERRIDE;
583 // These are not currently used, hopefully we can do something in the future
584 // to ensure that the most important things happen first.
585 enum SchedulePriority {
586 PRIORITY_UI, // The highest priority (must respond to UI events).
587 PRIORITY_NORMAL, // Normal stuff like adding a page.
588 PRIORITY_LOW, // Low priority things like indexing or expiration.
592 class BackendDelegate;
593 #if defined(OS_ANDROID)
594 friend class AndroidHistoryProviderService;
596 friend class base::RefCountedThreadSafe<HistoryService>;
597 friend class BackendDelegate;
598 friend class FaviconService;
599 friend class history::HistoryBackend;
600 friend class history::HistoryQueryTest;
601 friend class HistoryOperation;
602 friend class HistoryQuickProviderTest;
603 friend class HistoryURLProvider;
604 friend class HistoryURLProviderTest;
605 friend class history::InMemoryURLIndexTest;
606 template<typename Info, typename Callback> friend class DownloadRequest;
607 friend class PageUsageRequest;
608 friend class RedirectRequest;
609 friend class TestingProfile;
611 // Implementation of content::NotificationObserver.
612 virtual void Observe(int type,
613 const content::NotificationSource& source,
614 const content::NotificationDetails& details) OVERRIDE;
616 // Implementation of visitedlink::VisitedLinkDelegate.
617 virtual void RebuildTable(
618 const scoped_refptr<URLEnumerator>& enumerator) OVERRIDE;
620 // Low-level Init(). Same as the public version, but adds a |no_db| parameter
621 // that is only set by unittests which causes the backend to not init its DB.
622 bool Init(const base::FilePath& history_dir,
623 BookmarkService* bookmark_service,
626 // Called by the HistoryURLProvider class to schedule an autocomplete, it
627 // will be called back on the internal history thread with the history
628 // database so it can query. See history_autocomplete.cc for a diagram.
629 void ScheduleAutocomplete(HistoryURLProvider* provider,
630 HistoryURLProviderParams* params);
632 // Broadcasts the given notification. This is called by the backend so that
633 // the notification will be broadcast on the main thread.
635 // Compared to BroadcastNotifications(), this function does not take
636 // ownership of |details|.
637 void BroadcastNotificationsHelper(int type,
638 history::HistoryDetails* details);
640 // Initializes the backend.
641 void LoadBackendIfNecessary();
643 // Notification from the backend that it has finished loading. Sends
644 // notification (NOTIFY_HISTORY_LOADED) and sets backend_loaded_ to true.
645 void OnDBLoaded(int backend_id);
647 // Helper function for getting URL information.
648 // Reads a URLRow from in-memory database. Returns false if database is not
649 // available or the URL does not exist.
650 bool GetRowForURL(const GURL& url, history::URLRow* url_row);
652 // Favicon -------------------------------------------------------------------
654 // These favicon methods are exposed to the FaviconService. Instead of calling
655 // these methods directly you should call the respective method on the
658 // Used by FaviconService to get the favicon bitmaps from the history backend
659 // which most closely match |desired_size_in_dip| x |desired_size_in_dip| and
660 // |desired_scale_factors| for |icon_types|. If |desired_size_in_dip| is 0,
661 // the largest favicon bitmap for |icon_types| is returned. The returned
662 // FaviconBitmapResults will have at most one result for each of
663 // |desired_scale_factors|. If a favicon bitmap is determined to be the best
664 // candidate for multiple scale factors there will be less results.
665 // If |icon_types| has several types, results for only a single type will be
666 // returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and
668 base::CancelableTaskTracker::TaskId GetFavicons(
669 const std::vector<GURL>& icon_urls,
671 int desired_size_in_dip,
672 const std::vector<ui::ScaleFactor>& desired_scale_factors,
673 const FaviconService::FaviconResultsCallback& callback,
674 base::CancelableTaskTracker* tracker);
676 // Used by the FaviconService to get favicons mapped to |page_url| for
677 // |icon_types| which most closely match |desired_size_in_dip| and
678 // |desired_scale_factors|. If |desired_size_in_dip| is 0, the largest favicon
679 // bitmap for |icon_types| is returned. The returned FaviconBitmapResults will
680 // have at most one result for each of |desired_scale_factors|. If a favicon
681 // bitmap is determined to be the best candidate for multiple scale factors
682 // there will be less results. If |icon_types| has several types, results for
683 // only a single type will be returned in the priority of
684 // TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and FAVICON.
685 base::CancelableTaskTracker::TaskId GetFaviconsForURL(
686 const GURL& page_url,
688 int desired_size_in_dip,
689 const std::vector<ui::ScaleFactor>& desired_scale_factors,
690 const FaviconService::FaviconResultsCallback& callback,
691 base::CancelableTaskTracker* tracker);
693 // Used by FaviconService to find the first favicon bitmap whose width and
694 // height are greater than that of |minimum_size_in_pixels|. This searches
695 // for icons by IconType. Each element of |icon_types| is a bitmask of
696 // IconTypes indicating the types to search for.
697 // If the largest icon of |icon_types[0]| is not larger than
698 // |minimum_size_in_pixel|, the next icon types of
699 // |icon_types| will be searched and so on.
700 // If no icon is larger than |minimum_size_in_pixel|, the largest one of all
701 // icon types in |icon_types| is returned.
702 // This feature is especially useful when some types of icon is perfered as
703 // long as its size is larger than a specific value.
704 base::CancelableTaskTracker::TaskId GetLargestFaviconForURL(
705 const GURL& page_url,
706 const std::vector<int>& icon_types,
707 int minimum_size_in_pixels,
708 const FaviconService::FaviconRawCallback& callback,
709 base::CancelableTaskTracker* tracker);
711 // Used by the FaviconService to get the favicon bitmap which most closely
712 // matches |desired_size_in_dip| and |desired_scale_factor| from the favicon
713 // with |favicon_id| from the history backend. If |desired_size_in_dip| is 0,
714 // the largest favicon bitmap for |favicon_id| is returned.
715 base::CancelableTaskTracker::TaskId GetFaviconForID(
716 chrome::FaviconID favicon_id,
717 int desired_size_in_dip,
718 ui::ScaleFactor desired_scale_factor,
719 const FaviconService::FaviconResultsCallback& callback,
720 base::CancelableTaskTracker* tracker);
722 // Used by the FaviconService to replace the favicon mappings to |page_url|
723 // for |icon_types| on the history backend.
724 // Sample |icon_urls|:
725 // { ICON_URL1 -> TOUCH_ICON, known to the database,
726 // ICON_URL2 -> TOUCH_ICON, not known to the database,
727 // ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
728 // The new mappings are computed from |icon_urls| with these rules:
729 // 1) Any urls in |icon_urls| which are not already known to the database are
731 // Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
732 // 2) If |icon_types| has multiple types, the mappings are only set for the
733 // largest icon type.
734 // Sample new mappings to |page_url|: { ICON_URL3 }
735 // |icon_types| can only have multiple IconTypes if
736 // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
737 // The favicon bitmaps which most closely match |desired_size_in_dip|
738 // and |desired_scale_factors| from the favicons which were just mapped
739 // to |page_url| are returned. If |desired_size_in_dip| is 0, the
740 // largest favicon bitmap is returned.
741 base::CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
742 const GURL& page_url,
743 const std::vector<GURL>& icon_urls,
745 int desired_size_in_dip,
746 const std::vector<ui::ScaleFactor>& desired_scale_factors,
747 const FaviconService::FaviconResultsCallback& callback,
748 base::CancelableTaskTracker* tracker);
750 // Used by FaviconService to set a favicon for |page_url| and |icon_url| with
753 // |page_url|: www.google.com
754 // 2 favicons in history for |page_url|:
755 // www.google.com/a.ico 16x16
756 // www.google.com/b.ico 32x32
757 // MergeFavicon(|page_url|, www.google.com/a.ico, ..., ..., 16x16)
759 // Merging occurs in the following manner:
760 // 1) |page_url| is set to map to only to |icon_url|. In order to not lose
761 // data, favicon bitmaps mapped to |page_url| but not to |icon_url| are
762 // copied to the favicon at |icon_url|.
763 // For the example above, |page_url| will only be mapped to a.ico.
764 // The 32x32 favicon bitmap at b.ico is copied to a.ico
765 // 2) |bitmap_data| is added to the favicon at |icon_url|, overwriting any
766 // favicon bitmaps of |pixel_size|.
767 // For the example above, |bitmap_data| overwrites the 16x16 favicon
769 // TODO(pkotwicz): Remove once no longer required by sync.
770 void MergeFavicon(const GURL& page_url,
771 const GURL& icon_url,
772 chrome::IconType icon_type,
773 scoped_refptr<base::RefCountedMemory> bitmap_data,
774 const gfx::Size& pixel_size);
776 // Used by the FaviconService to set the favicons for a page on the history
778 // |favicon_bitmap_data| replaces all the favicon bitmaps mapped to
780 // |expired| and |icon_type| fields in FaviconBitmapData are ignored.
781 // Use MergeFavicon() if |favicon_bitmap_data| is incomplete, and favicon
782 // bitmaps in the database should be preserved if possible. For instance,
783 // favicon bitmaps from sync are 1x only. MergeFavicon() is used to avoid
784 // deleting the 2x favicon bitmap if it is present in the history backend.
785 // See HistoryBackend::ValidateSetFaviconsParams() for more details on the
786 // criteria for |favicon_bitmap_data| to be valid.
788 const GURL& page_url,
789 chrome::IconType icon_type,
790 const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data);
792 // Used by the FaviconService to mark the favicon for the page as being out
794 void SetFaviconsOutOfDateForPage(const GURL& page_url);
796 // Used by the FaviconService to clone favicons from one page to another,
797 // provided that other page does not already have favicons.
798 void CloneFavicons(const GURL& old_page_url, const GURL& new_page_url);
800 // Used by the FaviconService for importing many favicons for many pages at
801 // once. The pages must exist, any favicon sets for unknown pages will be
802 // discarded. Existing favicons will not be overwritten.
803 void SetImportedFavicons(
804 const std::vector<ImportedFaviconUsage>& favicon_usage);
806 // Sets the in-memory URL database. This is called by the backend once the
807 // database is loaded to make it available.
808 void SetInMemoryBackend(
810 scoped_ptr<history::InMemoryHistoryBackend> mem_backend);
812 // Called by our BackendDelegate when there is a problem reading the database.
813 void NotifyProfileError(int backend_id, sql::InitStatus init_status);
815 // Call to schedule a given task for running on the history thread with the
816 // specified priority. The task will have ownership taken.
817 void ScheduleTask(SchedulePriority priority, const base::Closure& task);
819 // Schedule ------------------------------------------------------------------
821 // Functions for scheduling operations on the history thread that have a
822 // handle and may be cancelable. For fire-and-forget operations, see
823 // ScheduleAndForget below.
825 template<typename BackendFunc, class RequestType>
826 Handle Schedule(SchedulePriority priority,
827 BackendFunc func, // Function to call on the HistoryBackend.
828 CancelableRequestConsumerBase* consumer,
829 RequestType* request) {
830 DCHECK(thread_) << "History service being called after cleanup";
831 DCHECK(thread_checker_.CalledOnValidThread());
832 LoadBackendIfNecessary();
834 AddRequest(request, consumer);
835 ScheduleTask(priority,
836 base::Bind(func, history_backend_.get(),
837 scoped_refptr<RequestType>(request)));
838 return request->handle();
841 template<typename BackendFunc, class RequestType, typename ArgA>
842 Handle Schedule(SchedulePriority priority,
843 BackendFunc func, // Function to call on the HistoryBackend.
844 CancelableRequestConsumerBase* consumer,
845 RequestType* request,
847 DCHECK(thread_) << "History service being called after cleanup";
848 DCHECK(thread_checker_.CalledOnValidThread());
849 LoadBackendIfNecessary();
851 AddRequest(request, consumer);
852 ScheduleTask(priority,
853 base::Bind(func, history_backend_.get(),
854 scoped_refptr<RequestType>(request), a));
855 return request->handle();
858 template<typename BackendFunc,
859 class RequestType, // Descendant of CancelableRequestBase.
862 Handle Schedule(SchedulePriority priority,
863 BackendFunc func, // Function to call on the HistoryBackend.
864 CancelableRequestConsumerBase* consumer,
865 RequestType* request,
868 DCHECK(thread_) << "History service being called after cleanup";
869 DCHECK(thread_checker_.CalledOnValidThread());
870 LoadBackendIfNecessary();
872 AddRequest(request, consumer);
873 ScheduleTask(priority,
874 base::Bind(func, history_backend_.get(),
875 scoped_refptr<RequestType>(request), a, b));
876 return request->handle();
879 template<typename BackendFunc,
880 class RequestType, // Descendant of CancelableRequestBase.
884 Handle Schedule(SchedulePriority priority,
885 BackendFunc func, // Function to call on the HistoryBackend.
886 CancelableRequestConsumerBase* consumer,
887 RequestType* request,
891 DCHECK(thread_) << "History service being called after cleanup";
892 DCHECK(thread_checker_.CalledOnValidThread());
893 LoadBackendIfNecessary();
895 AddRequest(request, consumer);
896 ScheduleTask(priority,
897 base::Bind(func, history_backend_.get(),
898 scoped_refptr<RequestType>(request), a, b, c));
899 return request->handle();
902 template<typename BackendFunc,
903 class RequestType, // Descendant of CancelableRequestBase.
908 Handle Schedule(SchedulePriority priority,
909 BackendFunc func, // Function to call on the HistoryBackend.
910 CancelableRequestConsumerBase* consumer,
911 RequestType* request,
916 DCHECK(thread_) << "History service being called after cleanup";
917 DCHECK(thread_checker_.CalledOnValidThread());
918 LoadBackendIfNecessary();
920 AddRequest(request, consumer);
921 ScheduleTask(priority,
922 base::Bind(func, history_backend_.get(),
923 scoped_refptr<RequestType>(request), a, b, c, d));
924 return request->handle();
927 // ScheduleAndForget ---------------------------------------------------------
929 // Functions for scheduling operations on the history thread that do not need
930 // any callbacks and are not cancelable.
932 template<typename BackendFunc>
933 void ScheduleAndForget(SchedulePriority priority,
934 BackendFunc func) { // Function to call on backend.
935 DCHECK(thread_) << "History service being called after cleanup";
936 DCHECK(thread_checker_.CalledOnValidThread());
937 LoadBackendIfNecessary();
938 ScheduleTask(priority, base::Bind(func, history_backend_.get()));
941 template<typename BackendFunc, typename ArgA>
942 void ScheduleAndForget(SchedulePriority priority,
943 BackendFunc func, // Function to call on backend.
945 DCHECK(thread_) << "History service being called after cleanup";
946 DCHECK(thread_checker_.CalledOnValidThread());
947 LoadBackendIfNecessary();
948 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a));
951 template<typename BackendFunc, typename ArgA, typename ArgB>
952 void ScheduleAndForget(SchedulePriority priority,
953 BackendFunc func, // Function to call on backend.
956 DCHECK(thread_) << "History service being called after cleanup";
957 DCHECK(thread_checker_.CalledOnValidThread());
958 LoadBackendIfNecessary();
959 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b));
962 template<typename BackendFunc, typename ArgA, typename ArgB, typename ArgC>
963 void ScheduleAndForget(SchedulePriority priority,
964 BackendFunc func, // Function to call on backend.
968 DCHECK(thread_) << "History service being called after cleanup";
969 DCHECK(thread_checker_.CalledOnValidThread());
970 LoadBackendIfNecessary();
971 ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b, c));
974 template<typename BackendFunc,
979 void ScheduleAndForget(SchedulePriority priority,
980 BackendFunc func, // Function to call on backend.
985 DCHECK(thread_) << "History service being called after cleanup";
986 DCHECK(thread_checker_.CalledOnValidThread());
987 LoadBackendIfNecessary();
988 ScheduleTask(priority, base::Bind(func, history_backend_.get(),
992 template<typename BackendFunc,
998 void ScheduleAndForget(SchedulePriority priority,
999 BackendFunc func, // Function to call on backend.
1005 DCHECK(thread_) << "History service being called after cleanup";
1006 DCHECK(thread_checker_.CalledOnValidThread());
1007 LoadBackendIfNecessary();
1008 ScheduleTask(priority, base::Bind(func, history_backend_.get(),
1012 // All vended weak pointers are invalidated in Cleanup().
1013 base::WeakPtrFactory<HistoryService> weak_ptr_factory_;
1015 base::ThreadChecker thread_checker_;
1017 content::NotificationRegistrar registrar_;
1019 // Some void primitives require some internal processing in the main thread
1020 // when done. We use this internal consumer for this purpose.
1021 CancelableRequestConsumer internal_consumer_;
1023 // The thread used by the history service to run complicated operations.
1024 // |thread_| is NULL once |Cleanup| is NULL.
1025 base::Thread* thread_;
1027 // This class has most of the implementation and runs on the 'thread_'.
1028 // You MUST communicate with this class ONLY through the thread_'s
1031 // This pointer will be NULL once Cleanup() has been called, meaning no
1032 // more calls should be made to the history thread.
1033 scoped_refptr<history::HistoryBackend> history_backend_;
1035 // A cache of the user-typed URLs kept in memory that is used by the
1036 // autocomplete system. This will be NULL until the database has been created
1037 // on the background thread.
1038 // TODO(mrossetti): Consider changing ownership. See http://crbug.com/138321
1039 scoped_ptr<history::InMemoryHistoryBackend> in_memory_backend_;
1041 // The profile, may be null when testing.
1044 // Used for propagating link highlighting data across renderers. May be null
1046 scoped_ptr<visitedlink::VisitedLinkMaster> visitedlink_master_;
1048 // Has the backend finished loading? The backend is loaded once Init has
1050 bool backend_loaded_;
1052 // The id of the current backend. This is only valid when history_backend_
1054 int current_backend_id_;
1056 // Cached values from Init(), used whenever we need to reload the backend.
1057 base::FilePath history_dir_;
1058 BookmarkService* bookmark_service_;
1061 // The index used for quick history lookups.
1062 // TODO(mrossetti): Move in_memory_url_index out of history_service.
1063 // See http://crbug.com/138321
1064 scoped_ptr<history::InMemoryURLIndex> in_memory_url_index_;
1066 ObserverList<history::VisitDatabaseObserver> visit_database_observers_;
1068 history::DeleteDirectiveHandler delete_directive_handler_;
1070 DISALLOW_COPY_AND_ASSIGN(HistoryService);
1073 #endif // CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_