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/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | |
8 #include "chrome/browser/chrome_notification_types.h" | 9 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/favicon/favicon_changed_details.h" | 10 #include "chrome/browser/favicon/favicon_changed_details.h" |
10 #include "chrome/browser/favicon/favicon_service.h" | 11 #include "chrome/browser/favicon/favicon_service.h" |
11 #include "chrome/browser/favicon/favicon_service_factory.h" | 12 #include "chrome/browser/favicon/favicon_service_factory.h" |
12 #include "chrome/browser/history/history_service.h" | 13 #include "chrome/browser/history/history_service.h" |
13 #include "chrome/browser/history/history_service_factory.h" | 14 #include "chrome/browser/history/history_service_factory.h" |
14 #include "chrome/browser/history/url_database.h" | 15 #include "chrome/browser/history/url_database.h" |
16 #include "chrome/browser/policy/profile_policy_connector.h" | |
17 #include "chrome/browser/policy/profile_policy_connector_factory.h" | |
15 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
16 #include "components/bookmarks/browser/bookmark_model.h" | 19 #include "components/bookmarks/browser/bookmark_model.h" |
17 #include "components/bookmarks/browser/bookmark_node.h" | 20 #include "components/bookmarks/browser/bookmark_node.h" |
18 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
19 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
20 #include "content/public/browser/user_metrics.h" | 23 #include "content/public/browser/user_metrics.h" |
24 #include "grit/components_strings.h" | |
25 #include "policy/policy_constants.h" | |
26 #include "ui/base/l10n/l10n_util.h" | |
21 | 27 |
22 namespace { | 28 namespace { |
23 | 29 |
24 void NotifyHistoryOfRemovedURLs(Profile* profile, | 30 void NotifyHistoryOfRemovedURLs(Profile* profile, |
25 const std::set<GURL>& removed_urls) { | 31 const std::set<GURL>& removed_urls) { |
26 HistoryService* history_service = | 32 HistoryService* history_service = |
27 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 33 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
28 if (history_service) | 34 if (history_service) |
29 history_service->URLsNoLongerBookmarked(removed_urls); | 35 history_service->URLsNoLongerBookmarked(removed_urls); |
30 } | 36 } |
31 | 37 |
32 } // namespace | 38 } // namespace |
33 | 39 |
34 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile, bool index_urls) | 40 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile, bool index_urls) |
35 : profile_(profile), | 41 : profile_(profile), |
36 model_(new BookmarkModel(this, index_urls)) { | 42 model_(new BookmarkModel(this, index_urls)), |
43 managed_bookmarks_tracker_( | |
44 model_.get(), | |
45 profile_->GetPrefs(), | |
46 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, | |
47 base::Unretained(this))), | |
48 managed_node_(NULL) { | |
37 model_->AddObserver(this); | 49 model_->AddObserver(this); |
38 // Listen for changes to favicons so that we can update the favicon of the | 50 // Listen for changes to favicons so that we can update the favicon of the |
39 // node appropriately. | 51 // node appropriately. |
40 registrar_.Add(this, | 52 registrar_.Add(this, |
41 chrome::NOTIFICATION_FAVICON_CHANGED, | 53 chrome::NOTIFICATION_FAVICON_CHANGED, |
42 content::Source<Profile>(profile_)); | 54 content::Source<Profile>(profile_)); |
43 } | 55 } |
44 | 56 |
45 ChromeBookmarkClient::~ChromeBookmarkClient() { | 57 ChromeBookmarkClient::~ChromeBookmarkClient() { |
46 model_->RemoveObserver(this); | 58 model_->RemoveObserver(this); |
47 | 59 |
48 registrar_.RemoveAll(); | 60 registrar_.RemoveAll(); |
49 } | 61 } |
50 | 62 |
63 bool ChromeBookmarkClient::IsAManagedNode(const BookmarkNode* node) { | |
64 while (node && node != managed_node_) | |
sky
2014/06/04 20:39:52
return node->HasAncestor(managed_node_);
Joao da Silva
2014/06/04 22:35:58
Done.
| |
65 node = node->parent(); | |
66 return node == managed_node_; | |
67 } | |
68 | |
51 bool ChromeBookmarkClient::PreferTouchIcon() { | 69 bool ChromeBookmarkClient::PreferTouchIcon() { |
52 #if !defined(OS_IOS) | 70 #if !defined(OS_IOS) |
53 return false; | 71 return false; |
54 #else | 72 #else |
55 return true; | 73 return true; |
56 #endif | 74 #endif |
57 } | 75 } |
58 | 76 |
59 base::CancelableTaskTracker::TaskId ChromeBookmarkClient::GetFaviconImageForURL( | 77 base::CancelableTaskTracker::TaskId ChromeBookmarkClient::GetFaviconImageForURL( |
60 const GURL& page_url, | 78 const GURL& page_url, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 | 114 |
97 NodeTypedCountPair pair(*i, typed_count); | 115 NodeTypedCountPair pair(*i, typed_count); |
98 node_typed_count_pairs->push_back(pair); | 116 node_typed_count_pairs->push_back(pair); |
99 } | 117 } |
100 } | 118 } |
101 | 119 |
102 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { | 120 void ChromeBookmarkClient::RecordAction(const base::UserMetricsAction& action) { |
103 content::RecordAction(action); | 121 content::RecordAction(action); |
104 } | 122 } |
105 | 123 |
106 bool ChromeBookmarkClient::IsPermanentNodeVisible(int node_type) { | 124 bool ChromeBookmarkClient::IsPermanentNodeVisible(const BookmarkNode* node) { |
107 DCHECK(node_type == BookmarkNode::BOOKMARK_BAR || | 125 DCHECK(node->type() == BookmarkNode::BOOKMARK_BAR || |
108 node_type == BookmarkNode::OTHER_NODE || | 126 node->type() == BookmarkNode::OTHER_NODE || |
109 node_type == BookmarkNode::MOBILE); | 127 node->type() == BookmarkNode::MOBILE || |
128 node == managed_node_); | |
129 if (node == managed_node_) | |
130 return false; | |
110 #if !defined(OS_IOS) | 131 #if !defined(OS_IOS) |
111 return node_type != BookmarkNode::MOBILE; | 132 return node->type() != BookmarkNode::MOBILE; |
112 #else | 133 #else |
113 return node_type == BookmarkNode::MOBILE; | 134 return node->type() == BookmarkNode::MOBILE; |
114 #endif | 135 #endif |
115 } | 136 } |
116 | 137 |
138 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { | |
139 return base::Bind( | |
140 &ChromeBookmarkClient::LoadExtraNodes, | |
141 base::Passed(managed_bookmarks_tracker_.GetInitialManagedBookmarks())); | |
142 } | |
143 | |
144 void ChromeBookmarkClient::ExtraNodesLoaded( | |
145 const std::vector<BookmarkPermanentNode*>& extra_nodes) { | |
146 CHECK_EQ(1u, extra_nodes.size()); | |
147 managed_node_ = extra_nodes[0]; | |
148 } | |
149 | |
150 bool ChromeBookmarkClient::CanRemoveNode(const BookmarkNode* node) { | |
151 return !IsAManagedNode(node); | |
152 } | |
153 | |
154 bool ChromeBookmarkClient::CanSetTitle(const BookmarkNode* node) { | |
155 // The |managed_node_| can have its title updated if the user signs in or | |
156 // out. | |
157 return !IsAManagedNode(node) || node == managed_node_; | |
158 } | |
159 | |
160 bool ChromeBookmarkClient::CanSyncNode(const BookmarkNode* node) { | |
161 return !IsAManagedNode(node); | |
162 } | |
163 | |
164 bool ChromeBookmarkClient::CanReorderChildren(const BookmarkNode* parent) { | |
165 return !IsAManagedNode(parent); | |
166 } | |
167 | |
117 void ChromeBookmarkClient::Observe( | 168 void ChromeBookmarkClient::Observe( |
118 int type, | 169 int type, |
119 const content::NotificationSource& source, | 170 const content::NotificationSource& source, |
120 const content::NotificationDetails& details) { | 171 const content::NotificationDetails& details) { |
121 switch (type) { | 172 switch (type) { |
122 case chrome::NOTIFICATION_FAVICON_CHANGED: { | 173 case chrome::NOTIFICATION_FAVICON_CHANGED: { |
123 content::Details<FaviconChangedDetails> favicon_details(details); | 174 content::Details<FaviconChangedDetails> favicon_details(details); |
124 model_->OnFaviconChanged(favicon_details->urls); | 175 model_->OnFaviconChanged(favicon_details->urls); |
125 break; | 176 break; |
126 } | 177 } |
(...skipping 18 matching lines...) Expand all Loading... | |
145 const BookmarkNode* node, | 196 const BookmarkNode* node, |
146 const std::set<GURL>& removed_urls) { | 197 const std::set<GURL>& removed_urls) { |
147 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 198 NotifyHistoryOfRemovedURLs(profile_, removed_urls); |
148 } | 199 } |
149 | 200 |
150 void ChromeBookmarkClient::BookmarkAllNodesRemoved( | 201 void ChromeBookmarkClient::BookmarkAllNodesRemoved( |
151 BookmarkModel* model, | 202 BookmarkModel* model, |
152 const std::set<GURL>& removed_urls) { | 203 const std::set<GURL>& removed_urls) { |
153 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 204 NotifyHistoryOfRemovedURLs(profile_, removed_urls); |
154 } | 205 } |
206 | |
207 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, | |
208 bool ids_reassigned) { | |
209 // Start tracking the managed bookmarks. This will detect any changes that | |
210 // may have occurred while the initial managed bookmarks were being loaded | |
211 // on the background. | |
212 managed_bookmarks_tracker_.Init(managed_node_); | |
213 } | |
214 | |
215 // static | |
216 bookmarks::BookmarkPermanentNodeList ChromeBookmarkClient::LoadExtraNodes( | |
217 scoped_ptr<base::ListValue> initial_managed_bookmarks, | |
218 int64* next_node_id) { | |
219 // Create the managed node now, and load its initial contents. | |
sky
2014/06/04 20:39:52
DCHECK not on UI thread.
Joao da Silva
2014/06/04 22:35:58
That fails in around 100 unit tests in ~10 suites,
| |
220 int64 managed_id = *next_node_id; | |
221 BookmarkPermanentNode* managed_node = new BookmarkPermanentNode(managed_id); | |
222 *next_node_id = policy::ManagedBookmarksTracker::LoadInitial( | |
223 managed_node, initial_managed_bookmarks.get(), managed_id + 1); | |
224 managed_node->set_visible(!managed_node->empty()); | |
225 managed_node->SetTitle( | |
226 l10n_util::GetStringUTF16(IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME)); | |
227 | |
228 bookmarks::BookmarkPermanentNodeList extra_nodes; | |
229 extra_nodes.push_back(managed_node); | |
230 return extra_nodes.Pass(); | |
231 } | |
232 | |
233 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { | |
234 policy::ProfilePolicyConnector* connector = | |
235 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | |
236 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) | |
237 return connector->GetManagementDomain(); | |
238 return std::string(); | |
239 } | |
OLD | NEW |