| 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 kDocumentAvailableTriggersSnapshot = true; |
| 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 kDelayAfterDocumentOnLoadCompletedMsForeground = 1000; |
| 24 const int64_t kDelayAfterDocumentOnLoadCompletedMsBackground = 2000; |
| 22 | 25 |
| 23 // Delay for testing to keep polling times reasonable. | 26 // Delay for testing to keep polling times reasonable. |
| 24 const int64_t kDelayForTests = 0; | 27 const int64_t kDelayForTests = 0; |
| 25 | 28 |
| 26 } // namespace | 29 } // namespace |
| 27 | 30 |
| 28 namespace offline_pages { | 31 namespace offline_pages { |
| 29 | 32 |
| 30 SnapshotController::SnapshotController( | 33 // static |
| 34 std::unique_ptr<SnapshotController> |
| 35 SnapshotController::CreateForForegroundOfflining( |
| 31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 36 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 32 SnapshotController::Client* client) | 37 SnapshotController::Client* client) { |
| 33 : SnapshotController(task_runner, | 38 return std::unique_ptr<SnapshotController>(new SnapshotController( |
| 34 client, | 39 task_runner, client, kDefaultDelayAfterDocumentAvailableMs, |
| 35 kDefaultDelayAfterDocumentAvailableMs, | 40 kDelayAfterDocumentOnLoadCompletedMsForeground, |
| 36 kDelayAfterDocumentOnLoadCompletedMs) {} | 41 kDocumentAvailableTriggersSnapshot)); |
| 42 } |
| 43 |
| 44 // static |
| 45 std::unique_ptr<SnapshotController> |
| 46 SnapshotController::CreateForBackgroundOfflining( |
| 47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 48 SnapshotController::Client* client) { |
| 49 return std::unique_ptr<SnapshotController>(new SnapshotController( |
| 50 task_runner, client, kDefaultDelayAfterDocumentAvailableMs, |
| 51 kDelayAfterDocumentOnLoadCompletedMsBackground, |
| 52 !kDocumentAvailableTriggersSnapshot)); |
| 53 } |
| 37 | 54 |
| 38 SnapshotController::SnapshotController( | 55 SnapshotController::SnapshotController( |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 56 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 40 SnapshotController::Client* client, | 57 SnapshotController::Client* client, |
| 41 int64_t delay_after_document_available_ms, | 58 int64_t delay_after_document_available_ms, |
| 42 int64_t delay_after_document_on_load_completed_ms) | 59 int64_t delay_after_document_on_load_completed_ms, |
| 60 bool document_available_triggers_snapshot) |
| 43 : task_runner_(task_runner), | 61 : task_runner_(task_runner), |
| 44 client_(client), | 62 client_(client), |
| 45 state_(State::READY), | 63 state_(State::READY), |
| 46 delay_after_document_available_ms_(delay_after_document_available_ms), | 64 delay_after_document_available_ms_(delay_after_document_available_ms), |
| 47 delay_after_document_on_load_completed_ms_( | 65 delay_after_document_on_load_completed_ms_( |
| 48 delay_after_document_on_load_completed_ms), | 66 delay_after_document_on_load_completed_ms), |
| 67 document_available_triggers_snapshot_( |
| 68 document_available_triggers_snapshot), |
| 49 weak_ptr_factory_(this) { | 69 weak_ptr_factory_(this) { |
| 50 if (offline_pages::ShouldUseTestingSnapshotDelay()) { | 70 if (offline_pages::ShouldUseTestingSnapshotDelay()) { |
| 51 delay_after_document_available_ms_ = kDelayForTests; | 71 delay_after_document_available_ms_ = kDelayForTests; |
| 52 delay_after_document_on_load_completed_ms_ = kDelayForTests; | 72 delay_after_document_on_load_completed_ms_ = kDelayForTests; |
| 53 } | 73 } |
| 54 } | 74 } |
| 55 | 75 |
| 56 SnapshotController::~SnapshotController() {} | 76 SnapshotController::~SnapshotController() {} |
| 57 | 77 |
| 58 void SnapshotController::Reset() { | 78 void SnapshotController::Reset() { |
| 59 // Cancel potentially delayed tasks that relate to the previous 'session'. | 79 // Cancel potentially delayed tasks that relate to the previous 'session'. |
| 60 weak_ptr_factory_.InvalidateWeakPtrs(); | 80 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 61 state_ = State::READY; | 81 state_ = State::READY; |
| 62 current_page_quality_ = PageQuality::POOR; | 82 current_page_quality_ = PageQuality::POOR; |
| 63 } | 83 } |
| 64 | 84 |
| 65 void SnapshotController::Stop() { | 85 void SnapshotController::Stop() { |
| 66 state_ = State::STOPPED; | 86 state_ = State::STOPPED; |
| 67 } | 87 } |
| 68 | 88 |
| 69 void SnapshotController::PendingSnapshotCompleted() { | 89 void SnapshotController::PendingSnapshotCompleted() { |
| 70 // Unless the controller is "stopped", enable the subsequent snapshots. | 90 // Unless the controller is "stopped", enable the subsequent snapshots. |
| 71 // Stopped state prevents any further snapshots form being started. | 91 // Stopped state prevents any further snapshots form being started. |
| 72 if (state_ == State::STOPPED) | 92 if (state_ == State::STOPPED) |
| 73 return; | 93 return; |
| 74 state_ = State::READY; | 94 state_ = State::READY; |
| 75 } | 95 } |
| 76 | 96 |
| 77 void SnapshotController::DocumentAvailableInMainFrame() { | 97 void SnapshotController::DocumentAvailableInMainFrame() { |
| 78 DCHECK_EQ(PageQuality::POOR, current_page_quality_); | 98 if (document_available_triggers_snapshot_) { |
| 79 // Post a delayed task to snapshot. | 99 DCHECK_EQ(PageQuality::POOR, current_page_quality_); |
| 80 task_runner_->PostDelayedTask( | 100 // Post a delayed task to snapshot. |
| 81 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshot, | 101 task_runner_->PostDelayedTask( |
| 82 weak_ptr_factory_.GetWeakPtr(), | 102 FROM_HERE, |
| 83 PageQuality::FAIR_AND_IMPROVING), | 103 base::Bind(&SnapshotController::MaybeStartSnapshot, |
| 84 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); | 104 weak_ptr_factory_.GetWeakPtr(), |
| 105 PageQuality::FAIR_AND_IMPROVING), |
| 106 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); |
| 107 } |
| 85 } | 108 } |
| 86 | 109 |
| 87 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | 110 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { |
| 88 // Post a delayed task to snapshot and then stop this controller. | 111 // Post a delayed task to snapshot and then stop this controller. |
| 89 task_runner_->PostDelayedTask( | 112 task_runner_->PostDelayedTask( |
| 90 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, | 113 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, |
| 91 weak_ptr_factory_.GetWeakPtr()), | 114 weak_ptr_factory_.GetWeakPtr()), |
| 92 base::TimeDelta::FromMilliseconds( | 115 base::TimeDelta::FromMilliseconds( |
| 93 delay_after_document_on_load_completed_ms_)); | 116 delay_after_document_on_load_completed_ms_)); |
| 94 } | 117 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 | 132 |
| 110 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | 133 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { |
| 111 return delay_after_document_available_ms_; | 134 return delay_after_document_available_ms_; |
| 112 } | 135 } |
| 113 | 136 |
| 114 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { | 137 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { |
| 115 return delay_after_document_on_load_completed_ms_; | 138 return delay_after_document_on_load_completed_ms_; |
| 116 } | 139 } |
| 117 | 140 |
| 118 } // namespace offline_pages | 141 } // namespace offline_pages |
| OLD | NEW |