Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / bookmarks / managed / managed_bookmark_service.cc
blobacf63ad4e76761e925149e30701ccf460cfe3960
1 // Copyright 2015 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.
5 #include "components/bookmarks/managed/managed_bookmark_service.h"
7 #include <stdlib.h>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/string16.h"
16 #include "base/values.h"
17 #include "components/bookmarks/browser/bookmark_model.h"
18 #include "components/bookmarks/browser/bookmark_utils.h"
19 #include "components/bookmarks/managed/managed_bookmarks_tracker.h"
20 #include "grit/components_strings.h"
21 #include "ui/base/l10n/l10n_util.h"
23 namespace bookmarks {
24 namespace {
26 // BookmarkPermanentNodeLoader initializes a BookmarkPermanentNode from a JSON
27 // representation, title id and starting node id.
28 class BookmarkPermanentNodeLoader {
29 public:
30 BookmarkPermanentNodeLoader(scoped_ptr<BookmarkPermanentNode> node,
31 scoped_ptr<base::ListValue> initial_bookmarks,
32 int title_id)
33 : node_(node.Pass()),
34 initial_bookmarks_(initial_bookmarks.Pass()),
35 title_id_(title_id) {
36 DCHECK(node_);
39 ~BookmarkPermanentNodeLoader() {}
41 // Initializes |node_| from |initial_bookmarks_| and |title_id_| and returns
42 // it. The ids are assigned starting at |next_node_id| and the value is
43 // updated as a side-effect.
44 scoped_ptr<BookmarkPermanentNode> Load(int64* next_node_id) {
45 node_->set_id(*next_node_id);
46 *next_node_id = ManagedBookmarksTracker::LoadInitial(
47 node_.get(), initial_bookmarks_.get(), node_->id() + 1);
48 node_->set_visible(!node_->empty());
49 node_->SetTitle(l10n_util::GetStringUTF16(title_id_));
50 return node_.Pass();
53 private:
54 scoped_ptr<BookmarkPermanentNode> node_;
55 scoped_ptr<base::ListValue> initial_bookmarks_;
56 int title_id_;
58 DISALLOW_COPY_AND_ASSIGN(BookmarkPermanentNodeLoader);
61 // Returns a list of initialized BookmarkPermanentNodes using |next_node_id| to
62 // start assigning id. |next_node_id| is updated as a side effect of calling
63 // this method.
64 BookmarkPermanentNodeList LoadExtraNodes(
65 ScopedVector<BookmarkPermanentNodeLoader> loaders,
66 int64* next_node_id) {
67 BookmarkPermanentNodeList extra_nodes;
68 for (const auto& loader : loaders)
69 extra_nodes.push_back(loader->Load(next_node_id).release());
70 return extra_nodes.Pass();
73 } // namespace
75 ManagedBookmarkService::ManagedBookmarkService(
76 PrefService* prefs,
77 const GetManagementDomainCallback& callback)
78 : prefs_(prefs),
79 bookmark_model_(nullptr),
80 managed_domain_callback_(callback),
81 managed_node_(nullptr),
82 supervised_node_(nullptr) {}
84 ManagedBookmarkService::~ManagedBookmarkService() {
85 DCHECK(!bookmark_model_);
88 void ManagedBookmarkService::BookmarkModelCreated(
89 BookmarkModel* bookmark_model) {
90 DCHECK(bookmark_model);
91 DCHECK(!bookmark_model_);
92 bookmark_model_ = bookmark_model;
93 bookmark_model_->AddObserver(this);
95 managed_bookmarks_tracker_.reset(new ManagedBookmarksTracker(
96 bookmark_model_, prefs_, false /* is_supervised */,
97 managed_domain_callback_));
99 GetManagementDomainCallback unbound_callback;
100 supervised_bookmarks_tracker_.reset(new ManagedBookmarksTracker(
101 bookmark_model_, prefs_, true /* is_supervised */, unbound_callback));
104 LoadExtraCallback ManagedBookmarkService::GetLoadExtraNodesCallback() {
105 // Create two BookmarkPermanentNode with a temporary id of 0. They will be
106 // populated and assigned proper ids in the LoadExtraNodes callback. Until
107 // then, they are owned by the returned closure.
108 scoped_ptr<BookmarkPermanentNode> managed(new BookmarkPermanentNode(0));
109 scoped_ptr<BookmarkPermanentNode> supervised(new BookmarkPermanentNode(0));
111 managed_node_ = managed.get();
112 supervised_node_ = supervised.get();
114 ScopedVector<BookmarkPermanentNodeLoader> loaders;
115 loaders.push_back(new BookmarkPermanentNodeLoader(
116 managed.Pass(), managed_bookmarks_tracker_->GetInitialManagedBookmarks(),
117 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME));
118 loaders.push_back(new BookmarkPermanentNodeLoader(
119 supervised.Pass(),
120 supervised_bookmarks_tracker_->GetInitialManagedBookmarks(),
121 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME));
123 return base::Bind(&LoadExtraNodes, base::Passed(&loaders));
126 bool ManagedBookmarkService::CanSetPermanentNodeTitle(
127 const BookmarkNode* node) {
128 // |managed_node_| can have its title updated if the user signs in or out,
129 // since the name of the managed domain can appear in it. Also both
130 // |managed_node_| and |supervised_node_| can have their title updated on
131 // locale changes (http://crbug.com/459448).
132 if (node == managed_node_ || node == supervised_node_)
133 return true;
134 return !IsDescendantOf(node, managed_node_) &&
135 !IsDescendantOf(node, supervised_node_);
138 bool ManagedBookmarkService::CanSyncNode(const BookmarkNode* node) {
139 return !IsDescendantOf(node, managed_node_) &&
140 !IsDescendantOf(node, supervised_node_);
143 bool ManagedBookmarkService::CanBeEditedByUser(const BookmarkNode* node) {
144 return !IsDescendantOf(node, managed_node_) &&
145 !IsDescendantOf(node, supervised_node_);
148 void ManagedBookmarkService::Shutdown() {
149 Cleanup();
152 void ManagedBookmarkService::BookmarkModelChanged() {}
154 void ManagedBookmarkService::BookmarkModelLoaded(BookmarkModel* bookmark_model,
155 bool ids_reassigned) {
156 BaseBookmarkModelObserver::BookmarkModelLoaded(bookmark_model,
157 ids_reassigned);
158 // Start tracking the managed and supervised bookmarks. This will detect any
159 // changes that may have occurred while the initial managed and supervised
160 // bookmarks were being loaded on the background.
161 managed_bookmarks_tracker_->Init(managed_node_);
162 supervised_bookmarks_tracker_->Init(supervised_node_);
165 void ManagedBookmarkService::BookmarkModelBeingDeleted(
166 BookmarkModel* bookmark_model) {
167 Cleanup();
170 void ManagedBookmarkService::Cleanup() {
171 if (bookmark_model_) {
172 bookmark_model_->RemoveObserver(this);
173 bookmark_model_ = nullptr;
176 managed_bookmarks_tracker_.reset();
177 supervised_bookmarks_tracker_.reset();
179 managed_node_ = nullptr;
180 supervised_node_ = nullptr;
183 } // namespace bookmarks