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