OLD | NEW |
(Empty) | |
| 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/bookmarks/chrome_bookmark_client.h" |
| 6 |
| 7 #include "chrome/browser/bookmarks/bookmark_model.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 "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/user_metrics.h" |
| 19 |
| 20 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile, bool index_urls) |
| 21 : profile_(profile), |
| 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. |
| 25 registrar_.Add(this, |
| 26 chrome::NOTIFICATION_FAVICON_CHANGED, |
| 27 content::Source<Profile>(profile_)); |
| 28 } |
| 29 |
| 30 ChromeBookmarkClient::~ChromeBookmarkClient() { |
| 31 registrar_.RemoveAll(); |
| 32 } |
| 33 |
| 34 base::CancelableTaskTracker::TaskId ChromeBookmarkClient::GetFaviconImageForURL( |
| 35 const GURL& page_url, |
| 36 int icon_types, |
| 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); |
| 42 if (!favicon_service) |
| 43 return base::CancelableTaskTracker::kBadTaskId; |
| 44 return favicon_service->GetFaviconImageForURL( |
| 45 FaviconService::FaviconForURLParams( |
| 46 page_url, icon_types, desired_size_in_dip), |
| 47 callback, |
| 48 tracker); |
| 49 } |
| 50 |
| 51 bool ChromeBookmarkClient::SupportsTypedCountForNodes() { |
| 52 return true; |
| 53 } |
| 54 |
| 55 void ChromeBookmarkClient::GetTypedCountForNodes( |
| 56 const NodeSet& nodes, |
| 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) { |
| 63 int typed_count = 0; |
| 64 |
| 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. |
| 68 history::URLRow url; |
| 69 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) |
| 70 typed_count = url.typed_count(); |
| 71 |
| 72 NodeTypedCountPair pair(*i, typed_count); |
| 73 node_typed_count_pairs->push_back(pair); |
| 74 } |
| 75 } |
| 76 |
| 77 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { |
| 78 content::RecordAction(action); |
| 79 } |
| 80 |
| 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. |
| 85 return; |
| 86 } |
| 87 |
| 88 HistoryService* history_service = |
| 89 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
| 90 if (history_service) |
| 91 history_service->URLsNoLongerBookmarked(removed_bookmark_urls); |
| 92 } |
| 93 |
| 94 void ChromeBookmarkClient::Observe( |
| 95 int type, |
| 96 const content::NotificationSource& source, |
| 97 const content::NotificationDetails& details) { |
| 98 switch (type) { |
| 99 case chrome::NOTIFICATION_FAVICON_CHANGED: { |
| 100 content::Details<FaviconChangedDetails> favicon_details(details); |
| 101 model_->OnFaviconChanged(favicon_details->urls); |
| 102 break; |
| 103 } |
| 104 |
| 105 default: |
| 106 NOTREACHED(); |
| 107 break; |
| 108 } |
| 109 } |
| 110 |
| 111 void ChromeBookmarkClient::Shutdown() { |
| 112 model_->Shutdown(); |
| 113 } |
OLD | NEW |