| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 "chrome/browser/history/starred_url_database.h" | |
| 6 | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/json/json_writer.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_vector.h" | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/string_util.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/bookmarks/bookmark_codec.h" | |
| 16 #include "chrome/browser/bookmarks/bookmark_model.h" | |
| 17 #include "chrome/browser/history/history.h" | |
| 18 #include "sql/statement.h" | |
| 19 | |
| 20 // The following table is used to store star (aka bookmark) information. This | |
| 21 // class derives from URLDatabase, which has its own schema. | |
| 22 // | |
| 23 // starred | |
| 24 // id Unique identifier (primary key) for the entry. | |
| 25 // type Type of entry, if 0 this corresponds to a URL, 1 for | |
| 26 // a system folder, 2 for a user created folder, 3 for | |
| 27 // other. Note that 4 (synced) will never appear in the | |
| 28 // database because it was added after this storage was | |
| 29 // deprecated. | |
| 30 // url_id ID of the url, only valid if type == 0 | |
| 31 // group_id ID of the folder, only valid if type != 0. This id comes | |
| 32 // from the UI and is NOT the same as id. | |
| 33 // title User assigned title. | |
| 34 // date_added Creation date. | |
| 35 // visual_order Visual order within parent. | |
| 36 // parent_id Folder ID of the parent this entry is contained in, if 0 | |
| 37 // entry is not in a folder. | |
| 38 // date_modified Time the folder was last modified. See comments in | |
| 39 // StarredEntry::date_folder_modified | |
| 40 // NOTE: group_id and parent_id come from the UI, id is assigned by the | |
| 41 // db. | |
| 42 | |
| 43 namespace history { | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 // Fields used by FillInStarredEntry. | |
| 48 #define STAR_FIELDS \ | |
| 49 " starred.id, starred.type, starred.title, starred.date_added, " \ | |
| 50 "starred.visual_order, starred.parent_id, urls.url, urls.id, " \ | |
| 51 "starred.group_id, starred.date_modified " | |
| 52 const char kHistoryStarFields[] = STAR_FIELDS; | |
| 53 | |
| 54 void FillInStarredEntry(const sql::Statement& s, StarredEntry* entry) { | |
| 55 DCHECK(entry); | |
| 56 entry->id = s.ColumnInt64(0); | |
| 57 switch (s.ColumnInt(1)) { | |
| 58 case 0: | |
| 59 entry->type = history::StarredEntry::URL; | |
| 60 entry->url = GURL(s.ColumnString(6)); | |
| 61 break; | |
| 62 case 1: | |
| 63 entry->type = history::StarredEntry::BOOKMARK_BAR; | |
| 64 break; | |
| 65 case 2: | |
| 66 entry->type = history::StarredEntry::USER_FOLDER; | |
| 67 break; | |
| 68 case 3: | |
| 69 entry->type = history::StarredEntry::OTHER; | |
| 70 break; | |
| 71 default: | |
| 72 NOTREACHED(); | |
| 73 break; | |
| 74 } | |
| 75 entry->title = s.ColumnString16(2); | |
| 76 entry->date_added = base::Time::FromInternalValue(s.ColumnInt64(3)); | |
| 77 entry->visual_order = s.ColumnInt(4); | |
| 78 entry->parent_folder_id = s.ColumnInt64(5); | |
| 79 entry->url_id = s.ColumnInt64(7); | |
| 80 entry->folder_id = s.ColumnInt64(8); | |
| 81 entry->date_folder_modified = base::Time::FromInternalValue(s.ColumnInt64(9)); | |
| 82 } | |
| 83 | |
| 84 // Resets the properties of |node| from the supplied |entry|. | |
| 85 void ResetBookmarkNode(const history::StarredEntry& entry, | |
| 86 BookmarkNode* node) { | |
| 87 DCHECK(entry.type != history::StarredEntry::URL || | |
| 88 entry.url == node->url()); | |
| 89 | |
| 90 node->set_date_added(entry.date_added); | |
| 91 node->set_date_folder_modified(entry.date_folder_modified); | |
| 92 node->SetTitle(entry.title); | |
| 93 | |
| 94 switch (entry.type) { | |
| 95 case history::StarredEntry::URL: | |
| 96 node->set_type(BookmarkNode::URL); | |
| 97 break; | |
| 98 case history::StarredEntry::USER_FOLDER: | |
| 99 node->set_type(BookmarkNode::FOLDER); | |
| 100 break; | |
| 101 case history::StarredEntry::BOOKMARK_BAR: | |
| 102 node->set_type(BookmarkNode::BOOKMARK_BAR); | |
| 103 break; | |
| 104 case history::StarredEntry::OTHER: | |
| 105 node->set_type(BookmarkNode::OTHER_NODE); | |
| 106 break; | |
| 107 case history::StarredEntry::MOBILE: | |
| 108 node->set_type(BookmarkNode::MOBILE); | |
| 109 break; | |
| 110 default: | |
| 111 NOTREACHED(); | |
| 112 break; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 } // namespace | |
| 117 | |
| 118 // static | |
| 119 const int64 StarredURLDatabase::kBookmarkBarID = 1; | |
| 120 | |
| 121 StarredURLDatabase::StarredURLDatabase(sql::Connection* db) | |
| 122 : db_(db) { | |
| 123 } | |
| 124 | |
| 125 StarredURLDatabase::~StarredURLDatabase() { | |
| 126 } | |
| 127 | |
| 128 bool StarredURLDatabase::MigrateBookmarksToFile(const FilePath& path) { | |
| 129 if (!GetDB().DoesTableExist("starred")) | |
| 130 return true; | |
| 131 | |
| 132 if (EnsureStarredIntegrity() && !MigrateBookmarksToFileImpl(path)) { | |
| 133 NOTREACHED() << " Bookmarks migration failed"; | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 if (!GetDB().Execute("DROP TABLE starred")) { | |
| 138 NOTREACHED() << "Unable to drop starred table"; | |
| 139 return false; | |
| 140 } | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 bool StarredURLDatabase::GetAllStarredEntries( | |
| 145 std::vector<StarredEntry>* entries) { | |
| 146 DCHECK(entries); | |
| 147 std::string sql = "SELECT "; | |
| 148 sql.append(kHistoryStarFields); | |
| 149 sql.append("FROM starred LEFT JOIN urls ON starred.url_id = urls.id "); | |
| 150 sql += "ORDER BY parent_id, visual_order"; | |
| 151 | |
| 152 sql::Statement s(GetDB().GetUniqueStatement(sql.c_str())); | |
| 153 if (!s.is_valid()) | |
| 154 return false; | |
| 155 | |
| 156 history::StarredEntry entry; | |
| 157 while (s.Step()) { | |
| 158 FillInStarredEntry(s, &entry); | |
| 159 // Reset the url for non-url types. This is needed as we're reusing the | |
| 160 // same entry for the loop. | |
| 161 if (entry.type != history::StarredEntry::URL) | |
| 162 entry.url = GURL(); | |
| 163 entries->push_back(entry); | |
| 164 } | |
| 165 return true; | |
| 166 } | |
| 167 | |
| 168 sql::Connection& StarredURLDatabase::GetDB() { | |
| 169 return *db_; | |
| 170 } | |
| 171 | |
| 172 bool StarredURLDatabase::EnsureStarredIntegrity() { | |
| 173 std::set<StarredNode*> roots; | |
| 174 std::set<StarID> folders_with_duplicate_ids; | |
| 175 std::set<StarredNode*> unparented_urls; | |
| 176 std::set<StarID> empty_url_ids; | |
| 177 | |
| 178 if (!BuildStarNodes(&roots, &folders_with_duplicate_ids, &unparented_urls, | |
| 179 &empty_url_ids)) { | |
| 180 return false; | |
| 181 } | |
| 182 | |
| 183 bool valid = EnsureStarredIntegrityImpl(&roots, folders_with_duplicate_ids, | |
| 184 &unparented_urls, empty_url_ids); | |
| 185 | |
| 186 STLDeleteElements(&roots); | |
| 187 STLDeleteElements(&unparented_urls); | |
| 188 return valid; | |
| 189 } | |
| 190 | |
| 191 bool StarredURLDatabase::UpdateStarredEntryRow(StarID star_id, | |
| 192 const string16& title, | |
| 193 UIStarID parent_folder_id, | |
| 194 int visual_order, | |
| 195 base::Time date_modified) { | |
| 196 DCHECK(star_id && visual_order >= 0); | |
| 197 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 198 "UPDATE starred SET title=?, parent_id=?, visual_order=?, " | |
| 199 "date_modified=? WHERE id=?")); | |
| 200 statement.BindString16(0, title); | |
| 201 statement.BindInt64(1, parent_folder_id); | |
| 202 statement.BindInt(2, visual_order); | |
| 203 statement.BindInt64(3, date_modified.ToInternalValue()); | |
| 204 statement.BindInt64(4, star_id); | |
| 205 | |
| 206 return statement.Run(); | |
| 207 } | |
| 208 | |
| 209 bool StarredURLDatabase::AdjustStarredVisualOrder(UIStarID parent_folder_id, | |
| 210 int start_visual_order, | |
| 211 int delta) { | |
| 212 DCHECK(parent_folder_id && start_visual_order >= 0); | |
| 213 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 214 "UPDATE starred SET visual_order=visual_order+? " | |
| 215 "WHERE parent_id=? AND visual_order >= ?")); | |
| 216 statement.BindInt(0, delta); | |
| 217 statement.BindInt64(1, parent_folder_id); | |
| 218 statement.BindInt(2, start_visual_order); | |
| 219 | |
| 220 return statement.Run(); | |
| 221 } | |
| 222 | |
| 223 StarID StarredURLDatabase::CreateStarredEntryRow(URLID url_id, | |
| 224 UIStarID folder_id, | |
| 225 UIStarID parent_folder_id, | |
| 226 const string16& title, | |
| 227 const base::Time& date_added, | |
| 228 int visual_order, | |
| 229 StarredEntry::Type type) { | |
| 230 DCHECK(visual_order >= 0 && | |
| 231 (type != history::StarredEntry::URL || url_id)); | |
| 232 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 233 "INSERT INTO starred " | |
| 234 "(type, url_id, group_id, title, date_added, visual_order, parent_id, " | |
| 235 "date_modified) VALUES (?,?,?,?,?,?,?,?)")); | |
| 236 | |
| 237 switch (type) { | |
| 238 case history::StarredEntry::URL: | |
| 239 statement.BindInt(0, 0); | |
| 240 break; | |
| 241 case history::StarredEntry::BOOKMARK_BAR: | |
| 242 statement.BindInt(0, 1); | |
| 243 break; | |
| 244 case history::StarredEntry::USER_FOLDER: | |
| 245 statement.BindInt(0, 2); | |
| 246 break; | |
| 247 case history::StarredEntry::OTHER: | |
| 248 statement.BindInt(0, 3); | |
| 249 break; | |
| 250 default: | |
| 251 NOTREACHED(); | |
| 252 } | |
| 253 statement.BindInt64(1, url_id); | |
| 254 statement.BindInt64(2, folder_id); | |
| 255 statement.BindString16(3, title); | |
| 256 statement.BindInt64(4, date_added.ToInternalValue()); | |
| 257 statement.BindInt(5, visual_order); | |
| 258 statement.BindInt64(6, parent_folder_id); | |
| 259 statement.BindInt64(7, base::Time().ToInternalValue()); | |
| 260 | |
| 261 if (statement.Run()) | |
| 262 return GetDB().GetLastInsertRowId(); | |
| 263 return 0; | |
| 264 } | |
| 265 | |
| 266 bool StarredURLDatabase::DeleteStarredEntryRow(StarID star_id) { | |
| 267 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 268 "DELETE FROM starred WHERE id=?")); | |
| 269 statement.BindInt64(0, star_id); | |
| 270 | |
| 271 return statement.Run(); | |
| 272 } | |
| 273 | |
| 274 bool StarredURLDatabase::GetStarredEntry(StarID star_id, StarredEntry* entry) { | |
| 275 DCHECK(entry && star_id); | |
| 276 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, | |
| 277 "SELECT" STAR_FIELDS "FROM starred LEFT JOIN urls ON " | |
| 278 "starred.url_id = urls.id WHERE starred.id=?")); | |
| 279 statement.BindInt64(0, star_id); | |
| 280 | |
| 281 if (statement.Step()) { | |
| 282 FillInStarredEntry(statement, entry); | |
| 283 return true; | |
| 284 } | |
| 285 return false; | |
| 286 } | |
| 287 | |
| 288 StarID StarredURLDatabase::CreateStarredEntry(StarredEntry* entry) { | |
| 289 entry->id = 0; // Ensure 0 for failure case. | |
| 290 | |
| 291 // Adjust the visual order when we are inserting it somewhere. | |
| 292 if (entry->parent_folder_id) | |
| 293 AdjustStarredVisualOrder(entry->parent_folder_id, entry->visual_order, 1); | |
| 294 | |
| 295 // Insert the new entry. | |
| 296 switch (entry->type) { | |
| 297 case StarredEntry::USER_FOLDER: | |
| 298 entry->id = CreateStarredEntryRow(0, entry->folder_id, | |
| 299 entry->parent_folder_id, entry->title, entry->date_added, | |
| 300 entry->visual_order, entry->type); | |
| 301 break; | |
| 302 | |
| 303 case StarredEntry::URL: { | |
| 304 // Get the row for this URL. | |
| 305 URLRow url_row; | |
| 306 if (!GetRowForURL(entry->url, &url_row)) { | |
| 307 // Create a new URL row for this entry. | |
| 308 url_row = URLRow(entry->url); | |
| 309 url_row.set_title(entry->title); | |
| 310 url_row.set_hidden(false); | |
| 311 entry->url_id = this->AddURL(url_row); | |
| 312 } else { | |
| 313 entry->url_id = url_row.id(); // The caller doesn't have to set this. | |
| 314 } | |
| 315 | |
| 316 // Create the star entry referring to the URL row. | |
| 317 entry->id = CreateStarredEntryRow(entry->url_id, entry->folder_id, | |
| 318 entry->parent_folder_id, entry->title, entry->date_added, | |
| 319 entry->visual_order, entry->type); | |
| 320 | |
| 321 // Update the URL row to refer to this new starred entry. | |
| 322 UpdateURLRow(entry->url_id, url_row); | |
| 323 break; | |
| 324 } | |
| 325 | |
| 326 default: | |
| 327 NOTREACHED(); | |
| 328 break; | |
| 329 } | |
| 330 return entry->id; | |
| 331 } | |
| 332 | |
| 333 UIStarID StarredURLDatabase::GetMaxFolderID() { | |
| 334 sql::Statement max_folder_id_statement(GetDB().GetUniqueStatement( | |
| 335 "SELECT MAX(group_id) FROM starred")); | |
| 336 | |
| 337 if (!max_folder_id_statement.Step()) { | |
| 338 return 0; | |
| 339 } | |
| 340 return max_folder_id_statement.ColumnInt64(0); | |
| 341 } | |
| 342 | |
| 343 bool StarredURLDatabase::BuildStarNodes( | |
| 344 std::set<StarredURLDatabase::StarredNode*>* roots, | |
| 345 std::set<StarID>* folders_with_duplicate_ids, | |
| 346 std::set<StarredNode*>* unparented_urls, | |
| 347 std::set<StarID>* empty_url_ids) { | |
| 348 std::vector<StarredEntry> star_entries; | |
| 349 if (!GetAllStarredEntries(&star_entries)) { | |
| 350 NOTREACHED() << "Unable to get bookmarks from database"; | |
| 351 return false; | |
| 352 } | |
| 353 | |
| 354 // Create the folder/bookmark-bar/other nodes. | |
| 355 std::map<UIStarID, StarredNode*> folder_id_to_node_map; | |
| 356 for (size_t i = 0; i < star_entries.size(); ++i) { | |
| 357 if (star_entries[i].type != StarredEntry::URL) { | |
| 358 if (folder_id_to_node_map.find(star_entries[i].folder_id) != | |
| 359 folder_id_to_node_map.end()) { | |
| 360 // There's already a folder with this ID. | |
| 361 folders_with_duplicate_ids->insert(star_entries[i].id); | |
| 362 } else { | |
| 363 // Create the node and update the mapping. | |
| 364 StarredNode* node = new StarredNode(star_entries[i]); | |
| 365 folder_id_to_node_map[star_entries[i].folder_id] = node; | |
| 366 } | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 // Iterate again, creating nodes for URL bookmarks and parenting all | |
| 371 // bookmarks/folders. In addition populate the empty_url_ids with all entries | |
| 372 // of type URL that have an empty URL. | |
| 373 std::map<StarID, StarredNode*> id_to_node_map; | |
| 374 for (size_t i = 0; i < star_entries.size(); ++i) { | |
| 375 if (star_entries[i].type == StarredEntry::URL) { | |
| 376 if (star_entries[i].url.is_empty()) { | |
| 377 empty_url_ids->insert(star_entries[i].id); | |
| 378 } else if (!star_entries[i].parent_folder_id || | |
| 379 folder_id_to_node_map.find(star_entries[i].parent_folder_id) == | |
| 380 folder_id_to_node_map.end()) { | |
| 381 // This entry has no parent, or we couldn't find the parent. | |
| 382 StarredNode* node = new StarredNode(star_entries[i]); | |
| 383 unparented_urls->insert(node); | |
| 384 } else { | |
| 385 // Add the node to its parent. | |
| 386 StarredNode* parent = | |
| 387 folder_id_to_node_map[star_entries[i].parent_folder_id]; | |
| 388 StarredNode* node = new StarredNode(star_entries[i]); | |
| 389 parent->Add(node, parent->child_count()); | |
| 390 } | |
| 391 } else if (folders_with_duplicate_ids->find(star_entries[i].id) == | |
| 392 folders_with_duplicate_ids->end()) { | |
| 393 // The entry is a folder (or bookmark bar/other node) that isn't | |
| 394 // marked as a duplicate. | |
| 395 if (!star_entries[i].parent_folder_id || | |
| 396 folder_id_to_node_map.find(star_entries[i].parent_folder_id) == | |
| 397 folder_id_to_node_map.end()) { | |
| 398 // Entry has no parent, or the parent wasn't found. | |
| 399 roots->insert(folder_id_to_node_map[star_entries[i].folder_id]); | |
| 400 } else { | |
| 401 // Parent the folder node. | |
| 402 StarredNode* parent = | |
| 403 folder_id_to_node_map[star_entries[i].parent_folder_id]; | |
| 404 StarredNode* node = folder_id_to_node_map[star_entries[i].folder_id]; | |
| 405 if (!node->HasAncestor(parent) && !parent->HasAncestor(node)) { | |
| 406 parent->Add(node, parent->child_count()); | |
| 407 } else { | |
| 408 // The node has a cycle. Add it to the list of roots so the cycle is | |
| 409 // broken. | |
| 410 roots->insert(node); | |
| 411 } | |
| 412 } | |
| 413 } | |
| 414 } | |
| 415 return true; | |
| 416 } | |
| 417 | |
| 418 StarredURLDatabase::StarredNode* StarredURLDatabase::GetNodeByType( | |
| 419 const std::set<StarredURLDatabase::StarredNode*>& nodes, | |
| 420 StarredEntry::Type type) { | |
| 421 for (std::set<StarredNode*>::const_iterator i = nodes.begin(); | |
| 422 i != nodes.end(); ++i) { | |
| 423 if ((*i)->value.type == type) | |
| 424 return *i; | |
| 425 } | |
| 426 return NULL; | |
| 427 } | |
| 428 | |
| 429 bool StarredURLDatabase::EnsureVisualOrder( | |
| 430 StarredURLDatabase::StarredNode* node) { | |
| 431 for (int i = 0; i < node->child_count(); ++i) { | |
| 432 if (node->GetChild(i)->value.visual_order != i) { | |
| 433 StarredEntry& entry = node->GetChild(i)->value; | |
| 434 entry.visual_order = i; | |
| 435 LOG(WARNING) << "Bookmark visual order is wrong"; | |
| 436 if (!UpdateStarredEntryRow(entry.id, entry.title, entry.parent_folder_id, | |
| 437 i, entry.date_folder_modified)) { | |
| 438 NOTREACHED() << "Unable to update visual order"; | |
| 439 return false; | |
| 440 } | |
| 441 } | |
| 442 if (!EnsureVisualOrder(node->GetChild(i))) | |
| 443 return false; | |
| 444 } | |
| 445 return true; | |
| 446 } | |
| 447 | |
| 448 bool StarredURLDatabase::EnsureStarredIntegrityImpl( | |
| 449 std::set<StarredURLDatabase::StarredNode*>* roots, | |
| 450 const std::set<StarID>& folders_with_duplicate_ids, | |
| 451 std::set<StarredNode*>* unparented_urls, | |
| 452 const std::set<StarID>& empty_url_ids) { | |
| 453 // Make sure the bookmark bar entry exists. | |
| 454 StarredNode* bookmark_node = | |
| 455 GetNodeByType(*roots, StarredEntry::BOOKMARK_BAR); | |
| 456 if (!bookmark_node) { | |
| 457 LOG(WARNING) << "No bookmark bar folder in database"; | |
| 458 // If there is no bookmark bar entry in the db things are really | |
| 459 // screwed. Return false, which won't trigger migration and we'll just | |
| 460 // drop the tables. | |
| 461 return false; | |
| 462 } | |
| 463 | |
| 464 // Make sure the other node exists. | |
| 465 StarredNode* other_node = GetNodeByType(*roots, StarredEntry::OTHER); | |
| 466 if (!other_node) { | |
| 467 LOG(WARNING) << "No bookmark other folder in database"; | |
| 468 StarredEntry entry; | |
| 469 entry.folder_id = GetMaxFolderID() + 1; | |
| 470 if (entry.folder_id == 1) { | |
| 471 NOTREACHED() << "Unable to get new id for other bookmarks folder"; | |
| 472 return false; | |
| 473 } | |
| 474 entry.id = CreateStarredEntryRow( | |
| 475 0, entry.folder_id, 0, UTF8ToUTF16("other"), base::Time::Now(), 0, | |
| 476 history::StarredEntry::OTHER); | |
| 477 if (!entry.id) { | |
| 478 NOTREACHED() << "Unable to create other bookmarks folder"; | |
| 479 return false; | |
| 480 } | |
| 481 entry.type = StarredEntry::OTHER; | |
| 482 StarredNode* other_node = new StarredNode(entry); | |
| 483 roots->insert(other_node); | |
| 484 } | |
| 485 | |
| 486 // We could potentially make sure only one folder with type | |
| 487 // BOOKMARK_BAR/OTHER, but history backend enforces this. | |
| 488 | |
| 489 // Nuke any entries with no url. | |
| 490 for (std::set<StarID>::const_iterator i = empty_url_ids.begin(); | |
| 491 i != empty_url_ids.end(); ++i) { | |
| 492 LOG(WARNING) << "Bookmark exists with no URL"; | |
| 493 if (!DeleteStarredEntryRow(*i)) { | |
| 494 NOTREACHED() << "Unable to delete bookmark"; | |
| 495 return false; | |
| 496 } | |
| 497 } | |
| 498 | |
| 499 // Make sure the visual order of the nodes is correct. | |
| 500 for (std::set<StarredNode*>::const_iterator i = roots->begin(); | |
| 501 i != roots->end(); ++i) { | |
| 502 if (!EnsureVisualOrder(*i)) | |
| 503 return false; | |
| 504 } | |
| 505 | |
| 506 // Move any unparented bookmarks to the bookmark bar. | |
| 507 { | |
| 508 std::set<StarredNode*>::iterator i = unparented_urls->begin(); | |
| 509 while (i != unparented_urls->end()) { | |
| 510 LOG(WARNING) << "Bookmark not in a bookmark folder found"; | |
| 511 if (!Move(*i, bookmark_node)) | |
| 512 return false; | |
| 513 unparented_urls->erase(i++); | |
| 514 } | |
| 515 } | |
| 516 | |
| 517 // Nuke any folders with duplicate ids. A duplicate id means there are two | |
| 518 // folders in the starred table with the same folder_id. We only keep the | |
| 519 // first folder, all other folders are removed. | |
| 520 for (std::set<StarID>::const_iterator i = folders_with_duplicate_ids.begin(); | |
| 521 i != folders_with_duplicate_ids.end(); ++i) { | |
| 522 LOG(WARNING) << "Duplicate folder id in bookmark database"; | |
| 523 if (!DeleteStarredEntryRow(*i)) { | |
| 524 NOTREACHED() << "Unable to delete folder"; | |
| 525 return false; | |
| 526 } | |
| 527 } | |
| 528 | |
| 529 // Move unparented user folders back to the bookmark bar. | |
| 530 { | |
| 531 std::set<StarredNode*>::iterator i = roots->begin(); | |
| 532 while (i != roots->end()) { | |
| 533 if ((*i)->value.type == StarredEntry::USER_FOLDER) { | |
| 534 LOG(WARNING) << "Bookmark folder not on bookmark bar found"; | |
| 535 if (!Move(*i, bookmark_node)) | |
| 536 return false; | |
| 537 roots->erase(i++); | |
| 538 } else { | |
| 539 ++i; | |
| 540 } | |
| 541 } | |
| 542 } | |
| 543 | |
| 544 return true; | |
| 545 } | |
| 546 | |
| 547 bool StarredURLDatabase::Move(StarredNode* source, StarredNode* new_parent) { | |
| 548 history::StarredEntry& entry = source->value; | |
| 549 entry.visual_order = new_parent->child_count(); | |
| 550 entry.parent_folder_id = new_parent->value.folder_id; | |
| 551 if (!UpdateStarredEntryRow(entry.id, entry.title, | |
| 552 entry.parent_folder_id, entry.visual_order, | |
| 553 entry.date_folder_modified)) { | |
| 554 NOTREACHED() << "Unable to move folder"; | |
| 555 return false; | |
| 556 } | |
| 557 new_parent->Add(source, new_parent->child_count()); | |
| 558 return true; | |
| 559 } | |
| 560 | |
| 561 bool StarredURLDatabase::MigrateBookmarksToFileImpl(const FilePath& path) { | |
| 562 std::vector<history::StarredEntry> entries; | |
| 563 if (!GetAllStarredEntries(&entries)) | |
| 564 return false; | |
| 565 | |
| 566 // Create the bookmark bar and other folder nodes. | |
| 567 history::StarredEntry entry; | |
| 568 entry.type = history::StarredEntry::BOOKMARK_BAR; | |
| 569 BookmarkNode bookmark_bar_node(0, GURL()); | |
| 570 ResetBookmarkNode(entry, &bookmark_bar_node); | |
| 571 entry.type = history::StarredEntry::OTHER; | |
| 572 BookmarkNode other_node(0, GURL()); | |
| 573 ResetBookmarkNode(entry, &other_node); | |
| 574 // NOTE(yfriedman): We don't do anything with the mobile node because it won't | |
| 575 // ever exist in the starred node DB. We only need to create it to pass to | |
| 576 // "encode". | |
| 577 entry.type = history::StarredEntry::MOBILE; | |
| 578 BookmarkNode mobile_node(0, GURL()); | |
| 579 ResetBookmarkNode(entry, &mobile_node); | |
| 580 | |
| 581 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; | |
| 582 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; | |
| 583 IDToNodeMap id_to_node_map; | |
| 584 | |
| 585 history::UIStarID other_folder_folder_id = 0; | |
| 586 history::StarID other_folder_id = 0; | |
| 587 | |
| 588 // Iterate through the entries building a mapping between folder_id and id. | |
| 589 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); | |
| 590 i != entries.end(); ++i) { | |
| 591 if (i->type != history::StarredEntry::URL) { | |
| 592 folder_id_to_id_map[i->folder_id] = i->id; | |
| 593 if (i->type == history::StarredEntry::OTHER) { | |
| 594 other_folder_id = i->id; | |
| 595 other_folder_folder_id = i->folder_id; | |
| 596 } | |
| 597 } | |
| 598 } | |
| 599 | |
| 600 // Register the bookmark bar and other folder nodes in the maps. | |
| 601 id_to_node_map[kBookmarkBarID] = &bookmark_bar_node; | |
| 602 folder_id_to_id_map[kBookmarkBarID] = kBookmarkBarID; | |
| 603 if (other_folder_folder_id) { | |
| 604 id_to_node_map[other_folder_id] = &other_node; | |
| 605 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; | |
| 606 } | |
| 607 | |
| 608 // Iterate through the entries again creating the nodes. | |
| 609 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); | |
| 610 i != entries.end(); ++i) { | |
| 611 if (!i->parent_folder_id) { | |
| 612 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || | |
| 613 i->type == history::StarredEntry::MOBILE || | |
| 614 i->type == history::StarredEntry::OTHER); | |
| 615 // Only entries with no parent should be the bookmark bar and other | |
| 616 // bookmarks folders. | |
| 617 continue; | |
| 618 } | |
| 619 | |
| 620 BookmarkNode* node = id_to_node_map[i->id]; | |
| 621 if (!node) { | |
| 622 // Creating a node results in creating the parent. As such, it is | |
| 623 // possible for the node representing a folder to have been created before | |
| 624 // encountering the details. | |
| 625 | |
| 626 // The created nodes are owned by the root node. | |
| 627 node = new BookmarkNode(i->url); | |
| 628 id_to_node_map[i->id] = node; | |
| 629 } | |
| 630 ResetBookmarkNode(*i, node); | |
| 631 | |
| 632 DCHECK(folder_id_to_id_map.find(i->parent_folder_id) != | |
| 633 folder_id_to_id_map.end()); | |
| 634 history::StarID parent_id = folder_id_to_id_map[i->parent_folder_id]; | |
| 635 BookmarkNode* parent = id_to_node_map[parent_id]; | |
| 636 if (!parent) { | |
| 637 // Haven't encountered the parent yet, create it now. | |
| 638 parent = new BookmarkNode(GURL()); | |
| 639 id_to_node_map[parent_id] = parent; | |
| 640 } | |
| 641 | |
| 642 // Add the node to its parent. |entries| is ordered by parent then | |
| 643 // visual order so that we know we maintain visual order by always adding | |
| 644 // to the end. | |
| 645 parent->Add(node, parent->child_count()); | |
| 646 } | |
| 647 | |
| 648 // Save to file. | |
| 649 BookmarkCodec encoder; | |
| 650 scoped_ptr<Value> encoded_bookmarks( | |
| 651 encoder.Encode(&bookmark_bar_node, &other_node, &mobile_node)); | |
| 652 std::string content; | |
| 653 base::JSONWriter::WriteWithOptions(encoded_bookmarks.get(), | |
| 654 base::JSONWriter::OPTIONS_PRETTY_PRINT, | |
| 655 &content); | |
| 656 | |
| 657 return (file_util::WriteFile(path, content.c_str(), | |
| 658 static_cast<int>(content.length())) != -1); | |
| 659 } | |
| 660 | |
| 661 } // namespace history | |
| OLD | NEW |