history_service.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
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.
4
5#ifndef CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_
6#define CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_
7
8#include <set>
9#include <vector>
10
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/string16.h"
21#include "base/threading/thread_checker.h"
22#include "base/time.h"
23#include "chrome/browser/common/cancelable_request.h"
24#include "chrome/browser/favicon/favicon_service.h"
25#include "chrome/browser/history/delete_directive_handler.h"
26#include "chrome/browser/history/history_types.h"
27#include "chrome/browser/history/typed_url_syncable_service.h"
28#include "chrome/browser/search_engines/template_url_id.h"
29#include "chrome/common/cancelable_task_tracker.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/notification_observer.h"
34#include "content/public/browser/notification_registrar.h"
35#include "content/public/common/page_transition_types.h"
36#include "sql/init_status.h"
37#include "sync/api/syncable_service.h"
38#include "ui/base/layout.h"
39
40#if defined(OS_ANDROID)
41#include "chrome/browser/history/android/android_history_provider_service.h"
42#endif
43
44class BookmarkService;
45class GURL;
46class HistoryURLProvider;
47class PageUsageData;
48class PageUsageRequest;
49class Profile;
50struct HistoryURLProviderParams;
51
52namespace base {
53class FilePath;
54class Thread;
55}
56
57namespace visitedlink {
58class VisitedLinkMaster;
59}
60
61namespace history {
62
63class HistoryBackend;
64class HistoryDatabase;
65class HistoryDBTask;
66class HistoryQueryTest;
67class InMemoryHistoryBackend;
68class InMemoryURLIndex;
69class InMemoryURLIndexTest;
70class URLDatabase;
71class VisitDatabaseObserver;
72class VisitFilter;
73struct DownloadRow;
74struct HistoryAddPageArgs;
75struct HistoryDetails;
76
77}  // namespace history
78
79// The history service records page titles, and visit times, as well as
80// (eventually) information about autocomplete.
81//
82// This service is thread safe. Each request callback is invoked in the
83// thread that made the request.
84class HistoryService : public CancelableRequestProvider,
85                       public content::NotificationObserver,
86                       public syncer::SyncableService,
87                       public BrowserContextKeyedService,
88                       public visitedlink::VisitedLinkDelegate {
89 public:
90  // Miscellaneous commonly-used types.
91  typedef std::vector<PageUsageData*> PageUsageDataList;
92
93  // Must call Init after construction.
94  explicit HistoryService(Profile* profile);
95  // The empty constructor is provided only for testing.
96  HistoryService();
97
98  virtual ~HistoryService();
99
100  // Initializes the history service, returning true on success. On false, do
101  // not call any other functions. The given directory will be used for storing
102  // the history files. The BookmarkService is used when deleting URLs to
103  // test if a URL is bookmarked; it may be NULL during testing.
104  bool Init(const base::FilePath& history_dir, BookmarkService* bookmark_service) {
105    return Init(history_dir, bookmark_service, false);
106  }
107
108  // Triggers the backend to load if it hasn't already, and then returns whether
109  // it's finished loading.
110  // Note: Virtual needed for mocking.
111  virtual bool BackendLoaded();
112
113  // Returns true if the backend has finished loading.
114  bool backend_loaded() const { return backend_loaded_; }
115
116  // Unloads the backend without actually shutting down the history service.
117  // This can be used to temporarily reduce the browser process' memory
118  // footprint.
119  void UnloadBackend();
120
121  // Called on shutdown, this will tell the history backend to complete and
122  // will release pointers to it. No other functions should be called once
123  // cleanup has happened that may dispatch to the history thread (because it
124  // will be NULL).
125  //
126  // In practice, this will be called by the service manager (BrowserProcess)
127  // when it is being destroyed. Because that reference is being destroyed, it
128  // should be impossible for anybody else to call the service, even if it is
129  // still in memory (pending requests may be holding a reference to us).
130  void Cleanup();
131
132  // RenderProcessHost pointers are used to scope page IDs (see AddPage). These
133  // objects must tell us when they are being destroyed so that we can clear
134  // out any cached data associated with that scope.
135  //
136  // The given pointer will not be dereferenced, it is only used for
137  // identification purposes, hence it is a void*.
138  void NotifyRenderProcessHostDestruction(const void* host);
139
140  // Triggers the backend to load if it hasn't already, and then returns the
141  // in-memory URL database. The returned pointer MAY BE NULL if the in-memory
142  // database has not been loaded yet. This pointer is owned by the history
143  // system. Callers should not store or cache this value.
144  //
145  // TODO(brettw) this should return the InMemoryHistoryBackend.
146  history::URLDatabase* InMemoryDatabase();
147
148  // Following functions get URL information from in-memory database.
149  // They return false if database is not available (e.g. not loaded yet) or the
150  // URL does not exist.
151
152  // Reads the number of times the user has typed the given URL.
153  bool GetTypedCountForURL(const GURL& url, int* typed_count);
154
155  // Reads the last visit time for the given URL.
156  bool GetLastVisitTimeForURL(const GURL& url, base::Time* last_visit);
157
158  // Reads the number of times this URL has been visited.
159  bool GetVisitCountForURL(const GURL& url, int* visit_count);
160
161  // Returns a pointer to the TypedUrlSyncableService owned by HistoryBackend.
162  // This method should only be called from the history thread, because the
163  // returned service is intended to be accessed only via the history thread.
164  history::TypedUrlSyncableService* GetTypedUrlSyncableService() const;
165
166  // Return the quick history index.
167  history::InMemoryURLIndex* InMemoryIndex() const {
168    return in_memory_url_index_.get();
169  }
170
171  // BrowserContextKeyedService:
172  virtual void Shutdown() OVERRIDE;
173
174  // Navigation ----------------------------------------------------------------
175
176  // Adds the given canonical URL to history with the given time as the visit
177  // time. Referrer may be the empty string.
178  //
179  // The supplied render process host is used to scope the given page ID. Page
180  // IDs are only unique inside a given render process, so we need that to
181  // differentiate them. This pointer should not be dereferenced by the history
182  // system.
183  //
184  // The scope/ids can be NULL if there is no meaningful tracking information
185  // that can be performed on the given URL. The 'page_id' should be the ID of
186  // the current session history entry in the given process.
187  //
188  // 'redirects' is an array of redirect URLs leading to this page, with the
189  // page itself as the last item (so when there is no redirect, it will have
190  // one entry). If there are no redirects, this array may also be empty for
191  // the convenience of callers.
192  //
193  // 'did_replace_entry' is true when the navigation entry for this page has
194  // replaced the existing entry. A non-user initiated redirect causes such
195  // replacement.
196  //
197  // All "Add Page" functions will update the visited link database.
198  void AddPage(const GURL& url,
199               base::Time time,
200               const void* id_scope,
201               int32 page_id,
202               const GURL& referrer,
203               const history::RedirectList& redirects,
204               content::PageTransition transition,
205               history::VisitSource visit_source,
206               bool did_replace_entry);
207
208  // For adding pages to history where no tracking information can be done.
209  void AddPage(const GURL& url,
210               base::Time time,
211               history::VisitSource visit_source);
212
213  // All AddPage variants end up here.
214  void AddPage(const history::HistoryAddPageArgs& add_page_args);
215
216  // Adds an entry for the specified url without creating a visit. This should
217  // only be used when bookmarking a page, otherwise the row leaks in the
218  // history db (it never gets cleaned).
219  void AddPageNoVisitForBookmark(const GURL& url, const string16& title);
220
221  // Sets the title for the given page. The page should be in history. If it
222  // is not, this operation is ignored. This call will not update the full
223  // text index. The last title set when the page is indexed will be the
224  // title in the full text index.
225  void SetPageTitle(const GURL& url, const string16& title);
226
227  // Updates the history database with a page's ending time stamp information.
228  // The page can be identified by the combination of the pointer to
229  // a RenderProcessHost, the page id and the url.
230  //
231  // The given pointer will not be dereferenced, it is only used for
232  // identification purposes, hence it is a void*.
233  void UpdateWithPageEndTime(const void* host,
234                             int32 page_id,
235                             const GURL& url,
236                             base::Time end_ts);
237
238  // Indexing ------------------------------------------------------------------
239
240  // Notifies history of the body text of the given recently-visited URL.
241  // If the URL was not visited "recently enough," the history system may
242  // discard it.
243  void SetPageContents(const GURL& url, const string16& contents);
244
245  // Querying ------------------------------------------------------------------
246
247  // Returns the information about the requested URL. If the URL is found,
248  // success will be true and the information will be in the URLRow parameter.
249  // On success, the visits, if requested, will be sorted by date. If they have
250  // not been requested, the pointer will be valid, but the vector will be
251  // empty.
252  //
253  // If success is false, neither the row nor the vector will be valid.
254  typedef base::Callback<void(
255      Handle,
256      bool,  // Success flag, when false, nothing else is valid.
257      const history::URLRow*,
258      history::VisitVector*)> QueryURLCallback;
259
260  // Queries the basic information about the URL in the history database. If
261  // the caller is interested in the visits (each time the URL is visited),
262  // set |want_visits| to true. If these are not needed, the function will be
263  // faster by setting this to false.
264  Handle QueryURL(const GURL& url,
265                  bool want_visits,
266                  CancelableRequestConsumerBase* consumer,
267                  const QueryURLCallback& callback);
268
269  // Provides the result of a query. See QueryResults in history_types.h.
270  // The common use will be to use QueryResults.Swap to suck the contents of
271  // the results out of the passed in parameter and take ownership of them.
272  typedef base::Callback<void(Handle, history::QueryResults*)>
273      QueryHistoryCallback;
274
275  // Queries all history with the given options (see QueryOptions in
276  // history_types.h). If non-empty, the full-text database will be queried with
277  // the given |text_query|. If empty, all results matching the given options
278  // will be returned.
279  //
280  // This isn't totally hooked up yet, this will query the "new" full text
281  // database (see SetPageContents) which won't generally be set yet.
282  Handle QueryHistory(const string16& text_query,
283                      const history::QueryOptions& options,
284                      CancelableRequestConsumerBase* consumer,
285                      const QueryHistoryCallback& callback);
286
287  // Called when the results of QueryRedirectsFrom are available.
288  // The given vector will contain a list of all redirects, not counting
289  // the original page. If A redirects to B, the vector will contain only B,
290  // and A will be in 'source_url'.
291  //
292  // If there is no such URL in the database or the most recent visit has no
293  // redirect, the vector will be empty. If the history system failed for
294  // some reason, success will additionally be false. If the given page
295  // has redirected to multiple destinations, this will pick a random one.
296  typedef base::Callback<void(Handle,
297                              GURL,  // from_url
298                              bool,  // success
299                              history::RedirectList*)> QueryRedirectsCallback;
300
301  // Schedules a query for the most recent redirect coming out of the given
302  // URL. See the RedirectQuerySource above, which is guaranteed to be called
303  // if the request is not canceled.
304  Handle QueryRedirectsFrom(const GURL& from_url,
305                            CancelableRequestConsumerBase* consumer,
306                            const QueryRedirectsCallback& callback);
307
308  // Schedules a query to get the most recent redirects ending at the given
309  // URL.
310  Handle QueryRedirectsTo(const GURL& to_url,
311                          CancelableRequestConsumerBase* consumer,
312                          const QueryRedirectsCallback& callback);
313
314  typedef base::Callback<
315      void(Handle,
316           bool,        // Were we able to determine the # of visits?
317           int,         // Number of visits.
318           base::Time)> // Time of first visit. Only set if bool
319                        // is true and int is > 0.
320      GetVisibleVisitCountToHostCallback;
321
322  // Requests the number of user-visible visits (i.e. no redirects or subframes)
323  // to all urls on the same scheme/host/port as |url|.  This is only valid for
324  // HTTP and HTTPS URLs.
325  Handle GetVisibleVisitCountToHost(
326      const GURL& url,
327      CancelableRequestConsumerBase* consumer,
328      const GetVisibleVisitCountToHostCallback& callback);
329
330  // Called when QueryTopURLsAndRedirects completes. The vector contains a list
331  // of the top |result_count| URLs.  For each of these URLs, there is an entry
332  // in the map containing redirects from the URL.  For example, if we have the
333  // redirect chain A -> B -> C and A is a top visited URL, then A will be in
334  // the vector and "A => {B -> C}" will be in the map.
335  typedef base::Callback<
336      void(Handle,
337           bool,  // Did we get the top urls and redirects?
338           std::vector<GURL>*,  // List of top URLs.
339           history::RedirectMap*)>  // Redirects for top URLs.
340      QueryTopURLsAndRedirectsCallback;
341
342  // Request the top |result_count| most visited URLs and the chain of redirects
343  // leading to each of these URLs.
344  // TODO(Nik): remove this. Use QueryMostVisitedURLs instead.
345  Handle QueryTopURLsAndRedirects(
346      int result_count,
347      CancelableRequestConsumerBase* consumer,
348      const QueryTopURLsAndRedirectsCallback& callback);
349
350  typedef base::Callback<void(Handle, history::MostVisitedURLList)>
351      QueryMostVisitedURLsCallback;
352
353  typedef base::Callback<void(Handle, const history::FilteredURLList&)>
354      QueryFilteredURLsCallback;
355
356  // Request the |result_count| most visited URLs and the chain of
357  // redirects leading to each of these URLs. |days_back| is the
358  // number of days of history to use. Used by TopSites.
359  Handle QueryMostVisitedURLs(int result_count, int days_back,
360                              CancelableRequestConsumerBase* consumer,
361                              const QueryMostVisitedURLsCallback& callback);
362
363  // Request the |result_count| URLs filtered and sorted based on the |filter|.
364  // If |extended_info| is true, additional data will be provided in the
365  // results. Computing this additional data is expensive, likely to become
366  // more expensive as additional data points are added in future changes, and
367  // not useful in most cases. Set |extended_info| to true only if you
368  // explicitly require the additional data.
369  Handle QueryFilteredURLs(
370      int result_count,
371      const history::VisitFilter& filter,
372      bool extended_info,
373      CancelableRequestConsumerBase* consumer,
374      const QueryFilteredURLsCallback& callback);
375
376  // Thumbnails ----------------------------------------------------------------
377
378  // Implemented by consumers to get thumbnail data. Called when a request for
379  // the thumbnail data is complete. Once this callback is made, the request
380  // will be completed and no other calls will be made for that handle.
381  //
382  // This function will be called even on error conditions or if there is no
383  // thumbnail for that page. In these cases, the data pointer will be NULL.
384  typedef base::Callback<void(Handle, scoped_refptr<base::RefCountedBytes>)>
385      ThumbnailDataCallback;
386
387  // Requests a page thumbnail. See ThumbnailDataCallback definition above.
388  Handle GetPageThumbnail(const GURL& page_url,
389                          CancelableRequestConsumerBase* consumer,
390                          const ThumbnailDataCallback& callback);
391
392  // Database management operations --------------------------------------------
393
394  // Delete all the information related to a single url.
395  void DeleteURL(const GURL& url);
396
397  // Delete all the information related to a list of urls.  (Deleting
398  // URLs one by one is slow as it has to flush to disk each time.)
399  void DeleteURLsForTest(const std::vector<GURL>& urls);
400
401  // Removes all visits in the selected time range (including the start time),
402  // updating the URLs accordingly. This deletes the associated data, including
403  // the full text index. This function also deletes the associated favicons,
404  // if they are no longer referenced. |callback| runs when the expiration is
405  // complete. You may use null Time values to do an unbounded delete in
406  // either direction.
407  // If |restrict_urls| is not empty, only visits to the URLs in this set are
408  // removed.
409  void ExpireHistoryBetween(const std::set<GURL>& restrict_urls,
410                            base::Time begin_time,
411                            base::Time end_time,
412                            const base::Closure& callback,
413                            CancelableTaskTracker* tracker);
414
415  // Removes all visits to specified URLs in specific time ranges.
416  // This is the equivalent ExpireHistoryBetween() once for each element in the
417  // vector. The fields of |ExpireHistoryArgs| map directly to the arguments of
418  // of ExpireHistoryBetween().
419  void ExpireHistory(const std::vector<history::ExpireHistoryArgs>& expire_list,
420                     const base::Closure& callback,
421                     CancelableTaskTracker* tracker);
422
423  // Removes all visits to the given URLs in the specified time range. Calls
424  // ExpireHistoryBetween() to delete local visits, and handles deletion of
425  // synced visits if appropriate.
426  void ExpireLocalAndRemoteHistoryBetween(
427      const std::set<GURL>& restrict_urls,
428      base::Time begin_time,
429      base::Time end_time,
430      const base::Closure& callback,
431      CancelableTaskTracker* tracker);
432
433  // Processes the given |delete_directive| and sends it to the
434  // SyncChangeProcessor (if it exists).  Returns any error resulting
435  // from sending the delete directive to sync.
436  syncer::SyncError ProcessLocalDeleteDirective(
437      const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive);
438
439  // Downloads -----------------------------------------------------------------
440
441  // Implemented by the caller of 'CreateDownload' below, and is called when the
442  // history service has created a new entry for a download in the history db.
443  typedef base::Callback<void(int64)> DownloadCreateCallback;
444
445  // Begins a history request to create a new row for a download. 'info'
446  // contains all the download's creation state, and 'callback' runs when the
447  // history service request is complete. The callback is called on the thread
448  // that calls CreateDownload().
449  void CreateDownload(
450      const history::DownloadRow& info,
451      const DownloadCreateCallback& callback);
452
453  // Implemented by the caller of 'GetNextDownloadId' below.
454  typedef base::Callback<void(int)> DownloadNextIdCallback;
455
456  // Runs the callback with the next available download id. The callback is
457  // called on the thread that calls GetNextDownloadId().
458  void GetNextDownloadId(const DownloadNextIdCallback& callback);
459
460  // Implemented by the caller of 'QueryDownloads' below, and is called when the
461  // history service has retrieved a list of all download state. The call
462  typedef base::Callback<void(
463      scoped_ptr<std::vector<history::DownloadRow> >)>
464          DownloadQueryCallback;
465
466  // Begins a history request to retrieve the state of all downloads in the
467  // history db. 'callback' runs when the history service request is complete,
468  // at which point 'info' contains an array of history::DownloadRow, one per
469  // download. The callback is called on the thread that calls QueryDownloads().
470  void QueryDownloads(const DownloadQueryCallback& callback);
471
472  // Called to update the history service about the current state of a download.
473  // This is a 'fire and forget' query, so just pass the relevant state info to
474  // the database with no need for a callback.
475  void UpdateDownload(const history::DownloadRow& data);
476
477  // Permanently remove some downloads from the history system. This is a 'fire
478  // and forget' operation.
479  void RemoveDownloads(const std::set<int64>& db_handles);
480
481  // Visit Segments ------------------------------------------------------------
482
483  typedef base::Callback<void(Handle, std::vector<PageUsageData*>*)>
484      SegmentQueryCallback;
485
486  // Query usage data for all visit segments since the provided time.
487  //
488  // The request is performed asynchronously and can be cancelled by using the
489  // returned handle.
490  //
491  // The vector provided to the callback and its contents is owned by the
492  // history system. It will be deeply deleted after the callback is invoked.
493  // If you want to preserve any PageUsageData instance, simply remove them
494  // from the vector.
495  //
496  // The vector contains a list of PageUsageData. Each PageUsageData ID is set
497  // to the segment ID. The URL and all the other information is set to the page
498  // representing the segment.
499  Handle QuerySegmentUsageSince(CancelableRequestConsumerBase* consumer,
500                                const base::Time from_time,
501                                int max_result_count,
502                                const SegmentQueryCallback& callback);
503
504  // Increases the amount of time the user actively viewed the url.
505  void IncreaseSegmentDuration(const GURL& url,
506                               base::Time time,
507                               base::TimeDelta delta);
508
509  // Queries segments based on active time viewed.
510  Handle QuerySegmentDurationSince(CancelableRequestConsumerBase* consumer,
511                                   base::Time from_time,
512                                   int max_result_count,
513                                   const SegmentQueryCallback& callback);
514
515  // Keyword search terms -----------------------------------------------------
516
517  // Sets the search terms for the specified url and keyword. url_id gives the
518  // id of the url, keyword_id the id of the keyword and term the search term.
519  void SetKeywordSearchTermsForURL(const GURL& url,
520                                   TemplateURLID keyword_id,
521                                   const string16& term);
522
523  // Deletes all search terms for the specified keyword.
524  void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id);
525
526  typedef base::Callback<
527      void(Handle, std::vector<history::KeywordSearchTermVisit>*)>
528          GetMostRecentKeywordSearchTermsCallback;
529
530  // Returns up to max_count of the most recent search terms starting with the
531  // specified text. The matching is case insensitive. The results are ordered
532  // in descending order up to |max_count| with the most recent search term
533  // first.
534  Handle GetMostRecentKeywordSearchTerms(
535      TemplateURLID keyword_id,
536      const string16& prefix,
537      int max_count,
538      CancelableRequestConsumerBase* consumer,
539      const GetMostRecentKeywordSearchTermsCallback& callback);
540
541  // Bookmarks -----------------------------------------------------------------
542
543  // Notification that a URL is no longer bookmarked.
544  void URLsNoLongerBookmarked(const std::set<GURL>& urls);
545
546  // Generic Stuff -------------------------------------------------------------
547
548  // Schedules a HistoryDBTask for running on the history backend thread. See
549  // HistoryDBTask for details on what this does.
550  virtual void ScheduleDBTask(history::HistoryDBTask* task,
551                              CancelableRequestConsumerBase* consumer);
552
553  // Returns true if top sites needs to be migrated out of history into its own
554  // db.
555  bool needs_top_sites_migration() const { return needs_top_sites_migration_; }
556
557  // Adds or removes observers for the VisitDatabase.
558  void AddVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
559  void RemoveVisitDatabaseObserver(history::VisitDatabaseObserver* observer);
560
561  void NotifyVisitDBObserversOnAddVisit(const history::BriefVisitInfo& info);
562
563  // Testing -------------------------------------------------------------------
564
565  // Designed for unit tests, this passes the given task on to the history
566  // backend to be called once the history backend has terminated. This allows
567  // callers to know when the history thread is complete and the database files
568  // can be deleted and the next test run. Otherwise, the history thread may
569  // still be running, causing problems in subsequent tests.
570  //
571  // There can be only one closing task, so this will override any previously
572  // set task. We will take ownership of the pointer and delete it when done.
573  // The task will be run on the calling thread (this function is threadsafe).
574  void SetOnBackendDestroyTask(const base::Closure& task);
575
576  // Used for unit testing and potentially importing to get known information
577  // into the database. This assumes the URL doesn't exist in the database
578  //
579  // Calling this function many times may be slow because each call will
580  // dispatch to the history thread and will be a separate database
581  // transaction. If this functionality is needed for importing many URLs,
582  // callers should use AddPagesWithDetails() instead.
583  //
584  // Note that this routine (and AddPageWithDetails()) always adds a single
585  // visit using the |last_visit| timestamp, and a PageTransition type of LINK,
586  // if |visit_source| != SYNCED.
587  void AddPageWithDetails(const GURL& url,
588                          const string16& title,
589                          int visit_count,
590                          int typed_count,
591                          base::Time last_visit,
592                          bool hidden,
593                          history::VisitSource visit_source);
594
595  // The same as AddPageWithDetails() but takes a vector.
596  void AddPagesWithDetails(const history::URLRows& info,
597                           history::VisitSource visit_source);
598
599  // Starts the TopSites migration in the HistoryThread. Called by the
600  // BackendDelegate.
601  void StartTopSitesMigration(int backend_id);
602
603  // Called by TopSites after the thumbnails were read and it is safe
604  // to delete the thumbnails DB.
605  void OnTopSitesReady();
606
607  // Returns true if this looks like the type of URL we want to add to the
608  // history. We filter out some URLs such as JavaScript.
609  static bool CanAddURL(const GURL& url);
610
611  base::WeakPtr<HistoryService> AsWeakPtr();
612
613  // syncer::SyncableService implementation.
614  virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
615      syncer::ModelType type,
616      const syncer::SyncDataList& initial_sync_data,
617      scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
618      scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
619  virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
620  virtual syncer::SyncDataList GetAllSyncData(
621      syncer::ModelType type) const OVERRIDE;
622  virtual syncer::SyncError ProcessSyncChanges(
623      const tracked_objects::Location& from_here,
624      const syncer::SyncChangeList& change_list) OVERRIDE;
625
626 protected:
627  // These are not currently used, hopefully we can do something in the future
628  // to ensure that the most important things happen first.
629  enum SchedulePriority {
630    PRIORITY_UI,      // The highest priority (must respond to UI events).
631    PRIORITY_NORMAL,  // Normal stuff like adding a page.
632    PRIORITY_LOW,     // Low priority things like indexing or expiration.
633  };
634
635 private:
636  class BackendDelegate;
637#if defined(OS_ANDROID)
638  friend class AndroidHistoryProviderService;
639#endif
640  friend class base::RefCountedThreadSafe<HistoryService>;
641  friend class BackendDelegate;
642  friend class FaviconService;
643  friend class history::HistoryBackend;
644  friend class history::HistoryQueryTest;
645  friend class HistoryOperation;
646  friend class HistoryURLProvider;
647  friend class HistoryURLProviderTest;
648  friend class history::InMemoryURLIndexTest;
649  template<typename Info, typename Callback> friend class DownloadRequest;
650  friend class PageUsageRequest;
651  friend class RedirectRequest;
652  friend class TestingProfile;
653
654  // Implementation of content::NotificationObserver.
655  virtual void Observe(int type,
656                       const content::NotificationSource& source,
657                       const content::NotificationDetails& details) OVERRIDE;
658
659  // Implementation of visitedlink::VisitedLinkDelegate.
660  virtual void RebuildTable(
661      const scoped_refptr<URLEnumerator>& enumerator) OVERRIDE;
662
663  // Low-level Init().  Same as the public version, but adds a |no_db| parameter
664  // that is only set by unittests which causes the backend to not init its DB.
665  bool Init(const base::FilePath& history_dir,
666            BookmarkService* bookmark_service,
667            bool no_db);
668
669  // Called by the HistoryURLProvider class to schedule an autocomplete, it
670  // will be called back on the internal history thread with the history
671  // database so it can query. See history_autocomplete.cc for a diagram.
672  void ScheduleAutocomplete(HistoryURLProvider* provider,
673                            HistoryURLProviderParams* params);
674
675  // Broadcasts the given notification. This is called by the backend so that
676  // the notification will be broadcast on the main thread.
677  //
678  // Compared to BroadcastNotifications(), this function does not take
679  // ownership of |details|.
680  void BroadcastNotificationsHelper(int type,
681                                    history::HistoryDetails* details);
682
683  // Initializes the backend.
684  void LoadBackendIfNecessary();
685
686  // Notification from the backend that it has finished loading. Sends
687  // notification (NOTIFY_HISTORY_LOADED) and sets backend_loaded_ to true.
688  void OnDBLoaded(int backend_id);
689
690  // Helper function for getting URL information.
691  // Reads a URLRow from in-memory database. Returns false if database is not
692  // available or the URL does not exist.
693  bool GetRowForURL(const GURL& url, history::URLRow* url_row);
694
695  // Favicon -------------------------------------------------------------------
696
697  // These favicon methods are exposed to the FaviconService. Instead of calling
698  // these methods directly you should call the respective method on the
699  // FaviconService.
700
701  // Used by FaviconService to get the favicon bitmaps from the history backend
702  // which most closely match |desired_size_in_dip| x |desired_size_in_dip| and
703  // |desired_scale_factors| for |icon_types|. If |desired_size_in_dip| is 0,
704  // the largest favicon bitmap for |icon_types| is returned. The returned
705  // FaviconBitmapResults will have at most one result for each of
706  // |desired_scale_factors|. If a favicon bitmap is determined to be the best
707  // candidate for multiple scale factors there will be less results.
708  // If |icon_types| has several types, results for only a single type will be
709  // returned in the priority of TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and
710  // FAVICON.
711  CancelableTaskTracker::TaskId GetFavicons(
712      const std::vector<GURL>& icon_urls,
713      int icon_types,
714      int desired_size_in_dip,
715      const std::vector<ui::ScaleFactor>& desired_scale_factors,
716      const FaviconService::FaviconResultsCallback& callback,
717      CancelableTaskTracker* tracker);
718
719  // Used by the FaviconService to get favicons mapped to |page_url| for
720  // |icon_types| which most closely match |desired_size_in_dip| and
721  // |desired_scale_factors|. If |desired_size_in_dip| is 0, the largest favicon
722  // bitmap for |icon_types| is returned. The returned FaviconBitmapResults will
723  // have at most one result for each of |desired_scale_factors|. If a favicon
724  // bitmap is determined to be the best candidate for multiple scale factors
725  // there will be less results. If |icon_types| has several types, results for
726  // only a single type will be returned in the priority of
727  // TOUCH_PRECOMPOSED_ICON, TOUCH_ICON, and FAVICON.
728  CancelableTaskTracker::TaskId GetFaviconsForURL(
729      const GURL& page_url,
730      int icon_types,
731      int desired_size_in_dip,
732      const std::vector<ui::ScaleFactor>& desired_scale_factors,
733      const FaviconService::FaviconResultsCallback& callback,
734      CancelableTaskTracker* tracker);
735
736  // Used by the FaviconService to get the favicon bitmap which most closely
737  // matches |desired_size_in_dip| and |desired_scale_factor| from the favicon
738  // with |favicon_id| from the history backend. If |desired_size_in_dip| is 0,
739  // the largest favicon bitmap for |favicon_id| is returned.
740  CancelableTaskTracker::TaskId GetFaviconForID(
741      chrome::FaviconID favicon_id,
742      int desired_size_in_dip,
743      ui::ScaleFactor desired_scale_factor,
744      const FaviconService::FaviconResultsCallback& callback,
745      CancelableTaskTracker* tracker);
746
747  // Used by the FaviconService to replace the favicon mappings to |page_url|
748  // for |icon_types| on the history backend.
749  // Sample |icon_urls|:
750  //  { ICON_URL1 -> TOUCH_ICON, known to the database,
751  //    ICON_URL2 -> TOUCH_ICON, not known to the database,
752  //    ICON_URL3 -> TOUCH_PRECOMPOSED_ICON, known to the database }
753  // The new mappings are computed from |icon_urls| with these rules:
754  // 1) Any urls in |icon_urls| which are not already known to the database are
755  //    rejected.
756  //    Sample new mappings to |page_url|: { ICON_URL1, ICON_URL3 }
757  // 2) If |icon_types| has multiple types, the mappings are only set for the
758  //    largest icon type.
759  //    Sample new mappings to |page_url|: { ICON_URL3 }
760  // |icon_types| can only have multiple IconTypes if
761  // |icon_types| == TOUCH_ICON | TOUCH_PRECOMPOSED_ICON.
762  // The favicon bitmaps which most closely match |desired_size_in_dip|
763  // and |desired_scale_factors| from the favicons which were just mapped
764  // to |page_url| are returned. If |desired_size_in_dip| is 0, the
765  // largest favicon bitmap is returned.
766  CancelableTaskTracker::TaskId UpdateFaviconMappingsAndFetch(
767      const GURL& page_url,
768      const std::vector<GURL>& icon_urls,
769      int icon_types,
770      int desired_size_in_dip,
771      const std::vector<ui::ScaleFactor>& desired_scale_factors,
772      const FaviconService::FaviconResultsCallback& callback,
773      CancelableTaskTracker* tracker);
774
775  // Used by FaviconService to set a favicon for |page_url| and |icon_url| with
776  // |pixel_size|.
777  // Example:
778  //   |page_url|: www.google.com
779  // 2 favicons in history for |page_url|:
780  //   www.google.com/a.ico  16x16
781  //   www.google.com/b.ico  32x32
782  // MergeFavicon(|page_url|, www.google.com/a.ico, ..., ..., 16x16)
783  //
784  // Merging occurs in the following manner:
785  // 1) |page_url| is set to map to only to |icon_url|. In order to not lose
786  //    data, favicon bitmaps mapped to |page_url| but not to |icon_url| are
787  //    copied to the favicon at |icon_url|.
788  //    For the example above, |page_url| will only be mapped to a.ico.
789  //    The 32x32 favicon bitmap at b.ico is copied to a.ico
790  // 2) |bitmap_data| is added to the favicon at |icon_url|, overwriting any
791  //    favicon bitmaps of |pixel_size|.
792  //    For the example above, |bitmap_data| overwrites the 16x16 favicon
793  //    bitmap for a.ico.
794  // TODO(pkotwicz): Remove once no longer required by sync.
795  void MergeFavicon(const GURL& page_url,
796                    const GURL& icon_url,
797                    chrome::IconType icon_type,
798                    scoped_refptr<base::RefCountedMemory> bitmap_data,
799                    const gfx::Size& pixel_size);
800
801  // Used by the FaviconService to set the favicons for a page on the history
802  // backend.
803  // |favicon_bitmap_data| replaces all the favicon bitmaps mapped to
804  // |page_url|.
805  // |expired| and |icon_type| fields in FaviconBitmapData are ignored.
806  // Use MergeFavicon() if |favicon_bitmap_data| is incomplete, and favicon
807  // bitmaps in the database should be preserved if possible. For instance,
808  // favicon bitmaps from sync are 1x only. MergeFavicon() is used to avoid
809  // deleting the 2x favicon bitmap if it is present in the history backend.
810  // See HistoryBackend::ValidateSetFaviconsParams() for more details on the
811  // criteria for |favicon_bitmap_data| to be valid.
812  void SetFavicons(
813      const GURL& page_url,
814      chrome::IconType icon_type,
815      const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data);
816
817  // Used by the FaviconService to mark the favicon for the page as being out
818  // of date.
819  void SetFaviconsOutOfDateForPage(const GURL& page_url);
820
821  // Used by the FaviconService to clone favicons from one page to another,
822  // provided that other page does not already have favicons.
823  void CloneFavicons(const GURL& old_page_url, const GURL& new_page_url);
824
825  // Used by the FaviconService for importing many favicons for many pages at
826  // once. The pages must exist, any favicon sets for unknown pages will be
827  // discarded. Existing favicons will not be overwritten.
828  void SetImportedFavicons(
829      const std::vector<ImportedFaviconUsage>& favicon_usage);
830
831  // Sets the in-memory URL database. This is called by the backend once the
832  // database is loaded to make it available.
833  void SetInMemoryBackend(int backend_id,
834                          history::InMemoryHistoryBackend* mem_backend);
835
836  // Called by our BackendDelegate when there is a problem reading the database.
837  void NotifyProfileError(int backend_id, sql::InitStatus init_status);
838
839  // Call to schedule a given task for running on the history thread with the
840  // specified priority. The task will have ownership taken.
841  void ScheduleTask(SchedulePriority priority, const base::Closure& task);
842
843  // Schedule ------------------------------------------------------------------
844  //
845  // Functions for scheduling operations on the history thread that have a
846  // handle and may be cancelable. For fire-and-forget operations, see
847  // ScheduleAndForget below.
848
849  template<typename BackendFunc, class RequestType>
850  Handle Schedule(SchedulePriority priority,
851                  BackendFunc func,  // Function to call on the HistoryBackend.
852                  CancelableRequestConsumerBase* consumer,
853                  RequestType* request) {
854    DCHECK(thread_) << "History service being called after cleanup";
855    DCHECK(thread_checker_.CalledOnValidThread());
856    LoadBackendIfNecessary();
857    if (consumer)
858      AddRequest(request, consumer);
859    ScheduleTask(priority,
860                 base::Bind(func, history_backend_.get(),
861                            scoped_refptr<RequestType>(request)));
862    return request->handle();
863  }
864
865  template<typename BackendFunc, class RequestType, typename ArgA>
866  Handle Schedule(SchedulePriority priority,
867                  BackendFunc func,  // Function to call on the HistoryBackend.
868                  CancelableRequestConsumerBase* consumer,
869                  RequestType* request,
870                  const ArgA& a) {
871    DCHECK(thread_) << "History service being called after cleanup";
872    DCHECK(thread_checker_.CalledOnValidThread());
873    LoadBackendIfNecessary();
874    if (consumer)
875      AddRequest(request, consumer);
876    ScheduleTask(priority,
877                 base::Bind(func, history_backend_.get(),
878                            scoped_refptr<RequestType>(request), a));
879    return request->handle();
880  }
881
882  template<typename BackendFunc,
883           class RequestType,  // Descendant of CancelableRequestBase.
884           typename ArgA,
885           typename ArgB>
886  Handle Schedule(SchedulePriority priority,
887                  BackendFunc func,  // Function to call on the HistoryBackend.
888                  CancelableRequestConsumerBase* consumer,
889                  RequestType* request,
890                  const ArgA& a,
891                  const ArgB& b) {
892    DCHECK(thread_) << "History service being called after cleanup";
893    DCHECK(thread_checker_.CalledOnValidThread());
894    LoadBackendIfNecessary();
895    if (consumer)
896      AddRequest(request, consumer);
897    ScheduleTask(priority,
898                 base::Bind(func, history_backend_.get(),
899                            scoped_refptr<RequestType>(request), a, b));
900    return request->handle();
901  }
902
903  template<typename BackendFunc,
904           class RequestType,  // Descendant of CancelableRequestBase.
905           typename ArgA,
906           typename ArgB,
907           typename ArgC>
908  Handle Schedule(SchedulePriority priority,
909                  BackendFunc func,  // Function to call on the HistoryBackend.
910                  CancelableRequestConsumerBase* consumer,
911                  RequestType* request,
912                  const ArgA& a,
913                  const ArgB& b,
914                  const ArgC& c) {
915    DCHECK(thread_) << "History service being called after cleanup";
916    DCHECK(thread_checker_.CalledOnValidThread());
917    LoadBackendIfNecessary();
918    if (consumer)
919      AddRequest(request, consumer);
920    ScheduleTask(priority,
921                 base::Bind(func, history_backend_.get(),
922                            scoped_refptr<RequestType>(request), a, b, c));
923    return request->handle();
924  }
925
926  template<typename BackendFunc,
927           class RequestType,  // Descendant of CancelableRequestBase.
928           typename ArgA,
929           typename ArgB,
930           typename ArgC,
931           typename ArgD>
932  Handle Schedule(SchedulePriority priority,
933                  BackendFunc func,  // Function to call on the HistoryBackend.
934                  CancelableRequestConsumerBase* consumer,
935                  RequestType* request,
936                  const ArgA& a,
937                  const ArgB& b,
938                  const ArgC& c,
939                  const ArgD& d) {
940    DCHECK(thread_) << "History service being called after cleanup";
941    DCHECK(thread_checker_.CalledOnValidThread());
942    LoadBackendIfNecessary();
943    if (consumer)
944      AddRequest(request, consumer);
945    ScheduleTask(priority,
946                 base::Bind(func, history_backend_.get(),
947                            scoped_refptr<RequestType>(request), a, b, c, d));
948    return request->handle();
949  }
950
951  // ScheduleAndForget ---------------------------------------------------------
952  //
953  // Functions for scheduling operations on the history thread that do not need
954  // any callbacks and are not cancelable.
955
956  template<typename BackendFunc>
957  void ScheduleAndForget(SchedulePriority priority,
958                         BackendFunc func) {  // Function to call on backend.
959    DCHECK(thread_) << "History service being called after cleanup";
960    DCHECK(thread_checker_.CalledOnValidThread());
961    LoadBackendIfNecessary();
962    ScheduleTask(priority, base::Bind(func, history_backend_.get()));
963  }
964
965  template<typename BackendFunc, typename ArgA>
966  void ScheduleAndForget(SchedulePriority priority,
967                         BackendFunc func,  // Function to call on backend.
968                         const ArgA& a) {
969    DCHECK(thread_) << "History service being called after cleanup";
970    DCHECK(thread_checker_.CalledOnValidThread());
971    LoadBackendIfNecessary();
972    ScheduleTask(priority, base::Bind(func, history_backend_.get(), a));
973  }
974
975  template<typename BackendFunc, typename ArgA, typename ArgB>
976  void ScheduleAndForget(SchedulePriority priority,
977                         BackendFunc func,  // Function to call on backend.
978                         const ArgA& a,
979                         const ArgB& b) {
980    DCHECK(thread_) << "History service being called after cleanup";
981    DCHECK(thread_checker_.CalledOnValidThread());
982    LoadBackendIfNecessary();
983    ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b));
984  }
985
986  template<typename BackendFunc, typename ArgA, typename ArgB, typename ArgC>
987  void ScheduleAndForget(SchedulePriority priority,
988                         BackendFunc func,  // Function to call on backend.
989                         const ArgA& a,
990                         const ArgB& b,
991                         const ArgC& c) {
992    DCHECK(thread_) << "History service being called after cleanup";
993    DCHECK(thread_checker_.CalledOnValidThread());
994    LoadBackendIfNecessary();
995    ScheduleTask(priority, base::Bind(func, history_backend_.get(), a, b, c));
996  }
997
998  template<typename BackendFunc,
999           typename ArgA,
1000           typename ArgB,
1001           typename ArgC,
1002           typename ArgD>
1003  void ScheduleAndForget(SchedulePriority priority,
1004                         BackendFunc func,  // Function to call on backend.
1005                         const ArgA& a,
1006                         const ArgB& b,
1007                         const ArgC& c,
1008                         const ArgD& d) {
1009    DCHECK(thread_) << "History service being called after cleanup";
1010    DCHECK(thread_checker_.CalledOnValidThread());
1011    LoadBackendIfNecessary();
1012    ScheduleTask(priority, base::Bind(func, history_backend_.get(),
1013                                      a, b, c, d));
1014  }
1015
1016  template<typename BackendFunc,
1017           typename ArgA,
1018           typename ArgB,
1019           typename ArgC,
1020           typename ArgD,
1021           typename ArgE>
1022  void ScheduleAndForget(SchedulePriority priority,
1023                         BackendFunc func,  // Function to call on backend.
1024                         const ArgA& a,
1025                         const ArgB& b,
1026                         const ArgC& c,
1027                         const ArgD& d,
1028                         const ArgE& e) {
1029    DCHECK(thread_) << "History service being called after cleanup";
1030    DCHECK(thread_checker_.CalledOnValidThread());
1031    LoadBackendIfNecessary();
1032    ScheduleTask(priority, base::Bind(func, history_backend_.get(),
1033                                      a, b, c, d, e));
1034  }
1035
1036  // All vended weak pointers are invalidated in Cleanup().
1037  base::WeakPtrFactory<HistoryService> weak_ptr_factory_;
1038
1039  base::ThreadChecker thread_checker_;
1040
1041  content::NotificationRegistrar registrar_;
1042
1043  // Some void primitives require some internal processing in the main thread
1044  // when done. We use this internal consumer for this purpose.
1045  CancelableRequestConsumer internal_consumer_;
1046
1047  // The thread used by the history service to run complicated operations.
1048  // |thread_| is NULL once |Cleanup| is NULL.
1049  base::Thread* thread_;
1050
1051  // This class has most of the implementation and runs on the 'thread_'.
1052  // You MUST communicate with this class ONLY through the thread_'s
1053  // message_loop().
1054  //
1055  // This pointer will be NULL once Cleanup() has been called, meaning no
1056  // more calls should be made to the history thread.
1057  scoped_refptr<history::HistoryBackend> history_backend_;
1058
1059  // A cache of the user-typed URLs kept in memory that is used by the
1060  // autocomplete system. This will be NULL until the database has been created
1061  // on the background thread.
1062  // TODO(mrossetti): Consider changing ownership. See http://crbug.com/138321
1063  scoped_ptr<history::InMemoryHistoryBackend> in_memory_backend_;
1064
1065  // The profile, may be null when testing.
1066  Profile* profile_;
1067
1068  // Used for propagating link highlighting data across renderers. May be null
1069  // in tests.
1070  scoped_ptr<visitedlink::VisitedLinkMaster> visitedlink_master_;
1071
1072  // Has the backend finished loading? The backend is loaded once Init has
1073  // completed.
1074  bool backend_loaded_;
1075
1076  // The id of the current backend. This is only valid when history_backend_
1077  // is not NULL.
1078  int current_backend_id_;
1079
1080  // Cached values from Init(), used whenever we need to reload the backend.
1081  base::FilePath history_dir_;
1082  BookmarkService* bookmark_service_;
1083  bool no_db_;
1084
1085  // True if needs top site migration.
1086  bool needs_top_sites_migration_;
1087
1088  // The index used for quick history lookups.
1089  // TODO(mrossetti): Move in_memory_url_index out of history_service.
1090  // See http://crbug.com/138321
1091  scoped_ptr<history::InMemoryURLIndex> in_memory_url_index_;
1092
1093  ObserverList<history::VisitDatabaseObserver> visit_database_observers_;
1094
1095  history::DeleteDirectiveHandler delete_directive_handler_;
1096
1097  DISALLOW_COPY_AND_ASSIGN(HistoryService);
1098};
1099
1100#endif  // CHROME_BROWSER_HISTORY_HISTORY_SERVICE_H_
1101