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