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 "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/favicon/favicon_changed_details.h"
9 #include "chrome/browser/favicon/favicon_service.h"
10 #include "chrome/browser/favicon/favicon_service_factory.h"
11 #include "chrome/browser/history/history_service.h"
12 #include "chrome/browser/history/history_service_factory.h"
13 #include "chrome/browser/history/url_database.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "components/bookmarks/core/browser/bookmark_model.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_source.h"
18 #include "content/public/browser/user_metrics.h"
20 ChromeBookmarkClient::ChromeBookmarkClient(Profile
* profile
, bool index_urls
)
22 model_(new BookmarkModel(this, index_urls
)) {
23 // Listen for changes to favicons so that we can update the favicon of the
24 // node appropriately.
26 chrome::NOTIFICATION_FAVICON_CHANGED
,
27 content::Source
<Profile
>(profile_
));
30 ChromeBookmarkClient::~ChromeBookmarkClient() {
31 registrar_
.RemoveAll();
34 base::CancelableTaskTracker::TaskId
ChromeBookmarkClient::GetFaviconImageForURL(
37 int desired_size_in_dip
,
38 const FaviconImageCallback
& callback
,
39 base::CancelableTaskTracker
* tracker
) {
40 FaviconService
* favicon_service
=
41 FaviconServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
43 return base::CancelableTaskTracker::kBadTaskId
;
44 return favicon_service
->GetFaviconImageForURL(
45 FaviconService::FaviconForURLParams(
46 page_url
, icon_types
, desired_size_in_dip
),
51 bool ChromeBookmarkClient::SupportsTypedCountForNodes() {
55 void ChromeBookmarkClient::GetTypedCountForNodes(
57 NodeTypedCountPairs
* node_typed_count_pairs
) {
58 HistoryService
* history_service
=
59 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
60 history::URLDatabase
* url_db
=
61 history_service
? history_service
->InMemoryDatabase() : NULL
;
62 for (NodeSet::const_iterator i
= nodes
.begin(); i
!= nodes
.end(); ++i
) {
65 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but
66 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot
67 // fetch the URLRow, it is safe to assume that its |typed_count| is 0.
69 if (url_db
&& url_db
->GetRowForURL((*i
)->url(), &url
))
70 typed_count
= url
.typed_count();
72 NodeTypedCountPair
pair(*i
, typed_count
);
73 node_typed_count_pairs
->push_back(pair
);
77 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction
& action
) {
78 content::RecordAction(action
);
81 void ChromeBookmarkClient::NotifyHistoryAboutRemovedBookmarks(
82 const std::set
<GURL
>& removed_bookmark_urls
) {
83 if (removed_bookmark_urls
.empty()) {
84 // No point in sending out notification if the starred state didn't change.
88 HistoryService
* history_service
=
89 HistoryServiceFactory::GetForProfile(profile_
, Profile::EXPLICIT_ACCESS
);
91 history_service
->URLsNoLongerBookmarked(removed_bookmark_urls
);
94 void ChromeBookmarkClient::Observe(
96 const content::NotificationSource
& source
,
97 const content::NotificationDetails
& details
) {
99 case chrome::NOTIFICATION_FAVICON_CHANGED
: {
100 content::Details
<FaviconChangedDetails
> favicon_details(details
);
101 model_
->OnFaviconChanged(favicon_details
->urls
);
111 void ChromeBookmarkClient::Shutdown() {