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