1 // Copyright 2015 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 "ios/chrome/browser/bookmarks/bookmark_client_impl.h"
7 #include "base/logging.h"
8 #include "base/task/cancelable_task_tracker.h"
9 #include "components/bookmarks/browser/bookmark_node.h"
10 #include "components/bookmarks/browser/bookmark_storage.h"
11 #include "components/bookmarks/managed/managed_bookmark_service.h"
12 #include "components/bookmarks/managed/managed_bookmark_util.h"
13 #include "components/favicon/core/favicon_util.h"
14 #include "components/favicon_base/favicon_types.h"
15 #include "components/history/core/browser/history_service.h"
16 #include "components/history/core/browser/url_database.h"
17 #include "components/keyed_service/core/service_access_type.h"
18 #include "ios/chrome/browser/favicon/favicon_service_factory.h"
19 #include "ios/chrome/browser/history/history_service_factory.h"
20 #include "ios/web/public/user_metrics.h"
22 BookmarkClientImpl::BookmarkClientImpl(
23 ios::ChromeBrowserState
* browser_state
,
24 bookmarks::ManagedBookmarkService
* managed_bookmark_service
)
25 : browser_state_(browser_state
),
26 managed_bookmark_service_(managed_bookmark_service
) {}
28 BookmarkClientImpl::~BookmarkClientImpl() {}
30 void BookmarkClientImpl::Init(bookmarks::BookmarkModel
* bookmark_model
) {
31 if (managed_bookmark_service_
)
32 managed_bookmark_service_
->BookmarkModelCreated(bookmark_model
);
35 void BookmarkClientImpl::Shutdown() {
36 managed_bookmark_service_
= nullptr;
37 bookmarks::BookmarkClient::Shutdown();
40 bool BookmarkClientImpl::PreferTouchIcon() {
44 base::CancelableTaskTracker::TaskId
45 BookmarkClientImpl::GetFaviconImageForPageURL(
47 favicon_base::IconType type
,
48 const favicon_base::FaviconImageCallback
& callback
,
49 base::CancelableTaskTracker
* tracker
) {
50 return favicon::GetFaviconImageForPageURL(
51 ios::FaviconServiceFactory::GetForBrowserState(
52 browser_state_
, ServiceAccessType::EXPLICIT_ACCESS
),
53 page_url
, type
, callback
, tracker
);
56 bool BookmarkClientImpl::SupportsTypedCountForNodes() {
60 void BookmarkClientImpl::GetTypedCountForNodes(
62 NodeTypedCountPairs
* node_typed_count_pairs
) {
63 history::HistoryService
* history_service
=
64 ios::HistoryServiceFactory::GetForBrowserState(
65 browser_state_
, ServiceAccessType::EXPLICIT_ACCESS
);
66 history::URLDatabase
* url_db
=
67 history_service
? history_service
->InMemoryDatabase() : nullptr;
68 for (const auto& node
: nodes
) {
69 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but
70 // it guarantees to contain those with |typed_count| > 0. Thus, if fetching
71 // the URLRow fails, it is safe to assume that its |typed_count| is 0.
74 if (url_db
&& url_db
->GetRowForURL(node
->url(), &url
))
75 typed_count
= url
.typed_count();
77 NodeTypedCountPair
pair(node
, typed_count
);
78 node_typed_count_pairs
->push_back(pair
);
82 bool BookmarkClientImpl::IsPermanentNodeVisible(
83 const bookmarks::BookmarkPermanentNode
* node
) {
84 DCHECK(bookmarks::IsPermanentNode(node
, managed_bookmark_service_
));
85 if (bookmarks::IsManagedNode(node
, managed_bookmark_service_
))
87 return node
->type() == bookmarks::BookmarkNode::MOBILE
;
90 void BookmarkClientImpl::RecordAction(const base::UserMetricsAction
& action
) {
91 web::RecordAction(action
);
94 bookmarks::LoadExtraCallback
BookmarkClientImpl::GetLoadExtraNodesCallback() {
95 if (!managed_bookmark_service_
)
96 return bookmarks::LoadExtraCallback();
98 return managed_bookmark_service_
->GetLoadExtraNodesCallback();
101 bool BookmarkClientImpl::CanSetPermanentNodeTitle(
102 const bookmarks::BookmarkNode
* permanent_node
) {
103 return !managed_bookmark_service_
105 : managed_bookmark_service_
->CanSetPermanentNodeTitle(
109 bool BookmarkClientImpl::CanSyncNode(const bookmarks::BookmarkNode
* node
) {
110 return !managed_bookmark_service_
112 : managed_bookmark_service_
->CanSyncNode(node
);
115 bool BookmarkClientImpl::CanBeEditedByUser(
116 const bookmarks::BookmarkNode
* node
) {
117 return !managed_bookmark_service_
119 : managed_bookmark_service_
->CanBeEditedByUser(node
);