OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "components/bookmarks/managed/managed_bookmark_service.h" | |
6 | |
7 #include <stdlib.h> | |
8 #include <vector> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/logging.h" | |
13 #include "base/strings/string16.h" | |
14 #include "base/values.h" | |
15 #include "components/bookmarks/browser/bookmark_model.h" | |
16 #include "components/bookmarks/browser/bookmark_utils.h" | |
17 #include "components/bookmarks/managed/managed_bookmarks_tracker.h" | |
18 #include "grit/components_strings.h" | |
19 #include "ui/base/l10n/l10n_util.h" | |
20 | |
21 namespace bookmarks { | |
22 namespace { | |
23 | |
24 // Initializes |node| from |initial_bookmarks| and |title_id| and returns it. | |
25 // The ids are assigned starting at |next_node_id| value and the value is | |
26 // updated as a side effect. | |
27 scoped_ptr<BookmarkPermanentNode> LoadNodeInitialContent( | |
28 scoped_ptr<BookmarkPermanentNode> node, | |
29 scoped_ptr<base::ListValue> initial_bookmarks, | |
30 int title_id, | |
31 int64* next_node_id) { | |
32 // Load the initial contents of |node| now and assign it an unused id. | |
33 node->set_id(*next_node_id); | |
34 *next_node_id = ManagedBookmarksTracker::LoadInitial( | |
35 node.get(), initial_bookmarks.get(), node->id() + 1); | |
36 node->set_visible(!node->empty()); | |
37 node->SetTitle(l10n_util::GetStringUTF16(title_id)); | |
38 return node.Pass(); | |
39 } | |
40 | |
41 // BookmarkPermanentNodeLoader is a closure that returns initialized | |
42 // BookmarkPermanentNode when passed a mutable pointer to the next node id to | |
43 // use. | |
44 typedef base::Callback<scoped_ptr<BookmarkPermanentNode>(int64*)> | |
45 BookmarkPermanentNodeLoader; | |
46 | |
47 // Returns a list of initialized BookmarkPermanentNodes using |next_node_id| to | |
48 // start assigning id. |next_node_id| is updated as a side effect of calling | |
49 // this method. | |
50 BookmarkPermanentNodeList LoadExtraNodes( | |
51 scoped_ptr<std::vector<BookmarkPermanentNodeLoader>> loaders, | |
52 int64* next_node_id) { | |
53 BookmarkPermanentNodeList extra_nodes; | |
54 for (const auto& loader : *loaders.get()) { | |
sky
2015/07/20 15:53:19
nit: no {}
sdefresne
2015/07/20 17:43:40
Done.
| |
55 extra_nodes.push_back(loader.Run(next_node_id).release()); | |
56 } | |
57 return extra_nodes.Pass(); | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 ManagedBookmarkService::ManagedBookmarkService( | |
63 PrefService* prefs, | |
64 const GetManagementDomainCallback& callback) | |
65 : prefs_(prefs), | |
66 bookmark_model_(nullptr), | |
67 managed_domain_callback_(callback), | |
68 managed_node_(nullptr), | |
69 supervised_node_(nullptr) { | |
70 } | |
71 | |
72 ManagedBookmarkService::~ManagedBookmarkService() { | |
73 DCHECK(!bookmark_model_); | |
74 } | |
75 | |
76 void ManagedBookmarkService::BookmarkModelCreated( | |
77 BookmarkModel* bookmark_model) { | |
78 DCHECK(bookmark_model); | |
79 DCHECK(!bookmark_model_); | |
80 bookmark_model_ = bookmark_model; | |
81 bookmark_model_->AddObserver(this); | |
82 | |
83 managed_bookmarks_tracker_.reset(new ManagedBookmarksTracker( | |
84 bookmark_model_, prefs_, false /* is_supervised */, | |
85 managed_domain_callback_)); | |
86 | |
87 GetManagementDomainCallback unbound_callback; | |
88 supervised_bookmarks_tracker_.reset(new ManagedBookmarksTracker( | |
89 bookmark_model_, prefs_, true /* is_supervised */, unbound_callback)); | |
90 } | |
91 | |
92 LoadExtraCallback ManagedBookmarkService::GetLoadExtraNodesCallback() { | |
93 // Create two BookmarkPermanentNode with a temporary id of 0. They will be | |
94 // populated and assigned proper ids in the LoadExtraNodes callback. Until | |
95 // then, they are owned by the returned closure. | |
96 scoped_ptr<BookmarkPermanentNode> managed(new BookmarkPermanentNode(0)); | |
97 scoped_ptr<BookmarkPermanentNode> supervised(new BookmarkPermanentNode(0)); | |
98 | |
99 managed_node_ = managed.get(); | |
100 supervised_node_ = supervised.get(); | |
101 | |
102 scoped_ptr<std::vector<BookmarkPermanentNodeLoader>> loaders( | |
103 new std::vector<BookmarkPermanentNodeLoader>()); | |
104 loaders->push_back(base::Bind( | |
105 &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!
| |
106 base::Passed(managed_bookmarks_tracker_->GetInitialManagedBookmarks()), | |
107 IDS_BOOKMARK_BAR_MANAGED_FOLDER_DEFAULT_NAME)); | |
108 loaders->push_back(base::Bind( | |
109 &LoadNodeInitialContent, base::Passed(&supervised), | |
110 base::Passed(supervised_bookmarks_tracker_->GetInitialManagedBookmarks()), | |
111 IDS_BOOKMARK_BAR_SUPERVISED_FOLDER_DEFAULT_NAME)); | |
112 | |
113 return base::Bind(&LoadExtraNodes, base::Passed(&loaders)); | |
114 } | |
115 | |
116 bool ManagedBookmarkService::CanSetPermanentNodeTitle( | |
117 const BookmarkNode* node) { | |
118 // |managed_node_| can have its title updated if the user signs in or out, | |
119 // since the name of the managed domain can appear in it. Also both | |
120 // |managed_node_| and |supervised_node_| can have their title updated on | |
121 // locale changes (http://crbug.com/459448). | |
122 if (node == managed_node_ || node == supervised_node_) | |
123 return true; | |
124 return !IsDescendantOf(node, managed_node_) && | |
125 !IsDescendantOf(node, supervised_node_); | |
126 } | |
127 | |
128 bool ManagedBookmarkService::CanSyncNode(const BookmarkNode* node) { | |
129 return !IsDescendantOf(node, managed_node_) && | |
130 !IsDescendantOf(node, supervised_node_); | |
131 } | |
132 | |
133 bool ManagedBookmarkService::CanBeEditedByUser(const BookmarkNode* node) { | |
134 return !IsDescendantOf(node, managed_node_) && | |
135 !IsDescendantOf(node, supervised_node_); | |
136 } | |
137 | |
138 void ManagedBookmarkService::Shutdown() { | |
139 Cleanup(); | |
140 } | |
141 | |
142 void ManagedBookmarkService::BookmarkModelChanged() { | |
143 } | |
144 | |
145 void ManagedBookmarkService::BookmarkModelLoaded(BookmarkModel* bookmark_model, | |
146 bool ids_reassigned) { | |
147 BaseBookmarkModelObserver::BookmarkModelLoaded(bookmark_model, | |
148 ids_reassigned); | |
149 // Start tracking the managed and supervised bookmarks. This will detect any | |
150 // changes that may have occurred while the initial managed and supervised | |
151 // bookmarks were being loaded on the background. | |
152 managed_bookmarks_tracker_->Init(managed_node_); | |
153 supervised_bookmarks_tracker_->Init(supervised_node_); | |
154 } | |
155 | |
156 void ManagedBookmarkService::BookmarkModelBeingDeleted( | |
157 BookmarkModel* bookmark_model) { | |
158 Cleanup(); | |
159 } | |
160 | |
161 void ManagedBookmarkService::Cleanup() { | |
162 if (bookmark_model_) { | |
163 bookmark_model_->RemoveObserver(this); | |
164 bookmark_model_ = nullptr; | |
165 } | |
166 | |
167 managed_bookmarks_tracker_.reset(); | |
168 supervised_bookmarks_tracker_.reset(); | |
169 | |
170 managed_node_ = nullptr; | |
171 supervised_node_ = nullptr; | |
172 } | |
173 | |
174 } // namespace bookmarks | |
OLD | NEW |