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> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 DownloadDatabase (stores a list of downloads) | 61 DownloadDatabase (stores a list of downloads) |
62 VisitDatabase (stores a list of visits for the URLs) | 62 VisitDatabase (stores a list of visits for the URLs) |
63 VisitSegmentDatabase (stores groups of URLs for the most visited view). | 63 VisitSegmentDatabase (stores groups of URLs for the most visited view). |
64 | 64 |
65 ExpireHistoryBackend (manages deleting things older than 3 months) | 65 ExpireHistoryBackend (manages deleting things older than 3 months) |
66 */ | 66 */ |
67 | 67 |
68 namespace history { | 68 namespace history { |
69 | 69 |
70 namespace { | 70 namespace { |
71 | |
71 void RunUnlessCanceled( | 72 void RunUnlessCanceled( |
72 const base::Closure& closure, | 73 const base::Closure& closure, |
73 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled) { | 74 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled) { |
74 if (!is_canceled.Run()) | 75 if (!is_canceled.Run()) |
75 closure.Run(); | 76 closure.Run(); |
76 } | 77 } |
78 | |
79 void NullErrorHandler(int error, sql::Statement* stmt) { | |
80 // Does nothing. Prevents DCHECKING on SQLITE errors on Debug builds. | |
81 } | |
82 | |
77 } // namespace | 83 } // namespace |
78 | 84 |
79 // How long we'll wait to do a commit, so that things are batched together. | 85 // How long we'll wait to do a commit, so that things are batched together. |
80 const int kCommitIntervalSeconds = 10; | 86 const int kCommitIntervalSeconds = 10; |
81 | 87 |
82 // The amount of time before we re-fetch the favicon. | 88 // The amount of time before we re-fetch the favicon. |
83 const int kFaviconRefetchDays = 7; | 89 const int kFaviconRefetchDays = 7; |
84 | 90 |
85 // The maximum number of items we'll allow in the redirect list before | 91 // The maximum number of items we'll allow in the redirect list before |
86 // deleting some. | 92 // deleting some. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
199 } | 205 } |
200 | 206 |
201 // HistoryBackend -------------------------------------------------------------- | 207 // HistoryBackend -------------------------------------------------------------- |
202 | 208 |
203 HistoryBackend::HistoryBackend( | 209 HistoryBackend::HistoryBackend( |
204 Delegate* delegate, | 210 Delegate* delegate, |
205 std::unique_ptr<HistoryBackendClient> backend_client, | 211 std::unique_ptr<HistoryBackendClient> backend_client, |
206 scoped_refptr<base::SequencedTaskRunner> task_runner) | 212 scoped_refptr<base::SequencedTaskRunner> task_runner) |
207 : delegate_(delegate), | 213 : delegate_(delegate), |
208 scheduled_kill_db_(false), | 214 scheduled_kill_db_(false), |
215 error_callback_(base::Bind(&HistoryBackend::DatabaseErrorCallback, | |
216 base::Unretained(this))), | |
209 expirer_(this, backend_client.get(), task_runner), | 217 expirer_(this, backend_client.get(), task_runner), |
210 recent_redirects_(kMaxRedirectCount), | 218 recent_redirects_(kMaxRedirectCount), |
211 backend_destroy_message_loop_(nullptr), | 219 backend_destroy_message_loop_(nullptr), |
212 segment_queried_(false), | 220 segment_queried_(false), |
213 backend_client_(std::move(backend_client)), | 221 backend_client_(std::move(backend_client)), |
214 task_runner_(task_runner) {} | 222 task_runner_(task_runner) {} |
215 | 223 |
216 HistoryBackend::~HistoryBackend() { | 224 HistoryBackend::~HistoryBackend() { |
217 DCHECK(!scheduled_commit_) << "Deleting without cleanup"; | 225 DCHECK(!scheduled_commit_) << "Deleting without cleanup"; |
218 STLDeleteContainerPointers(queued_history_db_tasks_.begin(), | 226 STLDeleteContainerPointers(queued_history_db_tasks_.begin(), |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
641 base::FilePath thumbnail_name = GetFaviconsFileName(); | 649 base::FilePath thumbnail_name = GetFaviconsFileName(); |
642 | 650 |
643 // Delete the old index database files which are no longer used. | 651 // Delete the old index database files which are no longer used. |
644 DeleteFTSIndexDatabases(); | 652 DeleteFTSIndexDatabases(); |
645 | 653 |
646 // History database. | 654 // History database. |
647 db_.reset(new HistoryDatabase( | 655 db_.reset(new HistoryDatabase( |
648 history_database_params.download_interrupt_reason_none, | 656 history_database_params.download_interrupt_reason_none, |
649 history_database_params.download_interrupt_reason_crash)); | 657 history_database_params.download_interrupt_reason_crash)); |
650 | 658 |
651 // Unretained to avoid a ref loop with db_. | 659 db_->set_error_callback(error_callback_); |
652 db_->set_error_callback(base::Bind(&HistoryBackend::DatabaseErrorCallback, | |
653 base::Unretained(this))); | |
654 | 660 |
655 sql::InitStatus status = db_->Init(history_name); | 661 sql::InitStatus status = db_->Init(history_name); |
656 switch (status) { | 662 switch (status) { |
657 case sql::INIT_OK: | 663 case sql::INIT_OK: |
658 break; | 664 break; |
659 case sql::INIT_TOO_NEW: | |
660 delegate_->NotifyProfileError(status); | |
661 db_.reset(); | |
662 return; | |
663 case sql::INIT_FAILURE: { | 665 case sql::INIT_FAILURE: { |
664 // A null db_ will cause all calls on this object to notice this error | 666 // A null db_ will cause all calls on this object to notice this error |
665 // and to not continue. If the error callback scheduled killing the | 667 // and to not continue. If the error callback scheduled killing the |
666 // database, the task it posted has not executed yet. Try killing the | 668 // database, the task it posted has not executed yet. Try killing the |
667 // database now before we close it. | 669 // database now before we close it. |
668 bool kill_db = scheduled_kill_db_; | 670 bool kill_db = scheduled_kill_db_; |
669 if (kill_db) | 671 if (kill_db) |
670 KillHistoryDatabase(); | 672 KillHistoryDatabase(); |
671 UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError", kill_db); | 673 UMA_HISTOGRAM_BOOLEAN("History.AttemptedToFixProfileError", kill_db); |
672 delegate_->NotifyProfileError(status); | 674 } // Falls through. |
675 case sql::INIT_TOO_NEW: { | |
676 std::vector<base::FilePath::StringType> path_components; | |
677 history_name.GetComponents(&path_components); | |
678 #if defined(OS_WIN) | |
679 const base::FilePath::StringType default_dir(L"Default"); | |
680 #else | |
681 const base::FilePath::StringType default_dir("Default"); | |
682 #endif // defined(OS_WIN) | |
683 | |
684 auto itr = std::find(path_components.begin(), path_components.end(), | |
michaeln
2016/07/12 20:05:32
This test for default vs not a is probably not 100
afakhry
2016/07/13 19:57:59
I think you meant:
history_name.DirName().BaseNam
| |
685 default_dir); | |
686 const std::string corrupted_file_name = | |
687 history_name.BaseName().AsUTF8Unsafe() + (itr != path_components.end() | |
688 ? " [Default profile]" | |
689 : " [User profile]"); | |
690 db_diagnostics_["Corrupted file"] = corrupted_file_name; | |
691 delegate_->NotifyProfileError(status, db_diagnostics_); | |
673 db_.reset(); | 692 db_.reset(); |
674 return; | 693 return; |
675 } | 694 } |
676 default: | 695 default: |
677 NOTREACHED(); | 696 NOTREACHED(); |
678 } | 697 } |
679 | 698 |
680 // Fill the in-memory database and send it back to the history service on the | 699 // Fill the in-memory database and send it back to the history service on the |
681 // main thread. | 700 // main thread. |
682 { | 701 { |
(...skipping 1720 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2403 // that we can delete all associated icons in the case of deleting an | 2422 // that we can delete all associated icons in the case of deleting an |
2404 // unvisited bookmarked URL. | 2423 // unvisited bookmarked URL. |
2405 if (visits.empty()) | 2424 if (visits.empty()) |
2406 expirer_.DeleteURL(*i); // There are no more visits; nuke the URL. | 2425 expirer_.DeleteURL(*i); // There are no more visits; nuke the URL. |
2407 } | 2426 } |
2408 } | 2427 } |
2409 | 2428 |
2410 void HistoryBackend::DatabaseErrorCallback(int error, sql::Statement* stmt) { | 2429 void HistoryBackend::DatabaseErrorCallback(int error, sql::Statement* stmt) { |
2411 if (!scheduled_kill_db_ && sql::IsErrorCatastrophic(error)) { | 2430 if (!scheduled_kill_db_ && sql::IsErrorCatastrophic(error)) { |
2412 scheduled_kill_db_ = true; | 2431 scheduled_kill_db_ = true; |
2432 | |
2433 // Gather diagnostic info, but prevent reentrant error callbacks. | |
2434 db_->set_error_callback(base::Bind(&NullErrorHandler)); | |
2435 db_diagnostics_ = db_->GetDiagnosticMap(); | |
2436 db_->set_error_callback(error_callback_); | |
2437 | |
2413 // Don't just do the close/delete here, as we are being called by |db| and | 2438 // Don't just do the close/delete here, as we are being called by |db| and |
2414 // that seems dangerous. | 2439 // that seems dangerous. |
2415 // TODO(shess): Consider changing KillHistoryDatabase() to use | 2440 // TODO(shess): Consider changing KillHistoryDatabase() to use |
2416 // RazeAndClose(). Then it can be cleared immediately. | 2441 // RazeAndClose(). Then it can be cleared immediately. |
2417 task_runner_->PostTask( | 2442 task_runner_->PostTask( |
2418 FROM_HERE, base::Bind(&HistoryBackend::KillHistoryDatabase, this)); | 2443 FROM_HERE, base::Bind(&HistoryBackend::KillHistoryDatabase, this)); |
2419 } | 2444 } |
2420 } | 2445 } |
2421 | 2446 |
2422 void HistoryBackend::KillHistoryDatabase() { | 2447 void HistoryBackend::KillHistoryDatabase() { |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2623 // transaction is currently open. | 2648 // transaction is currently open. |
2624 db_->CommitTransaction(); | 2649 db_->CommitTransaction(); |
2625 db_->Vacuum(); | 2650 db_->Vacuum(); |
2626 db_->BeginTransaction(); | 2651 db_->BeginTransaction(); |
2627 db_->GetStartDate(&first_recorded_time_); | 2652 db_->GetStartDate(&first_recorded_time_); |
2628 | 2653 |
2629 return true; | 2654 return true; |
2630 } | 2655 } |
2631 | 2656 |
2632 } // namespace history | 2657 } // namespace history |
OLD | NEW |