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" |
11 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
12 #include "base/stl_util-inl.h" | 12 #include "base/stl_util-inl.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/bookmarks/bookmark_codec.h" | 16 #include "chrome/browser/bookmarks/bookmark_codec.h" |
17 #include "chrome/browser/bookmarks/bookmark_model.h" | 17 #include "chrome/browser/bookmarks/bookmark_model.h" |
18 #include "chrome/browser/history/history.h" | 18 #include "chrome/browser/history/history.h" |
19 | 19 |
20 // The following table is used to store star (aka bookmark) information. This | 20 // The following table is used to store star (aka bookmark) information. This |
21 // class derives from URLDatabase, which has its own schema. | 21 // class derives from URLDatabase, which has its own schema. |
22 // | 22 // |
23 // starred | 23 // starred |
24 // id Unique identifier (primary key) for the entry. | 24 // id Unique identifier (primary key) for the entry. |
25 // type Type of entry, if 0 this corresponds to a URL, 1 for | 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 | 26 // a system folder, 2 for a user created folder, 3 for |
27 // other. | 27 // other. Note that 4 (synced) will never appear in the |
| 28 // database because it was added after this storage was |
| 29 // deprecated. |
28 // url_id ID of the url, only valid if type == 0 | 30 // url_id ID of the url, only valid if type == 0 |
29 // group_id ID of the folder, only valid if type != 0. This id comes | 31 // group_id ID of the folder, only valid if type != 0. This id comes |
30 // from the UI and is NOT the same as id. | 32 // from the UI and is NOT the same as id. |
31 // title User assigned title. | 33 // title User assigned title. |
32 // date_added Creation date. | 34 // date_added Creation date. |
33 // visual_order Visual order within parent. | 35 // visual_order Visual order within parent. |
34 // parent_id Folder ID of the parent this entry is contained in, if 0 | 36 // parent_id Folder ID of the parent this entry is contained in, if 0 |
35 // entry is not in a folder. | 37 // entry is not in a folder. |
36 // date_modified Time the folder was last modified. See comments in | 38 // date_modified Time the folder was last modified. See comments in |
37 // StarredEntry::date_folder_modified | 39 // StarredEntry::date_folder_modified |
(...skipping 500 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 return false; | 540 return false; |
539 | 541 |
540 // Create the bookmark bar and other folder nodes. | 542 // Create the bookmark bar and other folder nodes. |
541 history::StarredEntry entry; | 543 history::StarredEntry entry; |
542 entry.type = history::StarredEntry::BOOKMARK_BAR; | 544 entry.type = history::StarredEntry::BOOKMARK_BAR; |
543 BookmarkNode bookmark_bar_node(0, GURL()); | 545 BookmarkNode bookmark_bar_node(0, GURL()); |
544 bookmark_bar_node.Reset(entry); | 546 bookmark_bar_node.Reset(entry); |
545 entry.type = history::StarredEntry::OTHER; | 547 entry.type = history::StarredEntry::OTHER; |
546 BookmarkNode other_node(0, GURL()); | 548 BookmarkNode other_node(0, GURL()); |
547 other_node.Reset(entry); | 549 other_node.Reset(entry); |
| 550 // NOTE(yfriedman): We don't do anything with the synced star node because it |
| 551 // won't ever exist in the starred node DB. We only need to create it to pass |
| 552 // to "encode". |
| 553 entry.type = history::StarredEntry::SYNCED; |
| 554 BookmarkNode synced_node(0, GURL()); |
| 555 synced_node.Reset(entry); |
548 | 556 |
549 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; | 557 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; |
550 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; | 558 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; |
551 IDToNodeMap id_to_node_map; | 559 IDToNodeMap id_to_node_map; |
552 | 560 |
553 history::UIStarID other_folder_folder_id = 0; | 561 history::UIStarID other_folder_folder_id = 0; |
554 history::StarID other_folder_id = 0; | 562 history::StarID other_folder_id = 0; |
555 | 563 |
556 // Iterate through the entries building a mapping between folder_id and id. | 564 // Iterate through the entries building a mapping between folder_id and id. |
557 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); | 565 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); |
(...skipping 14 matching lines...) Expand all Loading... |
572 if (other_folder_folder_id) { | 580 if (other_folder_folder_id) { |
573 id_to_node_map[other_folder_id] = &other_node; | 581 id_to_node_map[other_folder_id] = &other_node; |
574 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; | 582 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; |
575 } | 583 } |
576 | 584 |
577 // Iterate through the entries again creating the nodes. | 585 // Iterate through the entries again creating the nodes. |
578 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); | 586 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); |
579 i != entries.end(); ++i) { | 587 i != entries.end(); ++i) { |
580 if (!i->parent_folder_id) { | 588 if (!i->parent_folder_id) { |
581 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || | 589 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || |
| 590 i->type == history::StarredEntry::SYNCED || |
582 i->type == history::StarredEntry::OTHER); | 591 i->type == history::StarredEntry::OTHER); |
583 // Only entries with no parent should be the bookmark bar and other | 592 // Only entries with no parent should be the bookmark bar and other |
584 // bookmarks folders. | 593 // bookmarks folders. |
585 continue; | 594 continue; |
586 } | 595 } |
587 | 596 |
588 BookmarkNode* node = id_to_node_map[i->id]; | 597 BookmarkNode* node = id_to_node_map[i->id]; |
589 if (!node) { | 598 if (!node) { |
590 // Creating a node results in creating the parent. As such, it is | 599 // 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 | 600 // possible for the node representing a folder to have been created before |
(...skipping 17 matching lines...) Expand all Loading... |
609 | 618 |
610 // Add the node to its parent. |entries| is ordered by parent then | 619 // 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 | 620 // visual order so that we know we maintain visual order by always adding |
612 // to the end. | 621 // to the end. |
613 parent->Add(node, parent->child_count()); | 622 parent->Add(node, parent->child_count()); |
614 } | 623 } |
615 | 624 |
616 // Save to file. | 625 // Save to file. |
617 BookmarkCodec encoder; | 626 BookmarkCodec encoder; |
618 scoped_ptr<Value> encoded_bookmarks( | 627 scoped_ptr<Value> encoded_bookmarks( |
619 encoder.Encode(&bookmark_bar_node, &other_node)); | 628 encoder.Encode(&bookmark_bar_node, &other_node, &synced_node)); |
620 std::string content; | 629 std::string content; |
621 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); | 630 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); |
622 | 631 |
623 return (file_util::WriteFile(path, content.c_str(), | 632 return (file_util::WriteFile(path, content.c_str(), |
624 static_cast<int>(content.length())) != -1); | 633 static_cast<int>(content.length())) != -1); |
625 } | 634 } |
626 | 635 |
627 } // namespace history | 636 } // namespace history |
OLD | NEW |