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