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 kDocumentAvailableTriggersSnapshotForeground = true; | |
| 15 const bool kDocumentAvailableTriggersSnapshotBackground = false; | |
|
carlosk
2017/04/21 01:10:58
nit: it seems to me these constants are now unneed
fgorski
2017/04/21 16:05:10
Alternative (also nit):
kDocumentAvailableTriggers
chili
2017/04/24 20:34:14
Done.
| |
| 16 | |
| 14 // Default delay, in milliseconds, between the main document parsed event and | 17 // Default delay, in milliseconds, between the main document parsed event and |
| 15 // snapshot. Note: this snapshot might not occur if the OnLoad event and | 18 // snapshot. Note: this snapshot might not occur if the OnLoad event and |
| 16 // OnLoad delay elapses first to trigger a final snapshot. | 19 // OnLoad delay elapses first to trigger a final snapshot. |
| 17 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; | 20 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; |
|
fgorski
2017/04/21 16:05:10
Wasn't this set to 25 seconds for Background?
I se
chili
2017/04/24 20:34:14
Yes, but the DocumentAvailable signal is ignored a
fgorski
2017/04/24 20:56:54
Acknowledged.
Thanks for looking into it. I am gla
| |
| 18 | 21 |
| 19 // Default delay, in milliseconds, between the main document OnLoad event and | 22 // Default delay, in milliseconds, between the main document OnLoad event and |
| 20 // snapshot. | 23 // snapshot. |
| 21 const int64_t kDelayAfterDocumentOnLoadCompletedMs = 1000; | 24 const int64_t kDelayAfterDocumentOnLoadCompletedMsForeground = 1000; |
| 25 const int64_t kDelayAfterDocumentOnLoadCompletedMsBackground = 2000; | |
| 22 | 26 |
| 23 // Delay for testing to keep polling times reasonable. | 27 // Delay for testing to keep polling times reasonable. |
| 24 const int64_t kDelayForTests = 0; | 28 const int64_t kDelayForTests = 0; |
| 25 | 29 |
| 26 } // namespace | 30 } // namespace |
| 27 | 31 |
| 28 namespace offline_pages { | 32 namespace offline_pages { |
| 29 | 33 |
| 30 SnapshotController::SnapshotController( | 34 // static |
| 35 SnapshotController* 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 new SnapshotController(task_runner, client, |
| 34 client, | 39 kDefaultDelayAfterDocumentAvailableMs, |
| 35 kDefaultDelayAfterDocumentAvailableMs, | 40 kDelayAfterDocumentOnLoadCompletedMsForeground, |
| 36 kDelayAfterDocumentOnLoadCompletedMs) {} | 41 kDocumentAvailableTriggersSnapshotForeground); |
| 42 } | |
| 43 | |
| 44 // static | |
| 45 SnapshotController* SnapshotController::CreateForBackgroundOfflining( | |
| 46 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 47 SnapshotController::Client* client) { | |
| 48 return new SnapshotController(task_runner, client, | |
| 49 kDefaultDelayAfterDocumentAvailableMs, | |
| 50 kDelayAfterDocumentOnLoadCompletedMsBackground, | |
| 51 kDocumentAvailableTriggersSnapshotBackground); | |
| 52 } | |
| 37 | 53 |
| 38 SnapshotController::SnapshotController( | 54 SnapshotController::SnapshotController( |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 55 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 40 SnapshotController::Client* client, | 56 SnapshotController::Client* client, |
| 41 int64_t delay_after_document_available_ms, | 57 int64_t delay_after_document_available_ms, |
| 42 int64_t delay_after_document_on_load_completed_ms) | 58 int64_t delay_after_document_on_load_completed_ms, |
| 59 bool document_available_triggers_snapshot) | |
| 43 : task_runner_(task_runner), | 60 : task_runner_(task_runner), |
| 44 client_(client), | 61 client_(client), |
| 45 state_(State::READY), | 62 state_(State::READY), |
| 46 delay_after_document_available_ms_(delay_after_document_available_ms), | 63 delay_after_document_available_ms_(delay_after_document_available_ms), |
| 47 delay_after_document_on_load_completed_ms_( | 64 delay_after_document_on_load_completed_ms_( |
| 48 delay_after_document_on_load_completed_ms), | 65 delay_after_document_on_load_completed_ms), |
| 66 document_available_triggers_snapshot_( | |
| 67 document_available_triggers_snapshot), | |
| 49 weak_ptr_factory_(this) { | 68 weak_ptr_factory_(this) { |
| 50 if (offline_pages::ShouldUseTestingSnapshotDelay()) { | 69 if (offline_pages::ShouldUseTestingSnapshotDelay()) { |
| 51 delay_after_document_available_ms_ = kDelayForTests; | 70 delay_after_document_available_ms_ = kDelayForTests; |
| 52 delay_after_document_on_load_completed_ms_ = kDelayForTests; | 71 delay_after_document_on_load_completed_ms_ = kDelayForTests; |
| 53 } | 72 } |
| 54 } | 73 } |
| 55 | 74 |
| 56 SnapshotController::~SnapshotController() {} | 75 SnapshotController::~SnapshotController() {} |
| 57 | 76 |
| 58 void SnapshotController::Reset() { | 77 void SnapshotController::Reset() { |
| 59 // Cancel potentially delayed tasks that relate to the previous 'session'. | 78 // Cancel potentially delayed tasks that relate to the previous 'session'. |
| 60 weak_ptr_factory_.InvalidateWeakPtrs(); | 79 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 61 state_ = State::READY; | 80 state_ = State::READY; |
| 62 current_page_quality_ = PageQuality::POOR; | 81 current_page_quality_ = PageQuality::POOR; |
| 63 } | 82 } |
| 64 | 83 |
| 65 void SnapshotController::Stop() { | 84 void SnapshotController::Stop() { |
| 66 state_ = State::STOPPED; | 85 state_ = State::STOPPED; |
| 67 } | 86 } |
| 68 | 87 |
| 69 void SnapshotController::PendingSnapshotCompleted() { | 88 void SnapshotController::PendingSnapshotCompleted() { |
| 70 // Unless the controller is "stopped", enable the subsequent snapshots. | 89 // Unless the controller is "stopped", enable the subsequent snapshots. |
| 71 // Stopped state prevents any further snapshots form being started. | 90 // Stopped state prevents any further snapshots form being started. |
| 72 if (state_ == State::STOPPED) | 91 if (state_ == State::STOPPED) |
| 73 return; | 92 return; |
| 74 state_ = State::READY; | 93 state_ = State::READY; |
| 75 } | 94 } |
| 76 | 95 |
| 77 void SnapshotController::DocumentAvailableInMainFrame() { | 96 void SnapshotController::DocumentAvailableInMainFrame() { |
| 78 DCHECK_EQ(PageQuality::POOR, current_page_quality_); | 97 if (document_available_triggers_snapshot_) { |
| 79 // Post a delayed task to snapshot. | 98 DCHECK_EQ(PageQuality::POOR, current_page_quality_); |
| 80 task_runner_->PostDelayedTask( | 99 // Post a delayed task to snapshot. |
| 81 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshot, | 100 task_runner_->PostDelayedTask( |
| 82 weak_ptr_factory_.GetWeakPtr(), | 101 FROM_HERE, |
| 83 PageQuality::FAIR_AND_IMPROVING), | 102 base::Bind(&SnapshotController::MaybeStartSnapshot, |
| 84 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); | 103 weak_ptr_factory_.GetWeakPtr(), |
| 104 PageQuality::FAIR_AND_IMPROVING), | |
| 105 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); | |
| 106 } | |
| 85 } | 107 } |
| 86 | 108 |
| 87 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | 109 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { |
| 88 // Post a delayed task to snapshot and then stop this controller. | 110 // Post a delayed task to snapshot and then stop this controller. |
| 89 task_runner_->PostDelayedTask( | 111 task_runner_->PostDelayedTask( |
| 90 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, | 112 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, |
| 91 weak_ptr_factory_.GetWeakPtr()), | 113 weak_ptr_factory_.GetWeakPtr()), |
| 92 base::TimeDelta::FromMilliseconds( | 114 base::TimeDelta::FromMilliseconds( |
| 93 delay_after_document_on_load_completed_ms_)); | 115 delay_after_document_on_load_completed_ms_)); |
| 94 } | 116 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 109 | 131 |
| 110 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | 132 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { |
| 111 return delay_after_document_available_ms_; | 133 return delay_after_document_available_ms_; |
| 112 } | 134 } |
| 113 | 135 |
| 114 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { | 136 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { |
| 115 return delay_after_document_on_load_completed_ms_; | 137 return delay_after_document_on_load_completed_ms_; |
| 116 } | 138 } |
| 117 | 139 |
| 118 } // namespace offline_pages | 140 } // namespace offline_pages |
| OLD | NEW |