| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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_URL_DATABASE_H_ | |
| 6 #define CHROME_BROWSER_HISTORY_URL_DATABASE_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "chrome/browser/history/history_types.h" | |
| 10 #include "chrome/browser/search_engines/template_url_id.h" | |
| 11 #include "sql/statement.h" | |
| 12 | |
| 13 class GURL; | |
| 14 | |
| 15 namespace sql { | |
| 16 class Connection; | |
| 17 } | |
| 18 | |
| 19 namespace history { | |
| 20 | |
| 21 class VisitDatabase; // For friend statement. | |
| 22 | |
| 23 // Encapsulates an SQL database that holds URL info. This is a subset of the | |
| 24 // full history data. We split this class' functionality out from the larger | |
| 25 // HistoryDatabase class to support maintaining separate databases of URLs with | |
| 26 // different capabilities (for example, in-memory, or archived). | |
| 27 // | |
| 28 // This is refcounted to support calling InvokeLater() with some of its methods | |
| 29 // (necessary to maintain ordering of DB operations). | |
| 30 class URLDatabase { | |
| 31 public: | |
| 32 // Must call CreateURLTable() and CreateURLIndexes() before using to make | |
| 33 // sure the database is initialized. | |
| 34 URLDatabase(); | |
| 35 | |
| 36 // This object must be destroyed on the thread where all accesses are | |
| 37 // happening to avoid thread-safety problems. | |
| 38 virtual ~URLDatabase(); | |
| 39 | |
| 40 // Converts a GURL to a string used in the history database. We plan to | |
| 41 // do more complex operations than just getting the spec out involving | |
| 42 // punycode, so this function should be used instead of url.spec() when | |
| 43 // interacting with the database. | |
| 44 // | |
| 45 // TODO(brettw) this should be moved out of the public section and the | |
| 46 // entire public HistoryDatabase interface should use GURL. This should | |
| 47 // also probably return a string instead since that is what the DB uses | |
| 48 // internally and we can avoid the extra conversion. | |
| 49 static std::string GURLToDatabaseURL(const GURL& url); | |
| 50 | |
| 51 // URL table functions ------------------------------------------------------- | |
| 52 | |
| 53 // Looks up a url given an id. Fills info with the data. Returns true on | |
| 54 // success and false otherwise. | |
| 55 bool GetURLRow(URLID url_id, URLRow* info); | |
| 56 | |
| 57 // Looks up all urls that were typed in manually. Fills info with the data. | |
| 58 // Returns true on success and false otherwise. | |
| 59 bool GetAllTypedUrls(URLRows* urls); | |
| 60 | |
| 61 // Looks up the given URL and if it exists, fills the given pointers with the | |
| 62 // associated info and returns the ID of that URL. If the info pointer is | |
| 63 // NULL, no information about the URL will be filled in, only the ID will be | |
| 64 // returned. Returns 0 if the URL was not found. | |
| 65 URLID GetRowForURL(const GURL& url, URLRow* info); | |
| 66 | |
| 67 // Given an already-existing row in the URL table, updates that URL's stats. | |
| 68 // This can not change the URL. Returns true on success. | |
| 69 // | |
| 70 // This will NOT update the title used for full text indexing. If you are | |
| 71 // setting the title, call SetPageIndexedData with the new title. | |
| 72 bool UpdateURLRow(URLID url_id, const URLRow& info); | |
| 73 | |
| 74 // Adds a line to the URL database with the given information and returns the | |
| 75 // row ID. A row with the given URL must not exist. Returns 0 on error. | |
| 76 // | |
| 77 // This does NOT add a row to the full text search database. Use | |
| 78 // HistoryDatabase::SetPageIndexedData to do this. | |
| 79 URLID AddURL(const URLRow& info) { | |
| 80 return AddURLInternal(info, false); | |
| 81 } | |
| 82 | |
| 83 // Delete the row of the corresponding URL. Only the row in the URL table | |
| 84 // will be deleted, not any other data that may refer to it. Returns true if | |
| 85 // the row existed and was deleted. | |
| 86 bool DeleteURLRow(URLID id); | |
| 87 | |
| 88 // URL mass-deleting --------------------------------------------------------- | |
| 89 | |
| 90 // Begins the mass-deleting operation by creating a temporary URL table. | |
| 91 // The caller than adds the URLs it wants to preseve to the temporary table, | |
| 92 // and then deletes everything else by calling CommitTemporaryURLTable(). | |
| 93 // Returns true on success. | |
| 94 bool CreateTemporaryURLTable(); | |
| 95 | |
| 96 // Adds a row to the temporary URL table. This must be called between | |
| 97 // CreateTemporaryURLTable() and CommitTemporaryURLTable() (see those for more | |
| 98 // info). The ID of the URL will change in the temporary table, so the new ID | |
| 99 // is returned. Returns 0 on failure. | |
| 100 URLID AddTemporaryURL(const URLRow& row) { | |
| 101 return AddURLInternal(row, true); | |
| 102 } | |
| 103 | |
| 104 // Ends the mass-deleting by replacing the original URL table with the | |
| 105 // temporary one created in CreateTemporaryURLTable. Returns true on success. | |
| 106 // | |
| 107 // This function does not create the supplimentary indices. It is virtual so | |
| 108 // that the main history database can provide this additional behavior. | |
| 109 virtual bool CommitTemporaryURLTable(); | |
| 110 | |
| 111 // Enumeration --------------------------------------------------------------- | |
| 112 | |
| 113 // A basic enumerator to enumerate urls database. | |
| 114 class URLEnumeratorBase { | |
| 115 public: | |
| 116 URLEnumeratorBase(); | |
| 117 virtual ~URLEnumeratorBase(); | |
| 118 | |
| 119 private: | |
| 120 friend class URLDatabase; | |
| 121 | |
| 122 bool initialized_; | |
| 123 sql::Statement statement_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(URLEnumeratorBase); | |
| 126 }; | |
| 127 | |
| 128 // A basic enumerator to enumerate urls | |
| 129 class URLEnumerator : public URLEnumeratorBase { | |
| 130 public: | |
| 131 URLEnumerator(); | |
| 132 | |
| 133 // Retreives the next url. Returns false if no more urls are available | |
| 134 bool GetNextURL(history::URLRow* r); | |
| 135 | |
| 136 private: | |
| 137 DISALLOW_COPY_AND_ASSIGN(URLEnumerator); | |
| 138 }; | |
| 139 | |
| 140 // A basic enumerator to enumerate icon mapping, it is only used for icon | |
| 141 // mapping migration. | |
| 142 class IconMappingEnumerator : public URLEnumeratorBase { | |
| 143 public: | |
| 144 IconMappingEnumerator(); | |
| 145 | |
| 146 // Retreives the next url. Returns false if no more urls are available | |
| 147 bool GetNextIconMapping(IconMapping* r); | |
| 148 | |
| 149 private: | |
| 150 DISALLOW_COPY_AND_ASSIGN(IconMappingEnumerator); | |
| 151 }; | |
| 152 | |
| 153 // Initializes the given enumerator to enumerator all URLs in the database. | |
| 154 bool InitURLEnumeratorForEverything(URLEnumerator* enumerator); | |
| 155 | |
| 156 // Initializes the given enumerator to enumerator all URLs in the database | |
| 157 // that are historically significant: ones having been visited within 3 days, | |
| 158 // having their URL manually typed more than once, or having been visited | |
| 159 // more than 3 times. | |
| 160 bool InitURLEnumeratorForSignificant(URLEnumerator* enumerator); | |
| 161 | |
| 162 // Favicons ------------------------------------------------------------------ | |
| 163 | |
| 164 // Autocomplete -------------------------------------------------------------- | |
| 165 | |
| 166 // Fills the given array with URLs matching the given prefix. They will be | |
| 167 // sorted by typed count, then by visit count, then by visit date (most recent | |
| 168 // first) up to the given maximum number. If |typed_only| is true, only urls | |
| 169 // that have been typed once are returned. For caller convenience, returns | |
| 170 // whether any results were found. | |
| 171 bool AutocompleteForPrefix(const std::string& prefix, | |
| 172 size_t max_results, | |
| 173 bool typed_only, | |
| 174 URLRows* results); | |
| 175 | |
| 176 // Returns true if the database holds some past typed navigation to a URL on | |
| 177 // the provided hostname. | |
| 178 bool IsTypedHost(const std::string& host); | |
| 179 | |
| 180 // Tries to find the shortest URL beginning with |base| that strictly | |
| 181 // prefixes |url|, and has minimum visit_ and typed_counts as specified. | |
| 182 // If found, fills in |info| and returns true; otherwise returns false, | |
| 183 // leaving |info| unchanged. | |
| 184 // We allow matches of exactly |base| iff |allow_base| is true. | |
| 185 bool FindShortestURLFromBase(const std::string& base, | |
| 186 const std::string& url, | |
| 187 int min_visits, | |
| 188 int min_typed, | |
| 189 bool allow_base, | |
| 190 history::URLRow* info); | |
| 191 | |
| 192 // Keyword Search Terms ------------------------------------------------------ | |
| 193 | |
| 194 // Sets the search terms for the specified url/keyword pair. | |
| 195 bool SetKeywordSearchTermsForURL(URLID url_id, | |
| 196 TemplateURLID keyword_id, | |
| 197 const string16& term); | |
| 198 | |
| 199 // Looks up a keyword search term given a url id. Returns all the search terms | |
| 200 // in |rows|. Returns true on success. | |
| 201 bool GetKeywordSearchTermRow(URLID url_id, KeywordSearchTermRow* row); | |
| 202 | |
| 203 // Looks up all keyword search terms given a term, Fills the rows with data. | |
| 204 // Returns true on success and false otherwise. | |
| 205 bool GetKeywordSearchTermRows(const string16& term, | |
| 206 std::vector<KeywordSearchTermRow>* rows); | |
| 207 | |
| 208 // Deletes all search terms for the specified keyword that have been added by | |
| 209 // way of SetKeywordSearchTermsForURL. | |
| 210 void DeleteAllSearchTermsForKeyword(TemplateURLID keyword_id); | |
| 211 | |
| 212 // Returns up to max_count of the most recent search terms for the specified | |
| 213 // keyword. | |
| 214 void GetMostRecentKeywordSearchTerms( | |
| 215 TemplateURLID keyword_id, | |
| 216 const string16& prefix, | |
| 217 int max_count, | |
| 218 std::vector<KeywordSearchTermVisit>* matches); | |
| 219 | |
| 220 // Deletes all searches matching |term|. | |
| 221 bool DeleteKeywordSearchTerm(const string16& term); | |
| 222 | |
| 223 // Migration ----------------------------------------------------------------- | |
| 224 | |
| 225 // Do to a bug we were setting the favicon of about:blank. This forces | |
| 226 // about:blank to have no icon or title. Returns true on success, false if | |
| 227 // the favicon couldn't be updated. | |
| 228 bool MigrateFromVersion11ToVersion12(); | |
| 229 | |
| 230 // Initializes the given enumerator to enumerator all URL and icon mappings | |
| 231 // in the database. Only used for icon mapping migration. | |
| 232 bool InitIconMappingEnumeratorForEverything( | |
| 233 IconMappingEnumerator* enumerator); | |
| 234 | |
| 235 protected: | |
| 236 friend class VisitDatabase; | |
| 237 | |
| 238 // See HISTORY_URL_ROW_FIELDS below. | |
| 239 static const char kURLRowFields[]; | |
| 240 | |
| 241 // The number of fiends in kURLRowFields. If callers need additional | |
| 242 // fields, they can add their 0-based index to this value to get the index of | |
| 243 // fields following kURLRowFields. | |
| 244 static const int kNumURLRowFields; | |
| 245 | |
| 246 // Drops the starred_id column from urls, returning true on success. This does | |
| 247 // nothing (and returns true) if the urls doesn't contain the starred_id | |
| 248 // column. | |
| 249 bool DropStarredIDFromURLs(); | |
| 250 | |
| 251 // Initialization functions. The indexing functions are separate from the | |
| 252 // table creation functions so the in-memory database and the temporary tables | |
| 253 // used when clearing history can populate the table and then create the | |
| 254 // index, which is faster than the reverse. | |
| 255 // | |
| 256 // is_temporary is false when generating the "regular" URLs table. The expirer | |
| 257 // sets this to true to generate the temporary table, which will have a | |
| 258 // different name but the same schema. | |
| 259 bool CreateURLTable(bool is_temporary); | |
| 260 // We have two tiers of indices for the URL table. The main tier is used by | |
| 261 // all URL databases, and is an index over the URL itself. | |
| 262 bool CreateMainURLIndex(); | |
| 263 | |
| 264 // Ensures the keyword search terms table exists. | |
| 265 bool InitKeywordSearchTermsTable(); | |
| 266 | |
| 267 // Creates the indices used for keyword search terms. | |
| 268 bool CreateKeywordSearchTermsIndices(); | |
| 269 | |
| 270 // Deletes the keyword search terms table. | |
| 271 bool DropKeywordSearchTermsTable(); | |
| 272 | |
| 273 // Inserts the given URL row into the URLs table, using the regular table | |
| 274 // if is_temporary is false, or the temporary URL table if is temporary is | |
| 275 // true. The temporary table may only be used in between | |
| 276 // CreateTemporaryURLTable() and CommitTemporaryURLTable(). | |
| 277 URLID AddURLInternal(const URLRow& info, bool is_temporary); | |
| 278 | |
| 279 // Convenience to fill a history::URLRow. Must be in sync with the fields in | |
| 280 // kHistoryURLRowFields. | |
| 281 static void FillURLRow(sql::Statement& s, URLRow* i); | |
| 282 | |
| 283 // Returns the database for the functions in this interface. The decendent of | |
| 284 // this class implements these functions to return its objects. | |
| 285 virtual sql::Connection& GetDB() = 0; | |
| 286 | |
| 287 private: | |
| 288 // True if InitKeywordSearchTermsTable() has been invoked. Not all subclasses | |
| 289 // have keyword search terms. | |
| 290 bool has_keyword_search_terms_; | |
| 291 | |
| 292 DISALLOW_COPY_AND_ASSIGN(URLDatabase); | |
| 293 }; | |
| 294 | |
| 295 // The fields and order expected by FillURLRow(). ID is guaranteed to be first | |
| 296 // so that DISTINCT can be prepended to get distinct URLs. | |
| 297 // | |
| 298 // This is available BOTH as a macro and a static string (kURLRowFields). Use | |
| 299 // the macro if you want to put this in the middle of an otherwise constant | |
| 300 // string, it will save time doing string appends. If you have to build a SQL | |
| 301 // string dynamically anyway, use the constant, it will save space. | |
| 302 #define HISTORY_URL_ROW_FIELDS \ | |
| 303 " urls.id, urls.url, urls.title, urls.visit_count, urls.typed_count, " \ | |
| 304 "urls.last_visit_time, urls.hidden " | |
| 305 | |
| 306 } // history | |
| 307 | |
| 308 #endif // CHROME_BROWSER_HISTORY_URL_DATABASE_H_ | |
| OLD | NEW |