Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/offline_pages/core/snapshot_controller.h" | 5 #include "components/offline_pages/core/snapshot_controller.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "components/offline_pages/core/offline_page_feature.h" | 11 #include "components/offline_pages/core/offline_page_feature.h" |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 const bool kConsiderDocumentAvailableForSnapshot = true; | |
|
fgorski
2017/04/20 16:39:05
This is where I'd put all constants for foreground
chili
2017/04/21 00:12:54
Done.
| |
| 15 | |
| 14 // Default delay, in milliseconds, between the main document parsed event and | 16 // Default delay, in milliseconds, between the main document parsed event and |
| 15 // snapshot. Note: this snapshot might not occur if the OnLoad event and | 17 // snapshot. Note: this snapshot might not occur if the OnLoad event and |
| 16 // OnLoad delay elapses first to trigger a final snapshot. | 18 // OnLoad delay elapses first to trigger a final snapshot. |
| 17 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; | 19 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; |
| 18 | 20 |
| 19 // Default delay, in milliseconds, between the main document OnLoad event and | 21 // Default delay, in milliseconds, between the main document OnLoad event and |
| 20 // snapshot. | 22 // snapshot. |
| 21 const int64_t kDelayAfterDocumentOnLoadCompletedMs = 1000; | 23 const int64_t kDelayAfterDocumentOnLoadCompletedMs = 1000; |
| 22 | 24 |
| 23 // Delay for testing to keep polling times reasonable. | 25 // Delay for testing to keep polling times reasonable. |
| 24 const int64_t kDelayForTests = 0; | 26 const int64_t kDelayForTests = 0; |
| 25 | 27 |
| 26 } // namespace | 28 } // namespace |
| 27 | 29 |
| 28 namespace offline_pages { | 30 namespace offline_pages { |
| 29 | 31 |
| 30 SnapshotController::SnapshotController( | 32 SnapshotController::SnapshotController( |
| 31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 33 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 32 SnapshotController::Client* client) | 34 SnapshotController::Client* client) |
| 33 : SnapshotController(task_runner, | 35 : SnapshotController(task_runner, |
| 34 client, | 36 client, |
| 35 kDefaultDelayAfterDocumentAvailableMs, | 37 kDefaultDelayAfterDocumentAvailableMs, |
| 36 kDelayAfterDocumentOnLoadCompletedMs) {} | 38 kDelayAfterDocumentOnLoadCompletedMs, |
| 39 kConsiderDocumentAvailableForSnapshot) {} | |
| 37 | 40 |
| 38 SnapshotController::SnapshotController( | 41 SnapshotController::SnapshotController( |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 42 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 40 SnapshotController::Client* client, | 43 SnapshotController::Client* client, |
| 41 int64_t delay_after_document_available_ms, | 44 int64_t delay_after_document_available_ms, |
| 42 int64_t delay_after_document_on_load_completed_ms) | 45 int64_t delay_after_document_on_load_completed_ms, |
| 46 bool consider_document_available_for_snapshot) | |
| 43 : task_runner_(task_runner), | 47 : task_runner_(task_runner), |
| 44 client_(client), | 48 client_(client), |
| 45 state_(State::READY), | 49 state_(State::READY), |
| 46 delay_after_document_available_ms_(delay_after_document_available_ms), | 50 delay_after_document_available_ms_(delay_after_document_available_ms), |
| 47 delay_after_document_on_load_completed_ms_( | 51 delay_after_document_on_load_completed_ms_( |
| 48 delay_after_document_on_load_completed_ms), | 52 delay_after_document_on_load_completed_ms), |
| 53 consider_document_available_for_snapshot_( | |
| 54 consider_document_available_for_snapshot), | |
| 49 weak_ptr_factory_(this) { | 55 weak_ptr_factory_(this) { |
| 50 if (offline_pages::ShouldUseTestingSnapshotDelay()) { | 56 if (offline_pages::ShouldUseTestingSnapshotDelay()) { |
| 51 delay_after_document_available_ms_ = kDelayForTests; | 57 delay_after_document_available_ms_ = kDelayForTests; |
| 52 delay_after_document_on_load_completed_ms_ = kDelayForTests; | 58 delay_after_document_on_load_completed_ms_ = kDelayForTests; |
| 53 } | 59 } |
| 54 } | 60 } |
| 55 | 61 |
| 56 SnapshotController::~SnapshotController() {} | 62 SnapshotController::~SnapshotController() {} |
| 57 | 63 |
| 58 void SnapshotController::Reset() { | 64 void SnapshotController::Reset() { |
| 59 // Cancel potentially delayed tasks that relate to the previous 'session'. | 65 // Cancel potentially delayed tasks that relate to the previous 'session'. |
| 60 weak_ptr_factory_.InvalidateWeakPtrs(); | 66 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 61 state_ = State::READY; | 67 state_ = State::READY; |
| 62 current_page_quality_ = PageQuality::POOR; | 68 current_page_quality_ = PageQuality::POOR; |
| 63 } | 69 } |
| 64 | 70 |
| 65 void SnapshotController::Stop() { | 71 void SnapshotController::Stop() { |
| 66 state_ = State::STOPPED; | 72 state_ = State::STOPPED; |
| 67 } | 73 } |
| 68 | 74 |
| 69 void SnapshotController::PendingSnapshotCompleted() { | 75 void SnapshotController::PendingSnapshotCompleted() { |
| 70 // Unless the controller is "stopped", enable the subsequent snapshots. | 76 // Unless the controller is "stopped", enable the subsequent snapshots. |
| 71 // Stopped state prevents any further snapshots form being started. | 77 // Stopped state prevents any further snapshots form being started. |
| 72 if (state_ == State::STOPPED) | 78 if (state_ == State::STOPPED) |
| 73 return; | 79 return; |
| 74 state_ = State::READY; | 80 state_ = State::READY; |
| 75 } | 81 } |
| 76 | 82 |
| 77 void SnapshotController::DocumentAvailableInMainFrame() { | 83 void SnapshotController::DocumentAvailableInMainFrame() { |
| 78 DCHECK_EQ(PageQuality::POOR, current_page_quality_); | 84 if (consider_document_available_for_snapshot_) { |
| 79 // Post a delayed task to snapshot. | 85 DCHECK_EQ(PageQuality::POOR, current_page_quality_); |
| 80 task_runner_->PostDelayedTask( | 86 // Post a delayed task to snapshot. |
| 81 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshot, | 87 task_runner_->PostDelayedTask( |
| 82 weak_ptr_factory_.GetWeakPtr(), | 88 FROM_HERE, |
| 83 PageQuality::FAIR_AND_IMPROVING), | 89 base::Bind(&SnapshotController::MaybeStartSnapshot, |
| 84 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); | 90 weak_ptr_factory_.GetWeakPtr(), |
| 91 PageQuality::FAIR_AND_IMPROVING), | |
| 92 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); | |
| 93 } | |
| 85 } | 94 } |
| 86 | 95 |
| 87 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | 96 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { |
| 88 // Post a delayed task to snapshot and then stop this controller. | 97 // Post a delayed task to snapshot and then stop this controller. |
| 89 task_runner_->PostDelayedTask( | 98 task_runner_->PostDelayedTask( |
| 90 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, | 99 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, |
| 91 weak_ptr_factory_.GetWeakPtr()), | 100 weak_ptr_factory_.GetWeakPtr()), |
| 92 base::TimeDelta::FromMilliseconds( | 101 base::TimeDelta::FromMilliseconds( |
| 93 delay_after_document_on_load_completed_ms_)); | 102 delay_after_document_on_load_completed_ms_)); |
| 94 } | 103 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 109 | 118 |
| 110 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | 119 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { |
| 111 return delay_after_document_available_ms_; | 120 return delay_after_document_available_ms_; |
| 112 } | 121 } |
| 113 | 122 |
| 114 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { | 123 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { |
| 115 return delay_after_document_on_load_completed_ms_; | 124 return delay_after_document_on_load_completed_ms_; |
| 116 } | 125 } |
| 117 | 126 |
| 118 } // namespace offline_pages | 127 } // namespace offline_pages |
| OLD | NEW |