Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/history/thumbnail_database.cc

Issue 9071014: Database usage adjustment for .../history (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/history/thumbnail_database.h" 5 #include "chrome/browser/history/thumbnail_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 const char* alterations[] = { 169 const char* alterations[] = {
170 "ALTER TABLE thumbnails ADD boring_score DOUBLE DEFAULT 1.0", 170 "ALTER TABLE thumbnails ADD boring_score DOUBLE DEFAULT 1.0",
171 "ALTER TABLE thumbnails ADD good_clipping INTEGER DEFAULT 0", 171 "ALTER TABLE thumbnails ADD good_clipping INTEGER DEFAULT 0",
172 "ALTER TABLE thumbnails ADD at_top INTEGER DEFAULT 0", 172 "ALTER TABLE thumbnails ADD at_top INTEGER DEFAULT 0",
173 "ALTER TABLE thumbnails ADD last_updated INTEGER DEFAULT 0", 173 "ALTER TABLE thumbnails ADD last_updated INTEGER DEFAULT 0",
174 NULL 174 NULL
175 }; 175 };
176 176
177 for (int i = 0; alterations[i] != NULL; ++i) { 177 for (int i = 0; alterations[i] != NULL; ++i) {
178 if (!db_.Execute(alterations[i])) { 178 if (!db_.Execute(alterations[i])) {
179 NOTREACHED();
180 return false; 179 return false;
181 } 180 }
182 } 181 }
183 182
184 meta_table_.SetVersionNumber(3); 183 meta_table_.SetVersionNumber(3);
185 meta_table_.SetCompatibleVersionNumber(std::min(3, kCompatibleVersionNumber)); 184 meta_table_.SetCompatibleVersionNumber(std::min(3, kCompatibleVersionNumber));
186 return true; 185 return true;
187 } 186 }
188 187
189 bool ThumbnailDatabase::RecreateThumbnailTable() { 188 bool ThumbnailDatabase::RecreateThumbnailTable() {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 void ThumbnailDatabase::CommitTransaction() { 231 void ThumbnailDatabase::CommitTransaction() {
233 db_.CommitTransaction(); 232 db_.CommitTransaction();
234 } 233 }
235 234
236 void ThumbnailDatabase::Vacuum() { 235 void ThumbnailDatabase::Vacuum() {
237 DCHECK(db_.transaction_nesting() == 0) << 236 DCHECK(db_.transaction_nesting() == 0) <<
238 "Can not have a transaction when vacuuming."; 237 "Can not have a transaction when vacuuming.";
239 ignore_result(db_.Execute("VACUUM")); 238 ignore_result(db_.Execute("VACUUM"));
240 } 239 }
241 240
242 void ThumbnailDatabase::SetPageThumbnail( 241 bool ThumbnailDatabase::SetPageThumbnail(
243 const GURL& url, 242 const GURL& url,
244 URLID id, 243 URLID id,
245 const gfx::Image* thumbnail, 244 const gfx::Image* thumbnail,
246 const ThumbnailScore& score, 245 const ThumbnailScore& score,
247 base::Time time) { 246 base::Time time) {
248 if (use_top_sites_) { 247 if (use_top_sites_) {
249 LOG(WARNING) << "Use TopSites instead."; 248 LOG(WARNING) << "Use TopSites instead.";
250 return; // Not possible after migration to TopSites. 249 return false; // Not possible after migration to TopSites.
251 } 250 }
252 251
253 if (thumbnail) { 252 if (!thumbnail)
254 bool add_thumbnail = true; 253 return DeleteThumbnail(id);
255 ThumbnailScore current_score;
256 if (ThumbnailScoreForId(id, &current_score)) {
257 add_thumbnail = ShouldReplaceThumbnailWith(current_score, score);
258 }
259 254
260 if (add_thumbnail) { 255 bool add_thumbnail = true;
261 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 256 ThumbnailScore current_score;
262 "INSERT OR REPLACE INTO thumbnails " 257 if (ThumbnailScoreForId(id, &current_score)) {
263 "(url_id, boring_score, good_clipping, at_top, last_updated, data) " 258 add_thumbnail = ShouldReplaceThumbnailWith(current_score, score);
264 "VALUES (?,?,?,?,?,?)")); 259 }
265 if (!statement)
266 return;
267 260
268 std::vector<unsigned char> jpeg_data; 261 if (!add_thumbnail)
269 bool encoded = gfx::JPEGEncodedDataFromImage(*thumbnail, kImageQuality, 262 return true;
270 &jpeg_data);
271 if (encoded) {
272 statement.BindInt64(0, id);
273 statement.BindDouble(1, score.boring_score);
274 statement.BindBool(2, score.good_clipping);
275 statement.BindBool(3, score.at_top);
276 statement.BindInt64(4, score.time_at_snapshot.ToTimeT());
277 statement.BindBlob(5, &jpeg_data[0],
278 static_cast<int>(jpeg_data.size()));
279 if (!statement.Run())
280 NOTREACHED() << db_.GetErrorMessage();
281 }
282 263
283 // Publish the thumbnail to any indexers listening to us. 264 std::vector<unsigned char> jpeg_data;
284 // The tests may send an invalid url. Hence avoid publishing those. 265 bool encoded = gfx::JPEGEncodedDataFromImage(
285 if (url.is_valid() && history_publisher_ != NULL) 266 *thumbnail, kImageQuality, &jpeg_data);
286 history_publisher_->PublishPageThumbnail(jpeg_data, url, time); 267 if (encoded) {
287 } 268 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
288 } else { 269 "INSERT OR REPLACE INTO thumbnails "
289 if (!DeleteThumbnail(id) ) 270 "(url_id, boring_score, good_clipping, at_top, last_updated, data) "
290 DLOG(WARNING) << "Unable to delete thumbnail"; 271 "VALUES (?,?,?,?,?,?)"));
272 statement.BindInt64(0, id);
273 statement.BindDouble(1, score.boring_score);
274 statement.BindBool(2, score.good_clipping);
275 statement.BindBool(3, score.at_top);
276 statement.BindInt64(4, score.time_at_snapshot.ToTimeT());
277 statement.BindBlob(5, &jpeg_data[0],
278 static_cast<int>(jpeg_data.size()));
279
280 if (!statement.Run())
281 return false;
291 } 282 }
283
284 // Publish the thumbnail to any indexers listening to us.
285 // The tests may send an invalid url. Hence avoid publishing those.
286 if (url.is_valid() && history_publisher_ != NULL)
287 history_publisher_->PublishPageThumbnail(jpeg_data, url, time);
288
289 return true;
292 } 290 }
293 291
294 bool ThumbnailDatabase::GetPageThumbnail(URLID id, 292 bool ThumbnailDatabase::GetPageThumbnail(URLID id,
295 std::vector<unsigned char>* data) { 293 std::vector<unsigned char>* data) {
296 if (use_top_sites_) { 294 if (use_top_sites_) {
297 LOG(WARNING) << "Use TopSites instead."; 295 LOG(WARNING) << "Use TopSites instead.";
298 return false; // Not possible after migration to TopSites. 296 return false; // Not possible after migration to TopSites.
299 } 297 }
300 298
301 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 299 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
302 "SELECT data FROM thumbnails WHERE url_id=?")); 300 "SELECT data FROM thumbnails WHERE url_id=?"));
303 if (!statement) 301 statement.BindInt64(0, id);
304 return false;
305 302
306 statement.BindInt64(0, id);
307 if (!statement.Step()) 303 if (!statement.Step())
308 return false; // don't have a thumbnail for this ID 304 return false; // don't have a thumbnail for this ID
309 305
310 statement.ColumnBlobAsVector(0, data); 306 statement.ColumnBlobAsVector(0, data);
311 return true; 307 return true;
312 } 308 }
313 309
314 bool ThumbnailDatabase::DeleteThumbnail(URLID id) { 310 bool ThumbnailDatabase::DeleteThumbnail(URLID id) {
315 if (use_top_sites_) { 311 if (use_top_sites_) {
316 return true; // Not possible after migration to TopSites. 312 return true; // Not possible after migration to TopSites.
317 } 313 }
318 314
319 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 315 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
320 "DELETE FROM thumbnails WHERE url_id = ?")); 316 "DELETE FROM thumbnails WHERE url_id = ?"));
321 if (!statement) 317 statement.BindInt64(0, id);
322 return false;
323 318
324 statement.BindInt64(0, id);
325 return statement.Run(); 319 return statement.Run();
326 } 320 }
327 321
328 bool ThumbnailDatabase::ThumbnailScoreForId(URLID id, 322 bool ThumbnailDatabase::ThumbnailScoreForId(URLID id,
329 ThumbnailScore* score) { 323 ThumbnailScore* score) {
324 DCHECK(score);
330 if (use_top_sites_) { 325 if (use_top_sites_) {
331 LOG(WARNING) << "Use TopSites instead."; 326 LOG(WARNING) << "Use TopSites instead.";
332 return false; // Not possible after migration to TopSites. 327 return false; // Not possible after migration to TopSites.
333 } 328 }
334 329
335 // Fetch the current thumbnail's information to make sure we 330 // Fetch the current thumbnail's information to make sure we
336 // aren't replacing a good thumbnail with one that's worse. 331 // aren't replacing a good thumbnail with one that's worse.
337 sql::Statement select_statement(db_.GetCachedStatement(SQL_FROM_HERE, 332 sql::Statement select_statement(db_.GetCachedStatement(SQL_FROM_HERE,
338 "SELECT boring_score, good_clipping, at_top, last_updated " 333 "SELECT boring_score, good_clipping, at_top, last_updated "
339 "FROM thumbnails WHERE url_id=?")); 334 "FROM thumbnails WHERE url_id=?"));
340 if (!select_statement) { 335 select_statement.BindInt64(0, id);
341 NOTREACHED() << "Couldn't build select statement!";
342 } else {
343 select_statement.BindInt64(0, id);
344 if (select_statement.Step()) {
345 double current_boring_score = select_statement.ColumnDouble(0);
346 bool current_clipping = select_statement.ColumnBool(1);
347 bool current_at_top = select_statement.ColumnBool(2);
348 base::Time last_updated =
349 base::Time::FromTimeT(select_statement.ColumnInt64(3));
350 *score = ThumbnailScore(current_boring_score, current_clipping,
351 current_at_top, last_updated);
352 return true;
353 }
354 }
355 336
356 return false; 337 if (!select_statement.Step())
338 return false;
339
340 double current_boring_score = select_statement.ColumnDouble(0);
341 bool current_clipping = select_statement.ColumnBool(1);
342 bool current_at_top = select_statement.ColumnBool(2);
343 base::Time last_updated =
344 base::Time::FromTimeT(select_statement.ColumnInt64(3));
345 *score = ThumbnailScore(current_boring_score, current_clipping,
346 current_at_top, last_updated);
347 return true;
357 } 348 }
358 349
359 bool ThumbnailDatabase::SetFavicon(URLID icon_id, 350 bool ThumbnailDatabase::SetFavicon(URLID icon_id,
360 scoped_refptr<RefCountedMemory> icon_data, 351 scoped_refptr<RefCountedMemory> icon_data,
361 base::Time time) { 352 base::Time time) {
362 DCHECK(icon_id); 353 DCHECK(icon_id);
354 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
355 "UPDATE favicons SET image_data=?, last_updated=? WHERE id=?"));
363 if (icon_data->size()) { 356 if (icon_data->size()) {
364 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
365 "UPDATE favicons SET image_data=?, last_updated=? WHERE id=?"));
366 if (!statement)
367 return 0;
368
369 statement.BindBlob(0, icon_data->front(), 357 statement.BindBlob(0, icon_data->front(),
370 static_cast<int>(icon_data->size())); 358 static_cast<int>(icon_data->size()));
371 statement.BindInt64(1, time.ToTimeT());
372 statement.BindInt64(2, icon_id);
373 return statement.Run();
374 } else { 359 } else {
375 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 360 statement.BindNull(0);
376 "UPDATE favicons SET image_data=NULL, last_updated=? WHERE id=?")); 361 }
377 if (!statement) 362 statement.BindInt64(1, time.ToTimeT());
378 return 0; 363 statement.BindInt64(2, icon_id);
Scott Hess - ex-Googler 2012/01/10 22:29:49 +100!
Greg Billock 2012/01/11 23:42:12 winning! :-)
379 364
380 statement.BindInt64(0, time.ToTimeT()); 365 return statement.Run();
381 statement.BindInt64(1, icon_id);
382 return statement.Run();
383 }
384 } 366 }
385 367
386 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id, 368 bool ThumbnailDatabase::SetFaviconLastUpdateTime(FaviconID icon_id,
387 base::Time time) { 369 base::Time time) {
388 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 370 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
389 "UPDATE favicons SET last_updated=? WHERE id=?")); 371 "UPDATE favicons SET last_updated=? WHERE id=?"));
390 if (!statement)
391 return 0;
392
393 statement.BindInt64(0, time.ToTimeT()); 372 statement.BindInt64(0, time.ToTimeT());
394 statement.BindInt64(1, icon_id); 373 statement.BindInt64(1, icon_id);
374
395 return statement.Run(); 375 return statement.Run();
396 } 376 }
397 377
398 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url, 378 FaviconID ThumbnailDatabase::GetFaviconIDForFaviconURL(const GURL& icon_url,
399 int required_icon_type, 379 int required_icon_type,
400 IconType* icon_type) { 380 IconType* icon_type) {
401 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 381 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
402 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) " 382 "SELECT id, icon_type FROM favicons WHERE url=? AND (icon_type & ? > 0) "
403 "ORDER BY icon_type DESC")); 383 "ORDER BY icon_type DESC"));
404 if (!statement)
405 return 0;
406
407 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); 384 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url));
408 statement.BindInt(1, required_icon_type); 385 statement.BindInt(1, required_icon_type);
386
409 if (!statement.Step()) 387 if (!statement.Step())
410 return 0; // not cached 388 return 0; // not cached
411 389
412 if (icon_type) 390 if (icon_type)
413 *icon_type = static_cast<IconType>(statement.ColumnInt(1)); 391 *icon_type = static_cast<IconType>(statement.ColumnInt(1));
414 return statement.ColumnInt64(0); 392 return statement.ColumnInt64(0);
415 } 393 }
416 394
417 bool ThumbnailDatabase::GetFavicon( 395 bool ThumbnailDatabase::GetFavicon(
418 FaviconID icon_id, 396 FaviconID icon_id,
419 base::Time* last_updated, 397 base::Time* last_updated,
420 std::vector<unsigned char>* png_icon_data, 398 std::vector<unsigned char>* png_icon_data,
421 GURL* icon_url) { 399 GURL* icon_url) {
422 DCHECK(icon_id); 400 DCHECK(icon_id);
423 401
424 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 402 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
425 "SELECT last_updated, image_data, url FROM favicons WHERE id=?")); 403 "SELECT last_updated, image_data, url FROM favicons WHERE id=?"));
426 if (!statement)
427 return 0;
428
429 statement.BindInt64(0, icon_id); 404 statement.BindInt64(0, icon_id);
430 405
431 if (!statement.Step()) 406 if (!statement.Step())
432 return false; // No entry for the id. 407 return false; // No entry for the id.
433 408
434 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0)); 409 *last_updated = base::Time::FromTimeT(statement.ColumnInt64(0));
435 if (statement.ColumnByteLength(1) > 0) 410 if (statement.ColumnByteLength(1) > 0)
436 statement.ColumnBlobAsVector(1, png_icon_data); 411 statement.ColumnBlobAsVector(1, png_icon_data);
437 if (icon_url) 412 if (icon_url)
438 *icon_url = GURL(statement.ColumnString(2)); 413 *icon_url = GURL(statement.ColumnString(2));
439 414
440 return true; 415 return true;
441 } 416 }
442 417
443 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url, 418 FaviconID ThumbnailDatabase::AddFavicon(const GURL& icon_url,
444 IconType icon_type) { 419 IconType icon_type) {
445 420
446 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 421 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
447 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); 422 "INSERT INTO favicons (url, icon_type) VALUES (?, ?)"));
448 if (!statement)
449 return 0;
450
451 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url)); 423 statement.BindString(0, URLDatabase::GURLToDatabaseURL(icon_url));
452 statement.BindInt(1, icon_type); 424 statement.BindInt(1, icon_type);
425
453 if (!statement.Run()) 426 if (!statement.Run())
454 return 0; 427 return 0;
455 return db_.GetLastInsertRowId(); 428 return db_.GetLastInsertRowId();
456 } 429 }
457 430
458 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) { 431 bool ThumbnailDatabase::DeleteFavicon(FaviconID id) {
459 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 432 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
460 "DELETE FROM favicons WHERE id = ?")); 433 "DELETE FROM favicons WHERE id = ?"));
461 if (!statement) 434 statement.BindInt64(0, id);
462 return false;
463 435
464 statement.BindInt64(0, id);
465 return statement.Run(); 436 return statement.Run();
466 } 437 }
467 438
468 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url, 439 bool ThumbnailDatabase::GetIconMappingForPageURL(const GURL& page_url,
469 IconType required_icon_type, 440 IconType required_icon_type,
470 IconMapping* icon_mapping) { 441 IconMapping* icon_mapping) {
471 std::vector<IconMapping> icon_mappings; 442 std::vector<IconMapping> icon_mappings;
472 if (!GetIconMappingsForPageURL(page_url, &icon_mappings)) 443 if (!GetIconMappingsForPageURL(page_url, &icon_mappings))
473 return false; 444 return false;
474 445
(...skipping 12 matching lines...) Expand all
487 bool ThumbnailDatabase::GetIconMappingsForPageURL( 458 bool ThumbnailDatabase::GetIconMappingsForPageURL(
488 const GURL& page_url, 459 const GURL& page_url,
489 std::vector<IconMapping>* mapping_data) { 460 std::vector<IconMapping>* mapping_data) {
490 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 461 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
491 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type " 462 "SELECT icon_mapping.id, icon_mapping.icon_id, favicons.icon_type "
492 "FROM icon_mapping " 463 "FROM icon_mapping "
493 "INNER JOIN favicons " 464 "INNER JOIN favicons "
494 "ON icon_mapping.icon_id = favicons.id " 465 "ON icon_mapping.icon_id = favicons.id "
495 "WHERE icon_mapping.page_url=? " 466 "WHERE icon_mapping.page_url=? "
496 "ORDER BY favicons.icon_type DESC")); 467 "ORDER BY favicons.icon_type DESC"));
497 if (!statement)
498 return false;
499
500 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); 468 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
501 469
502 bool result = false; 470 bool result = false;
503 while (statement.Step()) { 471 while (statement.Step()) {
504 result = true; 472 result = true;
505 if (!mapping_data) 473 if (!mapping_data)
506 return result; 474 return result;
507 475
508 IconMapping icon_mapping; 476 IconMapping icon_mapping;
509 FillIconMapping(statement, page_url, &icon_mapping); 477 FillIconMapping(statement, page_url, &icon_mapping);
510 mapping_data->push_back(icon_mapping); 478 mapping_data->push_back(icon_mapping);
511 } 479 }
512 return result; 480 return result;
513 } 481 }
514 482
515 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url, 483 IconMappingID ThumbnailDatabase::AddIconMapping(const GURL& page_url,
516 FaviconID icon_id) { 484 FaviconID icon_id) {
517 return AddIconMapping(page_url, icon_id, false); 485 return AddIconMapping(page_url, icon_id, false);
518 } 486 }
519 487
520 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id, 488 bool ThumbnailDatabase::UpdateIconMapping(IconMappingID mapping_id,
521 FaviconID icon_id) { 489 FaviconID icon_id) {
522 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 490 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
523 "UPDATE icon_mapping SET icon_id=? WHERE id=?")); 491 "UPDATE icon_mapping SET icon_id=? WHERE id=?"));
524 if (!statement)
525 return 0;
526
527 statement.BindInt64(0, icon_id); 492 statement.BindInt64(0, icon_id);
528 statement.BindInt64(1, mapping_id); 493 statement.BindInt64(1, mapping_id);
494
529 return statement.Run(); 495 return statement.Run();
530 } 496 }
531 497
532 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) { 498 bool ThumbnailDatabase::DeleteIconMappings(const GURL& page_url) {
533 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 499 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
534 "DELETE FROM icon_mapping WHERE page_url = ?")); 500 "DELETE FROM icon_mapping WHERE page_url = ?"));
535 if (!statement) 501 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
536 return false;
537 502
538 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
539 return statement.Run(); 503 return statement.Run();
540 } 504 }
541 505
542 bool ThumbnailDatabase::HasMappingFor(FaviconID id) { 506 bool ThumbnailDatabase::HasMappingFor(FaviconID id) {
543 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 507 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
544 "SELECT id FROM icon_mapping " 508 "SELECT id FROM icon_mapping "
545 "WHERE icon_id=?")); 509 "WHERE icon_id=?"));
546 if (!statement) 510 statement.BindInt64(0, id);
547 return false;
548 511
549 statement.BindInt64(0, id);
550 return statement.Step(); 512 return statement.Step();
551 } 513 }
552 514
553 bool ThumbnailDatabase::CloneIconMapping(const GURL& old_page_url, 515 bool ThumbnailDatabase::CloneIconMapping(const GURL& old_page_url,
554 const GURL& new_page_url) { 516 const GURL& new_page_url) {
555 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 517 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
556 "SELECT icon_id FROM icon_mapping " 518 "SELECT icon_id FROM icon_mapping "
557 "WHERE page_url=?")); 519 "WHERE page_url=?"));
558 if (!statement) 520 if (!statement.is_valid())
559 return false; 521 return false;
560 522
561 // Do nothing if there are existing bindings 523 // Do nothing if there are existing bindings
562 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url)); 524 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url));
563 if (statement.Step()) 525 if (statement.Step())
564 return true; 526 return true;
565 527
566 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE, 528 statement.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
567 "INSERT INTO icon_mapping (page_url, icon_id) " 529 "INSERT INTO icon_mapping (page_url, icon_id) "
568 "SELECT ?, icon_id FROM icon_mapping " 530 "SELECT ?, icon_id FROM icon_mapping "
569 "WHERE page_url = ?")); 531 "WHERE page_url = ?"));
570 if (!statement)
571 return false;
572 532
573 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url)); 533 statement.BindString(0, URLDatabase::GURLToDatabaseURL(new_page_url));
574 statement.BindString(1, URLDatabase::GURLToDatabaseURL(old_page_url)); 534 statement.BindString(1, URLDatabase::GURLToDatabaseURL(old_page_url));
575 return statement.Run(); 535 return statement.Run();
576 } 536 }
577 537
578 538
579 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) { 539 bool ThumbnailDatabase::MigrateIconMappingData(URLDatabase* url_db) {
580 URLDatabase::IconMappingEnumerator e; 540 URLDatabase::IconMappingEnumerator e;
581 if (!url_db->InitIconMappingEnumeratorForEverything(&e)) 541 if (!url_db->InitIconMappingEnumeratorForEverything(&e))
(...skipping 24 matching lines...) Expand all
606 566
607 // The renamed table needs the index (the temporary table doesn't have one). 567 // The renamed table needs the index (the temporary table doesn't have one).
608 return InitIconMappingIndex(); 568 return InitIconMappingIndex();
609 } 569 }
610 570
611 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) { 571 FaviconID ThumbnailDatabase::CopyToTemporaryFaviconTable(FaviconID source) {
612 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE, 572 sql::Statement statement(db_.GetCachedStatement(SQL_FROM_HERE,
613 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)" 573 "INSERT INTO temp_favicons (url, last_updated, image_data, icon_type)"
614 "SELECT url, last_updated, image_data, icon_type " 574 "SELECT url, last_updated, image_data, icon_type "
615 "FROM favicons WHERE id = ?")); 575 "FROM favicons WHERE id = ?"));
616 if (!statement)
617 return 0;
618 statement.BindInt64(0, source); 576 statement.BindInt64(0, source);
577
619 if (!statement.Run()) 578 if (!statement.Run())
620 return 0; 579 return 0;
621 580
622 // We return the ID of the newly inserted favicon. 581 // We return the ID of the newly inserted favicon.
623 return db_.GetLastInsertRowId(); 582 return db_.GetLastInsertRowId();
624 } 583 }
625 584
626 bool ThumbnailDatabase::CommitTemporaryFaviconTable() { 585 bool ThumbnailDatabase::CommitTemporaryFaviconTable() {
627 // Delete the old favicons table. 586 // Delete the old favicons table.
628 if (!db_.Execute("DROP TABLE favicons")) 587 if (!db_.Execute("DROP TABLE favicons"))
(...skipping 13 matching lines...) Expand all
642 601
643 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file, 602 bool ThumbnailDatabase::RenameAndDropThumbnails(const FilePath& old_db_file,
644 const FilePath& new_db_file) { 603 const FilePath& new_db_file) {
645 // Init favicons table - same schema as the thumbnails. 604 // Init favicons table - same schema as the thumbnails.
646 sql::Connection favicons; 605 sql::Connection favicons;
647 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK) 606 if (OpenDatabase(&favicons, new_db_file) != sql::INIT_OK)
648 return false; 607 return false;
649 608
650 if (!InitFaviconsTable(&favicons, false) || 609 if (!InitFaviconsTable(&favicons, false) ||
651 !InitIconMappingTable(&favicons, false)) { 610 !InitIconMappingTable(&favicons, false)) {
652 NOTREACHED() << "Couldn't init favicons and icon-mapping table.";
653 favicons.Close(); 611 favicons.Close();
654 return false; 612 return false;
655 } 613 }
656 favicons.Close(); 614 favicons.Close();
657 615
658 // Can't attach within a transaction. 616 // Can't attach within a transaction.
659 if (transaction_nesting()) 617 if (transaction_nesting())
660 CommitTransaction(); 618 CommitTransaction();
661 619
662 // Attach new DB. 620 // Attach new DB.
663 { 621 {
664 // This block is needed because otherwise the attach statement is 622 // This block is needed because otherwise the attach statement is
665 // never cleared from cache and we can't close the DB :P 623 // never cleared from cache and we can't close the DB :P
666 sql::Statement attach(db_.GetUniqueStatement("ATTACH ? AS new_favicons")); 624 sql::Statement attach(db_.GetUniqueStatement("ATTACH ? AS new_favicons"));
667 if (!attach) { 625 if (!attach.is_valid()) {
668 NOTREACHED() << "Unable to attach database.";
669 // Keep the transaction open, even though we failed. 626 // Keep the transaction open, even though we failed.
670 BeginTransaction(); 627 BeginTransaction();
671 return false; 628 return false;
672 } 629 }
673 630
674 #if defined(OS_POSIX) 631 #if defined(OS_POSIX)
675 attach.BindString(0, new_db_file.value()); 632 attach.BindString(0, new_db_file.value());
676 #else 633 #else
677 attach.BindString(0, WideToUTF8(new_db_file.value())); 634 attach.BindString(0, WideToUTF8(new_db_file.value()));
678 #endif 635 #endif
679 636
680 if (!attach.Run()) { 637 if (!attach.Run()) {
681 NOTREACHED() << db_.GetErrorMessage();
682 BeginTransaction(); 638 BeginTransaction();
683 return false; 639 return false;
684 } 640 }
685 } 641 }
686 642
687 // Move favicons to the new DB. 643 // Move favicons to the new DB.
688 if (!db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons " 644 if (!db_.Execute("INSERT OR REPLACE INTO new_favicons.favicons "
689 "SELECT * FROM favicons")) { 645 "SELECT * FROM favicons")) {
690 NOTREACHED() << "Unable to copy favicons."; 646 DLOG(FATAL) << "Unable to copy favicons.";
691 BeginTransaction(); 647 BeginTransaction();
692 return false; 648 return false;
693 } 649 }
694 650
695 if (!db_.Execute("DETACH new_favicons")) { 651 if (!db_.Execute("DETACH new_favicons")) {
696 NOTREACHED() << "Unable to detach database."; 652 DLOG(FATAL) << "Unable to detach database.";
697 BeginTransaction(); 653 BeginTransaction();
698 return false; 654 return false;
699 } 655 }
700 656
701 db_.Close(); 657 db_.Close();
702 658
703 // Reset the DB to point to new file. 659 // Reset the DB to point to new file.
704 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK) 660 if (OpenDatabase(&db_, new_db_file) != sql::INIT_OK)
705 return false; 661 return false;
706 662
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 const char* statement_name = 708 const char* statement_name =
753 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping"; 709 is_temporary ? "add_temp_icon_mapping" : "add_icon_mapping";
754 710
755 std::string sql; 711 std::string sql;
756 sql.append("INSERT INTO "); 712 sql.append("INSERT INTO ");
757 sql.append(name); 713 sql.append(name);
758 sql.append("(page_url, icon_id) VALUES (?, ?)"); 714 sql.append("(page_url, icon_id) VALUES (?, ?)");
759 715
760 sql::Statement statement( 716 sql::Statement statement(
761 db_.GetCachedStatement(sql::StatementID(statement_name), sql.c_str())); 717 db_.GetCachedStatement(sql::StatementID(statement_name), sql.c_str()));
762 if (!statement)
763 return 0;
764
765 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url)); 718 statement.BindString(0, URLDatabase::GURLToDatabaseURL(page_url));
766 statement.BindInt64(1, icon_id); 719 statement.BindInt64(1, icon_id);
767 720
768 if (!statement.Run()) 721 if (!statement.Run())
769 return 0; 722 return 0;
770 723
771 return db_.GetLastInsertRowId(); 724 return db_.GetLastInsertRowId();
772 } 725 }
773 726
774 bool ThumbnailDatabase::IsLatestVersion() { 727 bool ThumbnailDatabase::IsLatestVersion() {
775 return meta_table_.GetVersionNumber() == kCurrentVersionNumber; 728 return meta_table_.GetVersionNumber() == kCurrentVersionNumber;
776 } 729 }
777 730
778 bool ThumbnailDatabase::UpgradeToVersion4() { 731 bool ThumbnailDatabase::UpgradeToVersion4() {
779 // Set the default icon type as favicon, so the current data are set 732 // Set the default icon type as favicon, so the current data are set
780 // correctly. 733 // correctly.
781 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) { 734 if (!db_.Execute("ALTER TABLE favicons ADD icon_type INTEGER DEFAULT 1")) {
782 NOTREACHED();
783 return false; 735 return false;
784 } 736 }
785 meta_table_.SetVersionNumber(4); 737 meta_table_.SetVersionNumber(4);
786 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber)); 738 meta_table_.SetCompatibleVersionNumber(std::min(4, kCompatibleVersionNumber));
787 return true; 739 return true;
788 } 740 }
789 741
790 bool ThumbnailDatabase::UpgradeToVersion5() { 742 bool ThumbnailDatabase::UpgradeToVersion5() {
791 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) { 743 if (!db_.Execute("ALTER TABLE favicons ADD sizes LONGVARCHAR")) {
792 NOTREACHED();
793 return false; 744 return false;
794 } 745 }
795 meta_table_.SetVersionNumber(5); 746 meta_table_.SetVersionNumber(5);
796 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber)); 747 meta_table_.SetCompatibleVersionNumber(std::min(5, kCompatibleVersionNumber));
797 return true; 748 return true;
798 } 749 }
799 750
800 } // namespace history 751 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698