| 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 "components/history/core/browser/history_backend.h" | 5 #include "components/history/core/browser/history_backend.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <functional> | 8 #include <functional> |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/files/file_enumerator.h" | 17 #include "base/files/file_enumerator.h" |
| 18 #include "base/memory/scoped_ptr.h" | 18 #include "base/memory/scoped_ptr.h" |
| 19 #include "base/memory/scoped_vector.h" | 19 #include "base/memory/scoped_vector.h" |
| 20 #include "base/message_loop/message_loop.h" | 20 #include "base/message_loop/message_loop.h" |
| 21 #include "base/metrics/histogram_macros.h" | 21 #include "base/metrics/histogram_macros.h" |
| 22 #include "base/rand_util.h" | 22 #include "base/rand_util.h" |
| 23 #include "base/sequenced_task_runner.h" | 23 #include "base/sequenced_task_runner.h" |
| 24 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 25 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 26 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 27 #include "components/favicon_base/select_favicon_frames.h" | 27 #include "components/favicon_base/select_favicon_frames.h" |
| 28 #include "components/history/core/browser/download_constants.h" | 28 #include "components/history/core/browser/download_constants.h" |
| 29 #include "components/history/core/browser/download_row.h" | 29 #include "components/history/core/browser/download_row.h" |
| 30 #include "components/history/core/browser/history_backend_client.h" |
| 30 #include "components/history/core/browser/history_backend_observer.h" | 31 #include "components/history/core/browser/history_backend_observer.h" |
| 31 #include "components/history/core/browser/history_client.h" | |
| 32 #include "components/history/core/browser/history_constants.h" | 32 #include "components/history/core/browser/history_constants.h" |
| 33 #include "components/history/core/browser/history_database.h" | 33 #include "components/history/core/browser/history_database.h" |
| 34 #include "components/history/core/browser/history_database_params.h" | 34 #include "components/history/core/browser/history_database_params.h" |
| 35 #include "components/history/core/browser/history_db_task.h" | 35 #include "components/history/core/browser/history_db_task.h" |
| 36 #include "components/history/core/browser/in_memory_history_backend.h" | 36 #include "components/history/core/browser/in_memory_history_backend.h" |
| 37 #include "components/history/core/browser/keyword_search_term.h" | 37 #include "components/history/core/browser/keyword_search_term.h" |
| 38 #include "components/history/core/browser/page_usage_data.h" | 38 #include "components/history/core/browser/page_usage_data.h" |
| 39 #include "components/history/core/browser/typed_url_syncable_service.h" | 39 #include "components/history/core/browser/typed_url_syncable_service.h" |
| 40 #include "components/history/core/browser/visit_filter.h" | 40 #include "components/history/core/browser/visit_filter.h" |
| 41 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 41 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 HistoryBackendHelper::HistoryBackendHelper() { | 192 HistoryBackendHelper::HistoryBackendHelper() { |
| 193 } | 193 } |
| 194 | 194 |
| 195 HistoryBackendHelper::~HistoryBackendHelper() { | 195 HistoryBackendHelper::~HistoryBackendHelper() { |
| 196 } | 196 } |
| 197 | 197 |
| 198 // HistoryBackend -------------------------------------------------------------- | 198 // HistoryBackend -------------------------------------------------------------- |
| 199 | 199 |
| 200 HistoryBackend::HistoryBackend( | 200 HistoryBackend::HistoryBackend( |
| 201 Delegate* delegate, | 201 Delegate* delegate, |
| 202 HistoryClient* history_client, | 202 scoped_ptr<HistoryBackendClient> backend_client, |
| 203 scoped_refptr<base::SequencedTaskRunner> task_runner) | 203 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 204 : delegate_(delegate), | 204 : delegate_(delegate), |
| 205 scheduled_kill_db_(false), | 205 scheduled_kill_db_(false), |
| 206 expirer_(this, history_client, task_runner), | 206 expirer_(this, backend_client.get(), task_runner), |
| 207 recent_redirects_(kMaxRedirectCount), | 207 recent_redirects_(kMaxRedirectCount), |
| 208 backend_destroy_message_loop_(nullptr), | 208 backend_destroy_message_loop_(nullptr), |
| 209 segment_queried_(false), | 209 segment_queried_(false), |
| 210 history_client_(history_client), | 210 backend_client_(backend_client.Pass()), |
| 211 task_runner_(task_runner) { | 211 task_runner_(task_runner) { |
| 212 } | 212 } |
| 213 | 213 |
| 214 HistoryBackend::~HistoryBackend() { | 214 HistoryBackend::~HistoryBackend() { |
| 215 DCHECK(!scheduled_commit_.get()) << "Deleting without cleanup"; | 215 DCHECK(!scheduled_commit_.get()) << "Deleting without cleanup"; |
| 216 STLDeleteContainerPointers(queued_history_db_tasks_.begin(), | 216 STLDeleteContainerPointers(queued_history_db_tasks_.begin(), |
| 217 queued_history_db_tasks_.end()); | 217 queued_history_db_tasks_.end()); |
| 218 queued_history_db_tasks_.clear(); | 218 queued_history_db_tasks_.clear(); |
| 219 | 219 |
| 220 // Release stashed embedder object before cleaning up the databases. | 220 // Release stashed embedder object before cleaning up the databases. |
| 221 supports_user_data_helper_.reset(); | 221 supports_user_data_helper_.reset(); |
| 222 | 222 |
| 223 // First close the databases before optionally running the "destroy" task. | 223 // First close the databases before optionally running the "destroy" task. |
| 224 CloseAllDatabases(); | 224 CloseAllDatabases(); |
| 225 | 225 |
| 226 if (!backend_destroy_task_.is_null()) { | 226 if (!backend_destroy_task_.is_null()) { |
| 227 // Notify an interested party (typically a unit test) that we're done. | 227 // Notify an interested party (typically a unit test) that we're done. |
| 228 DCHECK(backend_destroy_message_loop_); | 228 DCHECK(backend_destroy_message_loop_); |
| 229 backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_); | 229 backend_destroy_message_loop_->PostTask(FROM_HERE, backend_destroy_task_); |
| 230 } | 230 } |
| 231 | 231 |
| 232 if (history_client_ && !history_dir_.empty()) | 232 #if defined(OS_ANDROID) |
| 233 history_client_->OnHistoryBackendDestroyed(this, history_dir_); | 233 if (backend_client_ && !history_dir_.empty()) |
| 234 backend_client_->OnHistoryBackendDestroyed(this, history_dir_); |
| 235 #endif |
| 234 } | 236 } |
| 235 | 237 |
| 236 void HistoryBackend::Init( | 238 void HistoryBackend::Init( |
| 237 const std::string& languages, | 239 const std::string& languages, |
| 238 bool force_fail, | 240 bool force_fail, |
| 239 const HistoryDatabaseParams& history_database_params) { | 241 const HistoryDatabaseParams& history_database_params) { |
| 240 // HistoryBackend is created on the UI thread by HistoryService, then the | 242 // HistoryBackend is created on the UI thread by HistoryService, then the |
| 241 // HistoryBackend::Init() method is called on the DB thread. Create the | 243 // HistoryBackend::Init() method is called on the DB thread. Create the |
| 242 // base::SupportsUserData on the DB thread since it is not thread-safe. | 244 // base::SupportsUserData on the DB thread since it is not thread-safe. |
| 243 supports_user_data_helper_.reset(new HistoryBackendHelper); | 245 supports_user_data_helper_.reset(new HistoryBackendHelper); |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 650 if (mem_backend->Init(history_name)) | 652 if (mem_backend->Init(history_name)) |
| 651 delegate_->SetInMemoryBackend(mem_backend.Pass()); | 653 delegate_->SetInMemoryBackend(mem_backend.Pass()); |
| 652 } | 654 } |
| 653 db_->BeginExclusiveMode(); // Must be after the mem backend read the data. | 655 db_->BeginExclusiveMode(); // Must be after the mem backend read the data. |
| 654 | 656 |
| 655 // Thumbnail database. | 657 // Thumbnail database. |
| 656 // TODO(shess): "thumbnail database" these days only stores | 658 // TODO(shess): "thumbnail database" these days only stores |
| 657 // favicons. Thumbnails are stored in "top sites". Consider | 659 // favicons. Thumbnails are stored in "top sites". Consider |
| 658 // renaming "thumbnail" references to "favicons" or something of the | 660 // renaming "thumbnail" references to "favicons" or something of the |
| 659 // sort. | 661 // sort. |
| 660 thumbnail_db_.reset(new ThumbnailDatabase(history_client_)); | 662 thumbnail_db_.reset(new ThumbnailDatabase(backend_client_.get())); |
| 661 if (thumbnail_db_->Init(thumbnail_name) != sql::INIT_OK) { | 663 if (thumbnail_db_->Init(thumbnail_name) != sql::INIT_OK) { |
| 662 // Unlike the main database, we don't error out when the database is too | 664 // Unlike the main database, we don't error out when the database is too |
| 663 // new because this error is much less severe. Generally, this shouldn't | 665 // new because this error is much less severe. Generally, this shouldn't |
| 664 // happen since the thumbnail and main database versions should be in sync. | 666 // happen since the thumbnail and main database versions should be in sync. |
| 665 // We'll just continue without thumbnails & favicons in this case or any | 667 // We'll just continue without thumbnails & favicons in this case or any |
| 666 // other error. | 668 // other error. |
| 667 LOG(WARNING) << "Could not initialize the thumbnail database."; | 669 LOG(WARNING) << "Could not initialize the thumbnail database."; |
| 668 thumbnail_db_.reset(); | 670 thumbnail_db_.reset(); |
| 669 } | 671 } |
| 670 | 672 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 689 db_->BeginTransaction(); | 691 db_->BeginTransaction(); |
| 690 if (thumbnail_db_) | 692 if (thumbnail_db_) |
| 691 thumbnail_db_->BeginTransaction(); | 693 thumbnail_db_->BeginTransaction(); |
| 692 | 694 |
| 693 // Get the first item in our database. | 695 // Get the first item in our database. |
| 694 db_->GetStartDate(&first_recorded_time_); | 696 db_->GetStartDate(&first_recorded_time_); |
| 695 | 697 |
| 696 // Start expiring old stuff. | 698 // Start expiring old stuff. |
| 697 expirer_.StartExpiringOldStuff(TimeDelta::FromDays(kExpireDaysThreshold)); | 699 expirer_.StartExpiringOldStuff(TimeDelta::FromDays(kExpireDaysThreshold)); |
| 698 | 700 |
| 699 if (history_client_) { | 701 #if defined(OS_ANDROID) |
| 700 history_client_->OnHistoryBackendInitialized( | 702 if (backend_client_) { |
| 703 backend_client_->OnHistoryBackendInitialized( |
| 701 this, db_.get(), thumbnail_db_.get(), history_dir_); | 704 this, db_.get(), thumbnail_db_.get(), history_dir_); |
| 702 } | 705 } |
| 706 #endif |
| 703 | 707 |
| 704 LOCAL_HISTOGRAM_TIMES("History.InitTime", TimeTicks::Now() - beginning_time); | 708 LOCAL_HISTOGRAM_TIMES("History.InitTime", TimeTicks::Now() - beginning_time); |
| 705 } | 709 } |
| 706 | 710 |
| 707 void HistoryBackend::OnMemoryPressure( | 711 void HistoryBackend::OnMemoryPressure( |
| 708 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 712 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 709 bool trim_aggressively = | 713 bool trim_aggressively = |
| 710 memory_pressure_level == | 714 memory_pressure_level == |
| 711 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | 715 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| 712 if (db_) | 716 if (db_) |
| (...skipping 1094 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1807 if (!favicon_id) { | 1811 if (!favicon_id) { |
| 1808 // This favicon doesn't exist yet, so we create it using the given data. | 1812 // This favicon doesn't exist yet, so we create it using the given data. |
| 1809 // TODO(pkotwicz): Pass in real pixel size. | 1813 // TODO(pkotwicz): Pass in real pixel size. |
| 1810 favicon_id = thumbnail_db_->AddFavicon( | 1814 favicon_id = thumbnail_db_->AddFavicon( |
| 1811 favicon_usage[i].favicon_url, favicon_base::FAVICON, | 1815 favicon_usage[i].favicon_url, favicon_base::FAVICON, |
| 1812 new base::RefCountedBytes(favicon_usage[i].png_data), now, | 1816 new base::RefCountedBytes(favicon_usage[i].png_data), now, |
| 1813 gfx::Size()); | 1817 gfx::Size()); |
| 1814 } | 1818 } |
| 1815 | 1819 |
| 1816 // Save the mapping from all the URLs to the favicon. | 1820 // Save the mapping from all the URLs to the favicon. |
| 1817 HistoryClient* history_client = GetHistoryClient(); | |
| 1818 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); | 1821 for (std::set<GURL>::const_iterator url = favicon_usage[i].urls.begin(); |
| 1819 url != favicon_usage[i].urls.end(); ++url) { | 1822 url != favicon_usage[i].urls.end(); ++url) { |
| 1820 URLRow url_row; | 1823 URLRow url_row; |
| 1821 if (!db_->GetRowForURL(*url, &url_row)) { | 1824 if (!db_->GetRowForURL(*url, &url_row)) { |
| 1822 // If the URL is present as a bookmark, add the url in history to | 1825 // If the URL is present as a bookmark, add the url in history to |
| 1823 // save the favicon mapping. This will match with what history db does | 1826 // save the favicon mapping. This will match with what history db does |
| 1824 // for regular bookmarked URLs with favicons - when history db is | 1827 // for regular bookmarked URLs with favicons - when history db is |
| 1825 // cleaned, we keep an entry in the db with 0 visits as long as that | 1828 // cleaned, we keep an entry in the db with 0 visits as long as that |
| 1826 // url is bookmarked. | 1829 // url is bookmarked. |
| 1827 if (history_client && history_client->IsBookmarked(*url)) { | 1830 if (backend_client_ && backend_client_->IsBookmarked(*url)) { |
| 1828 URLRow url_info(*url); | 1831 URLRow url_info(*url); |
| 1829 url_info.set_visit_count(0); | 1832 url_info.set_visit_count(0); |
| 1830 url_info.set_typed_count(0); | 1833 url_info.set_typed_count(0); |
| 1831 url_info.set_last_visit(base::Time()); | 1834 url_info.set_last_visit(base::Time()); |
| 1832 url_info.set_hidden(false); | 1835 url_info.set_hidden(false); |
| 1833 db_->AddURL(url_info); | 1836 db_->AddURL(url_info); |
| 1834 thumbnail_db_->AddIconMapping(*url, favicon_id); | 1837 thumbnail_db_->AddIconMapping(*url, favicon_id); |
| 1835 favicons_changed.insert(*url); | 1838 favicons_changed.insert(*url); |
| 1836 } | 1839 } |
| 1837 } else { | 1840 } else { |
| (...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2551 // that any data we don't want to keep is now in an unused page. | 2554 // that any data we don't want to keep is now in an unused page. |
| 2552 // 3. Renaming the temporary tables to match the original. | 2555 // 3. Renaming the temporary tables to match the original. |
| 2553 // 4. Vacuuming the database to delete the unused pages. | 2556 // 4. Vacuuming the database to delete the unused pages. |
| 2554 // | 2557 // |
| 2555 // Since we are likely to have very few bookmarks and their dependencies | 2558 // Since we are likely to have very few bookmarks and their dependencies |
| 2556 // compared to all history, this is also much faster than just deleting from | 2559 // compared to all history, this is also much faster than just deleting from |
| 2557 // the original tables directly. | 2560 // the original tables directly. |
| 2558 | 2561 |
| 2559 // Get the bookmarked URLs. | 2562 // Get the bookmarked URLs. |
| 2560 std::vector<URLAndTitle> starred_url_and_titles; | 2563 std::vector<URLAndTitle> starred_url_and_titles; |
| 2561 HistoryClient* history_client = GetHistoryClient(); | 2564 if (backend_client_) |
| 2562 if (history_client) | 2565 backend_client_->GetBookmarks(&starred_url_and_titles); |
| 2563 history_client->GetBookmarks(&starred_url_and_titles); | |
| 2564 | 2566 |
| 2565 URLRows kept_url_rows; | 2567 URLRows kept_url_rows; |
| 2566 std::vector<GURL> starred_urls; | 2568 std::vector<GURL> starred_urls; |
| 2567 for (const URLAndTitle& url_and_title : starred_url_and_titles) { | 2569 for (const URLAndTitle& url_and_title : starred_url_and_titles) { |
| 2568 const GURL& url = url_and_title.url; | 2570 const GURL& url = url_and_title.url; |
| 2569 starred_urls.push_back(url); | 2571 starred_urls.push_back(url); |
| 2570 | 2572 |
| 2571 URLRow row; | 2573 URLRow row; |
| 2572 if (db_->GetRowForURL(url, &row)) { | 2574 if (db_->GetRowForURL(url, &row)) { |
| 2573 // Clear the last visit time so when we write these rows they are "clean." | 2575 // Clear the last visit time so when we write these rows they are "clean." |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2663 // when there is no transaction open, and we assume that our long-running | 2665 // when there is no transaction open, and we assume that our long-running |
| 2664 // transaction is currently open. | 2666 // transaction is currently open. |
| 2665 db_->CommitTransaction(); | 2667 db_->CommitTransaction(); |
| 2666 db_->Vacuum(); | 2668 db_->Vacuum(); |
| 2667 db_->BeginTransaction(); | 2669 db_->BeginTransaction(); |
| 2668 db_->GetStartDate(&first_recorded_time_); | 2670 db_->GetStartDate(&first_recorded_time_); |
| 2669 | 2671 |
| 2670 return true; | 2672 return true; |
| 2671 } | 2673 } |
| 2672 | 2674 |
| 2673 HistoryClient* HistoryBackend::GetHistoryClient() { | |
| 2674 if (history_client_) | |
| 2675 history_client_->BlockUntilBookmarksLoaded(); | |
| 2676 return history_client_; | |
| 2677 } | |
| 2678 | |
| 2679 } // namespace history | 2675 } // namespace history |
| OLD | NEW |