history_service.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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// The history system runs on a background thread so that potentially slow
6// database operations don't delay the browser. This backend processing is
7// represented by HistoryBackend. The HistoryService's job is to dispatch to
8// that thread.
9//
10// Main thread                       History thread
11// -----------                       --------------
12// HistoryService <----------------> HistoryBackend
13//                                   -> HistoryDatabase
14//                                      -> SQLite connection to History
15//                                   -> ArchivedDatabase
16//                                      -> SQLite connection to Archived History
17//                                   -> ThumbnailDatabase
18//                                      -> SQLite connection to Thumbnails
19//                                         (and favicons)
20
21#include "chrome/browser/history/history_service.h"
22
23#include "base/bind_helpers.h"
24#include "base/callback.h"
25#include "base/command_line.h"
26#include "base/compiler_specific.h"
27#include "base/location.h"
28#include "base/memory/ref_counted.h"
29#include "base/message_loop/message_loop.h"
30#include "base/path_service.h"
31#include "base/prefs/pref_service.h"
32#include "base/thread_task_runner_handle.h"
33#include "base/threading/thread.h"
34#include "base/time/time.h"
35#include "chrome/browser/autocomplete/history_url_provider.h"
36#include "chrome/browser/bookmarks/bookmark_model.h"
37#include "chrome/browser/bookmarks/bookmark_model_factory.h"
38#include "chrome/browser/browser_process.h"
39#include "chrome/browser/chrome_notification_types.h"
40#include "chrome/browser/history/download_row.h"
41#include "chrome/browser/history/history_backend.h"
42#include "chrome/browser/history/history_notifications.h"
43#include "chrome/browser/history/history_types.h"
44#include "chrome/browser/history/in_memory_database.h"
45#include "chrome/browser/history/in_memory_history_backend.h"
46#include "chrome/browser/history/in_memory_url_index.h"
47#include "chrome/browser/history/top_sites.h"
48#include "chrome/browser/history/visit_database.h"
49#include "chrome/browser/history/visit_filter.h"
50#include "chrome/browser/history/web_history_service.h"
51#include "chrome/browser/history/web_history_service_factory.h"
52#include "chrome/browser/profiles/profile.h"
53#include "chrome/browser/ui/profile_error_dialog.h"
54#include "chrome/common/chrome_constants.h"
55#include "chrome/common/chrome_switches.h"
56#include "chrome/common/importer/imported_favicon_usage.h"
57#include "chrome/common/pref_names.h"
58#include "chrome/common/thumbnail_score.h"
59#include "chrome/common/url_constants.h"
60#include "components/visitedlink/browser/visitedlink_master.h"
61#include "content/public/browser/browser_thread.h"
62#include "content/public/browser/download_item.h"
63#include "content/public/browser/notification_service.h"
64#include "grit/chromium_strings.h"
65#include "grit/generated_resources.h"
66#include "sync/api/sync_error_factory.h"
67#include "third_party/skia/include/core/SkBitmap.h"
68
69using base::Time;
70using history::HistoryBackend;
71
72namespace {
73
74static const char* kHistoryThreadName = "Chrome_HistoryThread";
75
76void RunWithFaviconResults(
77    const FaviconService::FaviconResultsCallback& callback,
78    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
79  callback.Run(*bitmap_results);
80}
81
82void RunWithFaviconResult(
83    const FaviconService::FaviconRawCallback& callback,
84    chrome::FaviconBitmapResult* bitmap_result) {
85  callback.Run(*bitmap_result);
86}
87
88// Extract history::URLRows into GURLs for VisitedLinkMaster.
89class URLIteratorFromURLRows
90    : public visitedlink::VisitedLinkMaster::URLIterator {
91 public:
92  explicit URLIteratorFromURLRows(const history::URLRows& url_rows)
93      : itr_(url_rows.begin()),
94        end_(url_rows.end()) {
95  }
96
97  virtual const GURL& NextURL() OVERRIDE {
98    return (itr_++)->url();
99  }
100
101  virtual bool HasNextURL() const OVERRIDE {
102    return itr_ != end_;
103  }
104
105 private:
106  history::URLRows::const_iterator itr_;
107  history::URLRows::const_iterator end_;
108
109  DISALLOW_COPY_AND_ASSIGN(URLIteratorFromURLRows);
110};
111
112// Callback from WebHistoryService::ExpireWebHistory().
113void ExpireWebHistoryComplete(bool success) {
114  // Ignore the result.
115  //
116  // TODO(davidben): ExpireLocalAndRemoteHistoryBetween callback should not fire
117  // until this completes.
118}
119
120}  // namespace
121
122// Sends messages from the backend to us on the main thread. This must be a
123// separate class from the history service so that it can hold a reference to
124// the history service (otherwise we would have to manually AddRef and
125// Release when the Backend has a reference to us).
126class HistoryService::BackendDelegate : public HistoryBackend::Delegate {
127 public:
128  BackendDelegate(
129      const base::WeakPtr<HistoryService>& history_service,
130      const scoped_refptr<base::SequencedTaskRunner>& service_task_runner,
131      Profile* profile)
132      : history_service_(history_service),
133        service_task_runner_(service_task_runner),
134        profile_(profile) {
135  }
136
137  virtual void NotifyProfileError(sql::InitStatus init_status) OVERRIDE {
138    // Send to the history service on the main thread.
139    service_task_runner_->PostTask(
140        FROM_HERE,
141        base::Bind(&HistoryService::NotifyProfileError, history_service_,
142                   init_status));
143  }
144
145  virtual void SetInMemoryBackend(
146      scoped_ptr<history::InMemoryHistoryBackend> backend) OVERRIDE {
147    // Send the backend to the history service on the main thread.
148    service_task_runner_->PostTask(
149        FROM_HERE,
150        base::Bind(&HistoryService::SetInMemoryBackend, history_service_,
151                   base::Passed(&backend)));
152  }
153
154  virtual void BroadcastNotifications(
155      int type,
156      scoped_ptr<history::HistoryDetails> details) OVERRIDE {
157    // Send the notification on the history thread.
158    if (content::NotificationService::current()) {
159      content::Details<history::HistoryDetails> det(details.get());
160      content::NotificationService::current()->Notify(
161          type, content::Source<Profile>(profile_), det);
162    }
163    // Send the notification to the history service on the main thread.
164    service_task_runner_->PostTask(
165        FROM_HERE,
166        base::Bind(&HistoryService::BroadcastNotificationsHelper,
167                   history_service_, type, base::Passed(&details)));
168  }
169
170  virtual void DBLoaded() OVERRIDE {
171    service_task_runner_->PostTask(
172        FROM_HERE,
173        base::Bind(&HistoryService::OnDBLoaded, history_service_));
174  }
175
176  virtual void NotifyVisitDBObserversOnAddVisit(
177      const history::BriefVisitInfo& info) OVERRIDE {
178    service_task_runner_->PostTask(
179        FROM_HERE,
180        base::Bind(&HistoryService::NotifyVisitDBObserversOnAddVisit,
181                   history_service_, info));
182  }
183
184 private:
185  const base::WeakPtr<HistoryService> history_service_;
186  const scoped_refptr<base::SequencedTaskRunner> service_task_runner_;
187  Profile* const profile_;
188};
189
190// The history thread is intentionally not a BrowserThread because the
191// sync integration unit tests depend on being able to create more than one
192// history thread.
193HistoryService::HistoryService()
194    : weak_ptr_factory_(this),
195      thread_(new base::Thread(kHistoryThreadName)),
196      profile_(NULL),
197      backend_loaded_(false),
198      bookmark_service_(NULL),
199      no_db_(false) {
200}
201
202HistoryService::HistoryService(Profile* profile)
203    : weak_ptr_factory_(this),
204      thread_(new base::Thread(kHistoryThreadName)),
205      profile_(profile),
206      visitedlink_master_(new visitedlink::VisitedLinkMaster(
207          profile, this, true)),
208      backend_loaded_(false),
209      bookmark_service_(NULL),
210      no_db_(false) {
211  DCHECK(profile_);
212  registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
213                 content::Source<Profile>(profile_));
214  registrar_.Add(this, chrome::NOTIFICATION_TEMPLATE_URL_REMOVED,
215                 content::Source<Profile>(profile_));
216}
217
218HistoryService::~HistoryService() {
219  DCHECK(thread_checker_.CalledOnValidThread());
220  // Shutdown the backend. This does nothing if Cleanup was already invoked.
221  Cleanup();
222}
223
224bool HistoryService::BackendLoaded() {
225  DCHECK(thread_checker_.CalledOnValidThread());
226  return backend_loaded_;
227}
228
229void HistoryService::Cleanup() {
230  DCHECK(thread_checker_.CalledOnValidThread());
231  if (!thread_) {
232    // We've already cleaned up.
233    return;
234  }
235
236  weak_ptr_factory_.InvalidateWeakPtrs();
237
238  // Unload the backend.
239  if (history_backend_) {
240    // Get rid of the in-memory backend.
241    in_memory_backend_.reset();
242
243    // Give the InMemoryURLIndex a chance to shutdown.
244    // NOTE: In tests, there may be no index.
245    if (in_memory_url_index_)
246      in_memory_url_index_->ShutDown();
247
248    // The backend's destructor must run on the history thread since it is not
249    // threadsafe. So this thread must not be the last thread holding a
250    // reference to the backend, or a crash could happen.
251    //
252    // We have a reference to the history backend. There is also an extra
253    // reference held by our delegate installed in the backend, which
254    // HistoryBackend::Closing will release. This means if we scheduled a call
255    // to HistoryBackend::Closing and *then* released our backend reference,
256    // there will be a race between us and the backend's Closing function to see
257    // who is the last holder of a reference. If the backend thread's Closing
258    // manages to run before we release our backend refptr, the last reference
259    // will be held by this thread and the destructor will be called from here.
260    //
261    // Therefore, we create a closure to run the Closing operation first. This
262    // holds a reference to the backend. Then we release our reference, then we
263    // schedule the task to run. After the task runs, it will delete its
264    // reference from the history thread, ensuring everything works properly.
265    //
266    // TODO(ajwong): Cleanup HistoryBackend lifetime issues.
267    //     See http://crbug.com/99767.
268    history_backend_->AddRef();
269    base::Closure closing_task =
270        base::Bind(&HistoryBackend::Closing, history_backend_.get());
271    ScheduleTask(PRIORITY_NORMAL, closing_task);
272    closing_task.Reset();
273    HistoryBackend* raw_ptr = history_backend_.get();
274    history_backend_ = NULL;
275    thread_->message_loop()->ReleaseSoon(FROM_HERE, raw_ptr);
276  }
277
278  // Delete the thread, which joins with the background thread. We defensively
279  // NULL the pointer before deleting it in case somebody tries to use it
280  // during shutdown, but this shouldn't happen.
281  base::Thread* thread = thread_;
282  thread_ = NULL;
283  delete thread;
284}
285
286void HistoryService::NotifyRenderProcessHostDestruction(const void* host) {
287  DCHECK(thread_checker_.CalledOnValidThread());
288  ScheduleAndForget(PRIORITY_NORMAL,
289                    &HistoryBackend::NotifyRenderProcessHostDestruction, host);
290}
291
292history::URLDatabase* HistoryService::InMemoryDatabase() {
293  DCHECK(thread_checker_.CalledOnValidThread());
294  return in_memory_backend_ ? in_memory_backend_->db() : NULL;
295}
296
297bool HistoryService::GetTypedCountForURL(const GURL& url, int* typed_count) {
298  DCHECK(thread_checker_.CalledOnValidThread());
299  history::URLRow url_row;
300  if (!GetRowForURL(url, &url_row))
301    return false;
302  *typed_count = url_row.typed_count();
303  return true;
304}
305
306bool HistoryService::GetLastVisitTimeForURL(const GURL& url,
307                                            base::Time* last_visit) {
308  DCHECK(thread_checker_.CalledOnValidThread());
309  history::URLRow url_row;
310  if (!GetRowForURL(url, &url_row))
311    return false;
312  *last_visit = url_row.last_visit();
313  return true;
314}
315
316bool HistoryService::GetVisitCountForURL(const GURL& url, int* visit_count) {
317  DCHECK(thread_checker_.CalledOnValidThread());
318  history::URLRow url_row;
319  if (!GetRowForURL(url, &url_row))
320    return false;
321  *visit_count = url_row.visit_count();
322  return true;
323}
324
325history::TypedUrlSyncableService* HistoryService::GetTypedUrlSyncableService()
326    const {
327  return history_backend_->GetTypedUrlSyncableService();
328}
329
330void HistoryService::Shutdown() {
331  DCHECK(thread_checker_.CalledOnValidThread());
332  // It's possible that bookmarks haven't loaded and history is waiting for
333  // bookmarks to complete loading. In such a situation history can't shutdown
334  // (meaning if we invoked history_service_->Cleanup now, we would
335  // deadlock). To break the deadlock we tell BookmarkModel it's about to be
336  // deleted so that it can release the signal history is waiting on, allowing
337  // history to shutdown (history_service_->Cleanup to complete). In such a
338  // scenario history sees an incorrect view of bookmarks, but it's better
339  // than a deadlock.
340  BookmarkModel* bookmark_model = static_cast<BookmarkModel*>(
341      BookmarkModelFactory::GetForProfileIfExists(profile_));
342  if (bookmark_model)
343    bookmark_model->Shutdown();
344
345  Cleanup();
346}
347
348void HistoryService::SetKeywordSearchTermsForURL(const GURL& url,
349                                                 TemplateURLID keyword_id,
350                                                 const base::string16& term) {
351  DCHECK(thread_checker_.CalledOnValidThread());
352  ScheduleAndForget(PRIORITY_UI,
353                    &HistoryBackend::SetKeywordSearchTermsForURL,
354                    url, keyword_id, term);
355}
356
357void HistoryService::DeleteAllSearchTermsForKeyword(
358    TemplateURLID keyword_id) {
359  DCHECK(thread_checker_.CalledOnValidThread());
360  ScheduleAndForget(PRIORITY_UI,
361                    &HistoryBackend::DeleteAllSearchTermsForKeyword,
362                    keyword_id);
363}
364
365HistoryService::Handle HistoryService::GetMostRecentKeywordSearchTerms(
366    TemplateURLID keyword_id,
367    const base::string16& prefix,
368    int max_count,
369    CancelableRequestConsumerBase* consumer,
370    const GetMostRecentKeywordSearchTermsCallback& callback) {
371  DCHECK(thread_checker_.CalledOnValidThread());
372  return Schedule(PRIORITY_UI, &HistoryBackend::GetMostRecentKeywordSearchTerms,
373                  consumer,
374                  new history::GetMostRecentKeywordSearchTermsRequest(callback),
375                  keyword_id, prefix, max_count);
376}
377
378void HistoryService::DeleteKeywordSearchTermForURL(const GURL& url) {
379  DCHECK(thread_checker_.CalledOnValidThread());
380  ScheduleAndForget(PRIORITY_UI, &HistoryBackend::DeleteKeywordSearchTermForURL,
381                    url);
382}
383
384void HistoryService::DeleteMatchingURLsForKeyword(TemplateURLID keyword_id,
385                                                  const base::string16& term) {
386  DCHECK(thread_checker_.CalledOnValidThread());
387  ScheduleAndForget(PRIORITY_UI, &HistoryBackend::DeleteMatchingURLsForKeyword,
388                    keyword_id, term);
389}
390
391void HistoryService::URLsNoLongerBookmarked(const std::set<GURL>& urls) {
392  DCHECK(thread_checker_.CalledOnValidThread());
393  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::URLsNoLongerBookmarked,
394                    urls);
395}
396
397void HistoryService::ScheduleDBTask(history::HistoryDBTask* task,
398                                    CancelableRequestConsumerBase* consumer) {
399  DCHECK(thread_checker_.CalledOnValidThread());
400  history::HistoryDBTaskRequest* request = new history::HistoryDBTaskRequest(
401      base::Bind(&history::HistoryDBTask::DoneRunOnMainThread, task));
402  request->value = task;  // The value is the task to execute.
403  Schedule(PRIORITY_UI, &HistoryBackend::ProcessDBTask, consumer, request);
404}
405
406HistoryService::Handle HistoryService::QuerySegmentUsageSince(
407    CancelableRequestConsumerBase* consumer,
408    const Time from_time,
409    int max_result_count,
410    const SegmentQueryCallback& callback) {
411  DCHECK(thread_checker_.CalledOnValidThread());
412  return Schedule(PRIORITY_UI, &HistoryBackend::QuerySegmentUsage,
413                  consumer, new history::QuerySegmentUsageRequest(callback),
414                  from_time, max_result_count);
415}
416
417void HistoryService::FlushForTest(const base::Closure& flushed) {
418  thread_->message_loop_proxy()->PostTaskAndReply(
419      FROM_HERE, base::Bind(&base::DoNothing), flushed);
420}
421
422void HistoryService::SetOnBackendDestroyTask(const base::Closure& task) {
423  DCHECK(thread_checker_.CalledOnValidThread());
424  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::SetOnBackendDestroyTask,
425                    base::MessageLoop::current(), task);
426}
427
428void HistoryService::AddPage(const GURL& url,
429                             Time time,
430                             const void* id_scope,
431                             int32 page_id,
432                             const GURL& referrer,
433                             const history::RedirectList& redirects,
434                             content::PageTransition transition,
435                             history::VisitSource visit_source,
436                             bool did_replace_entry) {
437  DCHECK(thread_checker_.CalledOnValidThread());
438  AddPage(
439      history::HistoryAddPageArgs(url, time, id_scope, page_id, referrer,
440                                  redirects, transition, visit_source,
441                                  did_replace_entry));
442}
443
444void HistoryService::AddPage(const GURL& url,
445                             base::Time time,
446                             history::VisitSource visit_source) {
447  DCHECK(thread_checker_.CalledOnValidThread());
448  AddPage(
449      history::HistoryAddPageArgs(url, time, NULL, 0, GURL(),
450                                  history::RedirectList(),
451                                  content::PAGE_TRANSITION_LINK,
452                                  visit_source, false));
453}
454
455void HistoryService::AddPage(const history::HistoryAddPageArgs& add_page_args) {
456  DCHECK(thread_checker_.CalledOnValidThread());
457  DCHECK(thread_) << "History service being called after cleanup";
458
459  // Filter out unwanted URLs. We don't add auto-subframe URLs. They are a
460  // large part of history (think iframes for ads) and we never display them in
461  // history UI. We will still add manual subframes, which are ones the user
462  // has clicked on to get.
463  if (!CanAddURL(add_page_args.url))
464    return;
465
466  // Add link & all redirects to visited link list.
467  if (visitedlink_master_) {
468    visitedlink_master_->AddURL(add_page_args.url);
469
470    if (!add_page_args.redirects.empty()) {
471      // We should not be asked to add a page in the middle of a redirect chain.
472      DCHECK_EQ(add_page_args.url,
473                add_page_args.redirects[add_page_args.redirects.size() - 1]);
474
475      // We need the !redirects.empty() condition above since size_t is unsigned
476      // and will wrap around when we subtract one from a 0 size.
477      for (size_t i = 0; i < add_page_args.redirects.size() - 1; i++)
478        visitedlink_master_->AddURL(add_page_args.redirects[i]);
479    }
480  }
481
482  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::AddPage, add_page_args);
483}
484
485void HistoryService::AddPageNoVisitForBookmark(const GURL& url,
486                                               const base::string16& title) {
487  DCHECK(thread_checker_.CalledOnValidThread());
488  if (!CanAddURL(url))
489    return;
490
491  ScheduleAndForget(PRIORITY_NORMAL,
492                    &HistoryBackend::AddPageNoVisitForBookmark, url, title);
493}
494
495void HistoryService::SetPageTitle(const GURL& url,
496                                  const base::string16& title) {
497  DCHECK(thread_checker_.CalledOnValidThread());
498  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::SetPageTitle, url, title);
499}
500
501void HistoryService::UpdateWithPageEndTime(const void* host,
502                                           int32 page_id,
503                                           const GURL& url,
504                                           Time end_ts) {
505  DCHECK(thread_checker_.CalledOnValidThread());
506  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::UpdateWithPageEndTime,
507                    host, page_id, url, end_ts);
508}
509
510void HistoryService::AddPageWithDetails(const GURL& url,
511                                        const base::string16& title,
512                                        int visit_count,
513                                        int typed_count,
514                                        Time last_visit,
515                                        bool hidden,
516                                        history::VisitSource visit_source) {
517  DCHECK(thread_checker_.CalledOnValidThread());
518  // Filter out unwanted URLs.
519  if (!CanAddURL(url))
520    return;
521
522  // Add to the visited links system.
523  if (visitedlink_master_)
524    visitedlink_master_->AddURL(url);
525
526  history::URLRow row(url);
527  row.set_title(title);
528  row.set_visit_count(visit_count);
529  row.set_typed_count(typed_count);
530  row.set_last_visit(last_visit);
531  row.set_hidden(hidden);
532
533  history::URLRows rows;
534  rows.push_back(row);
535
536  ScheduleAndForget(PRIORITY_NORMAL,
537                    &HistoryBackend::AddPagesWithDetails, rows, visit_source);
538}
539
540void HistoryService::AddPagesWithDetails(const history::URLRows& info,
541                                         history::VisitSource visit_source) {
542  DCHECK(thread_checker_.CalledOnValidThread());
543  // Add to the visited links system.
544  if (visitedlink_master_) {
545    std::vector<GURL> urls;
546    urls.reserve(info.size());
547    for (history::URLRows::const_iterator i = info.begin(); i != info.end();
548         ++i)
549      urls.push_back(i->url());
550
551    visitedlink_master_->AddURLs(urls);
552  }
553
554  ScheduleAndForget(PRIORITY_NORMAL,
555                    &HistoryBackend::AddPagesWithDetails, info, visit_source);
556}
557
558base::CancelableTaskTracker::TaskId HistoryService::GetFavicons(
559    const std::vector<GURL>& icon_urls,
560    int icon_types,
561    int desired_size_in_dip,
562    const std::vector<ui::ScaleFactor>& desired_scale_factors,
563    const FaviconService::FaviconResultsCallback& callback,
564    base::CancelableTaskTracker* tracker) {
565  DCHECK(thread_checker_.CalledOnValidThread());
566
567  std::vector<chrome::FaviconBitmapResult>* results =
568      new std::vector<chrome::FaviconBitmapResult>();
569  return tracker->PostTaskAndReply(
570      thread_->message_loop_proxy().get(),
571      FROM_HERE,
572      base::Bind(&HistoryBackend::GetFavicons,
573                 history_backend_.get(),
574                 icon_urls,
575                 icon_types,
576                 desired_size_in_dip,
577                 desired_scale_factors,
578                 results),
579      base::Bind(&RunWithFaviconResults, callback, base::Owned(results)));
580}
581
582base::CancelableTaskTracker::TaskId HistoryService::GetFaviconsForURL(
583    const GURL& page_url,
584    int icon_types,
585    int desired_size_in_dip,
586    const std::vector<ui::ScaleFactor>& desired_scale_factors,
587    const FaviconService::FaviconResultsCallback& callback,
588    base::CancelableTaskTracker* tracker) {
589  DCHECK(thread_checker_.CalledOnValidThread());
590
591  std::vector<chrome::FaviconBitmapResult>* results =
592      new std::vector<chrome::FaviconBitmapResult>();
593  return tracker->PostTaskAndReply(
594      thread_->message_loop_proxy().get(),
595      FROM_HERE,
596      base::Bind(&HistoryBackend::GetFaviconsForURL,
597                 history_backend_.get(),
598                 page_url,
599                 icon_types,
600                 desired_size_in_dip,
601                 desired_scale_factors,
602                 results),
603      base::Bind(&RunWithFaviconResults, callback, base::Owned(results)));
604}
605
606base::CancelableTaskTracker::TaskId HistoryService::GetLargestFaviconForURL(
607    const GURL& page_url,
608    const std::vector<int>& icon_types,
609    int minimum_size_in_pixels,
610    const FaviconService::FaviconRawCallback& callback,
611    base::CancelableTaskTracker* tracker) {
612  DCHECK(thread_checker_.CalledOnValidThread());
613
614  chrome::FaviconBitmapResult* result = new chrome::FaviconBitmapResult();
615  return tracker->PostTaskAndReply(
616      thread_->message_loop_proxy().get(),
617      FROM_HERE,
618      base::Bind(&HistoryBackend::GetLargestFaviconForURL,
619                 history_backend_.get(),
620                 page_url,
621                 icon_types,
622                 minimum_size_in_pixels,
623                 result),
624      base::Bind(&RunWithFaviconResult, callback, base::Owned(result)));
625}
626
627base::CancelableTaskTracker::TaskId HistoryService::GetFaviconForID(
628    chrome::FaviconID favicon_id,
629    int desired_size_in_dip,
630    ui::ScaleFactor desired_scale_factor,
631    const FaviconService::FaviconResultsCallback& callback,
632    base::CancelableTaskTracker* tracker) {
633  DCHECK(thread_checker_.CalledOnValidThread());
634
635  std::vector<chrome::FaviconBitmapResult>* results =
636      new std::vector<chrome::FaviconBitmapResult>();
637  return tracker->PostTaskAndReply(
638      thread_->message_loop_proxy().get(),
639      FROM_HERE,
640      base::Bind(&HistoryBackend::GetFaviconForID,
641                 history_backend_.get(),
642                 favicon_id,
643                 desired_size_in_dip,
644                 desired_scale_factor,
645                 results),
646      base::Bind(&RunWithFaviconResults, callback, base::Owned(results)));
647}
648
649base::CancelableTaskTracker::TaskId
650HistoryService::UpdateFaviconMappingsAndFetch(
651    const GURL& page_url,
652    const std::vector<GURL>& icon_urls,
653    int icon_types,
654    int desired_size_in_dip,
655    const std::vector<ui::ScaleFactor>& desired_scale_factors,
656    const FaviconService::FaviconResultsCallback& callback,
657    base::CancelableTaskTracker* tracker) {
658  DCHECK(thread_checker_.CalledOnValidThread());
659
660  std::vector<chrome::FaviconBitmapResult>* results =
661      new std::vector<chrome::FaviconBitmapResult>();
662  return tracker->PostTaskAndReply(
663      thread_->message_loop_proxy().get(),
664      FROM_HERE,
665      base::Bind(&HistoryBackend::UpdateFaviconMappingsAndFetch,
666                 history_backend_.get(),
667                 page_url,
668                 icon_urls,
669                 icon_types,
670                 desired_size_in_dip,
671                 desired_scale_factors,
672                 results),
673      base::Bind(&RunWithFaviconResults, callback, base::Owned(results)));
674}
675
676void HistoryService::MergeFavicon(
677    const GURL& page_url,
678    const GURL& icon_url,
679    chrome::IconType icon_type,
680    scoped_refptr<base::RefCountedMemory> bitmap_data,
681    const gfx::Size& pixel_size) {
682  DCHECK(thread_checker_.CalledOnValidThread());
683  if (!CanAddURL(page_url))
684    return;
685
686  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::MergeFavicon, page_url,
687                    icon_url, icon_type, bitmap_data, pixel_size);
688}
689
690void HistoryService::SetFavicons(
691    const GURL& page_url,
692    chrome::IconType icon_type,
693    const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data) {
694  DCHECK(thread_checker_.CalledOnValidThread());
695  if (!CanAddURL(page_url))
696    return;
697
698  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::SetFavicons, page_url,
699      icon_type, favicon_bitmap_data);
700}
701
702void HistoryService::SetFaviconsOutOfDateForPage(const GURL& page_url) {
703  DCHECK(thread_checker_.CalledOnValidThread());
704  ScheduleAndForget(PRIORITY_NORMAL,
705                    &HistoryBackend::SetFaviconsOutOfDateForPage, page_url);
706}
707
708void HistoryService::CloneFavicons(const GURL& old_page_url,
709                                   const GURL& new_page_url) {
710  DCHECK(thread_checker_.CalledOnValidThread());
711  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::CloneFavicons,
712                    old_page_url, new_page_url);
713}
714
715void HistoryService::SetImportedFavicons(
716    const std::vector<ImportedFaviconUsage>& favicon_usage) {
717  DCHECK(thread_checker_.CalledOnValidThread());
718  ScheduleAndForget(PRIORITY_NORMAL,
719                    &HistoryBackend::SetImportedFavicons, favicon_usage);
720}
721
722HistoryService::Handle HistoryService::QueryURL(
723    const GURL& url,
724    bool want_visits,
725    CancelableRequestConsumerBase* consumer,
726    const QueryURLCallback& callback) {
727  DCHECK(thread_checker_.CalledOnValidThread());
728  return Schedule(PRIORITY_UI, &HistoryBackend::QueryURL, consumer,
729                  new history::QueryURLRequest(callback), url, want_visits);
730}
731
732// Downloads -------------------------------------------------------------------
733
734// Handle creation of a download by creating an entry in the history service's
735// 'downloads' table.
736void HistoryService::CreateDownload(
737    const history::DownloadRow& create_info,
738    const HistoryService::DownloadCreateCallback& callback) {
739  DCHECK(thread_) << "History service being called after cleanup";
740  DCHECK(thread_checker_.CalledOnValidThread());
741  PostTaskAndReplyWithResult(
742      thread_->message_loop_proxy(), FROM_HERE,
743      base::Bind(&HistoryBackend::CreateDownload, history_backend_.get(),
744                 create_info),
745      callback);
746}
747
748void HistoryService::GetNextDownloadId(
749    const content::DownloadIdCallback& callback) {
750  DCHECK(thread_) << "History service being called after cleanup";
751  DCHECK(thread_checker_.CalledOnValidThread());
752  PostTaskAndReplyWithResult(
753      thread_->message_loop_proxy(), FROM_HERE,
754      base::Bind(&HistoryBackend::GetNextDownloadId, history_backend_.get()),
755      callback);
756}
757
758// Handle queries for a list of all downloads in the history database's
759// 'downloads' table.
760void HistoryService::QueryDownloads(
761    const DownloadQueryCallback& callback) {
762  DCHECK(thread_) << "History service being called after cleanup";
763  DCHECK(thread_checker_.CalledOnValidThread());
764  std::vector<history::DownloadRow>* rows =
765    new std::vector<history::DownloadRow>();
766  scoped_ptr<std::vector<history::DownloadRow> > scoped_rows(rows);
767  // Beware! The first Bind() does not simply |scoped_rows.get()| because
768  // base::Passed(&scoped_rows) nullifies |scoped_rows|, and compilers do not
769  // guarantee that the first Bind's arguments are evaluated before the second
770  // Bind's arguments.
771  thread_->message_loop_proxy()->PostTaskAndReply(
772      FROM_HERE,
773      base::Bind(&HistoryBackend::QueryDownloads, history_backend_.get(), rows),
774      base::Bind(callback, base::Passed(&scoped_rows)));
775}
776
777// Handle updates for a particular download. This is a 'fire and forget'
778// operation, so we don't need to be called back.
779void HistoryService::UpdateDownload(const history::DownloadRow& data) {
780  DCHECK(thread_checker_.CalledOnValidThread());
781  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::UpdateDownload, data);
782}
783
784void HistoryService::RemoveDownloads(const std::set<uint32>& ids) {
785  DCHECK(thread_checker_.CalledOnValidThread());
786  ScheduleAndForget(PRIORITY_NORMAL,
787                    &HistoryBackend::RemoveDownloads, ids);
788}
789
790HistoryService::Handle HistoryService::QueryHistory(
791    const base::string16& text_query,
792    const history::QueryOptions& options,
793    CancelableRequestConsumerBase* consumer,
794    const QueryHistoryCallback& callback) {
795  DCHECK(thread_checker_.CalledOnValidThread());
796  return Schedule(PRIORITY_UI, &HistoryBackend::QueryHistory, consumer,
797                  new history::QueryHistoryRequest(callback),
798                  text_query, options);
799}
800
801HistoryService::Handle HistoryService::QueryRedirectsFrom(
802    const GURL& from_url,
803    CancelableRequestConsumerBase* consumer,
804    const QueryRedirectsCallback& callback) {
805  DCHECK(thread_checker_.CalledOnValidThread());
806  return Schedule(PRIORITY_UI, &HistoryBackend::QueryRedirectsFrom, consumer,
807      new history::QueryRedirectsRequest(callback), from_url);
808}
809
810HistoryService::Handle HistoryService::QueryRedirectsTo(
811    const GURL& to_url,
812    CancelableRequestConsumerBase* consumer,
813    const QueryRedirectsCallback& callback) {
814  DCHECK(thread_checker_.CalledOnValidThread());
815  return Schedule(PRIORITY_NORMAL, &HistoryBackend::QueryRedirectsTo, consumer,
816      new history::QueryRedirectsRequest(callback), to_url);
817}
818
819HistoryService::Handle HistoryService::GetVisibleVisitCountToHost(
820    const GURL& url,
821    CancelableRequestConsumerBase* consumer,
822    const GetVisibleVisitCountToHostCallback& callback) {
823  DCHECK(thread_checker_.CalledOnValidThread());
824  return Schedule(PRIORITY_UI, &HistoryBackend::GetVisibleVisitCountToHost,
825      consumer, new history::GetVisibleVisitCountToHostRequest(callback), url);
826}
827
828HistoryService::Handle HistoryService::QueryTopURLsAndRedirects(
829    int result_count,
830    CancelableRequestConsumerBase* consumer,
831    const QueryTopURLsAndRedirectsCallback& callback) {
832  DCHECK(thread_checker_.CalledOnValidThread());
833  return Schedule(PRIORITY_NORMAL, &HistoryBackend::QueryTopURLsAndRedirects,
834      consumer, new history::QueryTopURLsAndRedirectsRequest(callback),
835      result_count);
836}
837
838HistoryService::Handle HistoryService::QueryMostVisitedURLs(
839    int result_count,
840    int days_back,
841    CancelableRequestConsumerBase* consumer,
842    const QueryMostVisitedURLsCallback& callback) {
843  DCHECK(thread_checker_.CalledOnValidThread());
844  return Schedule(PRIORITY_NORMAL, &HistoryBackend::QueryMostVisitedURLs,
845                  consumer,
846                  new history::QueryMostVisitedURLsRequest(callback),
847                  result_count, days_back);
848}
849
850HistoryService::Handle HistoryService::QueryFilteredURLs(
851    int result_count,
852    const history::VisitFilter& filter,
853    bool extended_info,
854    CancelableRequestConsumerBase* consumer,
855    const QueryFilteredURLsCallback& callback) {
856  DCHECK(thread_checker_.CalledOnValidThread());
857  return Schedule(PRIORITY_NORMAL,
858                  &HistoryBackend::QueryFilteredURLs,
859                  consumer,
860                  new history::QueryFilteredURLsRequest(callback),
861                  result_count, filter, extended_info);
862}
863
864void HistoryService::Observe(int type,
865                             const content::NotificationSource& source,
866                             const content::NotificationDetails& details) {
867  DCHECK(thread_checker_.CalledOnValidThread());
868  if (!thread_)
869    return;
870
871  switch (type) {
872    case chrome::NOTIFICATION_HISTORY_URLS_DELETED: {
873      // Update the visited link system for deleted URLs. We will update the
874      // visited link system for added URLs as soon as we get the add
875      // notification (we don't have to wait for the backend, which allows us to
876      // be faster to update the state).
877      //
878      // For deleted URLs, we don't typically know what will be deleted since
879      // delete notifications are by time. We would also like to be more
880      // respectful of privacy and never tell the user something is gone when it
881      // isn't. Therefore, we update the delete URLs after the fact.
882      if (visitedlink_master_) {
883        content::Details<history::URLsDeletedDetails> deleted_details(details);
884
885        if (deleted_details->all_history) {
886          visitedlink_master_->DeleteAllURLs();
887        } else {
888          URLIteratorFromURLRows iterator(deleted_details->rows);
889          visitedlink_master_->DeleteURLs(&iterator);
890        }
891      }
892      break;
893    }
894
895    case chrome::NOTIFICATION_TEMPLATE_URL_REMOVED:
896      DeleteAllSearchTermsForKeyword(
897          *(content::Details<TemplateURLID>(details).ptr()));
898      break;
899
900    default:
901      NOTREACHED();
902  }
903}
904
905void HistoryService::RebuildTable(
906    const scoped_refptr<URLEnumerator>& enumerator) {
907  DCHECK(thread_checker_.CalledOnValidThread());
908  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::IterateURLs, enumerator);
909}
910
911bool HistoryService::Init(const base::FilePath& history_dir,
912                          BookmarkService* bookmark_service,
913                          bool no_db) {
914  DCHECK(thread_checker_.CalledOnValidThread());
915  if (!thread_->Start()) {
916    Cleanup();
917    return false;
918  }
919
920  history_dir_ = history_dir;
921  bookmark_service_ = bookmark_service;
922  no_db_ = no_db;
923
924  if (profile_) {
925    std::string languages =
926        profile_->GetPrefs()->GetString(prefs::kAcceptLanguages);
927    in_memory_url_index_.reset(
928        new history::InMemoryURLIndex(profile_, history_dir_, languages));
929    in_memory_url_index_->Init();
930  }
931
932  // Create the history backend.
933  scoped_refptr<HistoryBackend> backend(
934      new HistoryBackend(history_dir_,
935                         new BackendDelegate(
936                             weak_ptr_factory_.GetWeakPtr(),
937                             base::ThreadTaskRunnerHandle::Get(),
938                             profile_),
939                         bookmark_service_));
940  history_backend_.swap(backend);
941
942  // There may not be a profile when unit testing.
943  std::string languages;
944  if (profile_) {
945    PrefService* prefs = profile_->GetPrefs();
946    languages = prefs->GetString(prefs::kAcceptLanguages);
947  }
948  ScheduleAndForget(PRIORITY_UI, &HistoryBackend::Init, languages, no_db_);
949
950  if (visitedlink_master_) {
951    bool result = visitedlink_master_->Init();
952    DCHECK(result);
953  }
954
955  return true;
956}
957
958void HistoryService::ScheduleAutocomplete(HistoryURLProvider* provider,
959                                          HistoryURLProviderParams* params) {
960  DCHECK(thread_checker_.CalledOnValidThread());
961  ScheduleAndForget(PRIORITY_UI, &HistoryBackend::ScheduleAutocomplete,
962                    scoped_refptr<HistoryURLProvider>(provider), params);
963}
964
965void HistoryService::ScheduleTask(SchedulePriority priority,
966                                  const base::Closure& task) {
967  DCHECK(thread_checker_.CalledOnValidThread());
968  CHECK(thread_);
969  CHECK(thread_->message_loop());
970  // TODO(brettw): Do prioritization.
971  thread_->message_loop()->PostTask(FROM_HERE, task);
972}
973
974// static
975bool HistoryService::CanAddURL(const GURL& url) {
976  if (!url.is_valid())
977    return false;
978
979  // TODO: We should allow kChromeUIScheme URLs if they have been explicitly
980  // typed.  Right now, however, these are marked as typed even when triggered
981  // by a shortcut or menu action.
982  if (url.SchemeIs(content::kJavaScriptScheme) ||
983      url.SchemeIs(content::kChromeDevToolsScheme) ||
984      url.SchemeIs(content::kChromeUIScheme) ||
985      url.SchemeIs(content::kViewSourceScheme) ||
986      url.SchemeIs(chrome::kChromeNativeScheme) ||
987      url.SchemeIs(chrome::kChromeSearchScheme) ||
988      url.SchemeIs(chrome::kDomDistillerScheme))
989    return false;
990
991  // Allow all about: and chrome: URLs except about:blank, since the user may
992  // like to see "chrome://memory/", etc. in their history and autocomplete.
993  if (url == GURL(content::kAboutBlankURL))
994    return false;
995
996  return true;
997}
998
999base::WeakPtr<HistoryService> HistoryService::AsWeakPtr() {
1000  DCHECK(thread_checker_.CalledOnValidThread());
1001  return weak_ptr_factory_.GetWeakPtr();
1002}
1003
1004syncer::SyncMergeResult HistoryService::MergeDataAndStartSyncing(
1005    syncer::ModelType type,
1006    const syncer::SyncDataList& initial_sync_data,
1007    scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
1008    scoped_ptr<syncer::SyncErrorFactory> error_handler) {
1009  DCHECK(thread_checker_.CalledOnValidThread());
1010  DCHECK_EQ(type, syncer::HISTORY_DELETE_DIRECTIVES);
1011  delete_directive_handler_.Start(this, initial_sync_data,
1012                                  sync_processor.Pass());
1013  return syncer::SyncMergeResult(type);
1014}
1015
1016void HistoryService::StopSyncing(syncer::ModelType type) {
1017  DCHECK(thread_checker_.CalledOnValidThread());
1018  DCHECK_EQ(type, syncer::HISTORY_DELETE_DIRECTIVES);
1019  delete_directive_handler_.Stop();
1020}
1021
1022syncer::SyncDataList HistoryService::GetAllSyncData(
1023    syncer::ModelType type) const {
1024  DCHECK(thread_checker_.CalledOnValidThread());
1025  DCHECK_EQ(type, syncer::HISTORY_DELETE_DIRECTIVES);
1026  // TODO(akalin): Keep track of existing delete directives.
1027  return syncer::SyncDataList();
1028}
1029
1030syncer::SyncError HistoryService::ProcessSyncChanges(
1031    const tracked_objects::Location& from_here,
1032    const syncer::SyncChangeList& change_list) {
1033  delete_directive_handler_.ProcessSyncChanges(this, change_list);
1034  return syncer::SyncError();
1035}
1036
1037syncer::SyncError HistoryService::ProcessLocalDeleteDirective(
1038    const sync_pb::HistoryDeleteDirectiveSpecifics& delete_directive) {
1039  DCHECK(thread_checker_.CalledOnValidThread());
1040  return delete_directive_handler_.ProcessLocalDeleteDirective(
1041      delete_directive);
1042}
1043
1044void HistoryService::SetInMemoryBackend(
1045    scoped_ptr<history::InMemoryHistoryBackend> mem_backend) {
1046  DCHECK(thread_checker_.CalledOnValidThread());
1047  DCHECK(!in_memory_backend_) << "Setting mem DB twice";
1048  in_memory_backend_.reset(mem_backend.release());
1049
1050  // The database requires additional initialization once we own it.
1051  in_memory_backend_->AttachToHistoryService(profile_);
1052}
1053
1054void HistoryService::NotifyProfileError(sql::InitStatus init_status) {
1055  DCHECK(thread_checker_.CalledOnValidThread());
1056  ShowProfileErrorDialog(
1057      PROFILE_ERROR_HISTORY,
1058      (init_status == sql::INIT_FAILURE) ?
1059      IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR);
1060}
1061
1062void HistoryService::DeleteURL(const GURL& url) {
1063  DCHECK(thread_checker_.CalledOnValidThread());
1064  // We will update the visited links when we observe the delete notifications.
1065  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::DeleteURL, url);
1066}
1067
1068void HistoryService::DeleteURLsForTest(const std::vector<GURL>& urls) {
1069  DCHECK(thread_checker_.CalledOnValidThread());
1070  // We will update the visited links when we observe the delete
1071  // notifications.
1072  ScheduleAndForget(PRIORITY_NORMAL, &HistoryBackend::DeleteURLs, urls);
1073}
1074
1075void HistoryService::ExpireHistoryBetween(
1076    const std::set<GURL>& restrict_urls,
1077    Time begin_time,
1078    Time end_time,
1079    const base::Closure& callback,
1080    base::CancelableTaskTracker* tracker) {
1081  DCHECK(thread_);
1082  DCHECK(thread_checker_.CalledOnValidThread());
1083  DCHECK(history_backend_.get());
1084  tracker->PostTaskAndReply(thread_->message_loop_proxy().get(),
1085                            FROM_HERE,
1086                            base::Bind(&HistoryBackend::ExpireHistoryBetween,
1087                                       history_backend_,
1088                                       restrict_urls,
1089                                       begin_time,
1090                                       end_time),
1091                            callback);
1092}
1093
1094void HistoryService::ExpireHistory(
1095    const std::vector<history::ExpireHistoryArgs>& expire_list,
1096    const base::Closure& callback,
1097    base::CancelableTaskTracker* tracker) {
1098  DCHECK(thread_);
1099  DCHECK(thread_checker_.CalledOnValidThread());
1100  DCHECK(history_backend_.get());
1101  tracker->PostTaskAndReply(
1102      thread_->message_loop_proxy().get(),
1103      FROM_HERE,
1104      base::Bind(&HistoryBackend::ExpireHistory, history_backend_, expire_list),
1105      callback);
1106}
1107
1108void HistoryService::ExpireLocalAndRemoteHistoryBetween(
1109    const std::set<GURL>& restrict_urls,
1110    Time begin_time,
1111    Time end_time,
1112    const base::Closure& callback,
1113    base::CancelableTaskTracker* tracker) {
1114  // TODO(dubroy): This should be factored out into a separate class that
1115  // dispatches deletions to the proper places.
1116
1117  history::WebHistoryService* web_history =
1118      WebHistoryServiceFactory::GetForProfile(profile_);
1119  if (web_history) {
1120    // TODO(dubroy): This API does not yet support deletion of specific URLs.
1121    DCHECK(restrict_urls.empty());
1122
1123    delete_directive_handler_.CreateDeleteDirectives(
1124        std::set<int64>(), begin_time, end_time);
1125
1126    // Attempt online deletion from the history server, but ignore the result.
1127    // Deletion directives ensure that the results will eventually be deleted.
1128    //
1129    // TODO(davidben): |callback| should not run until this operation completes
1130    // too.
1131    web_history->ExpireHistoryBetween(
1132        restrict_urls, begin_time, end_time,
1133        base::Bind(&ExpireWebHistoryComplete));
1134  }
1135  ExpireHistoryBetween(restrict_urls, begin_time, end_time, callback, tracker);
1136}
1137
1138void HistoryService::BroadcastNotificationsHelper(
1139    int type,
1140    scoped_ptr<history::HistoryDetails> details) {
1141  DCHECK(thread_checker_.CalledOnValidThread());
1142  // TODO(evanm): this is currently necessitated by generate_profile, which
1143  // runs without a browser process. generate_profile should really create
1144  // a browser process, at which point this check can then be nuked.
1145  if (!g_browser_process)
1146    return;
1147
1148  if (!thread_)
1149    return;
1150
1151  // The source of all of our notifications is the profile. Note that this
1152  // pointer is NULL in unit tests.
1153  content::Source<Profile> source(profile_);
1154
1155  // The details object just contains the pointer to the object that the
1156  // backend has allocated for us. The receiver of the notification will cast
1157  // this to the proper type.
1158  content::Details<history::HistoryDetails> det(details.get());
1159
1160  content::NotificationService::current()->Notify(type, source, det);
1161}
1162
1163void HistoryService::OnDBLoaded() {
1164  DCHECK(thread_checker_.CalledOnValidThread());
1165  backend_loaded_ = true;
1166  content::NotificationService::current()->Notify(
1167      chrome::NOTIFICATION_HISTORY_LOADED,
1168      content::Source<Profile>(profile_),
1169      content::Details<HistoryService>(this));
1170}
1171
1172bool HistoryService::GetRowForURL(const GURL& url, history::URLRow* url_row) {
1173  DCHECK(thread_checker_.CalledOnValidThread());
1174  history::URLDatabase* db = InMemoryDatabase();
1175  return db && (db->GetRowForURL(url, url_row) != 0);
1176}
1177
1178void HistoryService::AddVisitDatabaseObserver(
1179    history::VisitDatabaseObserver* observer) {
1180  DCHECK(thread_checker_.CalledOnValidThread());
1181  visit_database_observers_.AddObserver(observer);
1182}
1183
1184void HistoryService::RemoveVisitDatabaseObserver(
1185    history::VisitDatabaseObserver* observer) {
1186  DCHECK(thread_checker_.CalledOnValidThread());
1187  visit_database_observers_.RemoveObserver(observer);
1188}
1189
1190void HistoryService::NotifyVisitDBObserversOnAddVisit(
1191    const history::BriefVisitInfo& info) {
1192  DCHECK(thread_checker_.CalledOnValidThread());
1193  FOR_EACH_OBSERVER(history::VisitDatabaseObserver, visit_database_observers_,
1194                    OnAddVisit(info));
1195}
1196