aab15ff9212a60124ad7bd106578fcbe67cfeaba
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / chrome_history_client.cc
1 // Copyright 2014 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/chrome_history_client.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/history/history_notifications.h"
10 #include "chrome/browser/history/history_service.h"
11 #include "chrome/browser/history/top_sites.h"
12 #include "chrome/browser/ui/profile_error_dialog.h"
13 #include "chrome/common/chrome_version_info.h"
14 #include "chrome/grit/chromium_strings.h"
15 #include "chrome/grit/generated_resources.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "content/public/browser/notification_service.h"
18
19 ChromeHistoryClient::ChromeHistoryClient(BookmarkModel* bookmark_model,
20                                          Profile* profile,
21                                          history::TopSites* top_sites)
22     : bookmark_model_(bookmark_model),
23       profile_(profile),
24       history_service_(nullptr),
25       top_sites_(top_sites) {
26   DCHECK(bookmark_model_);
27   if (top_sites_)
28     top_sites_->AddObserver(this);
29 }
30
31 ChromeHistoryClient::~ChromeHistoryClient() {
32   if (top_sites_)
33     top_sites_->RemoveObserver(this);
34 }
35
36 void ChromeHistoryClient::SetHistoryService(HistoryService* history_service) {
37   DCHECK(history_service);
38   history_service_ = history_service;
39   history_service_->AddObserver(this);
40 }
41
42 void ChromeHistoryClient::BlockUntilBookmarksLoaded() {
43   bookmark_model_->BlockTillLoaded();
44 }
45
46 bool ChromeHistoryClient::IsBookmarked(const GURL& url) {
47   return bookmark_model_->IsBookmarked(url);
48 }
49
50 void ChromeHistoryClient::GetBookmarks(
51     std::vector<history::URLAndTitle>* bookmarks) {
52   std::vector<BookmarkModel::URLAndTitle> bookmarks_url_and_title;
53   bookmark_model_->GetBookmarks(&bookmarks_url_and_title);
54
55   bookmarks->reserve(bookmarks->size() + bookmarks_url_and_title.size());
56   for (size_t i = 0; i < bookmarks_url_and_title.size(); ++i) {
57     history::URLAndTitle value = {
58       bookmarks_url_and_title[i].url,
59       bookmarks_url_and_title[i].title,
60     };
61     bookmarks->push_back(value);
62   }
63 }
64
65 void ChromeHistoryClient::NotifyProfileError(sql::InitStatus init_status) {
66   ShowProfileErrorDialog(
67       PROFILE_ERROR_HISTORY,
68       (init_status == sql::INIT_FAILURE) ?
69       IDS_COULDNT_OPEN_PROFILE_ERROR : IDS_PROFILE_TOO_NEW_ERROR);
70 }
71
72 bool ChromeHistoryClient::ShouldReportDatabaseError() {
73   // TODO(shess): For now, don't report on beta or stable so as not to
74   // overwhelm the crash server.  Once the big fish are fried,
75   // consider reporting at a reduced rate on the bigger channels.
76   chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
77   return channel != chrome::VersionInfo::CHANNEL_STABLE &&
78       channel != chrome::VersionInfo::CHANNEL_BETA;
79 }
80
81 void ChromeHistoryClient::Shutdown() {
82   // It's possible that bookmarks haven't loaded and history is waiting for
83   // bookmarks to complete loading. In such a situation history can't shutdown
84   // (meaning if we invoked HistoryService::Cleanup now, we would deadlock). To
85   // break the deadlock we tell BookmarkModel it's about to be deleted so that
86   // it can release the signal history is waiting on, allowing history to
87   // shutdown (HistoryService::Cleanup to complete). In such a scenario history
88   // sees an incorrect view of bookmarks, but it's better than a deadlock.
89   bookmark_model_->Shutdown();
90   if (history_service_) {
91     history_service_->RemoveObserver(this);
92     history_service_ = nullptr;
93   }
94 }
95
96 void ChromeHistoryClient::TopSitesLoaded(history::TopSites* top_sites) {
97   content::NotificationService::current()->Notify(
98       chrome::NOTIFICATION_TOP_SITES_LOADED,
99       content::Source<Profile>(profile_),
100       content::Details<history::TopSites>(top_sites));
101 }
102
103 void ChromeHistoryClient::TopSitesChanged(history::TopSites* top_sites) {
104   content::NotificationService::current()->Notify(
105       chrome::NOTIFICATION_TOP_SITES_CHANGED,
106       content::Source<history::TopSites>(top_sites),
107       content::NotificationService::NoDetails());
108 }