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