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"
24 void NotifyHistoryOfRemovedURLs(Profile
* profile
,
25 const std::set
<GURL
>& removed_urls
) {
26 HistoryService
* history_service
=
27 HistoryServiceFactory::GetForProfile(profile
, Profile::EXPLICIT_ACCESS
);
29 history_service
->URLsNoLongerBookmarked(removed_urls
);
34 ChromeBookmarkClient::ChromeBookmarkClient(Profile
* profile
, bool index_urls
)
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.
41 chrome::NOTIFICATION_FAVICON_CHANGED
,
42 content::Source
<Profile
>(profile_
));
45 ChromeBookmarkClient::~ChromeBookmarkClient() {
46 model_
->RemoveObserver(this);
48 registrar_
.RemoveAll();
51 bool ChromeBookmarkClient::PreferTouchIcon() {
59 base::CancelableTaskTracker::TaskId
ChromeBookmarkClient::GetFaviconImageForURL(
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
);
68 return base::CancelableTaskTracker::kBadTaskId
;
69 return favicon_service
->GetFaviconImageForURL(
70 FaviconService::FaviconForURLParams(
71 page_url
, icon_types
, desired_size_in_dip
),
76 bool ChromeBookmarkClient::SupportsTypedCountForNodes() {
80 void ChromeBookmarkClient::GetTypedCountForNodes(
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
) {
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.
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
);
111 return node_type
!= BookmarkNode::MOBILE
;
113 return node_type
== BookmarkNode::MOBILE
;
117 void ChromeBookmarkClient::Observe(
119 const content::NotificationSource
& source
,
120 const content::NotificationDetails
& details
) {
122 case chrome::NOTIFICATION_FAVICON_CHANGED
: {
123 content::Details
<FaviconChangedDetails
> favicon_details(details
);
124 model_
->OnFaviconChanged(favicon_details
->urls
);
134 void ChromeBookmarkClient::Shutdown() {
138 void ChromeBookmarkClient::BookmarkModelChanged() {
141 void ChromeBookmarkClient::BookmarkNodeRemoved(
142 BookmarkModel
* model
,
143 const BookmarkNode
* parent
,
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
);