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