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