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