15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Copyright 2014 The Chromium Authors. All rights reserved.
25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// Use of this source code is governed by a BSD-style license that can be
35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// found in the LICENSE file.
45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/history/chrome_history_client.h"
65c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "base/logging.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/chrome_notification_types.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/history/history_notifications.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/history/top_sites.h"
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/ui/profile_error_dialog.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/common/chrome_version_info.h"
1303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/chromium_strings.h"
1403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/bookmarks/browser/bookmark_model.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "content/public/browser/notification_service.h"
1746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciChromeHistoryClient::ChromeHistoryClient(BookmarkModel* bookmark_model,
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         Profile* profile,
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                         history::TopSites* top_sites)
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : bookmark_model_(bookmark_model),
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      profile_(profile),
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      top_sites_(top_sites) {
2446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK(bookmark_model_);
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (top_sites_)
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    top_sites_->AddObserver(this);
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciChromeHistoryClient::~ChromeHistoryClient() {
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (top_sites_)
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    top_sites_->RemoveObserver(this);
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void ChromeHistoryClient::BlockUntilBookmarksLoaded() {
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bookmark_model_->BlockTillLoaded();
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)bool ChromeHistoryClient::IsBookmarked(const GURL& url) {
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return bookmark_model_->IsBookmarked(url);
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void ChromeHistoryClient::GetBookmarks(
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    std::vector<history::URLAndTitle>* bookmarks) {
4446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  std::vector<BookmarkModel::URLAndTitle> bookmarks_url_and_title;
4546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bookmark_model_->GetBookmarks(&bookmarks_url_and_title);
4646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bookmarks->reserve(bookmarks->size() + bookmarks_url_and_title.size());
4846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  for (size_t i = 0; i < bookmarks_url_and_title.size(); ++i) {
4946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    history::URLAndTitle value = {
5046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_url_and_title[i].url,
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_url_and_title[i].title,
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    };
5346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bookmarks->push_back(value);
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  }
5546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
5646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
57f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void ChromeHistoryClient::NotifyProfileError(sql::InitStatus init_status) {
58f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  ShowProfileErrorDialog(
59f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      PROFILE_ERROR_HISTORY,
60f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      (init_status == sql::INIT_FAILURE) ?
61f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR);
62f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
63f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool ChromeHistoryClient::ShouldReportDatabaseError() {
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(shess): For now, don't report on beta or stable so as not to
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // overwhelm the crash server.  Once the big fish are fried,
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // consider reporting at a reduced rate on the bigger channels.
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return channel != chrome::VersionInfo::CHANNEL_STABLE &&
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      channel != chrome::VersionInfo::CHANNEL_BETA;
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)void ChromeHistoryClient::Shutdown() {
7446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // It's possible that bookmarks haven't loaded and history is waiting for
7546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // bookmarks to complete loading. In such a situation history can't shutdown
7646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // (meaning if we invoked HistoryService::Cleanup now, we would deadlock). To
7746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // break the deadlock we tell BookmarkModel it's about to be deleted so that
7846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // it can release the signal history is waiting on, allowing history to
7946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // shutdown (HistoryService::Cleanup to complete). In such a scenario history
8046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // sees an incorrect view of bookmarks, but it's better than a deadlock.
8146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bookmark_model_->Shutdown();
825c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu}
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromeHistoryClient::TopSitesLoaded(history::TopSites* top_sites) {
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::NotificationService::current()->Notify(
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      chrome::NOTIFICATION_TOP_SITES_LOADED,
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::Source<Profile>(profile_),
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::Details<history::TopSites>(top_sites));
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid ChromeHistoryClient::TopSitesChanged(history::TopSites* top_sites) {
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  content::NotificationService::current()->Notify(
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      chrome::NOTIFICATION_TOP_SITES_CHANGED,
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::Source<history::TopSites>(top_sites),
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      content::NotificationService::NoDetails());
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
97