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