Temporarily re-enabling SizeAfterPrefChange test with traces (this time for Linux...
[chromium-blink-merge.git] / chrome / browser / bookmarks / chrome_bookmark_client.cc
blobb57fb06f3c37ae0563662a19d708b6557b4a880a
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.
5 #include "chrome/browser/bookmarks/chrome_bookmark_client.h"
7 #include "base/logging.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/favicon/favicon_changed_details.h"
10 #include "chrome/browser/favicon/favicon_service.h"
11 #include "chrome/browser/favicon/favicon_service_factory.h"
12 #include "chrome/browser/history/history_service.h"
13 #include "chrome/browser/history/history_service_factory.h"
14 #include "chrome/browser/history/url_database.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "components/bookmarks/browser/bookmark_model.h"
17 #include "components/bookmarks/browser/bookmark_node.h"
18 #include "content/public/browser/notification_details.h"
19 #include "content/public/browser/notification_source.h"
20 #include "content/public/browser/user_metrics.h"
22 namespace {
24 void NotifyHistoryOfRemovedURLs(Profile* profile,
25 const std::set<GURL>& removed_urls) {
26 HistoryService* history_service =
27 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS);
28 if (history_service)
29 history_service->URLsNoLongerBookmarked(removed_urls);
32 } // namespace
34 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile, bool index_urls)
35 : profile_(profile),
36 model_(new BookmarkModel(this, index_urls)) {
37 model_->AddObserver(this);
38 // Listen for changes to favicons so that we can update the favicon of the
39 // node appropriately.
40 registrar_.Add(this,
41 chrome::NOTIFICATION_FAVICON_CHANGED,
42 content::Source<Profile>(profile_));
45 ChromeBookmarkClient::~ChromeBookmarkClient() {
46 model_->RemoveObserver(this);
48 registrar_.RemoveAll();
51 bool ChromeBookmarkClient::PreferTouchIcon() {
52 #if !defined(OS_IOS)
53 return false;
54 #else
55 return true;
56 #endif
59 base::CancelableTaskTracker::TaskId ChromeBookmarkClient::GetFaviconImageForURL(
60 const GURL& page_url,
61 int icon_types,
62 int desired_size_in_dip,
63 const favicon_base::FaviconImageCallback& callback,
64 base::CancelableTaskTracker* tracker) {
65 FaviconService* favicon_service =
66 FaviconServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
67 if (!favicon_service)
68 return base::CancelableTaskTracker::kBadTaskId;
69 return favicon_service->GetFaviconImageForURL(
70 FaviconService::FaviconForURLParams(
71 page_url, icon_types, desired_size_in_dip),
72 callback,
73 tracker);
76 bool ChromeBookmarkClient::SupportsTypedCountForNodes() {
77 return true;
80 void ChromeBookmarkClient::GetTypedCountForNodes(
81 const NodeSet& nodes,
82 NodeTypedCountPairs* node_typed_count_pairs) {
83 HistoryService* history_service =
84 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
85 history::URLDatabase* url_db =
86 history_service ? history_service->InMemoryDatabase() : NULL;
87 for (NodeSet::const_iterator i = nodes.begin(); i != nodes.end(); ++i) {
88 int typed_count = 0;
90 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but
91 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot
92 // fetch the URLRow, it is safe to assume that its |typed_count| is 0.
93 history::URLRow url;
94 if (url_db && url_db->GetRowForURL((*i)->url(), &url))
95 typed_count = url.typed_count();
97 NodeTypedCountPair pair(*i, typed_count);
98 node_typed_count_pairs->push_back(pair);
102 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) {
103 content::RecordAction(action);
106 bool ChromeBookmarkClient::IsPermanentNodeVisible(int node_type) {
107 DCHECK(node_type == BookmarkNode::BOOKMARK_BAR ||
108 node_type == BookmarkNode::OTHER_NODE ||
109 node_type == BookmarkNode::MOBILE);
110 #if !defined(OS_IOS)
111 return node_type != BookmarkNode::MOBILE;
112 #else
113 return node_type == BookmarkNode::MOBILE;
114 #endif
117 void ChromeBookmarkClient::Observe(
118 int type,
119 const content::NotificationSource& source,
120 const content::NotificationDetails& details) {
121 switch (type) {
122 case chrome::NOTIFICATION_FAVICON_CHANGED: {
123 content::Details<FaviconChangedDetails> favicon_details(details);
124 model_->OnFaviconChanged(favicon_details->urls);
125 break;
128 default:
129 NOTREACHED();
130 break;
134 void ChromeBookmarkClient::Shutdown() {
135 model_->Shutdown();
138 void ChromeBookmarkClient::BookmarkModelChanged() {
141 void ChromeBookmarkClient::BookmarkNodeRemoved(
142 BookmarkModel* model,
143 const BookmarkNode* parent,
144 int old_index,
145 const BookmarkNode* node,
146 const std::set<GURL>& removed_urls) {
147 NotifyHistoryOfRemovedURLs(profile_, removed_urls);
150 void ChromeBookmarkClient::BookmarkAllNodesRemoved(
151 BookmarkModel* model,
152 const std::set<GURL>& removed_urls) {
153 NotifyHistoryOfRemovedURLs(profile_, removed_urls);