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/12 16:29:13
At one point bookmarks were saved in the db (this
Yaron
2011/05/12 18:19:23
So we never actually write to this DB anymore, you
| |
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; | |
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 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
449 0, entry.folder_id, 0, UTF8ToUTF16("other"), base::Time::Now(), 0, | 455 0, entry.folder_id, 0, UTF8ToUTF16("other"), base::Time::Now(), 0, |
450 history::StarredEntry::OTHER); | 456 history::StarredEntry::OTHER); |
451 if (!entry.id) { | 457 if (!entry.id) { |
452 NOTREACHED() << "Unable to create other bookmarks folder"; | 458 NOTREACHED() << "Unable to create other bookmarks folder"; |
453 return false; | 459 return false; |
454 } | 460 } |
455 entry.type = StarredEntry::OTHER; | 461 entry.type = StarredEntry::OTHER; |
456 StarredNode* other_node = new StarredNode(entry); | 462 StarredNode* other_node = new StarredNode(entry); |
457 roots->insert(other_node); | 463 roots->insert(other_node); |
458 } | 464 } |
459 | 465 |
Yaron
2011/05/12 18:19:23
Looks like I should just revert this part?
| |
466 // Make sure the synced node exists. | |
467 StarredNode* synced_node = GetNodeByType(*roots, StarredEntry::SYNCED); | |
468 if (!synced_node) { | |
469 LOG(WARNING) << "No bookmark synced folder in database"; | |
470 StarredEntry entry; | |
471 entry.folder_id = GetMaxFolderID() + 1; | |
472 if (entry.folder_id == 2) { | |
473 NOTREACHED() << "Unable to get new id for synced bookmarks folder"; | |
474 return false; | |
475 } | |
476 entry.id = CreateStarredEntryRow( | |
477 0, entry.folder_id, 0, UTF8ToUTF16("synced"), base::Time::Now(), 0, | |
478 history::StarredEntry::SYNCED); | |
479 if (!entry.id) { | |
480 NOTREACHED() << "Unable to create synced bookmarks folder"; | |
481 return false; | |
482 } | |
483 entry.type = StarredEntry::SYNCED; | |
484 StarredNode* synced_node = new StarredNode(entry); | |
485 roots->insert(synced_node); | |
486 } | |
487 | |
460 // We could potentially make sure only one folder with type | 488 // We could potentially make sure only one folder with type |
461 // BOOKMARK_BAR/OTHER, but history backend enforces this. | 489 // BOOKMARK_BAR/OTHER, but history backend enforces this. |
462 | 490 |
463 // Nuke any entries with no url. | 491 // Nuke any entries with no url. |
464 for (std::set<StarID>::const_iterator i = empty_url_ids.begin(); | 492 for (std::set<StarID>::const_iterator i = empty_url_ids.begin(); |
465 i != empty_url_ids.end(); ++i) { | 493 i != empty_url_ids.end(); ++i) { |
466 LOG(WARNING) << "Bookmark exists with no URL"; | 494 LOG(WARNING) << "Bookmark exists with no URL"; |
467 if (!DeleteStarredEntryRow(*i)) { | 495 if (!DeleteStarredEntryRow(*i)) { |
468 NOTREACHED() << "Unable to delete bookmark"; | 496 NOTREACHED() << "Unable to delete bookmark"; |
469 return false; | 497 return false; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 return false; | 566 return false; |
539 | 567 |
540 // Create the bookmark bar and other folder nodes. | 568 // Create the bookmark bar and other folder nodes. |
541 history::StarredEntry entry; | 569 history::StarredEntry entry; |
542 entry.type = history::StarredEntry::BOOKMARK_BAR; | 570 entry.type = history::StarredEntry::BOOKMARK_BAR; |
543 BookmarkNode bookmark_bar_node(0, GURL()); | 571 BookmarkNode bookmark_bar_node(0, GURL()); |
544 bookmark_bar_node.Reset(entry); | 572 bookmark_bar_node.Reset(entry); |
545 entry.type = history::StarredEntry::OTHER; | 573 entry.type = history::StarredEntry::OTHER; |
546 BookmarkNode other_node(0, GURL()); | 574 BookmarkNode other_node(0, GURL()); |
547 other_node.Reset(entry); | 575 other_node.Reset(entry); |
576 entry.type = history::StarredEntry::SYNCED; | |
577 BookmarkNode synced_node(0, GURL()); | |
578 synced_node.Reset(entry); | |
548 | 579 |
549 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; | 580 std::map<history::UIStarID, history::StarID> folder_id_to_id_map; |
550 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; | 581 typedef std::map<history::StarID, BookmarkNode*> IDToNodeMap; |
551 IDToNodeMap id_to_node_map; | 582 IDToNodeMap id_to_node_map; |
552 | 583 |
584 history::UIStarID synced_folder_folder_id = 0; | |
585 history::StarID synced_folder_id = 0; | |
586 | |
553 history::UIStarID other_folder_folder_id = 0; | 587 history::UIStarID other_folder_folder_id = 0; |
554 history::StarID other_folder_id = 0; | 588 history::StarID other_folder_id = 0; |
555 | 589 |
556 // Iterate through the entries building a mapping between folder_id and id. | 590 // Iterate through the entries building a mapping between folder_id and id. |
557 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); | 591 for (std::vector<history::StarredEntry>::const_iterator i = entries.begin(); |
558 i != entries.end(); ++i) { | 592 i != entries.end(); ++i) { |
559 if (i->type != history::StarredEntry::URL) { | 593 if (i->type != history::StarredEntry::URL) { |
560 folder_id_to_id_map[i->folder_id] = i->id; | 594 folder_id_to_id_map[i->folder_id] = i->id; |
561 if (i->type == history::StarredEntry::OTHER) { | 595 if (i->type == history::StarredEntry::OTHER) { |
562 other_folder_id = i->id; | 596 other_folder_id = i->id; |
563 other_folder_folder_id = i->folder_id; | 597 other_folder_folder_id = i->folder_id; |
564 } | 598 } |
599 if (i->type == history::StarredEntry::SYNCED) { | |
600 synced_folder_id = i->id; | |
601 synced_folder_folder_id = i->folder_id; | |
602 } | |
565 } | 603 } |
566 } | 604 } |
567 | 605 |
568 // Register the bookmark bar and other folder nodes in the maps. | 606 // Register the bookmark bar and other folder nodes in the maps. |
569 id_to_node_map[HistoryService::kBookmarkBarID] = &bookmark_bar_node; | 607 id_to_node_map[HistoryService::kBookmarkBarID] = &bookmark_bar_node; |
570 folder_id_to_id_map[HistoryService::kBookmarkBarID] = | 608 folder_id_to_id_map[HistoryService::kBookmarkBarID] = |
571 HistoryService::kBookmarkBarID; | 609 HistoryService::kBookmarkBarID; |
572 if (other_folder_folder_id) { | 610 if (other_folder_folder_id) { |
573 id_to_node_map[other_folder_id] = &other_node; | 611 id_to_node_map[other_folder_id] = &other_node; |
574 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; | 612 folder_id_to_id_map[other_folder_folder_id] = other_folder_id; |
575 } | 613 } |
614 if (synced_folder_folder_id) { | |
615 id_to_node_map[synced_folder_id] = &synced_node; | |
616 folder_id_to_id_map[synced_folder_folder_id] = synced_folder_id; | |
617 } | |
576 | 618 |
577 // Iterate through the entries again creating the nodes. | 619 // Iterate through the entries again creating the nodes. |
578 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); | 620 for (std::vector<history::StarredEntry>::iterator i = entries.begin(); |
579 i != entries.end(); ++i) { | 621 i != entries.end(); ++i) { |
580 if (!i->parent_folder_id) { | 622 if (!i->parent_folder_id) { |
581 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || | 623 DCHECK(i->type == history::StarredEntry::BOOKMARK_BAR || |
624 i->type == history::StarredEntry::SYNCED || | |
582 i->type == history::StarredEntry::OTHER); | 625 i->type == history::StarredEntry::OTHER); |
583 // Only entries with no parent should be the bookmark bar and other | 626 // Only entries with no parent should be the bookmark bar and other |
584 // bookmarks folders. | 627 // bookmarks folders. |
585 continue; | 628 continue; |
586 } | 629 } |
587 | 630 |
588 BookmarkNode* node = id_to_node_map[i->id]; | 631 BookmarkNode* node = id_to_node_map[i->id]; |
589 if (!node) { | 632 if (!node) { |
590 // Creating a node results in creating the parent. As such, it is | 633 // 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 | 634 // possible for the node representing a folder to have been created before |
(...skipping 17 matching lines...) Expand all Loading... | |
609 | 652 |
610 // Add the node to its parent. |entries| is ordered by parent then | 653 // 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 | 654 // visual order so that we know we maintain visual order by always adding |
612 // to the end. | 655 // to the end. |
613 parent->Add(node, parent->child_count()); | 656 parent->Add(node, parent->child_count()); |
614 } | 657 } |
615 | 658 |
616 // Save to file. | 659 // Save to file. |
617 BookmarkCodec encoder; | 660 BookmarkCodec encoder; |
618 scoped_ptr<Value> encoded_bookmarks( | 661 scoped_ptr<Value> encoded_bookmarks( |
619 encoder.Encode(&bookmark_bar_node, &other_node)); | 662 encoder.Encode(&bookmark_bar_node, &other_node, &synced_node)); |
620 std::string content; | 663 std::string content; |
621 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); | 664 base::JSONWriter::Write(encoded_bookmarks.get(), true, &content); |
622 | 665 |
623 return (file_util::WriteFile(path, content.c_str(), | 666 return (file_util::WriteFile(path, content.c_str(), |
624 static_cast<int>(content.length())) != -1); | 667 static_cast<int>(content.length())) != -1); |
625 } | 668 } |
626 | 669 |
627 } // namespace history | 670 } // namespace history |
OLD | NEW |