| 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 "base/file_util.h" | 5 #include "base/file_util.h" |
| 6 #include "base/string_split.h" | 6 #include "base/string_split.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" | 8 #include "chrome/browser/diagnostics/sqlite_diagnostics.h" |
| 9 #include "chrome/browser/history/history_types.h" | 9 #include "chrome/browser/history/history_types.h" |
| 10 #include "chrome/browser/history/top_sites.h" | 10 #include "chrome/browser/history/top_sites.h" |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 void TopSitesDatabase::GetPageThumbnails(MostVisitedURLList* urls, | 116 void TopSitesDatabase::GetPageThumbnails(MostVisitedURLList* urls, |
| 117 URLToImagesMap* thumbnails) { | 117 URLToImagesMap* thumbnails) { |
| 118 sql::Statement statement(db_->GetCachedStatement( | 118 sql::Statement statement(db_->GetCachedStatement( |
| 119 SQL_FROM_HERE, | 119 SQL_FROM_HERE, |
| 120 "SELECT url, url_rank, title, thumbnail, redirects, " | 120 "SELECT url, url_rank, title, thumbnail, redirects, " |
| 121 "boring_score, good_clipping, at_top, last_updated, load_completed " | 121 "boring_score, good_clipping, at_top, last_updated, load_completed " |
| 122 "FROM thumbnails ORDER BY url_rank ")); | 122 "FROM thumbnails ORDER BY url_rank ")); |
| 123 | 123 |
| 124 if (!statement) { | 124 if (!statement.is_valid()) { |
| 125 LOG(WARNING) << db_->GetErrorMessage(); | 125 LOG(WARNING) << db_->GetErrorMessage(); |
| 126 return; | 126 return; |
| 127 } | 127 } |
| 128 | 128 |
| 129 urls->clear(); | 129 urls->clear(); |
| 130 thumbnails->clear(); | 130 thumbnails->clear(); |
| 131 | 131 |
| 132 while (statement.Step()) { | 132 while (statement.Step()) { |
| 133 // Results are sorted by url_rank. | 133 // Results are sorted by url_rank. |
| 134 MostVisitedURL url; | 134 MostVisitedURL url; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 if (rank == -1) { | 182 if (rank == -1) { |
| 183 AddPageThumbnail(url, new_rank, thumbnail); | 183 AddPageThumbnail(url, new_rank, thumbnail); |
| 184 } else { | 184 } else { |
| 185 UpdatePageRankNoTransaction(url, new_rank); | 185 UpdatePageRankNoTransaction(url, new_rank); |
| 186 UpdatePageThumbnail(url, thumbnail); | 186 UpdatePageThumbnail(url, thumbnail); |
| 187 } | 187 } |
| 188 | 188 |
| 189 transaction.Commit(); | 189 transaction.Commit(); |
| 190 } | 190 } |
| 191 | 191 |
| 192 void TopSitesDatabase::UpdatePageThumbnail( | 192 bool TopSitesDatabase::UpdatePageThumbnail( |
| 193 const MostVisitedURL& url, const Images& thumbnail) { | 193 const MostVisitedURL& url, const Images& thumbnail) { |
| 194 sql::Statement statement(db_->GetCachedStatement( | 194 sql::Statement statement(db_->GetCachedStatement( |
| 195 SQL_FROM_HERE, | 195 SQL_FROM_HERE, |
| 196 "UPDATE thumbnails SET " | 196 "UPDATE thumbnails SET " |
| 197 "title = ?, thumbnail = ?, redirects = ?, " | 197 "title = ?, thumbnail = ?, redirects = ?, " |
| 198 "boring_score = ?, good_clipping = ?, at_top = ?, last_updated = ?, " | 198 "boring_score = ?, good_clipping = ?, at_top = ?, last_updated = ?, " |
| 199 "load_completed = ? " | 199 "load_completed = ? " |
| 200 "WHERE url = ? ")); | 200 "WHERE url = ? ")); |
| 201 if (!statement) | |
| 202 return; | |
| 203 | |
| 204 statement.BindString16(0, url.title); | 201 statement.BindString16(0, url.title); |
| 205 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { | 202 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { |
| 206 statement.BindBlob(1, thumbnail.thumbnail->front(), | 203 statement.BindBlob(1, thumbnail.thumbnail->front(), |
| 207 static_cast<int>(thumbnail.thumbnail->size())); | 204 static_cast<int>(thumbnail.thumbnail->size())); |
| 208 } | 205 } |
| 209 statement.BindString(2, GetRedirects(url)); | 206 statement.BindString(2, GetRedirects(url)); |
| 210 const ThumbnailScore& score = thumbnail.thumbnail_score; | 207 const ThumbnailScore& score = thumbnail.thumbnail_score; |
| 211 statement.BindDouble(3, score.boring_score); | 208 statement.BindDouble(3, score.boring_score); |
| 212 statement.BindBool(4, score.good_clipping); | 209 statement.BindBool(4, score.good_clipping); |
| 213 statement.BindBool(5, score.at_top); | 210 statement.BindBool(5, score.at_top); |
| 214 statement.BindInt64(6, score.time_at_snapshot.ToInternalValue()); | 211 statement.BindInt64(6, score.time_at_snapshot.ToInternalValue()); |
| 215 statement.BindBool(7, score.load_completed); | 212 statement.BindBool(7, score.load_completed); |
| 216 statement.BindString(8, url.url.spec()); | 213 statement.BindString(8, url.url.spec()); |
| 217 if (!statement.Run()) | 214 |
| 218 NOTREACHED() << db_->GetErrorMessage(); | 215 return statement.Run(); |
| 219 } | 216 } |
| 220 | 217 |
| 221 void TopSitesDatabase::AddPageThumbnail(const MostVisitedURL& url, | 218 void TopSitesDatabase::AddPageThumbnail(const MostVisitedURL& url, |
| 222 int new_rank, | 219 int new_rank, |
| 223 const Images& thumbnail) { | 220 const Images& thumbnail) { |
| 224 int count = GetRowCount(); | 221 int count = GetRowCount(); |
| 225 | 222 |
| 226 sql::Statement statement(db_->GetCachedStatement( | 223 sql::Statement statement(db_->GetCachedStatement( |
| 227 SQL_FROM_HERE, | 224 SQL_FROM_HERE, |
| 228 "INSERT OR REPLACE INTO thumbnails " | 225 "INSERT OR REPLACE INTO thumbnails " |
| 229 "(url, url_rank, title, thumbnail, redirects, " | 226 "(url, url_rank, title, thumbnail, redirects, " |
| 230 "boring_score, good_clipping, at_top, last_updated, load_completed) " | 227 "boring_score, good_clipping, at_top, last_updated, load_completed) " |
| 231 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); | 228 "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); |
| 232 if (!statement) | |
| 233 return; | |
| 234 | |
| 235 statement.BindString(0, url.url.spec()); | 229 statement.BindString(0, url.url.spec()); |
| 236 statement.BindInt(1, count); // Make it the last url. | 230 statement.BindInt(1, count); // Make it the last url. |
| 237 statement.BindString16(2, url.title); | 231 statement.BindString16(2, url.title); |
| 238 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { | 232 if (thumbnail.thumbnail.get() && thumbnail.thumbnail->front()) { |
| 239 statement.BindBlob(3, thumbnail.thumbnail->front(), | 233 statement.BindBlob(3, thumbnail.thumbnail->front(), |
| 240 static_cast<int>(thumbnail.thumbnail->size())); | 234 static_cast<int>(thumbnail.thumbnail->size())); |
| 241 } | 235 } |
| 242 statement.BindString(4, GetRedirects(url)); | 236 statement.BindString(4, GetRedirects(url)); |
| 243 const ThumbnailScore& score = thumbnail.thumbnail_score; | 237 const ThumbnailScore& score = thumbnail.thumbnail_score; |
| 244 statement.BindDouble(5, score.boring_score); | 238 statement.BindDouble(5, score.boring_score); |
| 245 statement.BindBool(6, score.good_clipping); | 239 statement.BindBool(6, score.good_clipping); |
| 246 statement.BindBool(7, score.at_top); | 240 statement.BindBool(7, score.at_top); |
| 247 statement.BindInt64(8, score.time_at_snapshot.ToInternalValue()); | 241 statement.BindInt64(8, score.time_at_snapshot.ToInternalValue()); |
| 248 statement.BindBool(9, score.load_completed); | 242 statement.BindBool(9, score.load_completed); |
| 249 if (!statement.Run()) | 243 if (!statement.Run()) |
| 250 NOTREACHED() << db_->GetErrorMessage(); | 244 return; |
| 251 | 245 |
| 252 UpdatePageRankNoTransaction(url, new_rank); | 246 UpdatePageRankNoTransaction(url, new_rank); |
| 253 } | 247 } |
| 254 | 248 |
| 255 void TopSitesDatabase::UpdatePageRank(const MostVisitedURL& url, | 249 void TopSitesDatabase::UpdatePageRank(const MostVisitedURL& url, |
| 256 int new_rank) { | 250 int new_rank) { |
| 257 sql::Transaction transaction(db_.get()); | 251 sql::Transaction transaction(db_.get()); |
| 258 transaction.Begin(); | 252 transaction.Begin(); |
| 259 UpdatePageRankNoTransaction(url, new_rank); | 253 UpdatePageRankNoTransaction(url, new_rank); |
| 260 transaction.Commit(); | 254 transaction.Commit(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 272 // Shift the ranks. | 266 // Shift the ranks. |
| 273 if (prev_rank > new_rank) { | 267 if (prev_rank > new_rank) { |
| 274 // Shift up | 268 // Shift up |
| 275 sql::Statement shift_statement(db_->GetCachedStatement( | 269 sql::Statement shift_statement(db_->GetCachedStatement( |
| 276 SQL_FROM_HERE, | 270 SQL_FROM_HERE, |
| 277 "UPDATE thumbnails " | 271 "UPDATE thumbnails " |
| 278 "SET url_rank = url_rank + 1 " | 272 "SET url_rank = url_rank + 1 " |
| 279 "WHERE url_rank >= ? AND url_rank < ?")); | 273 "WHERE url_rank >= ? AND url_rank < ?")); |
| 280 shift_statement.BindInt(0, new_rank); | 274 shift_statement.BindInt(0, new_rank); |
| 281 shift_statement.BindInt(1, prev_rank); | 275 shift_statement.BindInt(1, prev_rank); |
| 282 if (shift_statement) | 276 shift_statement.Run(); |
| 283 shift_statement.Run(); | |
| 284 } else if (prev_rank < new_rank) { | 277 } else if (prev_rank < new_rank) { |
| 285 // Shift down | 278 // Shift down |
| 286 sql::Statement shift_statement(db_->GetCachedStatement( | 279 sql::Statement shift_statement(db_->GetCachedStatement( |
| 287 SQL_FROM_HERE, | 280 SQL_FROM_HERE, |
| 288 "UPDATE thumbnails " | 281 "UPDATE thumbnails " |
| 289 "SET url_rank = url_rank - 1 " | 282 "SET url_rank = url_rank - 1 " |
| 290 "WHERE url_rank > ? AND url_rank <= ?")); | 283 "WHERE url_rank > ? AND url_rank <= ?")); |
| 291 shift_statement.BindInt(0, prev_rank); | 284 shift_statement.BindInt(0, prev_rank); |
| 292 shift_statement.BindInt(1, new_rank); | 285 shift_statement.BindInt(1, new_rank); |
| 293 if (shift_statement) | 286 shift_statement.Run(); |
| 294 shift_statement.Run(); | |
| 295 } | 287 } |
| 296 | 288 |
| 297 // Set the url's rank. | 289 // Set the url's rank. |
| 298 sql::Statement set_statement(db_->GetCachedStatement( | 290 sql::Statement set_statement(db_->GetCachedStatement( |
| 299 SQL_FROM_HERE, | 291 SQL_FROM_HERE, |
| 300 "UPDATE thumbnails " | 292 "UPDATE thumbnails " |
| 301 "SET url_rank = ? " | 293 "SET url_rank = ? " |
| 302 "WHERE url == ?")); | 294 "WHERE url == ?")); |
| 303 set_statement.BindInt(0, new_rank); | 295 set_statement.BindInt(0, new_rank); |
| 304 set_statement.BindString(1, url.url.spec()); | 296 set_statement.BindString(1, url.url.spec()); |
| 305 if (set_statement) | 297 set_statement.Run(); |
| 306 set_statement.Run(); | |
| 307 } | 298 } |
| 308 | 299 |
| 309 bool TopSitesDatabase::GetPageThumbnail(const GURL& url, | 300 bool TopSitesDatabase::GetPageThumbnail(const GURL& url, |
| 310 Images* thumbnail) { | 301 Images* thumbnail) { |
| 311 sql::Statement statement(db_->GetCachedStatement( | 302 sql::Statement statement(db_->GetCachedStatement( |
| 312 SQL_FROM_HERE, | 303 SQL_FROM_HERE, |
| 313 "SELECT thumbnail, boring_score, good_clipping, at_top, last_updated " | 304 "SELECT thumbnail, boring_score, good_clipping, at_top, last_updated " |
| 314 "FROM thumbnails WHERE url=?")); | 305 "FROM thumbnails WHERE url=?")); |
| 315 | |
| 316 if (!statement) { | |
| 317 LOG(WARNING) << db_->GetErrorMessage(); | |
| 318 return false; | |
| 319 } | |
| 320 | |
| 321 statement.BindString(0, url.spec()); | 306 statement.BindString(0, url.spec()); |
| 322 if (!statement.Step()) | 307 if (!statement.Step()) |
| 323 return false; | 308 return false; |
| 324 | 309 |
| 325 std::vector<unsigned char> data; | 310 std::vector<unsigned char> data; |
| 326 statement.ColumnBlobAsVector(0, &data); | 311 statement.ColumnBlobAsVector(0, &data); |
| 327 thumbnail->thumbnail = RefCountedBytes::TakeVector(&data); | 312 thumbnail->thumbnail = RefCountedBytes::TakeVector(&data); |
| 328 thumbnail->thumbnail_score.boring_score = statement.ColumnDouble(1); | 313 thumbnail->thumbnail_score.boring_score = statement.ColumnDouble(1); |
| 329 thumbnail->thumbnail_score.good_clipping = statement.ColumnBool(2); | 314 thumbnail->thumbnail_score.good_clipping = statement.ColumnBool(2); |
| 330 thumbnail->thumbnail_score.at_top = statement.ColumnBool(3); | 315 thumbnail->thumbnail_score.at_top = statement.ColumnBool(3); |
| 331 thumbnail->thumbnail_score.time_at_snapshot = | 316 thumbnail->thumbnail_score.time_at_snapshot = |
| 332 base::Time::FromInternalValue(statement.ColumnInt64(4)); | 317 base::Time::FromInternalValue(statement.ColumnInt64(4)); |
| 333 return true; | 318 return true; |
| 334 } | 319 } |
| 335 | 320 |
| 336 int TopSitesDatabase::GetRowCount() { | 321 int TopSitesDatabase::GetRowCount() { |
| 337 int result = 0; | |
| 338 sql::Statement select_statement(db_->GetCachedStatement( | 322 sql::Statement select_statement(db_->GetCachedStatement( |
| 339 SQL_FROM_HERE, | 323 SQL_FROM_HERE, |
| 340 "SELECT COUNT (url) FROM thumbnails")); | 324 "SELECT COUNT (url) FROM thumbnails")); |
| 341 if (!select_statement) { | 325 if (select_statement.Step()) |
| 342 LOG(WARNING) << db_->GetErrorMessage(); | 326 return select_statement.ColumnInt(0); |
| 343 return result; | |
| 344 } | |
| 345 | 327 |
| 346 if (select_statement.Step()) | 328 return 0; |
| 347 result = select_statement.ColumnInt(0); | |
| 348 | |
| 349 return result; | |
| 350 } | 329 } |
| 351 | 330 |
| 352 int TopSitesDatabase::GetURLRank(const MostVisitedURL& url) { | 331 int TopSitesDatabase::GetURLRank(const MostVisitedURL& url) { |
| 353 int result = -1; | |
| 354 sql::Statement select_statement(db_->GetCachedStatement( | 332 sql::Statement select_statement(db_->GetCachedStatement( |
| 355 SQL_FROM_HERE, | 333 SQL_FROM_HERE, |
| 356 "SELECT url_rank " | 334 "SELECT url_rank " |
| 357 "FROM thumbnails WHERE url=?")); | 335 "FROM thumbnails WHERE url=?")); |
| 358 if (!select_statement) { | |
| 359 LOG(WARNING) << db_->GetErrorMessage(); | |
| 360 return result; | |
| 361 } | |
| 362 | |
| 363 select_statement.BindString(0, url.url.spec()); | 336 select_statement.BindString(0, url.url.spec()); |
| 364 if (select_statement.Step()) | 337 if (select_statement.Step()) |
| 365 result = select_statement.ColumnInt(0); | 338 return select_statement.ColumnInt(0); |
| 366 | 339 |
| 367 return result; | 340 return -1; |
| 368 } | 341 } |
| 369 | 342 |
| 370 // Remove the record for this URL. Returns true iff removed successfully. | 343 // Remove the record for this URL. Returns true iff removed successfully. |
| 371 bool TopSitesDatabase::RemoveURL(const MostVisitedURL& url) { | 344 bool TopSitesDatabase::RemoveURL(const MostVisitedURL& url) { |
| 372 int old_rank = GetURLRank(url); | 345 int old_rank = GetURLRank(url); |
| 373 if (old_rank < 0) | 346 if (old_rank < 0) |
| 374 return false; | 347 return false; |
| 375 | 348 |
| 376 sql::Transaction transaction(db_.get()); | 349 sql::Transaction transaction(db_.get()); |
| 377 transaction.Begin(); | 350 transaction.Begin(); |
| 378 // Decrement all following ranks. | 351 // Decrement all following ranks. |
| 379 sql::Statement shift_statement(db_->GetCachedStatement( | 352 sql::Statement shift_statement(db_->GetCachedStatement( |
| 380 SQL_FROM_HERE, | 353 SQL_FROM_HERE, |
| 381 "UPDATE thumbnails " | 354 "UPDATE thumbnails " |
| 382 "SET url_rank = url_rank - 1 " | 355 "SET url_rank = url_rank - 1 " |
| 383 "WHERE url_rank > ?")); | 356 "WHERE url_rank > ?")); |
| 384 if (!shift_statement) | 357 shift_statement.BindInt(0, old_rank); |
| 358 |
| 359 if (!shift_statement.Run()) |
| 385 return false; | 360 return false; |
| 386 shift_statement.BindInt(0, old_rank); | |
| 387 shift_statement.Run(); | |
| 388 | 361 |
| 389 sql::Statement delete_statement( | 362 sql::Statement delete_statement( |
| 390 db_->GetCachedStatement(SQL_FROM_HERE, | 363 db_->GetCachedStatement(SQL_FROM_HERE, |
| 391 "DELETE FROM thumbnails WHERE url = ?")); | 364 "DELETE FROM thumbnails WHERE url = ?")); |
| 392 if (!delete_statement) | 365 delete_statement.BindString(0, url.url.spec()); |
| 366 |
| 367 if (!delete_statement.Run()) |
| 393 return false; | 368 return false; |
| 394 delete_statement.BindString(0, url.url.spec()); | |
| 395 delete_statement.Run(); | |
| 396 | 369 |
| 397 return transaction.Commit(); | 370 return transaction.Commit(); |
| 398 } | 371 } |
| 399 | 372 |
| 400 sql::Connection* TopSitesDatabase::CreateDB(const FilePath& db_name) { | 373 sql::Connection* TopSitesDatabase::CreateDB(const FilePath& db_name) { |
| 401 scoped_ptr<sql::Connection> db(new sql::Connection()); | 374 scoped_ptr<sql::Connection> db(new sql::Connection()); |
| 402 // Settings copied from ThumbnailDatabase. | 375 // Settings copied from ThumbnailDatabase. |
| 403 db->set_error_delegate(GetErrorHandlerForThumbnailDb()); | 376 db->set_error_delegate(GetErrorHandlerForThumbnailDb()); |
| 404 db->set_page_size(4096); | 377 db->set_page_size(4096); |
| 405 db->set_cache_size(32); | 378 db->set_cache_size(32); |
| 406 | 379 |
| 407 if (!db->Open(db_name)) { | 380 if (!db->Open(db_name)) { |
| 408 LOG(ERROR) << db->GetErrorMessage(); | 381 LOG(ERROR) << db->GetErrorMessage(); |
| 409 return NULL; | 382 return NULL; |
| 410 } | 383 } |
| 411 | 384 |
| 412 return db.release(); | 385 return db.release(); |
| 413 } | 386 } |
| 414 | 387 |
| 415 } // namespace history | 388 } // namespace history |
| OLD | NEW |