OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/supervised_user/supervised_user_bookmarks_handler.h" | 5 #include "chrome/browser/supervised_user/supervised_user_bookmarks_handler.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "components/url_formatter/url_fixer.h" | 13 #include "components/url_formatter/url_fixer.h" |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // Keys of relevant managed user settings. | 17 // Keys of relevant managed user settings. |
17 const char kKeyLink[] = "SupervisedBookmarkLink"; | 18 const char kKeyLink[] = "SupervisedBookmarkLink"; |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 if (!ExtractIdAndValue(value, &parent_id, &name)) | 146 if (!ExtractIdAndValue(value, &parent_id, &name)) |
146 continue; | 147 continue; |
147 links_.push_back(Link(url, name, parent_id)); | 148 links_.push_back(Link(url, name, parent_id)); |
148 } | 149 } |
149 } | 150 } |
150 | 151 |
151 scoped_ptr<base::ListValue> SupervisedUserBookmarksHandler::BuildTree() { | 152 scoped_ptr<base::ListValue> SupervisedUserBookmarksHandler::BuildTree() { |
152 root_.reset(new base::ListValue); | 153 root_.reset(new base::ListValue); |
153 AddFoldersToTree(); | 154 AddFoldersToTree(); |
154 AddLinksToTree(); | 155 AddLinksToTree(); |
155 return root_.Pass(); | 156 return std::move(root_); |
156 } | 157 } |
157 | 158 |
158 void SupervisedUserBookmarksHandler::AddFoldersToTree() { | 159 void SupervisedUserBookmarksHandler::AddFoldersToTree() { |
159 // Go over all folders and try inserting them into the correct position in the | 160 // Go over all folders and try inserting them into the correct position in the |
160 // tree. This requires the respective parent folder to be there already. Since | 161 // tree. This requires the respective parent folder to be there already. Since |
161 // the parent might appear later in |folders_|, we might need multiple rounds | 162 // the parent might appear later in |folders_|, we might need multiple rounds |
162 // until all folders can be added successfully. | 163 // until all folders can be added successfully. |
163 // To avoid an infinite loop in the case of a non-existing parent, we take | 164 // To avoid an infinite loop in the case of a non-existing parent, we take |
164 // care to stop when no folders could be added in a round. | 165 // care to stop when no folders could be added in a round. |
165 std::vector<Folder> folders = folders_; | 166 std::vector<Folder> folders = folders_; |
166 std::vector<Folder> folders_failed; | 167 std::vector<Folder> folders_failed; |
167 while (!folders.empty() && folders.size() != folders_failed.size()) { | 168 while (!folders.empty() && folders.size() != folders_failed.size()) { |
168 folders_failed.clear(); | 169 folders_failed.clear(); |
169 for (const auto& folder : folders) { | 170 for (const auto& folder : folders) { |
170 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); | 171 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); |
171 node->SetIntegerWithoutPathExpansion(kId, folder.id); | 172 node->SetIntegerWithoutPathExpansion(kId, folder.id); |
172 node->SetStringWithoutPathExpansion(kName, folder.name); | 173 node->SetStringWithoutPathExpansion(kName, folder.name); |
173 node->SetWithoutPathExpansion(kChildren, new base::ListValue); | 174 node->SetWithoutPathExpansion(kChildren, new base::ListValue); |
174 if (!AddNodeToTree(folder.parent_id, node.Pass())) | 175 if (!AddNodeToTree(folder.parent_id, std::move(node))) |
175 folders_failed.push_back(folder); | 176 folders_failed.push_back(folder); |
176 } | 177 } |
177 folders.swap(folders_failed); | 178 folders.swap(folders_failed); |
178 } | 179 } |
179 if (!folders_failed.empty()) { | 180 if (!folders_failed.empty()) { |
180 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddFoldersToTree" | 181 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddFoldersToTree" |
181 << " failed adding the following folders (id,name,parent):"; | 182 << " failed adding the following folders (id,name,parent):"; |
182 for (const Folder& folder : folders_failed) { | 183 for (const Folder& folder : folders_failed) { |
183 LOG(WARNING) << folder.id << ", " << folder.name << ", " | 184 LOG(WARNING) << folder.id << ", " << folder.name << ", " |
184 << folder.parent_id; | 185 << folder.parent_id; |
185 } | 186 } |
186 } | 187 } |
187 } | 188 } |
188 | 189 |
189 void SupervisedUserBookmarksHandler::AddLinksToTree() { | 190 void SupervisedUserBookmarksHandler::AddLinksToTree() { |
190 for (const auto& link : links_) { | 191 for (const auto& link : links_) { |
191 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); | 192 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); |
192 GURL url = url_formatter::FixupURL(link.url, std::string()); | 193 GURL url = url_formatter::FixupURL(link.url, std::string()); |
193 if (!url.is_valid()) { | 194 if (!url.is_valid()) { |
194 LOG(WARNING) << "Got invalid URL: " << link.url; | 195 LOG(WARNING) << "Got invalid URL: " << link.url; |
195 continue; | 196 continue; |
196 } | 197 } |
197 node->SetStringWithoutPathExpansion(kUrl, url.spec()); | 198 node->SetStringWithoutPathExpansion(kUrl, url.spec()); |
198 node->SetStringWithoutPathExpansion(kName, link.name); | 199 node->SetStringWithoutPathExpansion(kName, link.name); |
199 if (!AddNodeToTree(link.parent_id, node.Pass())) { | 200 if (!AddNodeToTree(link.parent_id, std::move(node))) { |
200 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddLinksToTree" | 201 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddLinksToTree" |
201 << " failed to add link (url,name,parent): " | 202 << " failed to add link (url,name,parent): " |
202 << link.url << ", " << link.name << ", " << link.parent_id; | 203 << link.url << ", " << link.name << ", " << link.parent_id; |
203 } | 204 } |
204 } | 205 } |
205 } | 206 } |
206 | 207 |
207 bool SupervisedUserBookmarksHandler::AddNodeToTree( | 208 bool SupervisedUserBookmarksHandler::AddNodeToTree( |
208 int parent_id, | 209 int parent_id, |
209 scoped_ptr<base::DictionaryValue> node) { | 210 scoped_ptr<base::DictionaryValue> node) { |
210 base::ListValue* parent = FindFolder(root_.get(), parent_id); | 211 base::ListValue* parent = FindFolder(root_.get(), parent_id); |
211 if (!parent) | 212 if (!parent) |
212 return false; | 213 return false; |
213 parent->Append(node.release()); | 214 parent->Append(node.release()); |
214 return true; | 215 return true; |
215 } | 216 } |
OLD | NEW |