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