| 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" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 void NotifyHistoryOfRemovedURLs(Profile* profile, | 35 void NotifyHistoryOfRemovedURLs(Profile* profile, |
| 36 const std::set<GURL>& removed_urls) { | 36 const std::set<GURL>& removed_urls) { |
| 37 HistoryService* history_service = | 37 HistoryService* history_service = |
| 38 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); | 38 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS); |
| 39 if (history_service) | 39 if (history_service) |
| 40 history_service->URLsNoLongerBookmarked(removed_urls); | 40 history_service->URLsNoLongerBookmarked(removed_urls); |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile, bool index_urls) | 45 ChromeBookmarkClient::ChromeBookmarkClient(Profile* profile) |
| 46 : profile_(profile), | 46 : profile_(profile), model_(NULL) { |
| 47 model_(new BookmarkModel(this, index_urls)), | 47 } |
| 48 managed_bookmarks_tracker_( | 48 |
| 49 model_.get(), | 49 ChromeBookmarkClient::~ChromeBookmarkClient() { |
| 50 profile_->GetPrefs(), | 50 } |
| 51 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, | 51 |
| 52 base::Unretained(this))), | 52 void ChromeBookmarkClient::Init(BookmarkModel* model) { |
| 53 managed_node_(NULL) { | 53 DCHECK(model); |
| 54 DCHECK(!model_); |
| 55 model_ = model; |
| 54 model_->AddObserver(this); | 56 model_->AddObserver(this); |
| 57 |
| 58 managed_bookmarks_tracker_.reset(new policy::ManagedBookmarksTracker( |
| 59 model_, |
| 60 profile_->GetPrefs(), |
| 61 base::Bind(&ChromeBookmarkClient::GetManagedBookmarksDomain, |
| 62 base::Unretained(this)))); |
| 63 |
| 55 // Listen for changes to favicons so that we can update the favicon of the | 64 // Listen for changes to favicons so that we can update the favicon of the |
| 56 // node appropriately. | 65 // node appropriately. |
| 57 registrar_.Add(this, | 66 registrar_.Add(this, |
| 58 chrome::NOTIFICATION_FAVICON_CHANGED, | 67 chrome::NOTIFICATION_FAVICON_CHANGED, |
| 59 content::Source<Profile>(profile_)); | 68 content::Source<Profile>(profile_)); |
| 60 } | 69 } |
| 61 | 70 |
| 62 ChromeBookmarkClient::~ChromeBookmarkClient() { | 71 void ChromeBookmarkClient::Shutdown() { |
| 63 model_->RemoveObserver(this); | 72 if (model_) { |
| 73 registrar_.RemoveAll(); |
| 64 | 74 |
| 65 registrar_.RemoveAll(); | 75 model_->RemoveObserver(this); |
| 76 model_ = NULL; |
| 77 } |
| 78 BookmarkClient::Shutdown(); |
| 66 } | 79 } |
| 67 | 80 |
| 68 bool ChromeBookmarkClient::IsDescendantOfManagedNode(const BookmarkNode* node) { | 81 bool ChromeBookmarkClient::IsDescendantOfManagedNode(const BookmarkNode* node) { |
| 69 return node && node->HasAncestor(managed_node_); | 82 return node && node->HasAncestor(managed_node_); |
| 70 } | 83 } |
| 71 | 84 |
| 72 bool ChromeBookmarkClient::HasDescendantsOfManagedNode( | 85 bool ChromeBookmarkClient::HasDescendantsOfManagedNode( |
| 73 const std::vector<const BookmarkNode*>& list) { | 86 const std::vector<const BookmarkNode*>& list) { |
| 74 for (size_t i = 0; i < list.size(); ++i) { | 87 for (size_t i = 0; i < list.size(); ++i) { |
| 75 if (IsDescendantOfManagedNode(list[i])) | 88 if (IsDescendantOfManagedNode(list[i])) |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 | 163 |
| 151 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { | 164 bookmarks::LoadExtraCallback ChromeBookmarkClient::GetLoadExtraNodesCallback() { |
| 152 // Create the managed_node now; it will be populated in the LoadExtraNodes | 165 // Create the managed_node now; it will be populated in the LoadExtraNodes |
| 153 // callback. | 166 // callback. |
| 154 managed_node_ = new BookmarkPermanentNode(0); | 167 managed_node_ = new BookmarkPermanentNode(0); |
| 155 return base::Bind( | 168 return base::Bind( |
| 156 &ChromeBookmarkClient::LoadExtraNodes, | 169 &ChromeBookmarkClient::LoadExtraNodes, |
| 157 StartupTaskRunnerServiceFactory::GetForProfile(profile_) | 170 StartupTaskRunnerServiceFactory::GetForProfile(profile_) |
| 158 ->GetBookmarkTaskRunner(), | 171 ->GetBookmarkTaskRunner(), |
| 159 managed_node_, | 172 managed_node_, |
| 160 base::Passed(managed_bookmarks_tracker_.GetInitialManagedBookmarks())); | 173 base::Passed(managed_bookmarks_tracker_->GetInitialManagedBookmarks())); |
| 161 } | 174 } |
| 162 | 175 |
| 163 bool ChromeBookmarkClient::CanSetPermanentNodeTitle( | 176 bool ChromeBookmarkClient::CanSetPermanentNodeTitle( |
| 164 const BookmarkNode* permanent_node) { | 177 const BookmarkNode* permanent_node) { |
| 165 // The |managed_node_| can have its title updated if the user signs in or | 178 // The |managed_node_| can have its title updated if the user signs in or |
| 166 // out. | 179 // out. |
| 167 return !IsDescendantOfManagedNode(permanent_node) || | 180 return !IsDescendantOfManagedNode(permanent_node) || |
| 168 permanent_node == managed_node_; | 181 permanent_node == managed_node_; |
| 169 } | 182 } |
| 170 | 183 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 186 model_->OnFaviconChanged(favicon_details->urls); | 199 model_->OnFaviconChanged(favicon_details->urls); |
| 187 break; | 200 break; |
| 188 } | 201 } |
| 189 | 202 |
| 190 default: | 203 default: |
| 191 NOTREACHED(); | 204 NOTREACHED(); |
| 192 break; | 205 break; |
| 193 } | 206 } |
| 194 } | 207 } |
| 195 | 208 |
| 196 void ChromeBookmarkClient::Shutdown() { | |
| 197 model_->Shutdown(); | |
| 198 } | |
| 199 | |
| 200 void ChromeBookmarkClient::BookmarkModelChanged() { | 209 void ChromeBookmarkClient::BookmarkModelChanged() { |
| 201 } | 210 } |
| 202 | 211 |
| 203 void ChromeBookmarkClient::BookmarkNodeRemoved( | 212 void ChromeBookmarkClient::BookmarkNodeRemoved( |
| 204 BookmarkModel* model, | 213 BookmarkModel* model, |
| 205 const BookmarkNode* parent, | 214 const BookmarkNode* parent, |
| 206 int old_index, | 215 int old_index, |
| 207 const BookmarkNode* node, | 216 const BookmarkNode* node, |
| 208 const std::set<GURL>& removed_urls) { | 217 const std::set<GURL>& removed_urls) { |
| 209 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 218 NotifyHistoryOfRemovedURLs(profile_, removed_urls); |
| 210 } | 219 } |
| 211 | 220 |
| 212 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( | 221 void ChromeBookmarkClient::BookmarkAllUserNodesRemoved( |
| 213 BookmarkModel* model, | 222 BookmarkModel* model, |
| 214 const std::set<GURL>& removed_urls) { | 223 const std::set<GURL>& removed_urls) { |
| 215 NotifyHistoryOfRemovedURLs(profile_, removed_urls); | 224 NotifyHistoryOfRemovedURLs(profile_, removed_urls); |
| 216 } | 225 } |
| 217 | 226 |
| 218 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, | 227 void ChromeBookmarkClient::BookmarkModelLoaded(BookmarkModel* model, |
| 219 bool ids_reassigned) { | 228 bool ids_reassigned) { |
| 220 // Start tracking the managed bookmarks. This will detect any changes that | 229 // Start tracking the managed bookmarks. This will detect any changes that |
| 221 // may have occurred while the initial managed bookmarks were being loaded | 230 // may have occurred while the initial managed bookmarks were being loaded |
| 222 // on the background. | 231 // on the background. |
| 223 managed_bookmarks_tracker_.Init(managed_node_); | 232 managed_bookmarks_tracker_->Init(managed_node_); |
| 224 } | 233 } |
| 225 | 234 |
| 226 // static | 235 // static |
| 227 bookmarks::BookmarkPermanentNodeList ChromeBookmarkClient::LoadExtraNodes( | 236 bookmarks::BookmarkPermanentNodeList ChromeBookmarkClient::LoadExtraNodes( |
| 228 const scoped_refptr<base::DeferredSequencedTaskRunner>& profile_io_runner, | 237 const scoped_refptr<base::DeferredSequencedTaskRunner>& profile_io_runner, |
| 229 BookmarkPermanentNode* managed_node, | 238 BookmarkPermanentNode* managed_node, |
| 230 scoped_ptr<base::ListValue> initial_managed_bookmarks, | 239 scoped_ptr<base::ListValue> initial_managed_bookmarks, |
| 231 int64* next_node_id) { | 240 int64* next_node_id) { |
| 232 DCHECK(profile_io_runner->RunsTasksOnCurrentThread()); | 241 DCHECK(profile_io_runner->RunsTasksOnCurrentThread()); |
| 233 // Load the initial contents of the |managed_node| now, and assign it an | 242 // Load the initial contents of the |managed_node| now, and assign it an |
| (...skipping 11 matching lines...) Expand all Loading... |
| 245 return extra_nodes.Pass(); | 254 return extra_nodes.Pass(); |
| 246 } | 255 } |
| 247 | 256 |
| 248 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { | 257 std::string ChromeBookmarkClient::GetManagedBookmarksDomain() { |
| 249 policy::ProfilePolicyConnector* connector = | 258 policy::ProfilePolicyConnector* connector = |
| 250 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); | 259 policy::ProfilePolicyConnectorFactory::GetForProfile(profile_); |
| 251 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) | 260 if (connector->IsPolicyFromCloudPolicy(policy::key::kManagedBookmarks)) |
| 252 return connector->GetManagementDomain(); | 261 return connector->GetManagementDomain(); |
| 253 return std::string(); | 262 return std::string(); |
| 254 } | 263 } |
| OLD | NEW |