| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/history/starred_url_database.h" | 5 #include "chrome/browser/history/starred_url_database.h" |
| 6 | 6 |
| 7 #include "app/sql/statement.h" | 7 #include "app/sql/statement.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 break; | 59 break; |
| 60 case 1: | 60 case 1: |
| 61 entry->type = history::StarredEntry::BOOKMARK_BAR; | 61 entry->type = history::StarredEntry::BOOKMARK_BAR; |
| 62 break; | 62 break; |
| 63 case 2: | 63 case 2: |
| 64 entry->type = history::StarredEntry::USER_FOLDER; | 64 entry->type = history::StarredEntry::USER_FOLDER; |
| 65 break; | 65 break; |
| 66 case 3: | 66 case 3: |
| 67 entry->type = history::StarredEntry::OTHER; | 67 entry->type = history::StarredEntry::OTHER; |
| 68 break; | 68 break; |
| 69 case 4: |
| 70 entry->type = history::StarredEntry::SYNCED; |
| 71 break; |
| 69 default: | 72 default: |
| 70 NOTREACHED(); | 73 NOTREACHED(); |
| 71 break; | 74 break; |
| 72 } | 75 } |
| 73 entry->title = s.ColumnString16(2); | 76 entry->title = s.ColumnString16(2); |
| 74 entry->date_added = base::Time::FromInternalValue(s.ColumnInt64(3)); | 77 entry->date_added = base::Time::FromInternalValue(s.ColumnInt64(3)); |
| 75 entry->visual_order = s.ColumnInt(4); | 78 entry->visual_order = s.ColumnInt(4); |
| 76 entry->parent_folder_id = s.ColumnInt64(5); | 79 entry->parent_folder_id = s.ColumnInt64(5); |
| 77 entry->url_id = s.ColumnInt64(7); | 80 entry->url_id = s.ColumnInt64(7); |
| 78 entry->folder_id = s.ColumnInt64(8); | 81 entry->folder_id = s.ColumnInt64(8); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 break; | 209 break; |
| 207 case history::StarredEntry::BOOKMARK_BAR: | 210 case history::StarredEntry::BOOKMARK_BAR: |
| 208 statement.BindInt(0, 1); | 211 statement.BindInt(0, 1); |
| 209 break; | 212 break; |
| 210 case history::StarredEntry::USER_FOLDER: | 213 case history::StarredEntry::USER_FOLDER: |
| 211 statement.BindInt(0, 2); | 214 statement.BindInt(0, 2); |
| 212 break; | 215 break; |
| 213 case history::StarredEntry::OTHER: | 216 case history::StarredEntry::OTHER: |
| 214 statement.BindInt(0, 3); | 217 statement.BindInt(0, 3); |
| 215 break; | 218 break; |
| 219 case history::StarredEntry::SYNCED: |
| 220 statement.BindInt(0, 4); |
| 221 break; |
| 216 default: | 222 default: |
| 217 NOTREACHED(); | 223 NOTREACHED(); |
| 218 } | 224 } |
| 219 statement.BindInt64(1, url_id); | 225 statement.BindInt64(1, url_id); |
| 220 statement.BindInt64(2, folder_id); | 226 statement.BindInt64(2, folder_id); |
| 221 statement.BindString16(3, title); | 227 statement.BindString16(3, title); |
| 222 statement.BindInt64(4, date_added.ToInternalValue()); | 228 statement.BindInt64(4, date_added.ToInternalValue()); |
| 223 statement.BindInt(5, visual_order); | 229 statement.BindInt(5, visual_order); |
| 224 statement.BindInt64(6, parent_folder_id); | 230 statement.BindInt64(6, parent_folder_id); |
| 225 statement.BindInt64(7, base::Time().ToInternalValue()); | 231 statement.BindInt64(7, base::Time().ToInternalValue()); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 StarredNode* bookmark_node = | 434 StarredNode* bookmark_node = |
| 429 GetNodeByType(*roots, StarredEntry::BOOKMARK_BAR); | 435 GetNodeByType(*roots, StarredEntry::BOOKMARK_BAR); |
| 430 if (!bookmark_node) { | 436 if (!bookmark_node) { |
| 431 LOG(WARNING) << "No bookmark bar folder in database"; | 437 LOG(WARNING) << "No bookmark bar folder in database"; |
| 432 // If there is no bookmark bar entry in the db things are really | 438 // If there is no bookmark bar entry in the db things are really |
| 433 // screwed. Return false, which won't trigger migration and we'll just | 439 // screwed. Return false, which won't trigger migration and we'll just |
| 434 // drop the tables. | 440 // drop the tables. |
| 435 return false; | 441 return false; |
| 436 } | 442 } |
| 437 | 443 |
| 444 // Make sure the synced node exists. |
| 445 StarredNode* synced_node = GetNodeByType(*roots, StarredEntry::SYNCED); |
| 446 if (!synced_node) { |
| 447 LOG(WARNING) << "No bookmark synced folder in database"; |
| 448 StarredEntry entry; |
| 449 entry.folder_id = GetMaxFolderID() + 1; |
| 450 // TODO (yfriedman): Is this index right? |
| 451 if (entry.folder_id == 1) { |
| 452 NOTREACHED() << "Unable to get new id for synced bookmarks folder"; |
| 453 return false; |
| 454 } |
| 455 entry.id = CreateStarredEntryRow( |
| 456 0, entry.folder_id, 0, UTF8ToUTF16("synced"), base::Time::Now(), 0, |
| 457 history::StarredEntry::SYNCED); |
| 458 if (!entry.id) { |
| 459 NOTREACHED() << "Unable to create synced bookmarks folder"; |
| 460 return false; |
| 461 } |
| 462 entry.type = StarredEntry::SYNCED; |
| 463 StarredNode* synced_node = new StarredNode(entry); |
| 464 roots->insert(synced_node); |
| 465 } |
| 466 |
| 438 // Make sure the other node exists. | 467 // Make sure the other node exists. |
| 439 StarredNode* other_node = GetNodeByType(*roots, StarredEntry::OTHER); | 468 StarredNode* other_node = GetNodeByType(*roots, StarredEntry::OTHER); |
| 440 if (!other_node) { | 469 if (!other_node) { |
| 441 LOG(WARNING) << "No bookmark other folder in database"; | 470 LOG(WARNING) << "No bookmark other folder in database"; |
| 442 StarredEntry entry; | 471 StarredEntry entry; |
| 443 entry.folder_id = GetMaxFolderID() + 1; | 472 entry.folder_id = GetMaxFolderID() + 1; |
| 444 if (entry.folder_id == 1) { | 473 if (entry.folder_id == 1) { |
| 445 NOTREACHED() << "Unable to get new id for other bookmarks folder"; | 474 NOTREACHED() << "Unable to get new id for other bookmarks folder"; |
| 446 return false; | 475 return false; |
| 447 } | 476 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 538 return false; | 567 return false; |
| 539 | 568 |
| 540 // Create the bookmark bar and other folder nodes. | 569 // Create the bookmark bar and other folder nodes. |
| 541 history::StarredEntry entry; | 570 history::StarredEntry entry; |
| 542 entry.type = history::StarredEntry::BOOKMARK_BAR; | 571 entry.type = history::StarredEntry::BOOKMARK_BAR; |
| 543 BookmarkNode bookmark_bar_node(0, GURL()); | 572 BookmarkNode bookmark_bar_node(0, GURL()); |
| 544 bookmark_bar_node.Reset(entry); | 573 bookmark_bar_node.Reset(entry); |
| 545 entry.type = history::StarredEntry::OTHER; | 574 entry.type = history::StarredEntry::OTHER; |
| 546 BookmarkNode other_node(0, GURL()); | 575 BookmarkNode other_node(0, GURL()); |
| 547 other_node.Reset(entry); | 576 other_node.Reset(entry); |
| 577 entry.type = history::StarredEntry::SYNCED; |
| 578 BookmarkNode synced_node(0, GURL()); |
| 579 synced_node.Reset(entry); |
| 548 | 580 |
| 549 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; | 581 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; |
| 550 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; | 582 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; |
| 551 IDToNodeMap id_to_node_map; | 583 IDToNodeMap id_to_node_map; |
| 552 | 584 |
| 585 history::UIStarID synced_folder_folder_id = 0; |
| 586 history::StarID synced_folder_id = 0; |
| 587 |
| 553 history::UIStarID other_folder_folder_id = 0; | 588 history::UIStarID other_folder_folder_id = 0; |
| 554 history::StarID other_folder_id = 0; | 589 history::StarID other_folder_id = 0; |
| 555 | 590 |
| 556 // Iterate through the entries building a mapping between folder_id and id. | 591 // Iterate through the entries building a mapping between folder_id and id. |
| 557 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); | 592 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); |
| 558 i != entries.end(); ++i) { | 593 i != entries.end(); ++i) { |
| 559 if (i->type != history::StarredEntry::URL) { | 594 if (i->type != history::StarredEntry::URL) { |
| 560 folder_id_to_id_map[i->folder_id] = i->id; | 595 folder_id_to_id_map[i->folder_id] = i->id; |
| 561 if (i->type == history::StarredEntry::OTHER) { | 596 if (i->type == history::StarredEntry::OTHER) { |
| 562 other_folder_id = i->id; | 597 other_folder_id = i->id; |
| 563 other_folder_folder_id = i->folder_id; | 598 other_folder_folder_id = i->folder_id; |
| 564 } | 599 } |
| 600 if (i->type == history::StarredEntry::SYNCED) { |
| 601 synced_folder_id = i->id; |
| 602 synced_folder_folder_id = i->folder_id; |
| 603 } |
| 565 } | 604 } |
| 566 } | 605 } |
| 567 | 606 |
| 568 // Register the bookmark bar and other folder nodes in the maps. | 607 // Register the bookmark bar and other folder nodes in the maps. |
| 569 id_to_node_map[HistoryService::kBookmarkBarID] = &bookmark_bar_node; | 608 id_to_node_map[HistoryService::kBookmarkBarID] = &bookmark_bar_node; |
| 570 folder_id_to_id_map[HistoryService::kBookmarkBarID] = | 609 folder_id_to_id_map[HistoryService::kBookmarkBarID] = |
| 571 HistoryService::kBookmarkBarID; | 610 HistoryService::kBookmarkBarID; |
| 572 if (other_folder_folder_id) { | 611 if (other_folder_folder_id) { |
| 573 id_to_node_map[other_folder_id] = &other_node; | 612 id_to_node_map[other_folder_id] = &other_node; |
| 574 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; | 613 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; |
| 575 } | 614 } |
| 615 if (synced_folder_folder_id) { |
| 616 id_to_node_map[synced_folder_id] = &synced_node; |
| 617 folder_id_to_id_map[synced_folder_folder_id] = synced_folder_id; |
| 618 } |
| 576 | 619 |
| 577 // Iterate through the entries again creating the nodes. | 620 // Iterate through the entries again creating the nodes. |
| 578 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); | 621 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); |
| 579 i != entries.end(); ++i) { | 622 i != entries.end(); ++i) { |
| 580 if (!i->parent_folder_id) { | 623 if (!i->parent_folder_id) { |
| 581 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || | 624 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || |
| 625 i->type == history::StarredEntry::SYNCED || |
| 582 i->type == history::StarredEntry::OTHER); | 626 i->type == history::StarredEntry::OTHER); |
| 583 // Only entries with no parent should be the bookmark bar and other | 627 // Only entries with no parent should be the bookmark bar and other |
| 584 // bookmarks folders. | 628 // bookmarks folders. |
| 585 continue; | 629 continue; |
| 586 } | 630 } |
| 587 | 631 |
| 588 BookmarkNode* node = id_to_node_map[i->id]; | 632 BookmarkNode* node = id_to_node_map[i->id]; |
| 589 if (!node) { | 633 if (!node) { |
| 590 // Creating a node results in creating the parent. As such, it is | 634 // Creating a node results in creating the parent. As such, it is |
| 591 // possible for the node representing a folder to have been created before | 635 // possible for the node representing a folder to have been created before |
| (...skipping 17 matching lines...) Expand all Loading... |
| 609 | 653 |
| 610 // Add the node to its parent. |entries| is ordered by parent then | 654 // Add the node to its parent. |entries| is ordered by parent then |
| 611 // visual order so that we know we maintain visual order by always adding | 655 // visual order so that we know we maintain visual order by always adding |
| 612 // to the end. | 656 // to the end. |
| 613 parent->Add(node, parent->child_count()); | 657 parent->Add(node, parent->child_count()); |
| 614 } | 658 } |
| 615 | 659 |
| 616 // Save to file. | 660 // Save to file. |
| 617 BookmarkCodec encoder; | 661 BookmarkCodec encoder; |
| 618 scoped_ptr<Value> encoded_bookmarks( | 662 scoped_ptr<Value> encoded_bookmarks( |
| 619 encoder.Encode(&bookmark_bar_node, &other_node)); | 663 encoder.Encode(&bookmark_bar_node, &other_node, &synced_node)); |
| 620 std::string content; | 664 std::string content; |
| 621 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); | 665 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); |
| 622 | 666 |
| 623 return (file_util::WriteFile(path, content.c_str(), | 667 return (file_util::WriteFile(path, content.c_str(), |
| 624 static_cast<int>(content.length())) != -1); | 668 static_cast<int>(content.length())) != -1); |
| 625 } | 669 } |
| 626 | 670 |
| 627 } // namespace history | 671 } // namespace history |
| OLD | NEW |