| 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 #include <utility> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 const std::string& url, const std::string& name, int parent_id) | 91 const std::string& url, const std::string& name, int parent_id) |
| 92 : url(url), name(name), parent_id(parent_id) { | 92 : url(url), name(name), parent_id(parent_id) { |
| 93 } | 93 } |
| 94 | 94 |
| 95 SupervisedUserBookmarksHandler::SupervisedUserBookmarksHandler() { | 95 SupervisedUserBookmarksHandler::SupervisedUserBookmarksHandler() { |
| 96 } | 96 } |
| 97 | 97 |
| 98 SupervisedUserBookmarksHandler::~SupervisedUserBookmarksHandler() { | 98 SupervisedUserBookmarksHandler::~SupervisedUserBookmarksHandler() { |
| 99 } | 99 } |
| 100 | 100 |
| 101 scoped_ptr<base::ListValue> SupervisedUserBookmarksHandler::BuildBookmarksTree( | 101 std::unique_ptr<base::ListValue> |
| 102 SupervisedUserBookmarksHandler::BuildBookmarksTree( |
| 102 const base::DictionaryValue& settings) { | 103 const base::DictionaryValue& settings) { |
| 103 SupervisedUserBookmarksHandler handler; | 104 SupervisedUserBookmarksHandler handler; |
| 104 handler.ParseSettings(settings); | 105 handler.ParseSettings(settings); |
| 105 return handler.BuildTree(); | 106 return handler.BuildTree(); |
| 106 } | 107 } |
| 107 | 108 |
| 108 void SupervisedUserBookmarksHandler::ParseSettings( | 109 void SupervisedUserBookmarksHandler::ParseSettings( |
| 109 const base::DictionaryValue& settings) { | 110 const base::DictionaryValue& settings) { |
| 110 const base::DictionaryValue* folders; | 111 const base::DictionaryValue* folders; |
| 111 if (settings.GetDictionary(kKeyFolder, &folders)) | 112 if (settings.GetDictionary(kKeyFolder, &folders)) |
| (...skipping 30 matching lines...) Expand all Loading... |
| 142 std::string value; | 143 std::string value; |
| 143 it.value().GetAsString(&value); | 144 it.value().GetAsString(&value); |
| 144 std::string name; | 145 std::string name; |
| 145 int parent_id; | 146 int parent_id; |
| 146 if (!ExtractIdAndValue(value, &parent_id, &name)) | 147 if (!ExtractIdAndValue(value, &parent_id, &name)) |
| 147 continue; | 148 continue; |
| 148 links_.push_back(Link(url, name, parent_id)); | 149 links_.push_back(Link(url, name, parent_id)); |
| 149 } | 150 } |
| 150 } | 151 } |
| 151 | 152 |
| 152 scoped_ptr<base::ListValue> SupervisedUserBookmarksHandler::BuildTree() { | 153 std::unique_ptr<base::ListValue> SupervisedUserBookmarksHandler::BuildTree() { |
| 153 root_.reset(new base::ListValue); | 154 root_.reset(new base::ListValue); |
| 154 AddFoldersToTree(); | 155 AddFoldersToTree(); |
| 155 AddLinksToTree(); | 156 AddLinksToTree(); |
| 156 return std::move(root_); | 157 return std::move(root_); |
| 157 } | 158 } |
| 158 | 159 |
| 159 void SupervisedUserBookmarksHandler::AddFoldersToTree() { | 160 void SupervisedUserBookmarksHandler::AddFoldersToTree() { |
| 160 // Go over all folders and try inserting them into the correct position in the | 161 // Go over all folders and try inserting them into the correct position in the |
| 161 // tree. This requires the respective parent folder to be there already. Since | 162 // tree. This requires the respective parent folder to be there already. Since |
| 162 // the parent might appear later in |folders_|, we might need multiple rounds | 163 // the parent might appear later in |folders_|, we might need multiple rounds |
| 163 // until all folders can be added successfully. | 164 // until all folders can be added successfully. |
| 164 // To avoid an infinite loop in the case of a non-existing parent, we take | 165 // To avoid an infinite loop in the case of a non-existing parent, we take |
| 165 // care to stop when no folders could be added in a round. | 166 // care to stop when no folders could be added in a round. |
| 166 std::vector<Folder> folders = folders_; | 167 std::vector<Folder> folders = folders_; |
| 167 std::vector<Folder> folders_failed; | 168 std::vector<Folder> folders_failed; |
| 168 while (!folders.empty() && folders.size() != folders_failed.size()) { | 169 while (!folders.empty() && folders.size() != folders_failed.size()) { |
| 169 folders_failed.clear(); | 170 folders_failed.clear(); |
| 170 for (const auto& folder : folders) { | 171 for (const auto& folder : folders) { |
| 171 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); | 172 std::unique_ptr<base::DictionaryValue> node(new base::DictionaryValue); |
| 172 node->SetIntegerWithoutPathExpansion(kId, folder.id); | 173 node->SetIntegerWithoutPathExpansion(kId, folder.id); |
| 173 node->SetStringWithoutPathExpansion(kName, folder.name); | 174 node->SetStringWithoutPathExpansion(kName, folder.name); |
| 174 node->SetWithoutPathExpansion(kChildren, new base::ListValue); | 175 node->SetWithoutPathExpansion(kChildren, new base::ListValue); |
| 175 if (!AddNodeToTree(folder.parent_id, std::move(node))) | 176 if (!AddNodeToTree(folder.parent_id, std::move(node))) |
| 176 folders_failed.push_back(folder); | 177 folders_failed.push_back(folder); |
| 177 } | 178 } |
| 178 folders.swap(folders_failed); | 179 folders.swap(folders_failed); |
| 179 } | 180 } |
| 180 if (!folders_failed.empty()) { | 181 if (!folders_failed.empty()) { |
| 181 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddFoldersToTree" | 182 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddFoldersToTree" |
| 182 << " failed adding the following folders (id,name,parent):"; | 183 << " failed adding the following folders (id,name,parent):"; |
| 183 for (const Folder& folder : folders_failed) { | 184 for (const Folder& folder : folders_failed) { |
| 184 LOG(WARNING) << folder.id << ", " << folder.name << ", " | 185 LOG(WARNING) << folder.id << ", " << folder.name << ", " |
| 185 << folder.parent_id; | 186 << folder.parent_id; |
| 186 } | 187 } |
| 187 } | 188 } |
| 188 } | 189 } |
| 189 | 190 |
| 190 void SupervisedUserBookmarksHandler::AddLinksToTree() { | 191 void SupervisedUserBookmarksHandler::AddLinksToTree() { |
| 191 for (const auto& link : links_) { | 192 for (const auto& link : links_) { |
| 192 scoped_ptr<base::DictionaryValue> node(new base::DictionaryValue); | 193 std::unique_ptr<base::DictionaryValue> node(new base::DictionaryValue); |
| 193 GURL url = url_formatter::FixupURL(link.url, std::string()); | 194 GURL url = url_formatter::FixupURL(link.url, std::string()); |
| 194 if (!url.is_valid()) { | 195 if (!url.is_valid()) { |
| 195 LOG(WARNING) << "Got invalid URL: " << link.url; | 196 LOG(WARNING) << "Got invalid URL: " << link.url; |
| 196 continue; | 197 continue; |
| 197 } | 198 } |
| 198 node->SetStringWithoutPathExpansion(kUrl, url.spec()); | 199 node->SetStringWithoutPathExpansion(kUrl, url.spec()); |
| 199 node->SetStringWithoutPathExpansion(kName, link.name); | 200 node->SetStringWithoutPathExpansion(kName, link.name); |
| 200 if (!AddNodeToTree(link.parent_id, std::move(node))) { | 201 if (!AddNodeToTree(link.parent_id, std::move(node))) { |
| 201 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddLinksToTree" | 202 LOG(WARNING) << "SupervisedUserBookmarksHandler::AddLinksToTree" |
| 202 << " failed to add link (url,name,parent): " | 203 << " failed to add link (url,name,parent): " |
| 203 << link.url << ", " << link.name << ", " << link.parent_id; | 204 << link.url << ", " << link.name << ", " << link.parent_id; |
| 204 } | 205 } |
| 205 } | 206 } |
| 206 } | 207 } |
| 207 | 208 |
| 208 bool SupervisedUserBookmarksHandler::AddNodeToTree( | 209 bool SupervisedUserBookmarksHandler::AddNodeToTree( |
| 209 int parent_id, | 210 int parent_id, |
| 210 scoped_ptr<base::DictionaryValue> node) { | 211 std::unique_ptr<base::DictionaryValue> node) { |
| 211 base::ListValue* parent = FindFolder(root_.get(), parent_id); | 212 base::ListValue* parent = FindFolder(root_.get(), parent_id); |
| 212 if (!parent) | 213 if (!parent) |
| 213 return false; | 214 return false; |
| 214 parent->Append(node.release()); | 215 parent->Append(node.release()); |
| 215 return true; | 216 return true; |
| 216 } | 217 } |
| OLD | NEW |