OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" | 5 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/favicon/favicon_changed_details.h" | |
13 #include "chrome/browser/favicon/favicon_service.h" | 11 #include "chrome/browser/favicon/favicon_service.h" |
14 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
15 #include "chrome/browser/history/history_service.h" | 13 #include "chrome/browser/history/history_service.h" |
16 #include "chrome/browser/history/history_service_factory.h" | |
17 #include "chrome/browser/policy/profile_policy_connector.h" | 14 #include "chrome/browser/policy/profile_policy_connector.h" |
18 #include "chrome/browser/policy/profile_policy_connector_factory.h" | 15 #include "chrome/browser/policy/profile_policy_connector_factory.h" |
19 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
20 #include "components/bookmarks/browser/bookmark_model.h" | 17 #include "components/bookmarks/browser/bookmark_model.h" |
21 #include "components/bookmarks/browser/bookmark_node.h" | 18 #include "components/bookmarks/browser/bookmark_node.h" |
22 #include "components/history/core/browser/url_database.h" | 19 #include "components/history/core/browser/url_database.h" |
23 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
26 #include "content/public/browser/user_metrics.h" | 23 #include "content/public/browser/user_metrics.h" |
27 #include "grit/components_strings.h" | 24 #include "grit/components_strings.h" |
28 #include "policy/policy_constants.h" | 25 #include "policy/policy_constants.h" |
29 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
30 | 27 |
31 namespace { | 28 namespace { |
32 | 29 |
33 void NotifyHistoryOfRemovedURLs(Profile* profile, | |
34 const std::set<GURL>& removed_urls) { | |
35 HistoryService* history_service = | |
36 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | |
37 if (history_service) | |
38 history_service->URLsNoLongerBookmarked(removed_urls); | |
39 } | |
40 | |
41 void RunCallbackWithImage( | 30 void RunCallbackWithImage( |
42 const favicon_base::FaviconImageCallback& callback, | 31 const favicon_base::FaviconImageCallback& callback, |
43 const favicon_base::FaviconRawBitmapResult& bitmap_result) { | 32 const favicon_base::FaviconRawBitmapResult& bitmap_result) { |
44 favicon_base::FaviconImageResult result; | 33 favicon_base::FaviconImageResult result; |
45 if (bitmap_result.is_valid()) { | 34 if (bitmap_result.is_valid()) { |
46 result.image = gfx::Image::CreateFrom1xPNGBytes( | 35 result.image = gfx::Image::CreateFrom1xPNGBytes( |
47 bitmap_result.bitmap_data->front(), bitmap_result.bitmap_data->size()); | 36 bitmap_result.bitmap_data->front(), bitmap_result.bitmap_data->size()); |
48 result.icon_url = bitmap_result.icon_url; | 37 result.icon_url = bitmap_result.icon_url; |
49 callback.Run(result); | 38 callback.Run(result); |
50 return; | 39 return; |
51 } | 40 } |
52 callback.Run(result); | 41 callback.Run(result); |
53 } | 42 } |
54 | 43 |
55 } // namespace | 44 } // namespace |
56 | 45 |
57 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile) | 46 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile) |
58 : profile_(profile), model_(NULL), managed_node_(NULL) { | 47 : profile_(profile), |
| 48 history_service_(NULL), |
| 49 model_(NULL), |
| 50 managed_node_(NULL) { |
59 } | 51 } |
60 | 52 |
61 ChromeBookmarkClient::~ChromeBookmarkClient() { | 53 ChromeBookmarkClient::~ChromeBookmarkClient() { |
62 } | 54 } |
63 | 55 |
64 void ChromeBookmarkClient::Init(BookmarkModel* model) { | 56 void ChromeBookmarkClient::Init(BookmarkModel* model) { |
65 DCHECK(model); | 57 DCHECK(model); |
66 DCHECK(!model_); | 58 DCHECK(!model_); |
67 model_ = model; | 59 model_ = model; |
68 model_->AddObserver(this); | 60 model_->AddObserver(this); |
69 | 61 |
70 managed_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( | 62 managed_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( |
71 model_, | 63 model_, |
72 profile_->GetPrefs(), | 64 profile_->GetPrefs(), |
73 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, | 65 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, |
74 base::Unretained(this)))); | 66 base::Unretained(this)))); |
75 | |
76 // Listen for changes to favicons so that we can update the favicon of the | |
77 // node appropriately. | |
78 registrar_.Add(this, | |
79 chrome::NOTIFICATION_FAVICON_CHANGED, | |
80 content::Source<Profile>(profile_)); | |
81 } | 67 } |
82 | 68 |
83 void ChromeBookmarkClient::Shutdown() { | 69 void ChromeBookmarkClient::Shutdown() { |
| 70 favicon_changed_subscription_.reset(); |
84 if (model_) { | 71 if (model_) { |
85 registrar_.RemoveAll(); | |
86 | |
87 model_->RemoveObserver(this); | 72 model_->RemoveObserver(this); |
88 model_ = NULL; | 73 model_ = NULL; |
89 } | 74 } |
90 BookmarkClient::Shutdown(); | 75 BookmarkClient::Shutdown(); |
91 } | 76 } |
92 | 77 |
93 bool ChromeBookmarkClient::IsDescendantOfManagedNode(const BookmarkNode* node) { | 78 bool ChromeBookmarkClient::IsDescendantOfManagedNode(const BookmarkNode* node) { |
94 return node && node->HasAncestor(managed_node_); | 79 return node && node->HasAncestor(managed_node_); |
95 } | 80 } |
96 | 81 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 } | 119 } |
135 } | 120 } |
136 | 121 |
137 bool ChromeBookmarkClient::SupportsTypedCountForNodes() { | 122 bool ChromeBookmarkClient::SupportsTypedCountForNodes() { |
138 return true; | 123 return true; |
139 } | 124 } |
140 | 125 |
141 void ChromeBookmarkClient::GetTypedCountForNodes( | 126 void ChromeBookmarkClient::GetTypedCountForNodes( |
142 const NodeSet& nodes, | 127 const NodeSet& nodes, |
143 NodeTypedCountPairs* node_typed_count_pairs) { | 128 NodeTypedCountPairs* node_typed_count_pairs) { |
144 HistoryService* history_service = | |
145 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | |
146 history::URLDatabase* url_db = | 129 history::URLDatabase* url_db = |
147 history_service ? history_service->InMemoryDatabase() : NULL; | 130 history_service_ ? history_service_->InMemoryDatabase() : NULL; |
148 for (NodeSet::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { | 131 for (NodeSet::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { |
149 int typed_count = 0; | 132 int typed_count = 0; |
150 | 133 |
151 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but | 134 // If |url_db| is the InMemoryDatabase, it might not cache all URLRows, but |
152 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot | 135 // it guarantees to contain those with |typed_count| > 0. Thus, if we cannot |
153 // fetch the URLRow, it is safe to assume that its |typed_count| is 0. | 136 // fetch the URLRow, it is safe to assume that its |typed_count| is 0. |
154 history::URLRow url; | 137 history::URLRow url; |
155 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) | 138 if (url_db && url_db->GetRowForURL((*i)->url(), &url)) |
156 typed_count = url.typed_count(); | 139 typed_count = url.typed_count(); |
157 | 140 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 } | 185 } |
203 | 186 |
204 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { | 187 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { |
205 return !IsDescendantOfManagedNode(node); | 188 return !IsDescendantOfManagedNode(node); |
206 } | 189 } |
207 | 190 |
208 bool ChromeBookmarkClient::CanBeEditedByUser(const BookmarkNode* node) { | 191 bool ChromeBookmarkClient::CanBeEditedByUser(const BookmarkNode* node) { |
209 return !IsDescendantOfManagedNode(node); | 192 return !IsDescendantOfManagedNode(node); |
210 } | 193 } |
211 | 194 |
212 void ChromeBookmarkClient::Observe( | 195 void ChromeBookmarkClient::SetHistoryService(HistoryService* history_service) { |
213 int type, | 196 DCHECK(history_service); |
214 const content::NotificationSource& source, | 197 history_service_ = history_service; |
215 const content::NotificationDetails& details) { | 198 favicon_changed_subscription_ = history_service_->AddFaviconChangedCallback( |
216 switch (type) { | 199 base::Bind(&BookmarkModel::OnFaviconChanged, base::Unretained(model_))); |
217 case chrome::NOTIFICATION_FAVICON_CHANGED: { | |
218 content::Details<FaviconChangedDetails> favicon_details(details); | |
219 model_->OnFaviconChanged(favicon_details->urls); | |
220 break; | |
221 } | |
222 | |
223 default: | |
224 NOTREACHED(); | |
225 break; | |
226 } | |
227 } | 200 } |
228 | 201 |
229 void ChromeBookmarkClient::BookmarkModelChanged() { | 202 void ChromeBookmarkClient::BookmarkModelChanged() { |
230 } | 203 } |
231 | 204 |
232 void ChromeBookmarkClient::BookmarkNodeRemoved( | 205 void ChromeBookmarkClient::BookmarkNodeRemoved( |
233 BookmarkModel* model, | 206 BookmarkModel* model, |
234 const BookmarkNode* parent, | 207 const BookmarkNode* parent, |
235 int old_index, | 208 int old_index, |
236 const BookmarkNode* node, | 209 const BookmarkNode* node, |
237 const std::set<GURL>& removed_urls) { | 210 const std::set<GURL>& removed_urls) { |
238 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 211 if (history_service_) |
| 212 history_service_->URLsNoLongerBookmarked(removed_urls); |
239 } | 213 } |
240 | 214 |
241 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( | 215 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( |
242 BookmarkModel* model, | 216 BookmarkModel* model, |
243 const std::set<GURL>& removed_urls) { | 217 const std::set<GURL>& removed_urls) { |
244 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 218 if (history_service_) |
| 219 history_service_->URLsNoLongerBookmarked(removed_urls); |
245 } | 220 } |
246 | 221 |
247 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, | 222 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, |
248 bool ids_reassigned) { | 223 bool ids_reassigned) { |
249 // Start tracking the managed bookmarks. This will detect any changes that | 224 // Start tracking the managed bookmarks. This will detect any changes that |
250 // may have occurred while the initial managed bookmarks were being loaded | 225 // may have occurred while the initial managed bookmarks were being loaded |
251 // on the background. | 226 // on the background. |
252 managed_bookmarks_tracker_->Init(managed_node_); | 227 managed_bookmarks_tracker_->Init(managed_node_); |
253 } | 228 } |
254 | 229 |
(...skipping 19 matching lines...) Expand all Loading... |
274 return extra_nodes.Pass(); | 249 return extra_nodes.Pass(); |
275 } | 250 } |
276 | 251 |
277 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { | 252 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { |
278 policy::ProfilePolicyConnector* connector = | 253 policy::ProfilePolicyConnector* connector = |
279 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 254 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
280 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) | 255 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) |
281 return connector->GetManagementDomain(); | 256 return connector->GetManagementDomain(); |
282 return std::string(); | 257 return std::string(); |
283 } | 258 } |
OLD | NEW |