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