Chromium Code Reviews| 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, 4 for synced. |
|
sky
2011/05/17 15:59:08
This will never be in the database, so you can rem
Yaron
2011/05/17 19:17:33
Done.
| |
| 28 // url_id ID of the url, only valid if type == 0 | 28 // 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 | 29 // 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. | 30 // from the UI and is NOT the same as id. |
| 31 // title User assigned title. | 31 // title User assigned title. |
| 32 // date_added Creation date. | 32 // date_added Creation date. |
| 33 // visual_order Visual order within parent. | 33 // visual_order Visual order within parent. |
| 34 // parent_id Folder ID of the parent this entry is contained in, if 0 | 34 // parent_id Folder ID of the parent this entry is contained in, if 0 |
| 35 // entry is not in a folder. | 35 // entry is not in a folder. |
| 36 // date_modified Time the folder was last modified. See comments in | 36 // date_modified Time the folder was last modified. See comments in |
| 37 // StarredEntry::date_folder_modified | 37 // StarredEntry::date_folder_modified |
| (...skipping 21 matching lines...) Expand all 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; | |
|
sky
2011/05/17 15:59:08
Remove this.
Yaron
2011/05/17 19:17:33
Done.
| |
| 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: | |
|
sky
2011/05/17 15:59:08
Remove this.
Yaron
2011/05/17 19:17:33
Done.
| |
| 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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 538 return false; | 544 return false; |
| 539 | 545 |
| 540 // Create the bookmark bar and other folder nodes. | 546 // Create the bookmark bar and other folder nodes. |
| 541 history::StarredEntry entry; | 547 history::StarredEntry entry; |
| 542 entry.type = history::StarredEntry::BOOKMARK_BAR; | 548 entry.type = history::StarredEntry::BOOKMARK_BAR; |
| 543 BookmarkNode bookmark_bar_node(0, GURL()); | 549 BookmarkNode bookmark_bar_node(0, GURL()); |
| 544 bookmark_bar_node.Reset(entry); | 550 bookmark_bar_node.Reset(entry); |
| 545 entry.type = history::StarredEntry::OTHER; | 551 entry.type = history::StarredEntry::OTHER; |
| 546 BookmarkNode other_node(0, GURL()); | 552 BookmarkNode other_node(0, GURL()); |
| 547 other_node.Reset(entry); | 553 other_node.Reset(entry); |
| 554 // NOTE(yfriedman): We don't do anything with the synced star node because it | |
| 555 // won't ever exist in the starred node DB. We only need to create it to pass | |
| 556 // to "encode". | |
| 557 entry.type = history::StarredEntry::SYNCED; | |
| 558 BookmarkNode synced_node(0, GURL()); | |
| 559 synced_node.Reset(entry); | |
| 548 | 560 |
| 549 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; | 561 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; |
| 550 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; | 562 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; |
| 551 IDToNodeMap id_to_node_map; | 563 IDToNodeMap id_to_node_map; |
| 552 | 564 |
| 553 history::UIStarID other_folder_folder_id = 0; | 565 history::UIStarID other_folder_folder_id = 0; |
| 554 history::StarID other_folder_id = 0; | 566 history::StarID other_folder_id = 0; |
| 555 | 567 |
| 556 // Iterate through the entries building a mapping between folder_id and id. | 568 // Iterate through the entries building a mapping between folder_id and id. |
| 557 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); | 569 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 572 if (other_folder_folder_id) { | 584 if (other_folder_folder_id) { |
| 573 id_to_node_map[other_folder_id] = &other_node; | 585 id_to_node_map[other_folder_id] = &other_node; |
| 574 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; | 586 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; |
| 575 } | 587 } |
| 576 | 588 |
| 577 // Iterate through the entries again creating the nodes. | 589 // Iterate through the entries again creating the nodes. |
| 578 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); | 590 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); |
| 579 i != entries.end(); ++i) { | 591 i != entries.end(); ++i) { |
| 580 if (!i->parent_folder_id) { | 592 if (!i->parent_folder_id) { |
| 581 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || | 593 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || |
| 594 i->type == history::StarredEntry::SYNCED || | |
| 582 i->type == history::StarredEntry::OTHER); | 595 i->type == history::StarredEntry::OTHER); |
| 583 // Only entries with no parent should be the bookmark bar and other | 596 // Only entries with no parent should be the bookmark bar and other |
| 584 // bookmarks folders. | 597 // bookmarks folders. |
| 585 continue; | 598 continue; |
| 586 } | 599 } |
| 587 | 600 |
| 588 BookmarkNode* node = id_to_node_map[i->id]; | 601 BookmarkNode* node = id_to_node_map[i->id]; |
| 589 if (!node) { | 602 if (!node) { |
| 590 // Creating a node results in creating the parent. As such, it is | 603 // 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 | 604 // possible for the node representing a folder to have been created before |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 609 | 622 |
| 610 // Add the node to its parent. |entries| is ordered by parent then | 623 // 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 | 624 // visual order so that we know we maintain visual order by always adding |
| 612 // to the end. | 625 // to the end. |
| 613 parent->Add(node, parent->child_count()); | 626 parent->Add(node, parent->child_count()); |
| 614 } | 627 } |
| 615 | 628 |
| 616 // Save to file. | 629 // Save to file. |
| 617 BookmarkCodec encoder; | 630 BookmarkCodec encoder; |
| 618 scoped_ptr<Value> encoded_bookmarks( | 631 scoped_ptr<Value> encoded_bookmarks( |
| 619 encoder.Encode(&bookmark_bar_node, &other_node)); | 632 encoder.Encode(&bookmark_bar_node, &other_node, &synced_node)); |
| 620 std::string content; | 633 std::string content; |
| 621 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); | 634 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); |
| 622 | 635 |
| 623 return (file_util::WriteFile(path, content.c_str(), | 636 return (file_util::WriteFile(path, content.c_str(), |
| 624 static_cast<int>(content.length())) != -1); | 637 static_cast<int>(content.length())) != -1); |
| 625 } | 638 } |
| 626 | 639 |
| 627 } // namespace history | 640 } // namespace history |
| OLD | NEW |