Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Unified Diff: components/bookmarks/managed/managed_bookmark_service.cc

Issue 1233673002: Fix componentization of chrome/browser/bookmarks (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments by tfarina and fix compilation Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/bookmarks/managed/managed_bookmark_service.cc
diff --git a/components/bookmarks/managed/managed_bookmark_service.cc b/components/bookmarks/managed/managed_bookmark_service.cc
new file mode 100644
index 0000000000000000000000000000000000000000..521c383c7a4268b1451e159db48aa01be3c3fc0b
--- /dev/null
+++ b/components/bookmarks/managed/managed_bookmark_service.cc
@@ -0,0 +1,174 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/bookmarks/managed/managed_bookmark_service.h"
+
+#include <stdlib.h>
+#include <vector>
+
+#include "base/bind.h"
+#include "base/callback.h"
+#include "base/logging.h"
+#include "base/strings/string16.h"
+#include "base/values.h"
+#include "components/bookmarks/browser/bookmark_model.h"
+#include "components/bookmarks/browser/bookmark_utils.h"
+#include "components/bookmarks/managed/managed_bookmarks_tracker.h"
+#include "grit/components_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+
+namespace bookmarks {
+namespace {
+
+// Initializes |node| from |initial_bookmarks| and |title_id| and returns it.
+// The ids are assigned starting at |next_node_id| value and the value is
+// updated as a side effect.
+scoped_ptr<BookmarkPermanentNode> LoadNodeInitialContent(
+ scoped_ptr<BookmarkPermanentNode> node,
+ scoped_ptr<base::ListValue> initial_bookmarks,
+ int title_id,
+ int64* next_node_id) {
+ // Load the initial contents of |node| now and assign it an unused id.
+ node->set_id(*next_node_id);
+ *next_node_id = ManagedBookmarksTracker::LoadInitial(
+ node.get(), initial_bookmarks.get(), node->id() + 1);
+ node->set_visible(!node->empty());
+ node->SetTitle(l10n_util::GetStringUTF16(title_id));
+ return node.Pass();
+}
+
+// BookmarkPermanentNodeLoader is a closure that returns initialized
+// BookmarkPermanentNode when passed a mutable pointer to the next node id to
+// use.
+typedef base::Callback<scoped_ptr<BookmarkPermanentNode>(int64*)>
+ BookmarkPermanentNodeLoader;
+
+// Returns a list of initialized BookmarkPermanentNodes using |next_node_id| to
+// start assigning id. |next_node_id| is updated as a side effect of calling
+// this method.
+BookmarkPermanentNodeList LoadExtraNodes(
+ scoped_ptr<std::vector<BookmarkPermanentNodeLoader>> loaders,
+ int64* next_node_id) {
+ BookmarkPermanentNodeList extra_nodes;
+ for (const auto& loader : *loaders.get()) {
sky 2015/07/20 15:53:19 nit: no {}
sdefresne 2015/07/20 17:43:40 Done.
+ extra_nodes.push_back(loader.Run(next_node_id).release());
+ }
+ return extra_nodes.Pass();
+}
+
+} // namespace
+
+ManagedBookmarkService::ManagedBookmarkService(
+ PrefService* prefs,
+ const GetManagementDomainCallback& callback)
+ : prefs_(prefs),
+ bookmark_model_(nullptr),
+ managed_domain_callback_(callback),
+ managed_node_(nullptr),
+ supervised_node_(nullptr) {
+}
+
+ManagedBookmarkService::~ManagedBookmarkService() {
+ DCHECK(!bookmark_model_);
+}
+
+void ManagedBookmarkService::BookmarkModelCreated(
+ BookmarkModel* bookmark_model) {
+ DCHECK(bookmark_model);
+ DCHECK(!bookmark_model_);
+ bookmark_model_ = bookmark_model;
+ bookmark_model_->AddObserver(this);
+
+ managed_bookmarks_tracker_.reset(new ManagedBookmarksTracker(
+ bookmark_model_, prefs_, false /* is_supervised */,
+ managed_domain_callback_));
+
+ GetManagementDomainCallback unbound_callback;
+ supervised_bookmarks_tracker_.reset(new ManagedBookmarksTracker(
+ bookmark_model_, prefs_, true /* is_supervised */, unbound_callback));
+}
+
+LoadExtraCallback ManagedBookmarkService::GetLoadExtraNodesCallback() {
+ // Create two BookmarkPermanentNode with a temporary id of 0. They will be
+ // populated and assigned proper ids in the LoadExtraNodes callback. Until
+ // then, they are owned by the returned closure.
+ scoped_ptr<BookmarkPermanentNode> managed(new BookmarkPermanentNode(0));
+ scoped_ptr<BookmarkPermanentNode> supervised(new BookmarkPermanentNode(0));
+
+ managed_node_ = managed.get();
+ supervised_node_ = supervised.get();
+
+ scoped_ptr<std::vector<BookmarkPermanentNodeLoader>> loaders(
+ new std::vector<BookmarkPermanentNodeLoader>());
+ loaders->push_back(base::Bind(
+ &LoadNodeInitialContent, base::Passed(&managed),
sky 2015/07/20 15:53:20 As you always call LoadNodeInitialContext is there
sdefresne 2015/07/20 17:43:40 I've used objects instead. Is it clearer now?
sky 2015/07/20 20:10:56 Yes, thanks!
+ base::Passed(managed_bookmarks_tracker_->GetInitialManagedBookmarks()),
+ IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME));
+ loaders->push_back(base::Bind(
+ &LoadNodeInitialContent, base::Passed(&supervised),
+ base::Passed(supervised_bookmarks_tracker_->GetInitialManagedBookmarks()),
+ IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME));
+
+ return base::Bind(&LoadExtraNodes, base::Passed(&loaders));
+}
+
+bool ManagedBookmarkService::CanSetPermanentNodeTitle(
+ const BookmarkNode* node) {
+ // |managed_node_| can have its title updated if the user signs in or out,
+ // since the name of the managed domain can appear in it. Also both
+ // |managed_node_| and |supervised_node_| can have their title updated on
+ // locale changes (http://crbug.com/459448).
+ if (node == managed_node_ || node == supervised_node_)
+ return true;
+ return !IsDescendantOf(node, managed_node_) &&
+ !IsDescendantOf(node, supervised_node_);
+}
+
+bool ManagedBookmarkService::CanSyncNode(const BookmarkNode* node) {
+ return !IsDescendantOf(node, managed_node_) &&
+ !IsDescendantOf(node, supervised_node_);
+}
+
+bool ManagedBookmarkService::CanBeEditedByUser(const BookmarkNode* node) {
+ return !IsDescendantOf(node, managed_node_) &&
+ !IsDescendantOf(node, supervised_node_);
+}
+
+void ManagedBookmarkService::Shutdown() {
+ Cleanup();
+}
+
+void ManagedBookmarkService::BookmarkModelChanged() {
+}
+
+void ManagedBookmarkService::BookmarkModelLoaded(BookmarkModel* bookmark_model,
+ bool ids_reassigned) {
+ BaseBookmarkModelObserver::BookmarkModelLoaded(bookmark_model,
+ ids_reassigned);
+ // Start tracking the managed and supervised bookmarks. This will detect any
+ // changes that may have occurred while the initial managed and supervised
+ // bookmarks were being loaded on the background.
+ managed_bookmarks_tracker_->Init(managed_node_);
+ supervised_bookmarks_tracker_->Init(supervised_node_);
+}
+
+void ManagedBookmarkService::BookmarkModelBeingDeleted(
+ BookmarkModel* bookmark_model) {
+ Cleanup();
+}
+
+void ManagedBookmarkService::Cleanup() {
+ if (bookmark_model_) {
+ bookmark_model_->RemoveObserver(this);
+ bookmark_model_ = nullptr;
+ }
+
+ managed_bookmarks_tracker_.reset();
+ supervised_bookmarks_tracker_.reset();
+
+ managed_node_ = nullptr;
+ supervised_node_ = nullptr;
+}
+
+} // namespace bookmarks

Powered by Google App Engine
This is Rietveld 408576698