| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_HISTORY_STARRED_URL_DATABASE_H_ | |
| 6 #define CHROME_BROWSER_HISTORY_STARRED_URL_DATABASE_H_ | |
| 7 | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/compiler_specific.h" | |
| 12 #include "base/string16.h" | |
| 13 #include "chrome/browser/history/history_types.h" | |
| 14 #include "chrome/browser/history/url_database.h" | |
| 15 #include "ui/base/models/tree_node_model.h" | |
| 16 | |
| 17 class FilePath; | |
| 18 | |
| 19 namespace history { | |
| 20 | |
| 21 // Bookmarks were originally part of the url database, they have since been | |
| 22 // moved to a separate file. This file exists purely for historical reasons and | |
| 23 // contains just enough to allow migration. | |
| 24 class StarredURLDatabase : public URLDatabase { | |
| 25 public: | |
| 26 explicit StarredURLDatabase(sql::Connection* db); | |
| 27 virtual ~StarredURLDatabase(); | |
| 28 | |
| 29 // Writes bookmarks to the specified file. | |
| 30 bool MigrateBookmarksToFile(const FilePath& path); | |
| 31 | |
| 32 protected: | |
| 33 friend class StarredURLDatabaseTest; | |
| 34 | |
| 35 // ID (both star_id and folder_id) of the bookmark bar. | |
| 36 // This entry always exists. | |
| 37 static const int64 kBookmarkBarID; | |
| 38 | |
| 39 // Returns the database for the functions in this interface. | |
| 40 virtual sql::Connection& GetDB() OVERRIDE; | |
| 41 | |
| 42 private: | |
| 43 // Used when checking integrity of starred table. | |
| 44 typedef ui::TreeNodeWithValue<StarredEntry> StarredNode; | |
| 45 | |
| 46 // Makes sure the starred table is in a sane state. This does the following: | |
| 47 // . Makes sure there is a bookmark bar and other nodes. If no bookmark bar | |
| 48 // node is found, the table is dropped and recreated. | |
| 49 // . Removes any bookmarks with no URL. This can happen if a URL is removed | |
| 50 // from the urls table without updating the starred table correctly. | |
| 51 // . Makes sure the visual order of all nodes is correct. | |
| 52 // . Moves all bookmarks and folders that are not descendants of the bookmark | |
| 53 // bar or other folders to the bookmark bar. | |
| 54 // . Makes sure there isn't a cycle in the folders. A cycle means some folder | |
| 55 // has as its parent one of its children. | |
| 56 // | |
| 57 // This returns false if the starred table is in a bad state and couldn't | |
| 58 // be fixed, true otherwise. | |
| 59 // | |
| 60 // This should be invoked after migration. | |
| 61 bool EnsureStarredIntegrity(); | |
| 62 | |
| 63 // Gets all the starred entries. | |
| 64 bool GetAllStarredEntries(std::vector<StarredEntry>* entries); | |
| 65 | |
| 66 // Sets the title, parent_id, parent_folder_id, visual_order and date_modifed | |
| 67 // of the specified star entry. | |
| 68 // | |
| 69 // WARNING: Does not update the visual order. | |
| 70 bool UpdateStarredEntryRow(StarID star_id, | |
| 71 const string16& title, | |
| 72 UIStarID parent_folder_id, | |
| 73 int visual_order, | |
| 74 base::Time date_modified); | |
| 75 | |
| 76 // Adjusts the visual order of all children of parent_folder_id with a | |
| 77 // visual_order >= start_visual_order by delta. For example, | |
| 78 // AdjustStarredVisualOrder(10, 0, 1) increments the visual order all children | |
| 79 // of folder 10 with a visual order >= 0 by 1. | |
| 80 bool AdjustStarredVisualOrder(UIStarID parent_folder_id, | |
| 81 int start_visual_order, | |
| 82 int delta); | |
| 83 | |
| 84 // Creates a starred entry with the specified parameters in the database. | |
| 85 // Returns the newly created id, or 0 on failure. | |
| 86 // | |
| 87 // WARNING: Does not update the visual order. | |
| 88 StarID CreateStarredEntryRow(URLID url_id, | |
| 89 UIStarID folder_id, | |
| 90 UIStarID parent_folder_id, | |
| 91 const string16& title, | |
| 92 const base::Time& date_added, | |
| 93 int visual_order, | |
| 94 StarredEntry::Type type); | |
| 95 | |
| 96 // Deletes the entry from the starred database base on the starred id (NOT | |
| 97 // the url id). | |
| 98 // | |
| 99 // WARNING: Does not update the visual order. | |
| 100 bool DeleteStarredEntryRow(StarID star_id); | |
| 101 | |
| 102 // Gets the details for the specified star entry in entry. | |
| 103 bool GetStarredEntry(StarID star_id, StarredEntry* entry); | |
| 104 | |
| 105 // Creates a starred entry with the requested information. The structure will | |
| 106 // be updated with the ID of the newly created entry. The URL table will be | |
| 107 // updated to point to the entry. The URL row will be created if it doesn't | |
| 108 // exist. | |
| 109 // | |
| 110 // We currently only support one entry per URL. This URL should not already be | |
| 111 // starred when calling this function or it will fail and will return 0. | |
| 112 StarID CreateStarredEntry(StarredEntry* entry); | |
| 113 | |
| 114 // Returns the max folder id, or 0 if there is an error. | |
| 115 UIStarID GetMaxFolderID(); | |
| 116 | |
| 117 // Gets all the bookmarks and folders creating a StarredNode for each | |
| 118 // bookmark and folder. On success all the root nodes (bookmark bar node, | |
| 119 // other folder node, folders with no parent or folders with a parent that | |
| 120 // would make a cycle) are added to roots. | |
| 121 // | |
| 122 // If a folder_id occurs more than once, all but the first ones id is added to | |
| 123 // folders_with_duplicate_ids. | |
| 124 // | |
| 125 // All bookmarks not on the bookmark bar/other folder are added to | |
| 126 // unparented_urls. | |
| 127 // | |
| 128 // It's up to the caller to delete the nodes returned in roots and | |
| 129 // unparented_urls. | |
| 130 // | |
| 131 // This is used during integrity enforcing/checking of the starred table. | |
| 132 bool BuildStarNodes( | |
| 133 std::set<StarredNode*>* roots, | |
| 134 std::set<StarID>* folders_with_duplicate_ids, | |
| 135 std::set<StarredNode*>* unparented_urls, | |
| 136 std::set<StarID>* empty_url_ids); | |
| 137 | |
| 138 // Sets the visual order of all of node's children match the order in |node|. | |
| 139 // If the order differs, the database is updated. Returns false if the order | |
| 140 // differed and the db couldn't be updated. | |
| 141 bool EnsureVisualOrder(StarredNode* node); | |
| 142 | |
| 143 // Returns the first node in nodes with the specified type, or null if there | |
| 144 // is not a node with the specified type. | |
| 145 StarredNode* GetNodeByType( | |
| 146 const std::set<StarredNode*>& nodes, | |
| 147 StarredEntry::Type type); | |
| 148 | |
| 149 // Implementation for setting starred integrity. See description of | |
| 150 // EnsureStarredIntegrity for the details of what this does. | |
| 151 // | |
| 152 // All entries in roots that are not the bookmark bar and other node are | |
| 153 // moved to be children of the bookmark bar node. Similarly all nodes | |
| 154 // in unparented_urls are moved to be children of the bookmark bar. | |
| 155 // | |
| 156 // Returns true on success, false if the starred table is in a bad state and | |
| 157 // couldn't be repaired. | |
| 158 bool EnsureStarredIntegrityImpl( | |
| 159 std::set<StarredNode*>* roots, | |
| 160 const std::set<StarID>& folders_with_duplicate_ids, | |
| 161 std::set<StarredNode*>* unparented_urls, | |
| 162 const std::set<StarID>& empty_url_ids); | |
| 163 | |
| 164 // Resets the visual order and parent_folder_id of source's StarredEntry | |
| 165 // and adds it to the end of new_parent's children. | |
| 166 // | |
| 167 // This is used if the starred table is an unexpected state and an entry | |
| 168 // needs to be moved. | |
| 169 bool Move(StarredNode* source, StarredNode* new_parent); | |
| 170 | |
| 171 // Does the work of migrating bookmarks to a temporary file that | |
| 172 // BookmarkStorage will read from. | |
| 173 bool MigrateBookmarksToFileImpl(const FilePath& path); | |
| 174 | |
| 175 sql::Connection* db_; | |
| 176 | |
| 177 DISALLOW_COPY_AND_ASSIGN(StarredURLDatabase); | |
| 178 }; | |
| 179 | |
| 180 } // namespace history | |
| 181 | |
| 182 #endif // CHROME_BROWSER_HISTORY_STARRED_URL_DATABASE_H_ | |
| OLD | NEW |