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