OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/history_backend.h" | 5 #include "chrome/browser/history/history_backend.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <vector> | 10 #include <vector> |
(...skipping 1777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1788 void HistoryBackend::SetFaviconOutOfDateForPage(const GURL& page_url) { | 1788 void HistoryBackend::SetFaviconOutOfDateForPage(const GURL& page_url) { |
1789 std::vector<IconMapping> icon_mappings; | 1789 std::vector<IconMapping> icon_mappings; |
1790 | 1790 |
1791 if (!thumbnail_db_.get() || | 1791 if (!thumbnail_db_.get() || |
1792 !thumbnail_db_->GetIconMappingsForPageURL(page_url, | 1792 !thumbnail_db_->GetIconMappingsForPageURL(page_url, |
1793 &icon_mappings)) | 1793 &icon_mappings)) |
1794 return; | 1794 return; |
1795 | 1795 |
1796 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); | 1796 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); |
1797 m != icon_mappings.end(); ++m) { | 1797 m != icon_mappings.end(); ++m) { |
1798 thumbnail_db_->SetFaviconLastUpdateTime(m->icon_id, Time()); | 1798 thumbnail_db_->SetFaviconOutOfDate(m->icon_id); |
1799 } | 1799 } |
1800 ScheduleCommit(); | 1800 ScheduleCommit(); |
1801 } | 1801 } |
1802 | 1802 |
1803 void HistoryBackend::CloneFavicon(const GURL& old_page_url, | 1803 void HistoryBackend::CloneFavicon(const GURL& old_page_url, |
1804 const GURL& new_page_url) { | 1804 const GURL& new_page_url) { |
1805 if (!thumbnail_db_.get()) | 1805 if (!thumbnail_db_.get()) |
1806 return; | 1806 return; |
1807 | 1807 |
1808 // Prevent cross-domain cloning. | 1808 // Prevent cross-domain cloning. |
(...skipping 16 matching lines...) Expand all Loading... | |
1825 | 1825 |
1826 for (size_t i = 0; i < favicon_usage.size(); i++) { | 1826 for (size_t i = 0; i < favicon_usage.size(); i++) { |
1827 FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL( | 1827 FaviconID favicon_id = thumbnail_db_->GetFaviconIDForFaviconURL( |
1828 favicon_usage[i].favicon_url, history::FAVICON, NULL); | 1828 favicon_usage[i].favicon_url, history::FAVICON, NULL); |
1829 if (!favicon_id) { | 1829 if (!favicon_id) { |
1830 // This favicon doesn't exist yet, so we create it using the given data. | 1830 // This favicon doesn't exist yet, so we create it using the given data. |
1831 favicon_id = thumbnail_db_->AddFavicon(favicon_usage[i].favicon_url, | 1831 favicon_id = thumbnail_db_->AddFavicon(favicon_usage[i].favicon_url, |
1832 history::FAVICON); | 1832 history::FAVICON); |
1833 if (!favicon_id) | 1833 if (!favicon_id) |
1834 continue; // Unable to add the favicon. | 1834 continue; // Unable to add the favicon. |
1835 thumbnail_db_->SetFavicon(favicon_id, | 1835 thumbnail_db_->AddFaviconFrame(favicon_id, |
1836 new base::RefCountedBytes(favicon_usage[i].png_data), now); | 1836 new base::RefCountedBytes(favicon_usage[i].png_data), now); |
1837 } | 1837 } |
1838 | 1838 |
1839 // Save the mapping from all the URLs to the favicon. | 1839 // Save the mapping from all the URLs to the favicon. |
1840 BookmarkService* bookmark_service = GetBookmarkService(); | 1840 BookmarkService* bookmark_service = GetBookmarkService(); |
1841 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); | 1841 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); |
1842 url != favicon_usage[i].urls.end(); ++url) { | 1842 url != favicon_usage[i].urls.end(); ++url) { |
1843 URLRow url_row; | 1843 URLRow url_row; |
1844 if (!db_->GetRowForURL(*url, &url_row)) { | 1844 if (!db_->GetRowForURL(*url, &url_row)) { |
1845 // If the URL is present as a bookmark, add the url in history to | 1845 // If the URL is present as a bookmark, add the url in history to |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1948 DCHECK(data.get()); | 1948 DCHECK(data.get()); |
1949 if (!thumbnail_db_.get() || !db_.get()) | 1949 if (!thumbnail_db_.get() || !db_.get()) |
1950 return; | 1950 return; |
1951 | 1951 |
1952 FaviconID id = thumbnail_db_->GetFaviconIDForFaviconURL( | 1952 FaviconID id = thumbnail_db_->GetFaviconIDForFaviconURL( |
1953 icon_url, icon_type, NULL); | 1953 icon_url, icon_type, NULL); |
1954 if (!id) | 1954 if (!id) |
1955 id = thumbnail_db_->AddFavicon(icon_url, icon_type); | 1955 id = thumbnail_db_->AddFavicon(icon_url, icon_type); |
1956 | 1956 |
1957 // Set the image data. | 1957 // Set the image data. |
1958 thumbnail_db_->SetFavicon(id, data, Time::Now()); | 1958 thumbnail_db_->AddFaviconFrame(id, data, Time::Now()); |
1959 | 1959 |
1960 SetFaviconMapping(page_url, id, icon_type); | 1960 SetFaviconMapping(page_url, id, icon_type); |
1961 } | 1961 } |
1962 | 1962 |
1963 void HistoryBackend::SetFaviconMapping(const GURL& page_url, | 1963 void HistoryBackend::SetFaviconMapping(const GURL& page_url, |
1964 FaviconID id, | 1964 FaviconID id, |
1965 IconType icon_type) { | 1965 IconType icon_type) { |
1966 if (!thumbnail_db_.get()) | 1966 if (!thumbnail_db_.get()) |
1967 return; | 1967 return; |
1968 | 1968 |
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2327 bool HistoryBackend::ClearAllThumbnailHistory(URLRows* kept_urls) { | 2327 bool HistoryBackend::ClearAllThumbnailHistory(URLRows* kept_urls) { |
2328 if (!thumbnail_db_.get()) { | 2328 if (!thumbnail_db_.get()) { |
2329 // When we have no reference to the thumbnail database, maybe there was an | 2329 // When we have no reference to the thumbnail database, maybe there was an |
2330 // error opening it. In this case, we just try to blow it away to try to | 2330 // error opening it. In this case, we just try to blow it away to try to |
2331 // fix the error if it exists. This may fail, in which case either the | 2331 // fix the error if it exists. This may fail, in which case either the |
2332 // file doesn't exist or there's no more we can do. | 2332 // file doesn't exist or there's no more we can do. |
2333 file_util::Delete(GetThumbnailFileName(), false); | 2333 file_util::Delete(GetThumbnailFileName(), false); |
2334 return true; | 2334 return true; |
2335 } | 2335 } |
2336 | 2336 |
2337 // Create the duplicate favicon table, this is where the favicons we want | 2337 // Create duplicate favicon and favicon frames tables, this is where the |
2338 // to keep will be stored. | 2338 // favicons we want to keep will be stored. |
2339 if (!thumbnail_db_->InitTemporaryFaviconFramesTable()) | |
sky
2012/08/02 19:50:31
I think it less error prone and easier to maintain
| |
2340 return false; | |
2341 | |
2339 if (!thumbnail_db_->InitTemporaryFaviconsTable()) | 2342 if (!thumbnail_db_->InitTemporaryFaviconsTable()) |
2340 return false; | 2343 return false; |
2341 | 2344 |
2342 if (!thumbnail_db_->InitTemporaryIconMappingTable()) | 2345 if (!thumbnail_db_->InitTemporaryIconMappingTable()) |
2343 return false; | 2346 return false; |
2344 | 2347 |
2345 // This maps existing favicon IDs to the ones in the temporary table. | 2348 // This maps existing favicon IDs to the ones in the temporary table. |
2346 typedef std::map<FaviconID, FaviconID> FaviconMap; | 2349 typedef std::map<FaviconID, FaviconID> FaviconMap; |
2347 FaviconMap copied_favicons; | 2350 FaviconMap copied_favicons; |
2348 | 2351 |
2349 // Copy all unique favicons to the temporary table, and update all the | 2352 // Copy all unique favicons to the temporary table, and update all the |
2350 // URLs to have the new IDs. | 2353 // URLs to have the new IDs. |
2351 for (URLRows::iterator i = kept_urls->begin(); i != kept_urls->end(); ++i) { | 2354 for (URLRows::iterator i = kept_urls->begin(); i != kept_urls->end(); ++i) { |
2352 std::vector<IconMapping> icon_mappings; | 2355 std::vector<IconMapping> icon_mappings; |
2353 if (!thumbnail_db_->GetIconMappingsForPageURL(i->url(), &icon_mappings)) | 2356 if (!thumbnail_db_->GetIconMappingsForPageURL(i->url(), &icon_mappings)) |
2354 continue; | 2357 continue; |
2355 | 2358 |
2356 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); | 2359 for (std::vector<IconMapping>::iterator m = icon_mappings.begin(); |
2357 m != icon_mappings.end(); ++m) { | 2360 m != icon_mappings.end(); ++m) { |
2358 FaviconID old_id = m->icon_id; | 2361 FaviconID old_id = m->icon_id; |
2359 FaviconID new_id; | 2362 FaviconID new_id; |
2360 FaviconMap::const_iterator found = copied_favicons.find(old_id); | 2363 FaviconMap::const_iterator found = copied_favicons.find(old_id); |
2361 if (found == copied_favicons.end()) { | 2364 if (found == copied_favicons.end()) { |
2362 new_id = thumbnail_db_->CopyToTemporaryFaviconTable(old_id); | 2365 new_id = thumbnail_db_->CopyToTemporaryFaviconTable(old_id); |
2366 thumbnail_db_->CopyToTemporaryFaviconFramesTable(old_id); | |
2363 copied_favicons[old_id] = new_id; | 2367 copied_favicons[old_id] = new_id; |
2364 } else { | 2368 } else { |
2365 // We already encountered a URL that used this favicon, use the ID we | 2369 // We already encountered a URL that used this favicon, use the ID we |
2366 // previously got. | 2370 // previously got. |
2367 new_id = found->second; | 2371 new_id = found->second; |
2368 } | 2372 } |
2369 // Add Icon mapping, and we don't care wheteher it suceeded or not. | 2373 // Add Icon mapping, and we don't care wheteher it suceeded or not. |
2370 thumbnail_db_->AddToTemporaryIconMappingTable(i->url(), new_id); | 2374 thumbnail_db_->AddToTemporaryIconMappingTable(i->url(), new_id); |
2371 } | 2375 } |
2372 } | 2376 } |
2373 #if defined(OS_ANDROID) | 2377 #if defined(OS_ANDROID) |
2374 // TODO (michaelbai): Add the unit test once AndroidProviderBackend is | 2378 // TODO (michaelbai): Add the unit test once AndroidProviderBackend is |
2375 // avaliable in HistoryBackend. | 2379 // avaliable in HistoryBackend. |
2376 db_->ClearAndroidURLRows(); | 2380 db_->ClearAndroidURLRows(); |
2377 #endif | 2381 #endif |
2378 | 2382 |
2379 // Rename the duplicate favicon and icon_mapping back table and recreate the | 2383 // Drop original favicon_frames, favicons, and icon mappings tables and |
2380 // other tables. This will make the database consistent again. | 2384 // replace them with the duplicate tables. Recreate the other tables. This |
2385 // will make the database consistent again. | |
2386 thumbnail_db_->CommitTemporaryFaviconFrameTable(); | |
2381 thumbnail_db_->CommitTemporaryFaviconTable(); | 2387 thumbnail_db_->CommitTemporaryFaviconTable(); |
2382 thumbnail_db_->CommitTemporaryIconMappingTable(); | 2388 thumbnail_db_->CommitTemporaryIconMappingTable(); |
2383 | 2389 |
2384 thumbnail_db_->RecreateThumbnailTable(); | 2390 thumbnail_db_->RecreateThumbnailTable(); |
2385 | 2391 |
2386 // Vacuum to remove all the pages associated with the dropped tables. There | 2392 // Vacuum to remove all the pages associated with the dropped tables. There |
2387 // must be no transaction open on the table when we do this. We assume that | 2393 // must be no transaction open on the table when we do this. We assume that |
2388 // our long-running transaction is open, so we complete it and start it again. | 2394 // our long-running transaction is open, so we complete it and start it again. |
2389 DCHECK(thumbnail_db_->transaction_nesting() == 1); | 2395 DCHECK(thumbnail_db_->transaction_nesting() == 1); |
2390 thumbnail_db_->CommitTransaction(); | 2396 thumbnail_db_->CommitTransaction(); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2484 info.url_id = visit.url_id; | 2490 info.url_id = visit.url_id; |
2485 info.time = visit.visit_time; | 2491 info.time = visit.visit_time; |
2486 info.transition = visit.transition; | 2492 info.transition = visit.transition; |
2487 // If we don't have a delegate yet during setup or shutdown, we will drop | 2493 // If we don't have a delegate yet during setup or shutdown, we will drop |
2488 // these notifications. | 2494 // these notifications. |
2489 if (delegate_.get()) | 2495 if (delegate_.get()) |
2490 delegate_->NotifyVisitDBObserversOnAddVisit(info); | 2496 delegate_->NotifyVisitDBObserversOnAddVisit(info); |
2491 } | 2497 } |
2492 | 2498 |
2493 } // namespace history | 2499 } // namespace history |
OLD | NEW |