history_backend.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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#include "chrome/browser/history/history_backend.h"
6
7#include <algorithm>
8#include <functional>
9#include <list>
10#include <map>
11#include <set>
12#include <vector>
13
14#include "base/basictypes.h"
15#include "base/bind.h"
16#include "base/compiler_specific.h"
17#include "base/memory/scoped_ptr.h"
18#include "base/memory/scoped_vector.h"
19#include "base/message_loop/message_loop.h"
20#include "base/metrics/histogram.h"
21#include "base/rand_util.h"
22#include "base/strings/string_util.h"
23#include "base/strings/utf_string_conversions.h"
24#include "base/time/time.h"
25#include "chrome/browser/autocomplete/history_url_provider.h"
26#include "chrome/browser/bookmarks/bookmark_service.h"
27#include "chrome/browser/chrome_notification_types.h"
28#include "chrome/browser/favicon/favicon_changed_details.h"
29#include "chrome/browser/history/download_row.h"
30#include "chrome/browser/history/history_db_task.h"
31#include "chrome/browser/history/history_notifications.h"
32#include "chrome/browser/history/history_publisher.h"
33#include "chrome/browser/history/in_memory_history_backend.h"
34#include "chrome/browser/history/page_usage_data.h"
35#include "chrome/browser/history/select_favicon_frames.h"
36#include "chrome/browser/history/top_sites.h"
37#include "chrome/browser/history/typed_url_syncable_service.h"
38#include "chrome/browser/history/visit_filter.h"
39#include "chrome/common/chrome_constants.h"
40#include "chrome/common/importer/imported_favicon_usage.h"
41#include "chrome/common/url_constants.h"
42#include "grit/chromium_strings.h"
43#include "grit/generated_resources.h"
44#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
45#include "sql/error_delegate_util.h"
46#include "url/gurl.h"
47
48#if defined(OS_ANDROID)
49#include "chrome/browser/history/android/android_provider_backend.h"
50#endif
51
52using base::Time;
53using base::TimeDelta;
54using base::TimeTicks;
55
56/* The HistoryBackend consists of a number of components:
57
58    HistoryDatabase (stores past 3 months of history)
59      URLDatabase (stores a list of URLs)
60      DownloadDatabase (stores a list of downloads)
61      VisitDatabase (stores a list of visits for the URLs)
62      VisitSegmentDatabase (stores groups of URLs for the most visited view).
63
64    ArchivedDatabase (stores history older than 3 months)
65      URLDatabase (stores a list of URLs)
66      DownloadDatabase (stores a list of downloads)
67      VisitDatabase (stores a list of visits for the URLs)
68
69      (this does not store visit segments as they expire after 3 mos.)
70
71    TextDatabaseManager (manages multiple text database for different times)
72      TextDatabase (represents a single month of full-text index).
73      ...more TextDatabase objects...
74
75    ExpireHistoryBackend (manages moving things from HistoryDatabase to
76                          the ArchivedDatabase and deleting)
77*/
78
79namespace history {
80
81// How long we keep segment data for in days. Currently 3 months.
82// This value needs to be greater or equal to
83// MostVisitedModel::kMostVisitedScope but we don't want to introduce a direct
84// dependency between MostVisitedModel and the history backend.
85static const int kSegmentDataRetention = 90;
86
87// How long we'll wait to do a commit, so that things are batched together.
88static const int kCommitIntervalSeconds = 10;
89
90// The amount of time before we re-fetch the favicon.
91static const int kFaviconRefetchDays = 7;
92
93// GetSessionTabs returns all open tabs, or tabs closed kSessionCloseTimeWindow
94// seconds ago.
95static const int kSessionCloseTimeWindowSecs = 10;
96
97// The maximum number of items we'll allow in the redirect list before
98// deleting some.
99static const int kMaxRedirectCount = 32;
100
101// The number of days old a history entry can be before it is considered "old"
102// and is archived.
103static const int kArchiveDaysThreshold = 90;
104
105#if defined(OS_ANDROID)
106// The maximum number of top sites to track when recording top page visit stats.
107static const size_t kPageVisitStatsMaxTopSites = 50;
108#endif
109
110// Converts from PageUsageData to MostVisitedURL. |redirects| is a
111// list of redirects for this URL. Empty list means no redirects.
112MostVisitedURL MakeMostVisitedURL(const PageUsageData& page_data,
113                                  const RedirectList& redirects) {
114  MostVisitedURL mv;
115  mv.url = page_data.GetURL();
116  mv.title = page_data.GetTitle();
117  if (redirects.empty()) {
118    // Redirects must contain at least the target url.
119    mv.redirects.push_back(mv.url);
120  } else {
121    mv.redirects = redirects;
122    if (mv.redirects[mv.redirects.size() - 1] != mv.url) {
123      // The last url must be the target url.
124      mv.redirects.push_back(mv.url);
125    }
126  }
127  return mv;
128}
129
130// This task is run on a timer so that commits happen at regular intervals
131// so they are batched together. The important thing about this class is that
132// it supports canceling of the task so the reference to the backend will be
133// freed. The problem is that when history is shutting down, there is likely
134// to be one of these commits still pending and holding a reference.
135//
136// The backend can call Cancel to have this task release the reference. The
137// task will still run (if we ever get to processing the event before
138// shutdown), but it will not do anything.
139//
140// Note that this is a refcounted object and is not a task in itself. It should
141// be assigned to a RunnableMethod.
142//
143// TODO(brettw): bug 1165182: This should be replaced with a
144// base::WeakPtrFactory which will handle everything automatically (like we do
145// in ExpireHistoryBackend).
146class CommitLaterTask : public base::RefCounted<CommitLaterTask> {
147 public:
148  explicit CommitLaterTask(HistoryBackend* history_backend)
149      : history_backend_(history_backend) {
150  }
151
152  // The backend will call this function if it is being destroyed so that we
153  // release our reference.
154  void Cancel() {
155    history_backend_ = NULL;
156  }
157
158  void RunCommit() {
159    if (history_backend_.get())
160      history_backend_->Commit();
161  }
162
163 private:
164  friend class base::RefCounted<CommitLaterTask>;
165
166  ~CommitLaterTask() {}
167
168  scoped_refptr<HistoryBackend> history_backend_;
169};
170
171// Handles querying first the main database, then the full text database if that
172// fails. It will optionally keep track of all URLs seen so duplicates can be
173// eliminated. This is used by the querying sub-functions.
174//
175// TODO(brettw): This class may be able to be simplified or eliminated. After
176// this was written, QueryResults can efficiently look up by URL, so the need
177// for this extra set of previously queried URLs is less important.
178class HistoryBackend::URLQuerier {
179 public:
180  URLQuerier(URLDatabase* main_db, URLDatabase* archived_db, bool track_unique)
181      : main_db_(main_db),
182        archived_db_(archived_db),
183        track_unique_(track_unique) {
184  }
185
186  // When we're tracking unique URLs, returns true if this URL has been
187  // previously queried. Only call when tracking unique URLs.
188  bool HasURL(const GURL& url) {
189    DCHECK(track_unique_);
190    return unique_urls_.find(url) != unique_urls_.end();
191  }
192
193  bool GetRowForURL(const GURL& url, URLRow* row) {
194    if (!main_db_->GetRowForURL(url, row)) {
195      if (!archived_db_ || !archived_db_->GetRowForURL(url, row)) {
196        // This row is neither in the main nor the archived DB.
197        return false;
198      }
199    }
200
201    if (track_unique_)
202      unique_urls_.insert(url);
203    return true;
204  }
205
206 private:
207  URLDatabase* main_db_;  // Guaranteed non-NULL.
208  URLDatabase* archived_db_;  // Possibly NULL.
209
210  bool track_unique_;
211
212  // When track_unique_ is set, this is updated with every URL seen so far.
213  std::set<GURL> unique_urls_;
214
215  DISALLOW_COPY_AND_ASSIGN(URLQuerier);
216};
217
218// HistoryBackend --------------------------------------------------------------
219
220HistoryBackend::HistoryBackend(const base::FilePath& history_dir,
221                               int id,
222                               Delegate* delegate,
223                               BookmarkService* bookmark_service)
224    : delegate_(delegate),
225      id_(id),
226      history_dir_(history_dir),
227      scheduled_kill_db_(false),
228      expirer_(this, bookmark_service),
229      recent_redirects_(kMaxRedirectCount),
230      backend_destroy_message_loop_(NULL),
231      segment_queried_(false),
232      bookmark_service_(bookmark_service) {
233}
234
235HistoryBackend::~HistoryBackend() {
236  DCHECK(!scheduled_commit_.get()) << "Deleting without cleanup";
237  ReleaseDBTasks();
238
239#if defined(OS_ANDROID)
240  // Release AndroidProviderBackend before other objects.
241  android_provider_backend_.reset();
242#endif
243
244  // First close the databases before optionally running the "destroy" task.
245  CloseAllDatabases();
246
247  if (!backend_destroy_task_.is_null()) {
248    // Notify an interested party (typically a unit test) that we're done.
249    DCHECK(backend_destroy_message_loop_);
250    backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_);
251  }
252
253#if defined(OS_ANDROID)
254  sql::Connection::Delete(GetAndroidCacheFileName());
255#endif
256}
257
258void HistoryBackend::Init(const std::string& languages, bool force_fail) {
259  if (!force_fail)
260    InitImpl(languages);
261  delegate_->DBLoaded(id_);
262  typed_url_syncable_service_.reset(new TypedUrlSyncableService(this));
263  memory_pressure_listener_.reset(new base::MemoryPressureListener(
264      base::Bind(&HistoryBackend::OnMemoryPressure, base::Unretained(this))));
265#if defined(OS_ANDROID)
266  PopulateMostVisitedURLMap();
267#endif
268}
269
270void HistoryBackend::SetOnBackendDestroyTask(base::MessageLoop* message_loop,
271                                             const base::Closure& task) {
272  if (!backend_destroy_task_.is_null())
273    DLOG(WARNING) << "Setting more than one destroy task, overriding";
274  backend_destroy_message_loop_ = message_loop;
275  backend_destroy_task_ = task;
276}
277
278void HistoryBackend::Closing() {
279  // Any scheduled commit will have a reference to us, we must make it
280  // release that reference before we can be destroyed.
281  CancelScheduledCommit();
282
283  // Release our reference to the delegate, this reference will be keeping the
284  // history service alive.
285  delegate_.reset();
286}
287
288void HistoryBackend::NotifyRenderProcessHostDestruction(const void* host) {
289  tracker_.NotifyRenderProcessHostDestruction(host);
290}
291
292base::FilePath HistoryBackend::GetThumbnailFileName() const {
293  return history_dir_.Append(chrome::kThumbnailsFilename);
294}
295
296base::FilePath HistoryBackend::GetFaviconsFileName() const {
297  return history_dir_.Append(chrome::kFaviconsFilename);
298}
299
300base::FilePath HistoryBackend::GetArchivedFileName() const {
301  return history_dir_.Append(chrome::kArchivedHistoryFilename);
302}
303
304#if defined(OS_ANDROID)
305base::FilePath HistoryBackend::GetAndroidCacheFileName() const {
306  return history_dir_.Append(chrome::kAndroidCacheFilename);
307}
308#endif
309
310SegmentID HistoryBackend::GetLastSegmentID(VisitID from_visit) {
311  // Set is used to detect referrer loops.  Should not happen, but can
312  // if the database is corrupt.
313  std::set<VisitID> visit_set;
314  VisitID visit_id = from_visit;
315  while (visit_id) {
316    VisitRow row;
317    if (!db_->GetRowForVisit(visit_id, &row))
318      return 0;
319    if (row.segment_id)
320      return row.segment_id;  // Found a visit in this change with a segment.
321
322    // Check the referrer of this visit, if any.
323    visit_id = row.referring_visit;
324
325    if (visit_set.find(visit_id) != visit_set.end()) {
326      NOTREACHED() << "Loop in referer chain, giving up";
327      break;
328    }
329    visit_set.insert(visit_id);
330  }
331  return 0;
332}
333
334SegmentID HistoryBackend::UpdateSegments(
335    const GURL& url,
336    VisitID from_visit,
337    VisitID visit_id,
338    content::PageTransition transition_type,
339    const Time ts) {
340  if (!db_)
341    return 0;
342
343  // We only consider main frames.
344  if (!content::PageTransitionIsMainFrame(transition_type))
345    return 0;
346
347  SegmentID segment_id = 0;
348  content::PageTransition t =
349      content::PageTransitionStripQualifier(transition_type);
350
351  // Are we at the beginning of a new segment?
352  // Note that navigating to an existing entry (with back/forward) reuses the
353  // same transition type.  We are not adding it as a new segment in that case
354  // because if this was the target of a redirect, we might end up with
355  // 2 entries for the same final URL. Ex: User types google.net, gets
356  // redirected to google.com. A segment is created for google.net. On
357  // google.com users navigates through a link, then press back. That last
358  // navigation is for the entry google.com transition typed. We end up adding
359  // a segment for that one as well. So we end up with google.net and google.com
360  // in the segement table, showing as 2 entries in the NTP.
361  // Note also that we should still be updating the visit count for that segment
362  // which we are not doing now. It should be addressed when
363  // http://crbug.com/96860 is fixed.
364  if ((t == content::PAGE_TRANSITION_TYPED ||
365       t == content::PAGE_TRANSITION_AUTO_BOOKMARK) &&
366      (transition_type & content::PAGE_TRANSITION_FORWARD_BACK) == 0) {
367    // If so, create or get the segment.
368    std::string segment_name = db_->ComputeSegmentName(url);
369    URLID url_id = db_->GetRowForURL(url, NULL);
370    if (!url_id)
371      return 0;
372
373    if (!(segment_id = db_->GetSegmentNamed(segment_name))) {
374      if (!(segment_id = db_->CreateSegment(url_id, segment_name))) {
375        NOTREACHED();
376        return 0;
377      }
378    } else {
379      // Note: if we update an existing segment, we update the url used to
380      // represent that segment in order to minimize stale most visited
381      // images.
382      db_->UpdateSegmentRepresentationURL(segment_id, url_id);
383    }
384  } else {
385    // Note: it is possible there is no segment ID set for this visit chain.
386    // This can happen if the initial navigation wasn't AUTO_BOOKMARK or
387    // TYPED. (For example GENERATED). In this case this visit doesn't count
388    // toward any segment.
389    if (!(segment_id = GetLastSegmentID(from_visit)))
390      return 0;
391  }
392
393  // Set the segment in the visit.
394  if (!db_->SetSegmentID(visit_id, segment_id)) {
395    NOTREACHED();
396    return 0;
397  }
398
399  // Finally, increase the counter for that segment / day.
400  if (!db_->IncreaseSegmentVisitCount(segment_id, ts, 1)) {
401    NOTREACHED();
402    return 0;
403  }
404  return segment_id;
405}
406
407void HistoryBackend::UpdateWithPageEndTime(const void* host,
408                                           int32 page_id,
409                                           const GURL& url,
410                                           Time end_ts) {
411  // Will be filled with the URL ID and the visit ID of the last addition.
412  VisitID visit_id = tracker_.GetLastVisit(host, page_id, url);
413  UpdateVisitDuration(visit_id, end_ts);
414}
415
416void HistoryBackend::UpdateVisitDuration(VisitID visit_id, const Time end_ts) {
417  if (!db_)
418    return;
419
420  // Get the starting visit_time for visit_id.
421  VisitRow visit_row;
422  if (db_->GetRowForVisit(visit_id, &visit_row)) {
423    // We should never have a negative duration time even when time is skewed.
424    visit_row.visit_duration = end_ts > visit_row.visit_time ?
425        end_ts - visit_row.visit_time : TimeDelta::FromMicroseconds(0);
426    db_->UpdateVisitRow(visit_row);
427  }
428}
429
430void HistoryBackend::AddPage(const HistoryAddPageArgs& request) {
431  if (!db_)
432    return;
433
434  // Will be filled with the URL ID and the visit ID of the last addition.
435  std::pair<URLID, VisitID> last_ids(0, tracker_.GetLastVisit(
436      request.id_scope, request.page_id, request.referrer));
437
438  VisitID from_visit_id = last_ids.second;
439
440  // If a redirect chain is given, we expect the last item in that chain to be
441  // the final URL.
442  DCHECK(request.redirects.empty() ||
443         request.redirects.back() == request.url);
444
445  // If the user is adding older history, we need to make sure our times
446  // are correct.
447  if (request.time < first_recorded_time_)
448    first_recorded_time_ = request.time;
449
450  content::PageTransition request_transition = request.transition;
451  content::PageTransition stripped_transition =
452    content::PageTransitionStripQualifier(request_transition);
453  bool is_keyword_generated =
454      (stripped_transition == content::PAGE_TRANSITION_KEYWORD_GENERATED);
455
456  // If the user is navigating to a not-previously-typed intranet hostname,
457  // change the transition to TYPED so that the omnibox will learn that this is
458  // a known host.
459  bool has_redirects = request.redirects.size() > 1;
460  if (content::PageTransitionIsMainFrame(request_transition) &&
461      (stripped_transition != content::PAGE_TRANSITION_TYPED) &&
462      !is_keyword_generated) {
463    const GURL& origin_url(has_redirects ?
464        request.redirects[0] : request.url);
465    if (origin_url.SchemeIs(chrome::kHttpScheme) ||
466        origin_url.SchemeIs(chrome::kHttpsScheme) ||
467        origin_url.SchemeIs(chrome::kFtpScheme)) {
468      std::string host(origin_url.host());
469      size_t registry_length =
470          net::registry_controlled_domains::GetRegistryLength(
471              host,
472              net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
473              net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
474      if (registry_length == 0 && !db_->IsTypedHost(host)) {
475        stripped_transition = content::PAGE_TRANSITION_TYPED;
476        request_transition =
477            content::PageTransitionFromInt(
478                stripped_transition |
479                content::PageTransitionGetQualifier(request_transition));
480      }
481    }
482  }
483
484  if (!has_redirects) {
485    // The single entry is both a chain start and end.
486    content::PageTransition t = content::PageTransitionFromInt(
487        request_transition |
488        content::PAGE_TRANSITION_CHAIN_START |
489        content::PAGE_TRANSITION_CHAIN_END);
490
491    // No redirect case (one element means just the page itself).
492    last_ids = AddPageVisit(request.url, request.time,
493                            last_ids.second, t, request.visit_source);
494
495    // Update the segment for this visit. KEYWORD_GENERATED visits should not
496    // result in changing most visited, so we don't update segments (most
497    // visited db).
498    if (!is_keyword_generated) {
499      UpdateSegments(request.url, from_visit_id, last_ids.second, t,
500                     request.time);
501
502      // Update the referrer's duration.
503      UpdateVisitDuration(from_visit_id, request.time);
504    }
505  } else {
506    // Redirect case. Add the redirect chain.
507
508    content::PageTransition redirect_info =
509        content::PAGE_TRANSITION_CHAIN_START;
510
511    RedirectList redirects = request.redirects;
512    if (redirects[0].SchemeIs(chrome::kAboutScheme)) {
513      // When the redirect source + referrer is "about" we skip it. This
514      // happens when a page opens a new frame/window to about:blank and then
515      // script sets the URL to somewhere else (used to hide the referrer). It
516      // would be nice to keep all these redirects properly but we don't ever
517      // see the initial about:blank load, so we don't know where the
518      // subsequent client redirect came from.
519      //
520      // In this case, we just don't bother hooking up the source of the
521      // redirects, so we remove it.
522      redirects.erase(redirects.begin());
523    } else if (request_transition & content::PAGE_TRANSITION_CLIENT_REDIRECT) {
524      redirect_info = content::PAGE_TRANSITION_CLIENT_REDIRECT;
525      // The first entry in the redirect chain initiated a client redirect.
526      // We don't add this to the database since the referrer is already
527      // there, so we skip over it but change the transition type of the first
528      // transition to client redirect.
529      //
530      // The referrer is invalid when restoring a session that features an
531      // https tab that redirects to a different host or to http. In this
532      // case we don't need to reconnect the new redirect with the existing
533      // chain.
534      if (request.referrer.is_valid()) {
535        DCHECK(request.referrer == redirects[0]);
536        redirects.erase(redirects.begin());
537
538        // If the navigation entry for this visit has replaced that for the
539        // first visit, remove the CHAIN_END marker from the first visit. This
540        // can be called a lot, for example, the page cycler, and most of the
541        // time we won't have changed anything.
542        VisitRow visit_row;
543        if (request.did_replace_entry &&
544            db_->GetRowForVisit(last_ids.second, &visit_row) &&
545            visit_row.transition & content::PAGE_TRANSITION_CHAIN_END) {
546          visit_row.transition = content::PageTransitionFromInt(
547              visit_row.transition & ~content::PAGE_TRANSITION_CHAIN_END);
548          db_->UpdateVisitRow(visit_row);
549        }
550      }
551    }
552
553    for (size_t redirect_index = 0; redirect_index < redirects.size();
554         redirect_index++) {
555      content::PageTransition t =
556          content::PageTransitionFromInt(stripped_transition | redirect_info);
557
558      // If this is the last transition, add a CHAIN_END marker
559      if (redirect_index == (redirects.size() - 1)) {
560        t = content::PageTransitionFromInt(
561            t | content::PAGE_TRANSITION_CHAIN_END);
562      }
563
564      // Record all redirect visits with the same timestamp. We don't display
565      // them anyway, and if we ever decide to, we can reconstruct their order
566      // from the redirect chain.
567      last_ids = AddPageVisit(redirects[redirect_index],
568                              request.time, last_ids.second,
569                              t, request.visit_source);
570      if (t & content::PAGE_TRANSITION_CHAIN_START) {
571        // Update the segment for this visit.
572        UpdateSegments(redirects[redirect_index],
573                       from_visit_id, last_ids.second, t, request.time);
574
575        // Update the visit_details for this visit.
576        UpdateVisitDuration(from_visit_id, request.time);
577      }
578
579      // Subsequent transitions in the redirect list must all be server
580      // redirects.
581      redirect_info = content::PAGE_TRANSITION_SERVER_REDIRECT;
582    }
583
584    // Last, save this redirect chain for later so we can set titles & favicons
585    // on the redirected pages properly. It is indexed by the destination page.
586    recent_redirects_.Put(request.url, redirects);
587  }
588
589  // TODO(brettw) bug 1140015: Add an "add page" notification so the history
590  // views can keep in sync.
591
592  // Add the last visit to the tracker so we can get outgoing transitions.
593  // TODO(evanm): Due to http://b/1194536 we lose the referrers of a subframe
594  // navigation anyway, so last_visit_id is always zero for them.  But adding
595  // them here confuses main frame history, so we skip them for now.
596  if (stripped_transition != content::PAGE_TRANSITION_AUTO_SUBFRAME &&
597      stripped_transition != content::PAGE_TRANSITION_MANUAL_SUBFRAME &&
598      !is_keyword_generated) {
599    tracker_.AddVisit(request.id_scope, request.page_id, request.url,
600                      last_ids.second);
601  }
602
603  if (text_database_) {
604    text_database_->AddPageURL(request.url, last_ids.first, last_ids.second,
605                               request.time);
606  }
607
608  ScheduleCommit();
609}
610
611void HistoryBackend::InitImpl(const std::string& languages) {
612  DCHECK(!db_) << "Initializing HistoryBackend twice";
613  // In the rare case where the db fails to initialize a dialog may get shown
614  // the blocks the caller, yet allows other messages through. For this reason
615  // we only set db_ to the created database if creation is successful. That
616  // way other methods won't do anything as db_ is still NULL.
617
618  TimeTicks beginning_time = TimeTicks::Now();
619
620  // Compute the file names. Note that the index file can be removed when the
621  // text db manager is finished being hooked up.
622  base::FilePath history_name = history_dir_.Append(chrome::kHistoryFilename);
623  base::FilePath thumbnail_name = GetThumbnailFileName();
624  base::FilePath archived_name = GetArchivedFileName();
625
626  // History database.
627  db_.reset(new HistoryDatabase());
628
629  // Unretained to avoid a ref loop with db_.
630  db_->set_error_callback(
631      base::Bind(&HistoryBackend::DatabaseErrorCallback,
632                 base::Unretained(this)));
633
634  sql::InitStatus status = db_->Init(history_name);
635  switch (status) {
636    case sql::INIT_OK:
637      break;
638    case sql::INIT_FAILURE: {
639      // A NULL db_ will cause all calls on this object to notice this error
640      // and to not continue. If the error callback scheduled killing the
641      // database, the task it posted has not executed yet. Try killing the
642      // database now before we close it.
643      bool kill_db = scheduled_kill_db_;
644      if (kill_db)
645        KillHistoryDatabase();
646      UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError", kill_db);
647      delegate_->NotifyProfileError(id_, status);
648      db_.reset();
649      return;
650    }
651    default:
652      NOTREACHED();
653  }
654
655  // Fill the in-memory database and send it back to the history service on the
656  // main thread.
657  InMemoryHistoryBackend* mem_backend = new InMemoryHistoryBackend;
658  if (mem_backend->Init(history_name, db_.get()))
659    delegate_->SetInMemoryBackend(id_, mem_backend);  // Takes ownership of
660                                                      // pointer.
661  else
662    delete mem_backend;  // Error case, run without the in-memory DB.
663  db_->BeginExclusiveMode();  // Must be after the mem backend read the data.
664
665  // Create the history publisher which needs to be passed on to the text and
666  // thumbnail databases for publishing history.
667  history_publisher_.reset(new HistoryPublisher());
668  if (!history_publisher_->Init()) {
669    // The init may fail when there are no indexers wanting our history.
670    // Hence no need to log the failure.
671    history_publisher_.reset();
672  }
673
674  // Full-text database. This has to be first so we can pass it to the
675  // HistoryDatabase for migration.
676  text_database_.reset(new TextDatabaseManager(history_dir_,
677                                               db_.get(), db_.get()));
678  if (!text_database_->Init(history_publisher_.get())) {
679    LOG(WARNING) << "Text database initialization failed, running without it.";
680    text_database_.reset();
681  }
682  if (db_->needs_version_17_migration()) {
683    // See needs_version_17_migration() decl for more. In this case, we want
684    // to erase all the text database files. This must be done after the text
685    // database manager has been initialized, since it knows about all the
686    // files it manages.
687    text_database_->DeleteAll();
688  }
689
690  // Thumbnail database.
691  thumbnail_db_.reset(new ThumbnailDatabase());
692  if (!db_->GetNeedsThumbnailMigration()) {
693    // No convertion needed - use new filename right away.
694    thumbnail_name = GetFaviconsFileName();
695  }
696  if (thumbnail_db_->Init(thumbnail_name,
697                          history_publisher_.get(),
698                          db_.get()) != sql::INIT_OK) {
699    // Unlike the main database, we don't error out when the database is too
700    // new because this error is much less severe. Generally, this shouldn't
701    // happen since the thumbnail and main datbase versions should be in sync.
702    // We'll just continue without thumbnails & favicons in this case or any
703    // other error.
704    LOG(WARNING) << "Could not initialize the thumbnail database.";
705    thumbnail_db_.reset();
706  }
707
708  if (db_->GetNeedsThumbnailMigration()) {
709    VLOG(1) << "Starting TopSites migration";
710    delegate_->StartTopSitesMigration(id_);
711  }
712
713  // Archived database.
714  if (db_->needs_version_17_migration()) {
715    // See needs_version_17_migration() decl for more. In this case, we want
716    // to delete the archived database and need to do so before we try to
717    // open the file. We can ignore any error (maybe the file doesn't exist).
718    sql::Connection::Delete(archived_name);
719  }
720  archived_db_.reset(new ArchivedDatabase());
721  if (!archived_db_->Init(archived_name)) {
722    LOG(WARNING) << "Could not initialize the archived database.";
723    archived_db_.reset();
724  }
725
726  // Generate the history and thumbnail database metrics only after performing
727  // any migration work.
728  if (base::RandInt(1, 100) == 50) {
729    // Only do this computation sometimes since it can be expensive.
730    db_->ComputeDatabaseMetrics(history_name);
731    thumbnail_db_->ComputeDatabaseMetrics();
732  }
733
734  // Tell the expiration module about all the nice databases we made. This must
735  // happen before db_->Init() is called since the callback ForceArchiveHistory
736  // may need to expire stuff.
737  //
738  // *sigh*, this can all be cleaned up when that migration code is removed.
739  // The main DB initialization should intuitively be first (not that it
740  // actually matters) and the expirer should be set last.
741  expirer_.SetDatabases(db_.get(), archived_db_.get(),
742                        thumbnail_db_.get(), text_database_.get());
743
744  // Open the long-running transaction.
745  db_->BeginTransaction();
746  if (thumbnail_db_)
747    thumbnail_db_->BeginTransaction();
748  if (archived_db_)
749    archived_db_->BeginTransaction();
750  if (text_database_)
751    text_database_->BeginTransaction();
752
753  // Get the first item in our database.
754  db_->GetStartDate(&first_recorded_time_);
755
756  // Start expiring old stuff.
757  expirer_.StartArchivingOldStuff(TimeDelta::FromDays(kArchiveDaysThreshold));
758
759#if defined(OS_ANDROID)
760  if (thumbnail_db_) {
761    android_provider_backend_.reset(new AndroidProviderBackend(
762        GetAndroidCacheFileName(), db_.get(), thumbnail_db_.get(),
763        bookmark_service_, delegate_.get()));
764  }
765#endif
766
767  HISTOGRAM_TIMES("History.InitTime",
768                  TimeTicks::Now() - beginning_time);
769}
770
771void HistoryBackend::OnMemoryPressure(
772    base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
773  bool trim_aggressively = memory_pressure_level ==
774      base::MemoryPressureListener::MEMORY_PRESSURE_CRITICAL;
775  if (db_)
776    db_->TrimMemory(trim_aggressively);
777  if (thumbnail_db_)
778    thumbnail_db_->TrimMemory(trim_aggressively);
779  if (archived_db_)
780    archived_db_->TrimMemory(trim_aggressively);
781}
782
783void HistoryBackend::CloseAllDatabases() {
784  if (db_) {
785    // Commit the long-running transaction.
786    db_->CommitTransaction();
787    db_.reset();
788  }
789  if (thumbnail_db_) {
790    thumbnail_db_->CommitTransaction();
791    thumbnail_db_.reset();
792  }
793  if (archived_db_) {
794    archived_db_->CommitTransaction();
795    archived_db_.reset();
796  }
797  if (text_database_) {
798    text_database_->CommitTransaction();
799    text_database_.reset();
800  }
801}
802
803std::pair<URLID, VisitID> HistoryBackend::AddPageVisit(
804    const GURL& url,
805    Time time,
806    VisitID referring_visit,
807    content::PageTransition transition,
808    VisitSource visit_source) {
809  // Top-level frame navigations are visible, everything else is hidden
810  bool new_hidden = !content::PageTransitionIsMainFrame(transition);
811
812  // NOTE: This code must stay in sync with
813  // ExpireHistoryBackend::ExpireURLsForVisits().
814  // TODO(pkasting): http://b/1148304 We shouldn't be marking so many URLs as
815  // typed, which would eliminate the need for this code.
816  int typed_increment = 0;
817  content::PageTransition transition_type =
818      content::PageTransitionStripQualifier(transition);
819  if ((transition_type == content::PAGE_TRANSITION_TYPED &&
820      !content::PageTransitionIsRedirect(transition)) ||
821      transition_type == content::PAGE_TRANSITION_KEYWORD_GENERATED)
822    typed_increment = 1;
823
824#if defined(OS_ANDROID)
825  // Only count the page visit if it came from user browsing and only count it
826  // once when cycling through a redirect chain.
827  if (visit_source == SOURCE_BROWSED &&
828      (transition & content::PAGE_TRANSITION_CHAIN_END) != 0) {
829    RecordTopPageVisitStats(url);
830  }
831#endif
832
833  // See if this URL is already in the DB.
834  URLRow url_info(url);
835  URLID url_id = db_->GetRowForURL(url, &url_info);
836  if (url_id) {
837    // Update of an existing row.
838    if (content::PageTransitionStripQualifier(transition) !=
839        content::PAGE_TRANSITION_RELOAD)
840      url_info.set_visit_count(url_info.visit_count() + 1);
841    if (typed_increment)
842      url_info.set_typed_count(url_info.typed_count() + typed_increment);
843    if (url_info.last_visit() < time)
844      url_info.set_last_visit(time);
845
846    // Only allow un-hiding of pages, never hiding.
847    if (!new_hidden)
848      url_info.set_hidden(false);
849
850    db_->UpdateURLRow(url_id, url_info);
851  } else {
852    // Addition of a new row.
853    url_info.set_visit_count(1);
854    url_info.set_typed_count(typed_increment);
855    url_info.set_last_visit(time);
856    url_info.set_hidden(new_hidden);
857
858    url_id = db_->AddURL(url_info);
859    if (!url_id) {
860      NOTREACHED() << "Adding URL failed.";
861      return std::make_pair(0, 0);
862    }
863    url_info.id_ = url_id;
864
865    // We don't actually add the URL to the full text index at this point. It
866    // might be nice to do this so that even if we get no title or body, the
867    // user can search for URL components and get the page.
868    //
869    // However, in most cases, we'll get at least a title and usually contents,
870    // and this add will be redundant, slowing everything down. As a result,
871    // we ignore this edge case.
872  }
873
874  // Add the visit with the time to the database.
875  VisitRow visit_info(url_id, time, referring_visit, transition, 0);
876  VisitID visit_id = db_->AddVisit(&visit_info, visit_source);
877  NotifyVisitObservers(visit_info);
878
879  if (visit_info.visit_time < first_recorded_time_)
880    first_recorded_time_ = visit_info.visit_time;
881
882  // Broadcast a notification of the visit.
883  if (visit_id) {
884    if (typed_url_syncable_service_.get())
885      typed_url_syncable_service_->OnUrlVisited(transition, &url_info);
886
887    URLVisitedDetails* details = new URLVisitedDetails;
888    details->transition = transition;
889    details->row = url_info;
890    // TODO(meelapshah) Disabled due to potential PageCycler regression.
891    // Re-enable this.
892    // GetMostRecentRedirectsTo(url, &details->redirects);
893    BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URL_VISITED, details);
894  } else {
895    VLOG(0) << "Failed to build visit insert statement:  "
896            << "url_id = " << url_id;
897  }
898
899  return std::make_pair(url_id, visit_id);
900}
901
902void HistoryBackend::AddPagesWithDetails(const URLRows& urls,
903                                         VisitSource visit_source) {
904  if (!db_)
905    return;
906
907  scoped_ptr<URLsModifiedDetails> modified(new URLsModifiedDetails);
908  for (URLRows::const_iterator i = urls.begin(); i != urls.end(); ++i) {
909    DCHECK(!i->last_visit().is_null());
910
911    // We will add to either the archived database or the main one depending on
912    // the date of the added visit.
913    URLDatabase* url_database;
914    VisitDatabase* visit_database;
915    if (IsExpiredVisitTime(i->last_visit())) {
916      if (!archived_db_)
917        return;  // No archived database to save it to, just forget this.
918      url_database = archived_db_.get();
919      visit_database = archived_db_.get();
920    } else {
921      url_database = db_.get();
922      visit_database = db_.get();
923    }
924
925    URLRow existing_url;
926    URLID url_id = url_database->GetRowForURL(i->url(), &existing_url);
927    if (!url_id) {
928      // Add the page if it doesn't exist.
929      url_id = url_database->AddURL(*i);
930      if (!url_id) {
931        NOTREACHED() << "Could not add row to DB";
932        return;
933      }
934
935      if (i->typed_count() > 0) {
936        modified->changed_urls.push_back(*i);
937        modified->changed_urls.back().set_id(url_id);  // *i likely has |id_| 0.
938      }
939    }
940
941    // Add the page to the full text index. This function is also used for
942    // importing. Even though we don't have page contents, we can at least
943    // add the title and URL to the index so they can be searched. We don't
944    // bother to delete any already-existing FTS entries for the URL, since
945    // this is normally called on import.
946    //
947    // If you ever import *after* first run (selecting import from the menu),
948    // then these additional entries will "shadow" the originals when querying
949    // for the most recent match only, and the user won't get snippets. This is
950    // a very minor issue, and fixing it will make import slower, so we don't
951    // bother.
952    bool has_indexed = false;
953    if (text_database_) {
954      // We do not have to make it update the visit database, below, we will
955      // create the visit entry with the indexed flag set.
956      has_indexed = text_database_->AddPageData(i->url(), url_id, 0,
957                                                i->last_visit(),
958                                                i->title(), string16());
959    }
960
961    // Sync code manages the visits itself.
962    if (visit_source != SOURCE_SYNCED) {
963      // Make up a visit to correspond to the last visit to the page.
964      VisitRow visit_info(url_id, i->last_visit(), 0,
965                          content::PageTransitionFromInt(
966                              content::PAGE_TRANSITION_LINK |
967                              content::PAGE_TRANSITION_CHAIN_START |
968                              content::PAGE_TRANSITION_CHAIN_END), 0);
969      visit_info.is_indexed = has_indexed;
970      if (!visit_database->AddVisit(&visit_info, visit_source)) {
971        NOTREACHED() << "Adding visit failed.";
972        return;
973      }
974      NotifyVisitObservers(visit_info);
975
976      if (visit_info.visit_time < first_recorded_time_)
977        first_recorded_time_ = visit_info.visit_time;
978    }
979  }
980
981  if (typed_url_syncable_service_.get())
982    typed_url_syncable_service_->OnUrlsModified(&modified->changed_urls);
983
984  // Broadcast a notification for typed URLs that have been modified. This
985  // will be picked up by the in-memory URL database on the main thread.
986  //
987  // TODO(brettw) bug 1140015: Add an "add page" notification so the history
988  // views can keep in sync.
989  BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
990                         modified.release());
991
992  ScheduleCommit();
993}
994
995bool HistoryBackend::IsExpiredVisitTime(const base::Time& time) {
996  return time < expirer_.GetCurrentArchiveTime();
997}
998
999void HistoryBackend::SetPageTitle(const GURL& url,
1000                                  const string16& title) {
1001  if (!db_)
1002    return;
1003
1004  // Update the full text index.
1005  if (text_database_)
1006    text_database_->AddPageTitle(url, title);
1007
1008  // Search for recent redirects which should get the same title. We make a
1009  // dummy list containing the exact URL visited if there are no redirects so
1010  // the processing below can be the same.
1011  history::RedirectList dummy_list;
1012  history::RedirectList* redirects;
1013  RedirectCache::iterator iter = recent_redirects_.Get(url);
1014  if (iter != recent_redirects_.end()) {
1015    redirects = &iter->second;
1016
1017    // This redirect chain should have the destination URL as the last item.
1018    DCHECK(!redirects->empty());
1019    DCHECK(redirects->back() == url);
1020  } else {
1021    // No redirect chain stored, make up one containing the URL we want so we
1022    // can use the same logic below.
1023    dummy_list.push_back(url);
1024    redirects = &dummy_list;
1025  }
1026
1027  scoped_ptr<URLsModifiedDetails> details(new URLsModifiedDetails);
1028  for (size_t i = 0; i < redirects->size(); i++) {
1029    URLRow row;
1030    URLID row_id = db_->GetRowForURL(redirects->at(i), &row);
1031    if (row_id && row.title() != title) {
1032      row.set_title(title);
1033      db_->UpdateURLRow(row_id, row);
1034      details->changed_urls.push_back(row);
1035    }
1036  }
1037
1038  // Broadcast notifications for any URLs that have changed. This will
1039  // update the in-memory database and the InMemoryURLIndex.
1040  if (!details->changed_urls.empty()) {
1041    if (typed_url_syncable_service_.get())
1042      typed_url_syncable_service_->OnUrlsModified(&details->changed_urls);
1043    BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_MODIFIED,
1044                           details.release());
1045    ScheduleCommit();
1046  }
1047}
1048
1049void HistoryBackend::AddPageNoVisitForBookmark(const GURL& url,
1050                                               const string16& title) {
1051  if (!db_)
1052    return;
1053
1054  URLRow url_info(url);
1055  URLID url_id = db_->GetRowForURL(url, &url_info);
1056  if (url_id) {
1057    // URL is already known, nothing to do.
1058    return;
1059  }
1060
1061  if (!title.empty()) {
1062    url_info.set_title(title);
1063  } else {
1064    url_info.set_title(UTF8ToUTF16(url.spec()));
1065  }
1066
1067  url_info.set_last_visit(Time::Now());
1068  // Mark the page hidden. If the user types it in, it'll unhide.
1069  url_info.set_hidden(true);
1070
1071  db_->AddURL(url_info);
1072}
1073
1074void HistoryBackend::IterateURLs(
1075    const scoped_refptr<visitedlink::VisitedLinkDelegate::URLEnumerator>&
1076    iterator) {
1077  if (db_) {
1078    HistoryDatabase::URLEnumerator e;
1079    if (db_->InitURLEnumeratorForEverything(&e)) {
1080      URLRow info;
1081      while (e.GetNextURL(&info)) {
1082        iterator->OnURL(info.url());
1083      }
1084      iterator->OnComplete(true);  // Success.
1085      return;
1086    }
1087  }
1088  iterator->OnComplete(false);  // Failure.
1089}
1090
1091bool HistoryBackend::GetAllTypedURLs(URLRows* urls) {
1092  if (db_)
1093    return db_->GetAllTypedUrls(urls);
1094  return false;
1095}
1096
1097bool HistoryBackend::GetVisitsForURL(URLID id, VisitVector* visits) {
1098  if (db_)
1099    return db_->GetVisitsForURL(id, visits);
1100  return false;
1101}
1102
1103bool HistoryBackend::GetMostRecentVisitsForURL(URLID id,
1104                                               int max_visits,
1105                                               VisitVector* visits) {
1106  if (db_)
1107    return db_->GetMostRecentVisitsForURL(id, max_visits, visits);
1108  return false;
1109}
1110
1111bool HistoryBackend::UpdateURL(URLID id, const history::URLRow& url) {
1112  if (db_)
1113    return db_->UpdateURLRow(id, url);
1114  return false;
1115}
1116
1117bool HistoryBackend::AddVisits(const GURL& url,
1118                               const std::vector<VisitInfo>& visits,
1119                               VisitSource visit_source) {
1120  if (db_) {
1121    for (std::vector<VisitInfo>::const_iterator visit = visits.begin();
1122         visit != visits.end(); ++visit) {
1123      if (!AddPageVisit(
1124              url, visit->first, 0, visit->second, visit_source).first) {
1125        return false;
1126      }
1127    }
1128    ScheduleCommit();
1129    return true;
1130  }
1131  return false;
1132}
1133
1134bool HistoryBackend::RemoveVisits(const VisitVector& visits) {
1135  if (!db_)
1136    return false;
1137
1138  expirer_.ExpireVisits(visits);
1139  ScheduleCommit();
1140  return true;
1141}
1142
1143bool HistoryBackend::GetVisitsSource(const VisitVector& visits,
1144                                     VisitSourceMap* sources) {
1145  if (!db_)
1146    return false;
1147
1148  db_->GetVisitsSource(visits, sources);
1149  return true;
1150}
1151
1152bool HistoryBackend::GetURL(const GURL& url, history::URLRow* url_row) {
1153  if (db_)
1154    return db_->GetRowForURL(url, url_row) != 0;
1155  return false;
1156}
1157
1158void HistoryBackend::QueryURL(scoped_refptr<QueryURLRequest> request,
1159                              const GURL& url,
1160                              bool want_visits) {
1161  if (request->canceled())
1162    return;
1163
1164  bool success = false;
1165  URLRow* row = &request->value.a;
1166  VisitVector* visits = &request->value.b;
1167  if (db_) {
1168    if (db_->GetRowForURL(url, row)) {
1169      // Have a row.
1170      success = true;
1171
1172      // Optionally query the visits.
1173      if (want_visits)
1174        db_->GetVisitsForURL(row->id(), visits);
1175    }
1176  }
1177  request->ForwardResult(request->handle(), success, row, visits);
1178}
1179
1180TypedUrlSyncableService* HistoryBackend::GetTypedUrlSyncableService() const {
1181  return typed_url_syncable_service_.get();
1182}
1183
1184// Segment usage ---------------------------------------------------------------
1185
1186void HistoryBackend::DeleteOldSegmentData() {
1187  if (db_)
1188    db_->DeleteSegmentData(Time::Now() -
1189                           TimeDelta::FromDays(kSegmentDataRetention));
1190}
1191
1192void HistoryBackend::QuerySegmentUsage(
1193    scoped_refptr<QuerySegmentUsageRequest> request,
1194    const Time from_time,
1195    int max_result_count) {
1196  if (request->canceled())
1197    return;
1198
1199  if (db_) {
1200    db_->QuerySegmentUsage(from_time, max_result_count, &request->value.get());
1201
1202    // If this is the first time we query segments, invoke
1203    // DeleteOldSegmentData asynchronously. We do this to cleanup old
1204    // entries.
1205    if (!segment_queried_) {
1206      segment_queried_ = true;
1207      base::MessageLoop::current()->PostTask(
1208          FROM_HERE,
1209          base::Bind(&HistoryBackend::DeleteOldSegmentData, this));
1210    }
1211  }
1212  request->ForwardResult(request->handle(), &request->value.get());
1213}
1214
1215void HistoryBackend::IncreaseSegmentDuration(const GURL& url,
1216                                             base::Time time,
1217                                             base::TimeDelta delta) {
1218  if (!db_)
1219    return;
1220
1221  const std::string segment_name(VisitSegmentDatabase::ComputeSegmentName(url));
1222  SegmentID segment_id = db_->GetSegmentNamed(segment_name);
1223  if (!segment_id) {
1224    URLID url_id = db_->GetRowForURL(url, NULL);
1225    if (!url_id)
1226      return;
1227    segment_id = db_->CreateSegment(url_id, segment_name);
1228    if (!segment_id)
1229      return;
1230  }
1231  SegmentDurationID duration_id;
1232  base::TimeDelta total_delta;
1233  if (!db_->GetSegmentDuration(segment_id, time, &duration_id,
1234                               &total_delta)) {
1235    db_->CreateSegmentDuration(segment_id, time, delta);
1236    return;
1237  }
1238  total_delta += delta;
1239  db_->SetSegmentDuration(duration_id, total_delta);
1240}
1241
1242void HistoryBackend::QuerySegmentDuration(
1243    scoped_refptr<QuerySegmentUsageRequest> request,
1244    const base::Time from_time,
1245    int max_result_count) {
1246  if (request->canceled())
1247    return;
1248
1249  if (db_) {
1250    db_->QuerySegmentDuration(from_time, max_result_count,
1251                              &request->value.get());
1252  }
1253  request->ForwardResult(request->handle(), &request->value.get());
1254}
1255
1256// Keyword visits --------------------------------------------------------------
1257
1258void HistoryBackend::SetKeywordSearchTermsForURL(const GURL& url,
1259                                                 TemplateURLID keyword_id,
1260                                                 const string16& term) {
1261  if (!db_)
1262    return;
1263
1264  // Get the ID for this URL.
1265  URLRow url_row;
1266  if (!db_->GetRowForURL(url, &url_row)) {
1267    // There is a small possibility the url was deleted before the keyword
1268    // was added. Ignore the request.
1269    return;
1270  }
1271
1272  db_->SetKeywordSearchTermsForURL(url_row.id(), keyword_id, term);
1273
1274  // details is deleted by BroadcastNotifications.
1275  KeywordSearchTermDetails* details = new KeywordSearchTermDetails;
1276  details->url = url;
1277  details->keyword_id = keyword_id;
1278  details->term = term;
1279  BroadcastNotifications(
1280      chrome::NOTIFICATION_HISTORY_KEYWORD_SEARCH_TERM_UPDATED, details);
1281  ScheduleCommit();
1282}
1283
1284void HistoryBackend::DeleteAllSearchTermsForKeyword(
1285    TemplateURLID keyword_id) {
1286  if (!db_)
1287    return;
1288
1289  db_->DeleteAllSearchTermsForKeyword(keyword_id);
1290  // TODO(sky): bug 1168470. Need to move from archive dbs too.
1291  ScheduleCommit();
1292}
1293
1294void HistoryBackend::GetMostRecentKeywordSearchTerms(
1295    scoped_refptr<GetMostRecentKeywordSearchTermsRequest> request,
1296    TemplateURLID keyword_id,
1297    const string16& prefix,
1298    int max_count) {
1299  if (request->canceled())
1300    return;
1301
1302  if (db_) {
1303    db_->GetMostRecentKeywordSearchTerms(keyword_id, prefix, max_count,
1304                                         &(request->value));
1305  }
1306  request->ForwardResult(request->handle(), &request->value);
1307}
1308
1309// Downloads -------------------------------------------------------------------
1310
1311void HistoryBackend::GetNextDownloadId(uint32* next_id) {
1312  if (db_)
1313    db_->GetNextDownloadId(next_id);
1314}
1315
1316// Get all the download entries from the database.
1317void HistoryBackend::QueryDownloads(std::vector<DownloadRow>* rows) {
1318  if (db_)
1319    db_->QueryDownloads(rows);
1320}
1321
1322// Update a particular download entry.
1323void HistoryBackend::UpdateDownload(const history::DownloadRow& data) {
1324  if (!db_)
1325    return;
1326  db_->UpdateDownload(data);
1327  ScheduleCommit();
1328}
1329
1330void HistoryBackend::CreateDownload(const history::DownloadRow& history_info,
1331                                    bool* success) {
1332  if (!db_)
1333    return;
1334  *success = db_->CreateDownload(history_info);
1335  ScheduleCommit();
1336}
1337
1338void HistoryBackend::RemoveDownloads(const std::set<uint32>& ids) {
1339  if (!db_)
1340    return;
1341  size_t downloads_count_before = db_->CountDownloads();
1342  base::TimeTicks started_removing = base::TimeTicks::Now();
1343  // HistoryBackend uses a long-running Transaction that is committed
1344  // periodically, so this loop doesn't actually hit the disk too hard.
1345  for (std::set<uint32>::const_iterator it = ids.begin();
1346       it != ids.end(); ++it) {
1347    db_->RemoveDownload(*it);
1348  }
1349  ScheduleCommit();
1350  base::TimeTicks finished_removing = base::TimeTicks::Now();
1351  size_t downloads_count_after = db_->CountDownloads();
1352
1353  DCHECK_LE(downloads_count_after, downloads_count_before);
1354  if (downloads_count_after > downloads_count_before)
1355    return;
1356  size_t num_downloads_deleted = downloads_count_before - downloads_count_after;
1357  UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCount",
1358                        num_downloads_deleted);
1359  base::TimeDelta micros = (1000 * (finished_removing - started_removing));
1360  UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTime", micros);
1361  if (num_downloads_deleted > 0) {
1362    UMA_HISTOGRAM_TIMES("Download.DatabaseRemoveDownloadsTimePerRecord",
1363                        (1000 * micros) / num_downloads_deleted);
1364  }
1365  DCHECK_GE(ids.size(), num_downloads_deleted);
1366  if (ids.size() < num_downloads_deleted)
1367    return;
1368  UMA_HISTOGRAM_COUNTS("Download.DatabaseRemoveDownloadsCountNotRemoved",
1369                        ids.size() - num_downloads_deleted);
1370}
1371
1372void HistoryBackend::QueryHistory(scoped_refptr<QueryHistoryRequest> request,
1373                                  const string16& text_query,
1374                                  const QueryOptions& options) {
1375  if (request->canceled())
1376    return;
1377
1378  TimeTicks beginning_time = TimeTicks::Now();
1379
1380  if (db_) {
1381    if (text_query.empty()) {
1382      // Basic history query for the main database.
1383      QueryHistoryBasic(db_.get(), db_.get(), options, &request->value);
1384
1385      // Now query the archived database. This is a bit tricky because we don't
1386      // want to query it if the queried time range isn't going to find anything
1387      // in it.
1388      // TODO(brettw) bug 1171036: do blimpie querying for the archived database
1389      // as well.
1390      // if (archived_db_.get() &&
1391      //     expirer_.GetCurrentArchiveTime() - TimeDelta::FromDays(7)) {
1392    } else {
1393      // Text history query.
1394      QueryHistoryText(db_.get(), db_.get(), text_query, options,
1395                       &request->value);
1396      if (archived_db_.get() &&
1397          expirer_.GetCurrentArchiveTime() >= options.begin_time) {
1398        QueryHistoryText(archived_db_.get(), archived_db_.get(), text_query,
1399                         options, &request->value);
1400      }
1401    }
1402  }
1403
1404  request->ForwardResult(request->handle(), &request->value);
1405
1406  UMA_HISTOGRAM_TIMES("History.QueryHistory",
1407                      TimeTicks::Now() - beginning_time);
1408}
1409
1410// Basic time-based querying of history.
1411void HistoryBackend::QueryHistoryBasic(URLDatabase* url_db,
1412                                       VisitDatabase* visit_db,
1413                                       const QueryOptions& options,
1414                                       QueryResults* result) {
1415  // First get all visits.
1416  VisitVector visits;
1417  bool has_more_results = visit_db->GetVisibleVisitsInRange(options, &visits);
1418  DCHECK(static_cast<int>(visits.size()) <= options.EffectiveMaxCount());
1419
1420  // Now add them and the URL rows to the results.
1421  URLResult url_result;
1422  for (size_t i = 0; i < visits.size(); i++) {
1423    const VisitRow visit = visits[i];
1424
1425    // Add a result row for this visit, get the URL info from the DB.
1426    if (!url_db->GetURLRow(visit.url_id, &url_result)) {
1427      VLOG(0) << "Failed to get id " << visit.url_id
1428              << " from history.urls.";
1429      continue;  // DB out of sync and URL doesn't exist, try to recover.
1430    }
1431
1432    if (!url_result.url().is_valid()) {
1433      VLOG(0) << "Got invalid URL from history.urls with id "
1434              << visit.url_id << ":  "
1435              << url_result.url().possibly_invalid_spec();
1436      continue;  // Don't report invalid URLs in case of corruption.
1437    }
1438
1439    // The archived database may be out of sync with respect to starring,
1440    // titles, last visit date, etc. Therefore, we query the main DB if the
1441    // current URL database is not the main one.
1442    if (url_db == db_.get()) {
1443      // Currently querying the archived DB, update with the main database to
1444      // catch any interesting stuff. This will update it if it exists in the
1445      // main DB, and do nothing otherwise.
1446      db_->GetRowForURL(url_result.url(), &url_result);
1447    }
1448
1449    url_result.set_visit_time(visit.visit_time);
1450
1451    // Set whether the visit was blocked for a managed user by looking at the
1452    // transition type.
1453    url_result.set_blocked_visit(
1454        (visit.transition & content::PAGE_TRANSITION_BLOCKED) != 0);
1455
1456    // We don't set any of the query-specific parts of the URLResult, since
1457    // snippets and stuff don't apply to basic querying.
1458    result->AppendURLBySwapping(&url_result);
1459  }
1460
1461  if (!has_more_results && options.begin_time <= first_recorded_time_)
1462    result->set_reached_beginning(true);
1463}
1464
1465// Text-based querying of history.
1466void HistoryBackend::QueryHistoryText(URLDatabase* url_db,
1467                                      VisitDatabase* visit_db,
1468                                      const string16& text_query,
1469                                      const QueryOptions& options,
1470                                      QueryResults* result) {
1471  URLRows text_matches;
1472  url_db->GetTextMatches(text_query, &text_matches);
1473
1474  std::vector<URLResult> matching_visits;
1475  VisitVector visits;    // Declare outside loop to prevent re-construction.
1476  for (size_t i = 0; i < text_matches.size(); i++) {
1477    const URLRow& text_match = text_matches[i];
1478    // Get all visits for given URL match.
1479    visit_db->GetVisitsForURLWithOptions(text_match.id(), options, &visits);
1480    for (size_t j = 0; j < visits.size(); j++) {
1481      URLResult url_result(text_match);
1482      url_result.set_visit_time(visits[j].visit_time);
1483      matching_visits.push_back(url_result);
1484    }
1485  }
1486
1487  std::sort(matching_visits.begin(), matching_visits.end(),
1488            URLResult::CompareVisitTime);
1489
1490  size_t max_results = options.max_count == 0 ?
1491      std::numeric_limits<size_t>::max() : static_cast<int>(options.max_count);
1492  for (std::vector<URLResult>::iterator it = matching_visits.begin();
1493       it != matching_visits.end() && result->size() < max_results; ++it) {
1494    result->AppendURLBySwapping(&(*it));
1495  }
1496
1497  if (matching_visits.size() == result->size() &&
1498      options.begin_time <= first_recorded_time_)
1499    result->set_reached_beginning(true);
1500}
1501
1502void HistoryBackend::QueryHistoryFTS(const string16& text_query,
1503                                     const QueryOptions& options,
1504                                     QueryResults* result) {
1505  if (!text_database_)
1506    return;
1507
1508  // Full text query, first get all the FTS results in the time range.
1509  std::vector<TextDatabase::Match> fts_matches;
1510  Time first_time_searched;
1511  text_database_->GetTextMatches(text_query, options,
1512                                 &fts_matches, &first_time_searched);
1513
1514  URLQuerier querier(db_.get(), archived_db_.get(), true);
1515
1516  // Now get the row and visit information for each one.
1517  URLResult url_result;  // Declare outside loop to prevent re-construction.
1518  for (size_t i = 0; i < fts_matches.size(); i++) {
1519    if (options.max_count != 0 &&
1520        static_cast<int>(result->size()) >= options.max_count)
1521      break;  // Got too many items.
1522
1523    // Get the URL, querying the main and archived databases as necessary. If
1524    // this is not found, the history and full text search databases are out
1525    // of sync and we give up with this result.
1526    if (!querier.GetRowForURL(fts_matches[i].url, &url_result))
1527      continue;
1528
1529    if (!url_result.url().is_valid())
1530      continue;  // Don't report invalid URLs in case of corruption.
1531
1532    // Copy over the FTS stuff that the URLDatabase doesn't know about.
1533    // We do this with swap() to avoid copying, since we know we don't
1534    // need the original any more. Note that we override the title with the
1535    // one from FTS, since that will match the title_match_positions (the
1536    // FTS title and the history DB title may differ).
1537    url_result.set_title(fts_matches[i].title);
1538    url_result.title_match_positions_.swap(
1539        fts_matches[i].title_match_positions);
1540    url_result.snippet_.Swap(&fts_matches[i].snippet);
1541
1542    // The visit time also comes from the full text search database. Since it
1543    // has the time, we can avoid an extra query of the visits table.
1544    url_result.set_visit_time(fts_matches[i].time);
1545
1546    // Add it to the vector, this will clear our |url_row| object as a
1547    // result of the swap.
1548    result->AppendURLBySwapping(&url_result);
1549  }
1550
1551  if (first_time_searched <= first_recorded_time_)
1552    result->set_reached_beginning(true);
1553}
1554
1555// Frontend to GetMostRecentRedirectsFrom from the history thread.
1556void HistoryBackend::QueryRedirectsFrom(
1557    scoped_refptr<QueryRedirectsRequest> request,
1558    const GURL& url) {
1559  if (request->canceled())
1560    return;
1561  bool success = GetMostRecentRedirectsFrom(url, &request->value);
1562  request->ForwardResult(request->handle(), url, success, &request->value);
1563}
1564
1565void HistoryBackend::QueryRedirectsTo(
1566    scoped_refptr<QueryRedirectsRequest> request,
1567    const GURL& url) {
1568  if (request->canceled())
1569    return;
1570  bool success = GetMostRecentRedirectsTo(url, &request->value);
1571  request->ForwardResult(request->handle(), url, success, &request->value);
1572}
1573
1574void HistoryBackend::GetVisibleVisitCountToHost(
1575    scoped_refptr<GetVisibleVisitCountToHostRequest> request,
1576    const GURL& url) {
1577  if (request->canceled())
1578    return;
1579  int count = 0;
1580  Time first_visit;
1581  const bool success = db_.get() &&
1582      db_->GetVisibleVisitCountToHost(url, &count, &first_visit);
1583  request->ForwardResult(request->handle(), success, count, first_visit);
1584}
1585
1586void HistoryBackend::QueryTopURLsAndRedirects(
1587    scoped_refptr<QueryTopURLsAndRedirectsRequest> request,
1588    int result_count) {
1589  if (request->canceled())
1590    return;
1591
1592  if (!db_) {
1593    request->ForwardResult(request->handle(), false, NULL, NULL);
1594    return;
1595  }
1596
1597  std::vector<GURL>* top_urls = &request->value.a;
1598  history::RedirectMap* redirects = &request->value.b;
1599
1600  ScopedVector<PageUsageData> data;
1601  db_->QuerySegmentUsage(base::Time::Now() - base::TimeDelta::FromDays(90),
1602      result_count, &data.get());
1603
1604  for (size_t i = 0; i < data.size(); ++i) {
1605    top_urls->push_back(data[i]->GetURL());
1606    RefCountedVector<GURL>* list = new RefCountedVector<GURL>;
1607    GetMostRecentRedirectsFrom(top_urls->back(), &list->data);
1608    (*redirects)[top_urls->back()] = list;
1609  }
1610
1611  request->ForwardResult(request->handle(), true, top_urls, redirects);
1612}
1613
1614// Will replace QueryTopURLsAndRedirectsRequest.
1615void HistoryBackend::QueryMostVisitedURLs(
1616    scoped_refptr<QueryMostVisitedURLsRequest> request,
1617    int result_count,
1618    int days_back) {
1619  if (request->canceled())
1620    return;
1621
1622  if (!db_) {
1623    // No History Database - return an empty list.
1624    request->ForwardResult(request->handle(), MostVisitedURLList());
1625    return;
1626  }
1627
1628  MostVisitedURLList* result = &request->value;
1629  QueryMostVisitedURLsImpl(result_count, days_back, result);
1630  request->ForwardResult(request->handle(), *result);
1631}
1632
1633void HistoryBackend::QueryFilteredURLs(
1634      scoped_refptr<QueryFilteredURLsRequest> request,
1635      int result_count,
1636      const history::VisitFilter& filter,
1637      bool extended_info)  {
1638  if (request->canceled())
1639    return;
1640
1641  base::Time request_start = base::Time::Now();
1642
1643  if (!db_) {
1644    // No History Database - return an empty list.
1645    request->ForwardResult(request->handle(), FilteredURLList());
1646    return;
1647  }
1648
1649  VisitVector visits;
1650  db_->GetDirectVisitsDuringTimes(filter, 0, &visits);
1651
1652  std::map<URLID, double> score_map;
1653  for (size_t i = 0; i < visits.size(); ++i) {
1654    score_map[visits[i].url_id] += filter.GetVisitScore(visits[i]);
1655  }
1656
1657  // TODO(georgey): experiment with visit_segment database granularity (it is
1658  // currently 24 hours) to use it directly instead of using visits database,
1659  // which is considerably slower.
1660  ScopedVector<PageUsageData> data;
1661  data.reserve(score_map.size());
1662  for (std::map<URLID, double>::iterator it = score_map.begin();
1663       it != score_map.end(); ++it) {
1664    PageUsageData* pud = new PageUsageData(it->first);
1665    pud->SetScore(it->second);
1666    data.push_back(pud);
1667  }
1668
1669  // Limit to the top |result_count| results.
1670  std::sort(data.begin(), data.end(), PageUsageData::Predicate);
1671  if (result_count && implicit_cast<int>(data.size()) > result_count)
1672    data.resize(result_count);
1673
1674  for (size_t i = 0; i < data.size(); ++i) {
1675    URLRow info;
1676    if (db_->GetURLRow(data[i]->GetID(), &info)) {
1677      data[i]->SetURL(info.url());
1678      data[i]->SetTitle(info.title());
1679    }
1680  }
1681
1682  FilteredURLList& result = request->value;
1683  for (size_t i = 0; i < data.size(); ++i) {
1684    PageUsageData* current_data = data[i];
1685    FilteredURL url(*current_data);
1686
1687    if (extended_info) {
1688      VisitVector visits;
1689      db_->GetVisitsForURL(current_data->GetID(), &visits);
1690      if (visits.size() > 0) {
1691        url.extended_info.total_visits = visits.size();
1692        for (size_t i = 0; i < visits.size(); ++i) {
1693          url.extended_info.duration_opened +=
1694              visits[i].visit_duration.InSeconds();
1695          if (visits[i].visit_time > url.extended_info.last_visit_time) {
1696            url.extended_info.last_visit_time = visits[i].visit_time;
1697          }
1698        }
1699        // TODO(macourteau): implement the url.extended_info.visits stat.
1700      }
1701    }
1702    result.push_back(url);
1703  }
1704
1705  int delta_time = std::max(1, std::min(999,
1706      static_cast<int>((base::Time::Now() - request_start).InMilliseconds())));
1707  STATIC_HISTOGRAM_POINTER_BLOCK(
1708      "NewTabPage.SuggestedSitesLoadTime",
1709      Add(delta_time),
1710      base::LinearHistogram::FactoryGet("NewTabPage.SuggestedSitesLoadTime",
1711          1, 1000, 100, base::Histogram::kUmaTargetedHistogramFlag));
1712
1713  request->ForwardResult(request->handle(), result);
1714}
1715
1716void HistoryBackend::QueryMostVisitedURLsImpl(int result_count,
1717                                              int days_back,
1718                                              MostVisitedURLList* result) {
1719  if (!db_)
1720    return;
1721
1722  ScopedVector<PageUsageData> data;
1723  db_->QuerySegmentUsage(base::Time::Now() -
1724                         base::TimeDelta::FromDays(days_back),
1725                         result_count, &data.get());
1726
1727  for (size_t i = 0; i < data.size(); ++i) {
1728    PageUsageData* current_data = data[i];
1729    RedirectList redirects;
1730    GetMostRecentRedirectsFrom(current_data->GetURL(), &redirects);
1731    MostVisitedURL url = MakeMostVisitedURL(*current_data, redirects);
1732    result->push_back(url);
1733  }
1734}
1735
1736void HistoryBackend::GetRedirectsFromSpecificVisit(
1737    VisitID cur_visit, history::RedirectList* redirects) {
1738  // Follow any redirects from the given visit and add them to the list.
1739  // It *should* be impossible to get a circular chain here, but we check
1740  // just in case to avoid infinite loops.
1741  GURL cur_url;
1742  std::set<VisitID> visit_set;
1743  visit_set.insert(cur_visit);
1744  while (db_->GetRedirectFromVisit(cur_visit, &cur_visit, &cur_url)) {
1745    if (visit_set.find(cur_visit) != visit_set.end()) {
1746      NOTREACHED() << "Loop in visit chain, giving up";
1747      return;
1748    }
1749    visit_set.insert(cur_visit);
1750    redirects->push_back(cur_url);
1751  }
1752}
1753
1754void HistoryBackend::GetRedirectsToSpecificVisit(
1755    VisitID cur_visit,
1756    history::RedirectList* redirects) {
1757  // Follow redirects going to cur_visit. These are added to |redirects| in
1758  // the order they are found. If a redirect chain looks like A -> B -> C and
1759  // |cur_visit| = C, redirects will be {B, A} in that order.
1760  if (!db_)
1761    return;
1762
1763  GURL cur_url;
1764  std::set<VisitID> visit_set;
1765  visit_set.insert(cur_visit);
1766  while (db_->GetRedirectToVisit(cur_visit, &cur_visit, &cur_url)) {
1767    if (visit_set.find(cur_visit) != visit_set.end()) {
1768      NOTREACHED() << "Loop in visit chain, giving up";
1769      return;
1770    }
1771    visit_set.insert(cur_visit);
1772    redirects->push_back(cur_url);
1773  }
1774}
1775
1776bool HistoryBackend::GetMostRecentRedirectsFrom(
1777    const GURL& from_url,
1778    history::RedirectList* redirects) {
1779  redirects->clear();
1780  if (!db_)
1781    return false;
1782
1783  URLID from_url_id = db_->GetRowForURL(from_url, NULL);
1784  VisitID cur_visit = db_->GetMostRecentVisitForURL(from_url_id, NULL);
1785  if (!cur_visit)
1786    return false;  // No visits for URL.
1787
1788  GetRedirectsFromSpecificVisit(cur_visit, redirects);
1789  return true;
1790}
1791
1792bool HistoryBackend::GetMostRecentRedirectsTo(
1793    const GURL& to_url,
1794    history::RedirectList* redirects) {
1795  redirects->clear();
1796  if (!db_)
1797    return false;
1798
1799  URLID to_url_id = db_->GetRowForURL(to_url, NULL);
1800  VisitID cur_visit = db_->GetMostRecentVisitForURL(to_url_id, NULL);
1801  if (!cur_visit)
1802    return false;  // No visits for URL.
1803
1804  GetRedirectsToSpecificVisit(cur_visit, redirects);
1805  return true;
1806}
1807
1808void HistoryBackend::ScheduleAutocomplete(HistoryURLProvider* provider,
1809                                          HistoryURLProviderParams* params) {
1810  // ExecuteWithDB should handle the NULL database case.
1811  provider->ExecuteWithDB(this, db_.get(), params);
1812}
1813
1814void HistoryBackend::SetPageContents(const GURL& url,
1815                                     const string16& contents) {
1816  // This is histogrammed in the text database manager.
1817  if (!text_database_)
1818    return;
1819  text_database_->AddPageContents(url, contents);
1820}
1821
1822void HistoryBackend::SetPageThumbnail(
1823    const GURL& url,
1824    const gfx::Image* thumbnail,
1825    const ThumbnailScore& score) {
1826  if (!db_ || !thumbnail_db_)
1827    return;
1828
1829  URLRow url_row;
1830  URLID url_id = db_->GetRowForURL(url, &url_row);
1831  if (url_id) {
1832    thumbnail_db_->SetPageThumbnail(url, url_id, thumbnail, score,
1833                                    url_row.last_visit());
1834  }
1835
1836  ScheduleCommit();
1837}
1838
1839void HistoryBackend::GetPageThumbnail(
1840    scoped_refptr<GetPageThumbnailRequest> request,
1841    const GURL& page_url) {
1842  if (request->canceled())
1843    return;
1844
1845  scoped_refptr<base::RefCountedBytes> data;
1846  GetPageThumbnailDirectly(page_url, &data);
1847
1848  request->ForwardResult(request->handle(), data);
1849}
1850
1851void HistoryBackend::GetPageThumbnailDirectly(
1852    const GURL& page_url,
1853    scoped_refptr<base::RefCountedBytes>* data) {
1854  if (thumbnail_db_) {
1855    *data = new base::RefCountedBytes;
1856
1857    // Time the result.
1858    TimeTicks beginning_time = TimeTicks::Now();
1859
1860    history::RedirectList redirects;
1861    URLID url_id;
1862    bool success = false;
1863
1864    // If there are some redirects, try to get a thumbnail from the last
1865    // redirect destination.
1866    if (GetMostRecentRedirectsFrom(page_url, &redirects) &&
1867        !redirects.empty()) {
1868      if ((url_id = db_->GetRowForURL(redirects.back(), NULL)))
1869        success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data());
1870    }
1871
1872    // If we don't have a thumbnail from redirects, try the URL directly.
1873    if (!success) {
1874      if ((url_id = db_->GetRowForURL(page_url, NULL)))
1875        success = thumbnail_db_->GetPageThumbnail(url_id, &(*data)->data());
1876    }
1877
1878    // In this rare case, we start to mine the older redirect sessions
1879    // from the visit table to try to find a thumbnail.
1880    if (!success) {
1881      success = GetThumbnailFromOlderRedirect(page_url, &(*data)->data());
1882    }
1883
1884    if (!success)
1885      *data = NULL;  // This will tell the callback there was an error.
1886
1887    UMA_HISTOGRAM_TIMES("History.GetPageThumbnail",
1888                        TimeTicks::Now() - beginning_time);
1889  }
1890}
1891
1892void HistoryBackend::MigrateThumbnailsDatabase() {
1893  // If there is no History DB, we can't record that the migration was done.
1894  // It will be recorded on the next run.
1895  if (db_) {
1896    // If there is no thumbnail DB, we can still record a successful migration.
1897    if (thumbnail_db_) {
1898      thumbnail_db_->RenameAndDropThumbnails(GetThumbnailFileName(),
1899                                             GetFaviconsFileName());
1900    }
1901    db_->ThumbnailMigrationDone();
1902  }
1903}
1904
1905bool HistoryBackend::GetThumbnailFromOlderRedirect(
1906    const GURL& page_url,
1907    std::vector<unsigned char>* data) {
1908  // Look at a few previous visit sessions.
1909  VisitVector older_sessions;
1910  URLID page_url_id = db_->GetRowForURL(page_url, NULL);
1911  static const int kVisitsToSearchForThumbnail = 4;
1912  db_->GetMostRecentVisitsForURL(
1913      page_url_id, kVisitsToSearchForThumbnail, &older_sessions);
1914
1915  // Iterate across all those previous visits, and see if any of the
1916  // final destinations of those redirect chains have a good thumbnail
1917  // for us.
1918  bool success = false;
1919  for (VisitVector::const_iterator it = older_sessions.begin();
1920       !success && it != older_sessions.end(); ++it) {
1921    history::RedirectList redirects;
1922    if (it->visit_id) {
1923      GetRedirectsFromSpecificVisit(it->visit_id, &redirects);
1924
1925      if (!redirects.empty()) {
1926        URLID url_id;
1927        if ((url_id = db_->GetRowForURL(redirects.back(), NULL)))
1928          success = thumbnail_db_->GetPageThumbnail(url_id, data);
1929      }
1930    }
1931  }
1932
1933  return success;
1934}
1935
1936void HistoryBackend::GetFavicons(
1937    const std::vector<GURL>& icon_urls,
1938    int icon_types,
1939    int desired_size_in_dip,
1940    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1941    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
1942  UpdateFaviconMappingsAndFetchImpl(NULL, icon_urls, icon_types,
1943                                    desired_size_in_dip, desired_scale_factors,
1944                                    bitmap_results);
1945}
1946
1947void HistoryBackend::GetFaviconsForURL(
1948    const GURL& page_url,
1949    int icon_types,
1950    int desired_size_in_dip,
1951    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1952    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
1953  DCHECK(bitmap_results);
1954  GetFaviconsFromDB(page_url, icon_types, desired_size_in_dip,
1955                    desired_scale_factors, bitmap_results);
1956}
1957
1958void HistoryBackend::GetFaviconForID(
1959    chrome::FaviconID favicon_id,
1960    int desired_size_in_dip,
1961    ui::ScaleFactor desired_scale_factor,
1962    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
1963  std::vector<chrome::FaviconID> favicon_ids;
1964  favicon_ids.push_back(favicon_id);
1965  std::vector<ui::ScaleFactor> desired_scale_factors;
1966  desired_scale_factors.push_back(desired_scale_factor);
1967
1968  // Get results from DB.
1969  GetFaviconBitmapResultsForBestMatch(favicon_ids,
1970                                      desired_size_in_dip,
1971                                      desired_scale_factors,
1972                                      bitmap_results);
1973}
1974
1975void HistoryBackend::UpdateFaviconMappingsAndFetch(
1976    const GURL& page_url,
1977    const std::vector<GURL>& icon_urls,
1978    int icon_types,
1979    int desired_size_in_dip,
1980    const std::vector<ui::ScaleFactor>& desired_scale_factors,
1981    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
1982  UpdateFaviconMappingsAndFetchImpl(&page_url, icon_urls, icon_types,
1983                                    desired_size_in_dip, desired_scale_factors,
1984                                    bitmap_results);
1985}
1986
1987void HistoryBackend::MergeFavicon(
1988    const GURL& page_url,
1989    const GURL& icon_url,
1990    chrome::IconType icon_type,
1991    scoped_refptr<base::RefCountedMemory> bitmap_data,
1992    const gfx::Size& pixel_size) {
1993  if (!thumbnail_db_ || !db_)
1994    return;
1995
1996  chrome::FaviconID favicon_id =
1997      thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL);
1998
1999  if (!favicon_id) {
2000    // There is no favicon at |icon_url|, create it.
2001    favicon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
2002  }
2003
2004  std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
2005  thumbnail_db_->GetFaviconBitmapIDSizes(favicon_id, &bitmap_id_sizes);
2006
2007  // If there is already a favicon bitmap of |pixel_size| at |icon_url|,
2008  // replace it.
2009  bool bitmap_identical = false;
2010  bool replaced_bitmap = false;
2011  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
2012    if (bitmap_id_sizes[i].pixel_size == pixel_size) {
2013      if (IsFaviconBitmapDataEqual(bitmap_id_sizes[i].bitmap_id, bitmap_data)) {
2014        thumbnail_db_->SetFaviconBitmapLastUpdateTime(
2015            bitmap_id_sizes[i].bitmap_id, base::Time::Now());
2016        bitmap_identical = true;
2017      } else {
2018        thumbnail_db_->SetFaviconBitmap(bitmap_id_sizes[i].bitmap_id,
2019            bitmap_data, base::Time::Now());
2020        replaced_bitmap = true;
2021      }
2022      break;
2023    }
2024  }
2025
2026  // Create a vector of the pixel sizes of the favicon bitmaps currently at
2027  // |icon_url|.
2028  std::vector<gfx::Size> favicon_sizes;
2029  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i)
2030    favicon_sizes.push_back(bitmap_id_sizes[i].pixel_size);
2031
2032  if (!replaced_bitmap && !bitmap_identical) {
2033    // Set the preexisting favicon bitmaps as expired as the preexisting favicon
2034    // bitmaps are not consistent with the merged in data.
2035    thumbnail_db_->SetFaviconOutOfDate(favicon_id);
2036
2037    // Delete an arbitrary favicon bitmap to avoid going over the limit of
2038    // |kMaxFaviconBitmapsPerIconURL|.
2039    if (bitmap_id_sizes.size() >= kMaxFaviconBitmapsPerIconURL) {
2040      thumbnail_db_->DeleteFaviconBitmap(bitmap_id_sizes[0].bitmap_id);
2041      favicon_sizes.erase(favicon_sizes.begin());
2042    }
2043    thumbnail_db_->AddFaviconBitmap(favicon_id, bitmap_data, base::Time::Now(),
2044                                    pixel_size);
2045    favicon_sizes.push_back(pixel_size);
2046  }
2047
2048  // A site may have changed the favicons that it uses for |page_url|.
2049  // Example Scenario:
2050  //   page_url = news.google.com
2051  //   Intial State: www.google.com/favicon.ico 16x16, 32x32
2052  //   MergeFavicon(news.google.com, news.google.com/news_specific.ico, ...,
2053  //                ..., 16x16)
2054  //
2055  // Difficulties:
2056  // 1. Sync requires that a call to GetFaviconsForURL() returns the
2057  //    |bitmap_data| passed into MergeFavicon().
2058  //    - It is invalid for the 16x16 bitmap for www.google.com/favicon.ico to
2059  //      stay mapped to news.google.com because it would be unclear which 16x16
2060  //      bitmap should be returned via GetFaviconsForURL().
2061  //
2062  // 2. www.google.com/favicon.ico may be mapped to more than just
2063  //    news.google.com (eg www.google.com).
2064  //    - The 16x16 bitmap cannot be deleted from www.google.com/favicon.ico
2065  //
2066  // To resolve these problems, we copy all of the favicon bitmaps previously
2067  // mapped to news.google.com (|page_url|) and add them to the favicon at
2068  // news.google.com/news_specific.ico (|icon_url|). The favicon sizes for
2069  // |icon_url| are set to default to indicate that |icon_url| has incomplete
2070  // / incorrect data.
2071  // Difficlty 1: All but news.google.com/news_specific.ico are unmapped from
2072  //              news.google.com
2073  // Difficulty 2: The favicon bitmaps for www.google.com/favicon.ico are not
2074  //               modified.
2075
2076  std::vector<IconMapping> icon_mappings;
2077  thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, &icon_mappings);
2078
2079  // Copy the favicon bitmaps mapped to |page_url| to the favicon at |icon_url|
2080  // till the limit of |kMaxFaviconBitmapsPerIconURL| is reached.
2081  for (size_t i = 0; i < icon_mappings.size(); ++i) {
2082    if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
2083      break;
2084
2085    if (icon_mappings[i].icon_url == icon_url)
2086      continue;
2087
2088    std::vector<FaviconBitmap> bitmaps_to_copy;
2089    thumbnail_db_->GetFaviconBitmaps(icon_mappings[i].icon_id,
2090                                     &bitmaps_to_copy);
2091    for (size_t j = 0; j < bitmaps_to_copy.size(); ++j) {
2092      // Do not add a favicon bitmap at a pixel size for which there is already
2093      // a favicon bitmap mapped to |icon_url|. The one there is more correct
2094      // and having multiple equally sized favicon bitmaps for |page_url| is
2095      // ambiguous in terms of GetFaviconsForURL().
2096      std::vector<gfx::Size>::iterator it = std::find(favicon_sizes.begin(),
2097          favicon_sizes.end(), bitmaps_to_copy[j].pixel_size);
2098      if (it != favicon_sizes.end())
2099        continue;
2100
2101      // Add the favicon bitmap as expired as it is not consistent with the
2102      // merged in data.
2103      thumbnail_db_->AddFaviconBitmap(favicon_id,
2104          bitmaps_to_copy[j].bitmap_data, base::Time(),
2105          bitmaps_to_copy[j].pixel_size);
2106      favicon_sizes.push_back(bitmaps_to_copy[j].pixel_size);
2107
2108      if (favicon_sizes.size() >= kMaxFaviconBitmapsPerIconURL)
2109        break;
2110    }
2111  }
2112
2113  // Update the favicon mappings such that only |icon_url| is mapped to
2114  // |page_url|.
2115  bool mapping_changed = false;
2116  if (icon_mappings.size() != 1 || icon_mappings[0].icon_url != icon_url) {
2117    std::vector<chrome::FaviconID> favicon_ids;
2118    favicon_ids.push_back(favicon_id);
2119    SetFaviconMappingsForPageAndRedirects(page_url, icon_type, favicon_ids);
2120    mapping_changed = true;
2121  }
2122
2123  if (mapping_changed || !bitmap_identical)
2124    SendFaviconChangedNotificationForPageAndRedirects(page_url);
2125  ScheduleCommit();
2126}
2127
2128void HistoryBackend::SetFavicons(
2129    const GURL& page_url,
2130    chrome::IconType icon_type,
2131    const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data) {
2132  if (!thumbnail_db_ || !db_)
2133    return;
2134
2135  DCHECK(ValidateSetFaviconsParams(favicon_bitmap_data));
2136
2137  // Build map of FaviconBitmapData for each icon url.
2138  typedef std::map<GURL, std::vector<chrome::FaviconBitmapData> >
2139      BitmapDataByIconURL;
2140  BitmapDataByIconURL grouped_by_icon_url;
2141  for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
2142    const GURL& icon_url = favicon_bitmap_data[i].icon_url;
2143    grouped_by_icon_url[icon_url].push_back(favicon_bitmap_data[i]);
2144  }
2145
2146  // Track whether the method modifies or creates any favicon bitmaps, favicons
2147  // or icon mappings.
2148  bool data_modified = false;
2149
2150  std::vector<chrome::FaviconID> icon_ids;
2151  for (BitmapDataByIconURL::const_iterator it = grouped_by_icon_url.begin();
2152       it != grouped_by_icon_url.end(); ++it) {
2153    const GURL& icon_url = it->first;
2154    chrome::FaviconID icon_id =
2155        thumbnail_db_->GetFaviconIDForFaviconURL(icon_url, icon_type, NULL);
2156
2157    if (!icon_id) {
2158      // TODO(pkotwicz): Remove the favicon sizes attribute from
2159      // ThumbnailDatabase::AddFavicon().
2160      icon_id = thumbnail_db_->AddFavicon(icon_url, icon_type);
2161      data_modified = true;
2162    }
2163    icon_ids.push_back(icon_id);
2164
2165    if (!data_modified)
2166      SetFaviconBitmaps(icon_id, it->second, &data_modified);
2167    else
2168      SetFaviconBitmaps(icon_id, it->second, NULL);
2169  }
2170
2171  data_modified |=
2172    SetFaviconMappingsForPageAndRedirects(page_url, icon_type, icon_ids);
2173
2174  if (data_modified) {
2175    // Send notification to the UI as an icon mapping, favicon, or favicon
2176    // bitmap was changed by this function.
2177    SendFaviconChangedNotificationForPageAndRedirects(page_url);
2178  }
2179  ScheduleCommit();
2180}
2181
2182void HistoryBackend::SetFaviconsOutOfDateForPage(const GURL& page_url) {
2183  std::vector<IconMapping> icon_mappings;
2184
2185  if (!thumbnail_db_ ||
2186      !thumbnail_db_->GetIconMappingsForPageURL(page_url,
2187                                                &icon_mappings))
2188    return;
2189
2190  for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
2191       m != icon_mappings.end(); ++m) {
2192    thumbnail_db_->SetFaviconOutOfDate(m->icon_id);
2193  }
2194  ScheduleCommit();
2195}
2196
2197void HistoryBackend::CloneFavicons(const GURL& old_page_url,
2198                                   const GURL& new_page_url) {
2199  if (!thumbnail_db_)
2200    return;
2201
2202  // Prevent cross-domain cloning.
2203  if (old_page_url.GetOrigin() != new_page_url.GetOrigin())
2204    return;
2205
2206  thumbnail_db_->CloneIconMappings(old_page_url, new_page_url);
2207  ScheduleCommit();
2208}
2209
2210void HistoryBackend::SetImportedFavicons(
2211    const std::vector<ImportedFaviconUsage>& favicon_usage) {
2212  if (!db_ || !thumbnail_db_)
2213    return;
2214
2215  Time now = Time::Now();
2216
2217  // Track all URLs that had their favicons set or updated.
2218  std::set<GURL> favicons_changed;
2219
2220  for (size_t i = 0; i < favicon_usage.size(); i++) {
2221    chrome::FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL(
2222        favicon_usage[i].favicon_url, chrome::FAVICON, NULL);
2223    if (!favicon_id) {
2224      // This favicon doesn't exist yet, so we create it using the given data.
2225      // TODO(pkotwicz): Pass in real pixel size.
2226      favicon_id = thumbnail_db_->AddFavicon(
2227          favicon_usage[i].favicon_url,
2228          chrome::FAVICON,
2229          new base::RefCountedBytes(favicon_usage[i].png_data),
2230          now,
2231          gfx::Size());
2232    }
2233
2234    // Save the mapping from all the URLs to the favicon.
2235    BookmarkService* bookmark_service = GetBookmarkService();
2236    for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin();
2237         url != favicon_usage[i].urls.end(); ++url) {
2238      URLRow url_row;
2239      if (!db_->GetRowForURL(*url, &url_row)) {
2240        // If the URL is present as a bookmark, add the url in history to
2241        // save the favicon mapping. This will match with what history db does
2242        // for regular bookmarked URLs with favicons - when history db is
2243        // cleaned, we keep an entry in the db with 0 visits as long as that
2244        // url is bookmarked.
2245        if (bookmark_service && bookmark_service_->IsBookmarked(*url)) {
2246          URLRow url_info(*url);
2247          url_info.set_visit_count(0);
2248          url_info.set_typed_count(0);
2249          url_info.set_last_visit(base::Time());
2250          url_info.set_hidden(false);
2251          db_->AddURL(url_info);
2252          thumbnail_db_->AddIconMapping(*url, favicon_id);
2253          favicons_changed.insert(*url);
2254        }
2255      } else {
2256        if (!thumbnail_db_->GetIconMappingsForPageURL(
2257                *url, chrome::FAVICON, NULL)) {
2258          // URL is present in history, update the favicon *only* if it is not
2259          // set already.
2260          thumbnail_db_->AddIconMapping(*url, favicon_id);
2261          favicons_changed.insert(*url);
2262        }
2263      }
2264    }
2265  }
2266
2267  if (!favicons_changed.empty()) {
2268    // Send the notification about the changed favicon URLs.
2269    FaviconChangedDetails* changed_details = new FaviconChangedDetails;
2270    changed_details->urls.swap(favicons_changed);
2271    BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
2272                           changed_details);
2273  }
2274}
2275
2276void HistoryBackend::UpdateFaviconMappingsAndFetchImpl(
2277    const GURL* page_url,
2278    const std::vector<GURL>& icon_urls,
2279    int icon_types,
2280    int desired_size_in_dip,
2281    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2282    std::vector<chrome::FaviconBitmapResult>* bitmap_results) {
2283  // If |page_url| is specified, |icon_types| must be either a single icon
2284  // type or icon types which are equivalent.
2285  DCHECK(!page_url ||
2286         icon_types == chrome::FAVICON ||
2287         icon_types == chrome::TOUCH_ICON ||
2288         icon_types == chrome::TOUCH_PRECOMPOSED_ICON ||
2289         icon_types == (chrome::TOUCH_ICON | chrome::TOUCH_PRECOMPOSED_ICON));
2290  bitmap_results->clear();
2291
2292  if (!thumbnail_db_) {
2293    return;
2294  }
2295
2296  std::vector<chrome::FaviconID> favicon_ids;
2297
2298  // The icon type for which the mappings will the updated and data will be
2299  // returned.
2300  chrome::IconType selected_icon_type = chrome::INVALID_ICON;
2301
2302  for (size_t i = 0; i < icon_urls.size(); ++i) {
2303    const GURL& icon_url = icon_urls[i];
2304    chrome::IconType icon_type_out;
2305    const chrome::FaviconID favicon_id =
2306        thumbnail_db_->GetFaviconIDForFaviconURL(
2307            icon_url, icon_types, &icon_type_out);
2308
2309    if (favicon_id) {
2310      // Return and update icon mappings only for the largest icon type. As
2311      // |icon_urls| is not sorted in terms of icon type, clear |favicon_ids|
2312      // if an |icon_url| with a larger icon type is found.
2313      if (icon_type_out > selected_icon_type) {
2314        selected_icon_type = icon_type_out;
2315        favicon_ids.clear();
2316      }
2317      if (icon_type_out == selected_icon_type)
2318        favicon_ids.push_back(favicon_id);
2319    }
2320  }
2321
2322  if (page_url && !favicon_ids.empty()) {
2323    bool mappings_updated =
2324        SetFaviconMappingsForPageAndRedirects(*page_url, selected_icon_type,
2325                                              favicon_ids);
2326    if (mappings_updated) {
2327      SendFaviconChangedNotificationForPageAndRedirects(*page_url);
2328      ScheduleCommit();
2329    }
2330  }
2331
2332  GetFaviconBitmapResultsForBestMatch(favicon_ids, desired_size_in_dip,
2333      desired_scale_factors, bitmap_results);
2334}
2335
2336void HistoryBackend::SetFaviconBitmaps(
2337    chrome::FaviconID icon_id,
2338    const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data,
2339    bool* favicon_bitmaps_changed) {
2340  if (favicon_bitmaps_changed)
2341    *favicon_bitmaps_changed = false;
2342
2343  std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
2344  thumbnail_db_->GetFaviconBitmapIDSizes(icon_id, &bitmap_id_sizes);
2345
2346  std::vector<chrome::FaviconBitmapData> to_add = favicon_bitmap_data;
2347
2348  for (size_t i = 0; i < bitmap_id_sizes.size(); ++i) {
2349    const gfx::Size& pixel_size = bitmap_id_sizes[i].pixel_size;
2350    std::vector<chrome::FaviconBitmapData>::iterator match_it = to_add.end();
2351    for (std::vector<chrome::FaviconBitmapData>::iterator it = to_add.begin();
2352         it != to_add.end(); ++it) {
2353      if (it->pixel_size == pixel_size) {
2354        match_it = it;
2355        break;
2356      }
2357    }
2358
2359    FaviconBitmapID bitmap_id = bitmap_id_sizes[i].bitmap_id;
2360    if (match_it == to_add.end()) {
2361      thumbnail_db_->DeleteFaviconBitmap(bitmap_id);
2362
2363      if (favicon_bitmaps_changed)
2364        *favicon_bitmaps_changed = true;
2365    } else {
2366      if (favicon_bitmaps_changed &&
2367          !*favicon_bitmaps_changed &&
2368          IsFaviconBitmapDataEqual(bitmap_id, match_it->bitmap_data)) {
2369        thumbnail_db_->SetFaviconBitmapLastUpdateTime(
2370            bitmap_id, base::Time::Now());
2371      } else {
2372        thumbnail_db_->SetFaviconBitmap(bitmap_id, match_it->bitmap_data,
2373            base::Time::Now());
2374
2375        if (favicon_bitmaps_changed)
2376          *favicon_bitmaps_changed = true;
2377      }
2378      to_add.erase(match_it);
2379    }
2380  }
2381
2382  for (size_t i = 0; i < to_add.size(); ++i) {
2383    thumbnail_db_->AddFaviconBitmap(icon_id, to_add[i].bitmap_data,
2384        base::Time::Now(), to_add[i].pixel_size);
2385
2386    if (favicon_bitmaps_changed)
2387      *favicon_bitmaps_changed = true;
2388  }
2389}
2390
2391bool HistoryBackend::ValidateSetFaviconsParams(
2392    const std::vector<chrome::FaviconBitmapData>& favicon_bitmap_data) const {
2393  typedef std::map<GURL, size_t> BitmapsPerIconURL;
2394  BitmapsPerIconURL num_bitmaps_per_icon_url;
2395  for (size_t i = 0; i < favicon_bitmap_data.size(); ++i) {
2396    if (!favicon_bitmap_data[i].bitmap_data.get())
2397      return false;
2398
2399    const GURL& icon_url = favicon_bitmap_data[i].icon_url;
2400    if (!num_bitmaps_per_icon_url.count(icon_url))
2401      num_bitmaps_per_icon_url[icon_url] = 1u;
2402    else
2403      ++num_bitmaps_per_icon_url[icon_url];
2404  }
2405
2406  if (num_bitmaps_per_icon_url.size() > kMaxFaviconsPerPage)
2407    return false;
2408
2409  for (BitmapsPerIconURL::const_iterator it = num_bitmaps_per_icon_url.begin();
2410       it != num_bitmaps_per_icon_url.end(); ++it) {
2411    if (it->second > kMaxFaviconBitmapsPerIconURL)
2412      return false;
2413  }
2414  return true;
2415}
2416
2417bool HistoryBackend::IsFaviconBitmapDataEqual(
2418    FaviconBitmapID bitmap_id,
2419    const scoped_refptr<base::RefCountedMemory>& new_bitmap_data) {
2420  if (!new_bitmap_data.get())
2421    return false;
2422
2423  scoped_refptr<base::RefCountedMemory> original_bitmap_data;
2424  thumbnail_db_->GetFaviconBitmap(bitmap_id,
2425                                  NULL,
2426                                  &original_bitmap_data,
2427                                  NULL);
2428  return new_bitmap_data->Equals(original_bitmap_data);
2429}
2430
2431bool HistoryBackend::GetFaviconsFromDB(
2432    const GURL& page_url,
2433    int icon_types,
2434    int desired_size_in_dip,
2435    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2436    std::vector<chrome::FaviconBitmapResult>* favicon_bitmap_results) {
2437  DCHECK(favicon_bitmap_results);
2438  favicon_bitmap_results->clear();
2439
2440  if (!db_ || !thumbnail_db_)
2441    return false;
2442
2443  // Time the query.
2444  TimeTicks beginning_time = TimeTicks::Now();
2445
2446  // Get FaviconIDs for |page_url| and one of |icon_types|.
2447  std::vector<IconMapping> icon_mappings;
2448  thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_types,
2449                                           &icon_mappings);
2450  std::vector<chrome::FaviconID> favicon_ids;
2451  for (size_t i = 0; i < icon_mappings.size(); ++i)
2452    favicon_ids.push_back(icon_mappings[i].icon_id);
2453
2454  // Populate |favicon_bitmap_results| and |icon_url_sizes|.
2455  bool success = GetFaviconBitmapResultsForBestMatch(favicon_ids,
2456      desired_size_in_dip, desired_scale_factors, favicon_bitmap_results);
2457  UMA_HISTOGRAM_TIMES("History.GetFavIconFromDB",  // historical name
2458                      TimeTicks::Now() - beginning_time);
2459  return success && !favicon_bitmap_results->empty();
2460}
2461
2462bool HistoryBackend::GetFaviconBitmapResultsForBestMatch(
2463    const std::vector<chrome::FaviconID>& candidate_favicon_ids,
2464    int desired_size_in_dip,
2465    const std::vector<ui::ScaleFactor>& desired_scale_factors,
2466    std::vector<chrome::FaviconBitmapResult>* favicon_bitmap_results) {
2467  favicon_bitmap_results->clear();
2468
2469  if (candidate_favicon_ids.empty())
2470    return true;
2471
2472  // Find the FaviconID and the FaviconBitmapIDs which best match
2473  // |desired_size_in_dip| and |desired_scale_factors|.
2474  // TODO(pkotwicz): Select bitmap results from multiple favicons once
2475  // content::FaviconStatus supports multiple icon URLs.
2476  chrome::FaviconID best_favicon_id = 0;
2477  std::vector<FaviconBitmapID> best_bitmap_ids;
2478  float highest_score = kSelectFaviconFramesInvalidScore;
2479  for (size_t i = 0; i < candidate_favicon_ids.size(); ++i) {
2480    std::vector<FaviconBitmapIDSize> bitmap_id_sizes;
2481    thumbnail_db_->GetFaviconBitmapIDSizes(candidate_favicon_ids[i],
2482                                           &bitmap_id_sizes);
2483
2484    // Build vector of gfx::Size from |bitmap_id_sizes|.
2485    std::vector<gfx::Size> sizes;
2486    for (size_t j = 0; j < bitmap_id_sizes.size(); ++j)
2487      sizes.push_back(bitmap_id_sizes[j].pixel_size);
2488
2489    std::vector<size_t> candidate_bitmap_indices;
2490    float score = 0;
2491    SelectFaviconFrameIndices(sizes,
2492                              desired_scale_factors,
2493                              desired_size_in_dip,
2494                              &candidate_bitmap_indices,
2495                              &score);
2496    if (score > highest_score) {
2497      highest_score = score;
2498      best_favicon_id = candidate_favicon_ids[i],
2499      best_bitmap_ids.clear();
2500      for (size_t j = 0; j < candidate_bitmap_indices.size(); ++j) {
2501        size_t candidate_index = candidate_bitmap_indices[j];
2502        best_bitmap_ids.push_back(
2503            bitmap_id_sizes[candidate_index].bitmap_id);
2504      }
2505    }
2506  }
2507
2508  // Construct FaviconBitmapResults from |best_favicon_id| and
2509  // |best_bitmap_ids|.
2510  GURL icon_url;
2511  chrome::IconType icon_type;
2512  if (!thumbnail_db_->GetFaviconHeader(best_favicon_id, &icon_url,
2513                                       &icon_type)) {
2514    return false;
2515  }
2516
2517  for (size_t i = 0; i < best_bitmap_ids.size(); ++i) {
2518    base::Time last_updated;
2519    chrome::FaviconBitmapResult bitmap_result;
2520    bitmap_result.icon_url = icon_url;
2521    bitmap_result.icon_type = icon_type;
2522    if (!thumbnail_db_->GetFaviconBitmap(best_bitmap_ids[i],
2523                                         &last_updated,
2524                                         &bitmap_result.bitmap_data,
2525                                         &bitmap_result.pixel_size)) {
2526      return false;
2527    }
2528
2529    bitmap_result.expired = (Time::Now() - last_updated) >
2530        TimeDelta::FromDays(kFaviconRefetchDays);
2531    if (bitmap_result.is_valid())
2532      favicon_bitmap_results->push_back(bitmap_result);
2533  }
2534  return true;
2535}
2536
2537bool HistoryBackend::SetFaviconMappingsForPageAndRedirects(
2538    const GURL& page_url,
2539    chrome::IconType icon_type,
2540    const std::vector<chrome::FaviconID>& icon_ids) {
2541  if (!thumbnail_db_)
2542    return false;
2543
2544  // Find all the pages whose favicons we should set, we want to set it for
2545  // all the pages in the redirect chain if it redirected.
2546  history::RedirectList redirects;
2547  GetCachedRecentRedirects(page_url, &redirects);
2548
2549  bool mappings_changed = false;
2550
2551  // Save page <-> favicon associations.
2552  for (history::RedirectList::const_iterator i(redirects.begin());
2553       i != redirects.end(); ++i) {
2554    mappings_changed |= SetFaviconMappingsForPage(*i, icon_type, icon_ids);
2555  }
2556  return mappings_changed;
2557}
2558
2559bool HistoryBackend::SetFaviconMappingsForPage(
2560    const GURL& page_url,
2561    chrome::IconType icon_type,
2562    const std::vector<chrome::FaviconID>& icon_ids) {
2563  DCHECK_LE(icon_ids.size(), kMaxFaviconsPerPage);
2564  bool mappings_changed = false;
2565
2566  // Two icon types are considered 'equivalent' if one of the icon types is
2567  // TOUCH_ICON and the other is TOUCH_PRECOMPOSED_ICON.
2568  //
2569  // Sets the icon mappings from |page_url| for |icon_type| to the favicons
2570  // with |icon_ids|. Mappings for |page_url| to favicons of type |icon_type|
2571  // whose FaviconID is not in |icon_ids| are removed. All icon mappings for
2572  // |page_url| to favicons of a type equivalent to |icon_type| are removed.
2573  // Remove any favicons which are orphaned as a result of the removal of the
2574  // icon mappings.
2575
2576  std::vector<chrome::FaviconID> unmapped_icon_ids = icon_ids;
2577
2578  std::vector<IconMapping> icon_mappings;
2579  thumbnail_db_->GetIconMappingsForPageURL(page_url, &icon_mappings);
2580
2581  for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
2582       m != icon_mappings.end(); ++m) {
2583    std::vector<chrome::FaviconID>::iterator icon_id_it = std::find(
2584        unmapped_icon_ids.begin(), unmapped_icon_ids.end(), m->icon_id);
2585
2586    // If the icon mapping already exists, avoid removing it and adding it back.
2587    if (icon_id_it != unmapped_icon_ids.end()) {
2588      unmapped_icon_ids.erase(icon_id_it);
2589      continue;
2590    }
2591
2592    if ((icon_type == chrome::TOUCH_ICON &&
2593         m->icon_type == chrome::TOUCH_PRECOMPOSED_ICON) ||
2594        (icon_type == chrome::TOUCH_PRECOMPOSED_ICON &&
2595         m->icon_type == chrome::TOUCH_ICON) || (icon_type == m->icon_type)) {
2596      thumbnail_db_->DeleteIconMapping(m->mapping_id);
2597
2598      // Removing the icon mapping may have orphaned the associated favicon so
2599      // we must recheck it. This is not super fast, but this case will get
2600      // triggered rarely, since normally a page will always map to the same
2601      // favicon IDs. It will mostly happen for favicons we import.
2602      if (!thumbnail_db_->HasMappingFor(m->icon_id))
2603        thumbnail_db_->DeleteFavicon(m->icon_id);
2604      mappings_changed = true;
2605    }
2606  }
2607
2608  for (size_t i = 0; i < unmapped_icon_ids.size(); ++i) {
2609    thumbnail_db_->AddIconMapping(page_url, unmapped_icon_ids[i]);
2610    mappings_changed = true;
2611  }
2612  return mappings_changed;
2613}
2614
2615void HistoryBackend::GetCachedRecentRedirects(
2616    const GURL& page_url,
2617    history::RedirectList* redirect_list) {
2618  RedirectCache::iterator iter = recent_redirects_.Get(page_url);
2619  if (iter != recent_redirects_.end()) {
2620    *redirect_list = iter->second;
2621
2622    // The redirect chain should have the destination URL as the last item.
2623    DCHECK(!redirect_list->empty());
2624    DCHECK(redirect_list->back() == page_url);
2625  } else {
2626    // No known redirects, construct mock redirect chain containing |page_url|.
2627    redirect_list->push_back(page_url);
2628  }
2629}
2630
2631void HistoryBackend::SendFaviconChangedNotificationForPageAndRedirects(
2632    const GURL& page_url) {
2633  history::RedirectList redirect_list;
2634  GetCachedRecentRedirects(page_url, &redirect_list);
2635
2636  FaviconChangedDetails* changed_details = new FaviconChangedDetails;
2637  for (size_t i = 0; i < redirect_list.size(); ++i)
2638    changed_details->urls.insert(redirect_list[i]);
2639
2640  BroadcastNotifications(chrome::NOTIFICATION_FAVICON_CHANGED,
2641                         changed_details);
2642}
2643
2644void HistoryBackend::Commit() {
2645  if (!db_)
2646    return;
2647
2648  // Note that a commit may not actually have been scheduled if a caller
2649  // explicitly calls this instead of using ScheduleCommit. Likewise, we
2650  // may reset the flag written by a pending commit. But this is OK! It
2651  // will merely cause extra commits (which is kind of the idea). We
2652  // could optimize more for this case (we may get two extra commits in
2653  // some cases) but it hasn't been important yet.
2654  CancelScheduledCommit();
2655
2656  db_->CommitTransaction();
2657  DCHECK(db_->transaction_nesting() == 0) << "Somebody left a transaction open";
2658  db_->BeginTransaction();
2659
2660  if (thumbnail_db_) {
2661    thumbnail_db_->CommitTransaction();
2662    DCHECK(thumbnail_db_->transaction_nesting() == 0) <<
2663        "Somebody left a transaction open";
2664    thumbnail_db_->BeginTransaction();
2665  }
2666
2667  if (archived_db_) {
2668    archived_db_->CommitTransaction();
2669    archived_db_->BeginTransaction();
2670  }
2671
2672  if (text_database_) {
2673    text_database_->CommitTransaction();
2674    text_database_->BeginTransaction();
2675  }
2676}
2677
2678void HistoryBackend::ScheduleCommit() {
2679  if (scheduled_commit_.get())
2680    return;
2681  scheduled_commit_ = new CommitLaterTask(this);
2682  base::MessageLoop::current()->PostDelayedTask(
2683      FROM_HERE,
2684      base::Bind(&CommitLaterTask::RunCommit, scheduled_commit_.get()),
2685      base::TimeDelta::FromSeconds(kCommitIntervalSeconds));
2686}
2687
2688void HistoryBackend::CancelScheduledCommit() {
2689  if (scheduled_commit_.get()) {
2690    scheduled_commit_->Cancel();
2691    scheduled_commit_ = NULL;
2692  }
2693}
2694
2695void HistoryBackend::ProcessDBTaskImpl() {
2696  if (!db_) {
2697    // db went away, release all the refs.
2698    ReleaseDBTasks();
2699    return;
2700  }
2701
2702  // Remove any canceled tasks.
2703  while (!db_task_requests_.empty() && db_task_requests_.front()->canceled()) {
2704    db_task_requests_.front()->Release();
2705    db_task_requests_.pop_front();
2706  }
2707  if (db_task_requests_.empty())
2708    return;
2709
2710  // Run the first task.
2711  HistoryDBTaskRequest* request = db_task_requests_.front();
2712  db_task_requests_.pop_front();
2713  if (request->value->RunOnDBThread(this, db_.get())) {
2714    // The task is done. Notify the callback.
2715    request->ForwardResult();
2716    // We AddRef'd the request before adding, need to release it now.
2717    request->Release();
2718  } else {
2719    // Tasks wants to run some more. Schedule it at the end of current tasks.
2720    db_task_requests_.push_back(request);
2721    // And process it after an invoke later.
2722    base::MessageLoop::current()->PostTask(
2723        FROM_HERE, base::Bind(&HistoryBackend::ProcessDBTaskImpl, this));
2724  }
2725}
2726
2727void HistoryBackend::ReleaseDBTasks() {
2728  for (std::list<HistoryDBTaskRequest*>::iterator i =
2729       db_task_requests_.begin(); i != db_task_requests_.end(); ++i) {
2730    (*i)->Release();
2731  }
2732  db_task_requests_.clear();
2733}
2734
2735////////////////////////////////////////////////////////////////////////////////
2736//
2737// Generic operations
2738//
2739////////////////////////////////////////////////////////////////////////////////
2740
2741void HistoryBackend::DeleteURLs(const std::vector<GURL>& urls) {
2742  expirer_.DeleteURLs(urls);
2743
2744  db_->GetStartDate(&first_recorded_time_);
2745  // Force a commit, if the user is deleting something for privacy reasons, we
2746  // want to get it on disk ASAP.
2747  Commit();
2748}
2749
2750void HistoryBackend::DeleteURL(const GURL& url) {
2751  expirer_.DeleteURL(url);
2752
2753  db_->GetStartDate(&first_recorded_time_);
2754  // Force a commit, if the user is deleting something for privacy reasons, we
2755  // want to get it on disk ASAP.
2756  Commit();
2757}
2758
2759void HistoryBackend::ExpireHistoryBetween(
2760    const std::set<GURL>& restrict_urls,
2761    Time begin_time,
2762    Time end_time) {
2763  if (db_) {
2764    if (begin_time.is_null() && (end_time.is_null() || end_time.is_max()) &&
2765        restrict_urls.empty()) {
2766      // Special case deleting all history so it can be faster and to reduce the
2767      // possibility of an information leak.
2768      DeleteAllHistory();
2769    } else {
2770      // Clearing parts of history, have the expirer do the depend
2771      expirer_.ExpireHistoryBetween(restrict_urls, begin_time, end_time);
2772
2773      // Force a commit, if the user is deleting something for privacy reasons,
2774      // we want to get it on disk ASAP.
2775      Commit();
2776    }
2777  }
2778
2779  if (begin_time <= first_recorded_time_)
2780    db_->GetStartDate(&first_recorded_time_);
2781}
2782
2783void HistoryBackend::ExpireHistoryForTimes(
2784    const std::set<base::Time>& times,
2785    base::Time begin_time, base::Time end_time) {
2786  if (times.empty() || !db_)
2787    return;
2788
2789  DCHECK(*times.begin() >= begin_time)
2790      << "Min time is before begin time: "
2791      << times.begin()->ToJsTime() << " v.s. " << begin_time.ToJsTime();
2792  DCHECK(*times.rbegin() < end_time)
2793      << "Max time is after end time: "
2794      << times.rbegin()->ToJsTime() << " v.s. " << end_time.ToJsTime();
2795
2796  history::QueryOptions options;
2797  options.begin_time = begin_time;
2798  options.end_time = end_time;
2799  options.duplicate_policy = QueryOptions::KEEP_ALL_DUPLICATES;
2800  QueryResults results;
2801  QueryHistoryBasic(db_.get(), db_.get(), options, &results);
2802
2803  // 1st pass: find URLs that are visited at one of |times|.
2804  std::set<GURL> urls;
2805  for (size_t i = 0; i < results.size(); ++i) {
2806    if (times.count(results[i].visit_time()) > 0)
2807      urls.insert(results[i].url());
2808  }
2809  if (urls.empty())
2810    return;
2811
2812  // 2nd pass: collect all visit times of those URLs.
2813  std::vector<base::Time> times_to_expire;
2814  for (size_t i = 0; i < results.size(); ++i) {
2815    if (urls.count(results[i].url()))
2816      times_to_expire.push_back(results[i].visit_time());
2817  }
2818
2819  // Put the times in reverse chronological order and remove
2820  // duplicates (for expirer_.ExpireHistoryForTimes()).
2821  std::sort(times_to_expire.begin(), times_to_expire.end(),
2822            std::greater<base::Time>());
2823  times_to_expire.erase(
2824      std::unique(times_to_expire.begin(), times_to_expire.end()),
2825      times_to_expire.end());
2826
2827  // Expires by times and commit.
2828  DCHECK(!times_to_expire.empty());
2829  expirer_.ExpireHistoryForTimes(times_to_expire);
2830  Commit();
2831
2832  DCHECK(times_to_expire.back() >= first_recorded_time_);
2833  // Update |first_recorded_time_| if we expired it.
2834  if (times_to_expire.back() == first_recorded_time_)
2835    db_->GetStartDate(&first_recorded_time_);
2836}
2837
2838void HistoryBackend::ExpireHistory(
2839    const std::vector<history::ExpireHistoryArgs>& expire_list) {
2840  if (db_) {
2841    bool update_first_recorded_time = false;
2842
2843    for (std::vector<history::ExpireHistoryArgs>::const_iterator it =
2844         expire_list.begin(); it != expire_list.end(); ++it) {
2845      expirer_.ExpireHistoryBetween(it->urls, it->begin_time, it->end_time);
2846
2847      if (it->begin_time < first_recorded_time_)
2848        update_first_recorded_time = true;
2849    }
2850    Commit();
2851
2852    // Update |first_recorded_time_| if any deletion might have affected it.
2853    if (update_first_recorded_time)
2854      db_->GetStartDate(&first_recorded_time_);
2855  }
2856}
2857
2858void HistoryBackend::URLsNoLongerBookmarked(const std::set<GURL>& urls) {
2859  if (!db_)
2860    return;
2861
2862  for (std::set<GURL>::const_iterator i = urls.begin(); i != urls.end(); ++i) {
2863    URLRow url_row;
2864    if (!db_->GetRowForURL(*i, &url_row))
2865      continue;  // The URL isn't in the db; nothing to do.
2866
2867    VisitVector visits;
2868    db_->GetVisitsForURL(url_row.id(), &visits);
2869
2870    if (visits.empty())
2871      expirer_.DeleteURL(*i);  // There are no more visits; nuke the URL.
2872  }
2873}
2874
2875void HistoryBackend::DatabaseErrorCallback(int error, sql::Statement* stmt) {
2876  if (!scheduled_kill_db_ && sql::IsErrorCatastrophic(error)) {
2877    scheduled_kill_db_ = true;
2878    // Don't just do the close/delete here, as we are being called by |db| and
2879    // that seems dangerous.
2880    // TODO(shess): Consider changing KillHistoryDatabase() to use
2881    // RazeAndClose().  Then it can be cleared immediately.
2882    base::MessageLoop::current()->PostTask(
2883        FROM_HERE,
2884        base::Bind(&HistoryBackend::KillHistoryDatabase, this));
2885  }
2886}
2887
2888void HistoryBackend::KillHistoryDatabase() {
2889  scheduled_kill_db_ = false;
2890  if (!db_)
2891    return;
2892
2893  // Rollback transaction because Raze() cannot be called from within a
2894  // transaction.
2895  db_->RollbackTransaction();
2896  bool success = db_->Raze();
2897  UMA_HISTOGRAM_BOOLEAN("History.KillHistoryDatabaseResult", success);
2898
2899#if defined(OS_ANDROID)
2900  // Release AndroidProviderBackend before other objects.
2901  android_provider_backend_.reset();
2902#endif
2903
2904  // The expirer keeps tabs on the active databases. Tell it about the
2905  // databases which will be closed.
2906  expirer_.SetDatabases(NULL, NULL, NULL, NULL);
2907
2908  // Reopen a new transaction for |db_| for the sake of CloseAllDatabases().
2909  db_->BeginTransaction();
2910  CloseAllDatabases();
2911}
2912
2913void HistoryBackend::ProcessDBTask(
2914    scoped_refptr<HistoryDBTaskRequest> request) {
2915  DCHECK(request.get());
2916  if (request->canceled())
2917    return;
2918
2919  bool task_scheduled = !db_task_requests_.empty();
2920  // Make sure we up the refcount of the request. ProcessDBTaskImpl will
2921  // release when done with the task.
2922  request->AddRef();
2923  db_task_requests_.push_back(request.get());
2924  if (!task_scheduled) {
2925    // No other tasks are scheduled. Process request now.
2926    ProcessDBTaskImpl();
2927  }
2928}
2929
2930void HistoryBackend::BroadcastNotifications(
2931    int type,
2932    HistoryDetails* details_deleted) {
2933  // |delegate_| may be NULL if |this| is in the process of closing (closed by
2934  // HistoryService -> HistoryBackend::Closing().
2935  if (delegate_)
2936    delegate_->BroadcastNotifications(type, details_deleted);
2937  else
2938    delete details_deleted;
2939}
2940
2941void HistoryBackend::NotifySyncURLsDeleted(bool all_history,
2942                                           bool archived,
2943                                           URLRows* rows) {
2944  if (typed_url_syncable_service_.get())
2945    typed_url_syncable_service_->OnUrlsDeleted(all_history, archived, rows);
2946}
2947
2948// Deleting --------------------------------------------------------------------
2949
2950void HistoryBackend::DeleteAllHistory() {
2951  // Our approach to deleting all history is:
2952  //  1. Copy the bookmarks and their dependencies to new tables with temporary
2953  //     names.
2954  //  2. Delete the original tables. Since tables can not share pages, we know
2955  //     that any data we don't want to keep is now in an unused page.
2956  //  3. Renaming the temporary tables to match the original.
2957  //  4. Vacuuming the database to delete the unused pages.
2958  //
2959  // Since we are likely to have very few bookmarks and their dependencies
2960  // compared to all history, this is also much faster than just deleting from
2961  // the original tables directly.
2962
2963  // Get the bookmarked URLs.
2964  std::vector<BookmarkService::URLAndTitle> starred_urls;
2965  BookmarkService* bookmark_service = GetBookmarkService();
2966  if (bookmark_service)
2967    bookmark_service_->GetBookmarks(&starred_urls);
2968
2969  URLRows kept_urls;
2970  for (size_t i = 0; i < starred_urls.size(); i++) {
2971    URLRow row;
2972    if (!db_->GetRowForURL(starred_urls[i].url, &row))
2973      continue;
2974
2975    // Clear the last visit time so when we write these rows they are "clean."
2976    row.set_last_visit(Time());
2977    row.set_visit_count(0);
2978    row.set_typed_count(0);
2979    kept_urls.push_back(row);
2980  }
2981
2982  // Clear thumbnail and favicon history. The favicons for the given URLs will
2983  // be kept.
2984  if (!ClearAllThumbnailHistory(&kept_urls)) {
2985    LOG(ERROR) << "Thumbnail history could not be cleared";
2986    // We continue in this error case. If the user wants to delete their
2987    // history, we should delete as much as we can.
2988  }
2989
2990  // ClearAllMainHistory will change the IDs of the URLs in kept_urls. Therfore,
2991  // we clear the list afterwards to make sure nobody uses this invalid data.
2992  if (!ClearAllMainHistory(kept_urls))
2993    LOG(ERROR) << "Main history could not be cleared";
2994  kept_urls.clear();
2995
2996  // Delete FTS files & archived history.
2997  if (text_database_) {
2998    // We assume that the text database has one transaction on them that we need
2999    // to close & restart (the long-running history transaction).
3000    text_database_->CommitTransaction();
3001    text_database_->DeleteAll();
3002    text_database_->BeginTransaction();
3003  }
3004
3005  if (archived_db_) {
3006    // Close the database and delete the file.
3007    archived_db_.reset();
3008    base::FilePath archived_file_name = GetArchivedFileName();
3009    sql::Connection::Delete(archived_file_name);
3010
3011    // Now re-initialize the database (which may fail).
3012    archived_db_.reset(new ArchivedDatabase());
3013    if (!archived_db_->Init(archived_file_name)) {
3014      LOG(WARNING) << "Could not initialize the archived database.";
3015      archived_db_.reset();
3016    } else {
3017      // Open our long-running transaction on this database.
3018      archived_db_->BeginTransaction();
3019    }
3020  }
3021
3022  db_->GetStartDate(&first_recorded_time_);
3023
3024  // Send out the notfication that history is cleared. The in-memory datdabase
3025  // will pick this up and clear itself.
3026  URLsDeletedDetails* details = new URLsDeletedDetails;
3027  details->all_history = true;
3028  NotifySyncURLsDeleted(true, false, NULL);
3029  BroadcastNotifications(chrome::NOTIFICATION_HISTORY_URLS_DELETED, details);
3030}
3031
3032bool HistoryBackend::ClearAllThumbnailHistory(URLRows* kept_urls) {
3033  if (!thumbnail_db_) {
3034    // When we have no reference to the thumbnail database, maybe there was an
3035    // error opening it. In this case, we just try to blow it away to try to
3036    // fix the error if it exists. This may fail, in which case either the
3037    // file doesn't exist or there's no more we can do.
3038    sql::Connection::Delete(GetThumbnailFileName());
3039    return true;
3040  }
3041
3042  // Create duplicate icon_mapping, favicon, and favicon_bitmaps tables, this
3043  // is where the favicons we want to keep will be stored.
3044  if (!thumbnail_db_->InitTemporaryTables())
3045    return false;
3046
3047  // This maps existing favicon IDs to the ones in the temporary table.
3048  typedef std::map<chrome::FaviconID, chrome::FaviconID> FaviconMap;
3049  FaviconMap copied_favicons;
3050
3051  // Copy all unique favicons to the temporary table, and update all the
3052  // URLs to have the new IDs.
3053  for (URLRows::iterator i = kept_urls->begin(); i != kept_urls->end(); ++i) {
3054    std::vector<IconMapping> icon_mappings;
3055    if (!thumbnail_db_->GetIconMappingsForPageURL(i->url(), &icon_mappings))
3056      continue;
3057
3058    for (std::vector<IconMapping>::iterator m = icon_mappings.begin();
3059         m != icon_mappings.end(); ++m) {
3060      chrome::FaviconID old_id = m->icon_id;
3061      chrome::FaviconID new_id;
3062      FaviconMap::const_iterator found = copied_favicons.find(old_id);
3063      if (found == copied_favicons.end()) {
3064        new_id = thumbnail_db_->CopyFaviconAndFaviconBitmapsToTemporaryTables(
3065            old_id);
3066        copied_favicons[old_id] = new_id;
3067      } else {
3068        // We already encountered a URL that used this favicon, use the ID we
3069        // previously got.
3070        new_id = found->second;
3071      }
3072      // Add Icon mapping, and we don't care wheteher it suceeded or not.
3073      thumbnail_db_->AddToTemporaryIconMappingTable(i->url(), new_id);
3074    }
3075  }
3076#if defined(OS_ANDROID)
3077  // TODO (michaelbai): Add the unit test once AndroidProviderBackend is
3078  // avaliable in HistoryBackend.
3079  db_->ClearAndroidURLRows();
3080#endif
3081
3082  // Drop original favicon_bitmaps, favicons, and icon mapping tables and
3083  // replace them with the duplicate tables. Recreate the other tables. This
3084  // will make the database consistent again.
3085  thumbnail_db_->CommitTemporaryTables();
3086
3087  thumbnail_db_->RecreateThumbnailTable();
3088
3089  // Vacuum to remove all the pages associated with the dropped tables. There
3090  // must be no transaction open on the table when we do this. We assume that
3091  // our long-running transaction is open, so we complete it and start it again.
3092  DCHECK(thumbnail_db_->transaction_nesting() == 1);
3093  thumbnail_db_->CommitTransaction();
3094  thumbnail_db_->Vacuum();
3095  thumbnail_db_->BeginTransaction();
3096  return true;
3097}
3098
3099bool HistoryBackend::ClearAllMainHistory(const URLRows& kept_urls) {
3100  // Create the duplicate URL table. We will copy the kept URLs into this.
3101  if (!db_->CreateTemporaryURLTable())
3102    return false;
3103
3104  // Insert the URLs into the temporary table, we need to keep a map of changed
3105  // IDs since the ID will be different in the new table.
3106  typedef std::map<URLID, URLID> URLIDMap;
3107  URLIDMap old_to_new;  // Maps original ID to new one.
3108  for (URLRows::const_iterator i = kept_urls.begin(); i != kept_urls.end();
3109       ++i) {
3110    URLID new_id = db_->AddTemporaryURL(*i);
3111    old_to_new[i->id()] = new_id;
3112  }
3113
3114  // Replace the original URL table with the temporary one.
3115  if (!db_->CommitTemporaryURLTable())
3116    return false;
3117
3118  // Delete the old tables and recreate them empty.
3119  db_->RecreateAllTablesButURL();
3120
3121  // Vacuum to reclaim the space from the dropped tables. This must be done
3122  // when there is no transaction open, and we assume that our long-running
3123  // transaction is currently open.
3124  db_->CommitTransaction();
3125  db_->Vacuum();
3126  db_->BeginTransaction();
3127  db_->GetStartDate(&first_recorded_time_);
3128
3129  return true;
3130}
3131
3132BookmarkService* HistoryBackend::GetBookmarkService() {
3133  if (bookmark_service_)
3134    bookmark_service_->BlockTillLoaded();
3135  return bookmark_service_;
3136}
3137
3138void HistoryBackend::NotifyVisitObservers(const VisitRow& visit) {
3139  BriefVisitInfo info;
3140  info.url_id = visit.url_id;
3141  info.time = visit.visit_time;
3142  info.transition = visit.transition;
3143  // If we don't have a delegate yet during setup or shutdown, we will drop
3144  // these notifications.
3145  if (delegate_)
3146    delegate_->NotifyVisitDBObserversOnAddVisit(info);
3147}
3148
3149#if defined(OS_ANDROID)
3150void HistoryBackend::PopulateMostVisitedURLMap() {
3151  MostVisitedURLList most_visited_urls;
3152  QueryMostVisitedURLsImpl(kPageVisitStatsMaxTopSites, kSegmentDataRetention,
3153                           &most_visited_urls);
3154
3155  DCHECK_LE(most_visited_urls.size(), kPageVisitStatsMaxTopSites);
3156  for (size_t i = 0; i < most_visited_urls.size(); ++i) {
3157    most_visited_urls_map_[most_visited_urls[i].url] = i;
3158    for (size_t j = 0; j < most_visited_urls[i].redirects.size(); ++j)
3159      most_visited_urls_map_[most_visited_urls[i].redirects[j]] = i;
3160  }
3161}
3162
3163void HistoryBackend::RecordTopPageVisitStats(const GURL& url) {
3164  int rank = kPageVisitStatsMaxTopSites;
3165  std::map<GURL, int>::const_iterator it = most_visited_urls_map_.find(url);
3166  if (it != most_visited_urls_map_.end())
3167    rank = (*it).second;
3168  UMA_HISTOGRAM_ENUMERATION("History.TopSitesVisitsByRank",
3169                            rank, kPageVisitStatsMaxTopSites + 1);
3170}
3171#endif
3172
3173}  // namespace history
3174