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 | 12 |
| 12 namespace { | 13 namespace { |
| 13 // Default delay, in milliseconds, between the main document parsed event and | 14 // Default delay, in milliseconds, between the main document parsed event and |
| 14 // snapshot. Note: this snapshot might not occur if the OnLoad event and | 15 // snapshot. Note: this snapshot might not occur if the OnLoad event and |
| 15 // OnLoad delay elapses first to trigger a final snapshot. | 16 // OnLoad delay elapses first to trigger a final snapshot. |
| 16 const size_t kDefaultDelayAfterDocumentAvailableMs = 7000; | 17 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; |
| 17 | 18 |
| 18 // Default delay, in milliseconds, between the main document OnLoad event and | 19 // Default delay, in milliseconds, between the main document OnLoad event and |
| 19 // snapshot. | 20 // snapshot. |
| 20 const size_t kDelayAfterDocumentOnLoadCompletedMs = 1000; | 21 const int64_t kDelayAfterDocumentOnLoadCompletedMs = 1000; |
| 22 | |
| 23 // Delay for testing to keep polling times reasonable. | |
| 24 const int64_t kDelayForTests = 10; | |
| 21 | 25 |
| 22 } // namespace | 26 } // namespace |
| 23 | 27 |
| 24 namespace offline_pages { | 28 namespace offline_pages { |
| 25 | 29 |
| 26 SnapshotController::SnapshotController( | 30 SnapshotController::SnapshotController( |
| 27 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 28 SnapshotController::Client* client) | |
| 29 : task_runner_(task_runner), | |
| 30 client_(client), | |
| 31 state_(State::READY), | |
| 32 delay_after_document_available_ms_(kDefaultDelayAfterDocumentAvailableMs), | |
| 33 delay_after_document_on_load_completed_ms_( | |
| 34 kDelayAfterDocumentOnLoadCompletedMs), | |
| 35 weak_ptr_factory_(this) {} | |
| 36 | |
| 37 SnapshotController::SnapshotController( | |
| 38 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 39 SnapshotController::Client* client, | 32 SnapshotController::Client* client, |
| 40 size_t delay_after_document_available_ms, | 33 int64_t delay_after_document_available_ms, |
| 41 size_t delay_after_document_on_load_completed_ms) | 34 int64_t delay_after_document_on_load_completed_ms) |
| 42 : task_runner_(task_runner), | 35 : task_runner_(task_runner), |
| 43 client_(client), | 36 client_(client), |
| 44 state_(State::READY), | 37 state_(State::READY), |
| 45 delay_after_document_available_ms_(delay_after_document_available_ms), | 38 delay_after_document_available_ms_(delay_after_document_available_ms), |
| 46 delay_after_document_on_load_completed_ms_( | 39 delay_after_document_on_load_completed_ms_( |
| 47 delay_after_document_on_load_completed_ms), | 40 delay_after_document_on_load_completed_ms), |
| 48 weak_ptr_factory_(this) {} | 41 weak_ptr_factory_(this) { |
| 42 if (offline_pages::ShouldUseTestingSnapshotDelay()) { | |
|
Dmitry Titov
2017/01/27 03:10:25
Instead of adding a flag modifying behavior for te
dewittj
2017/01/30 17:32:37
That sounds like a lot of plumbing for this test.
| |
| 43 delay_after_document_available_ms_ = kDelayForTests; | |
| 44 delay_after_document_on_load_completed_ms_ = kDelayForTests; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 SnapshotController::SnapshotController( | |
| 49 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 50 SnapshotController::Client* client) | |
| 51 : SnapshotController(task_runner, | |
| 52 client, | |
| 53 kDefaultDelayAfterDocumentAvailableMs, | |
| 54 kDelayAfterDocumentOnLoadCompletedMs) {} | |
| 49 | 55 |
| 50 SnapshotController::~SnapshotController() {} | 56 SnapshotController::~SnapshotController() {} |
| 51 | 57 |
| 52 void SnapshotController::Reset() { | 58 void SnapshotController::Reset() { |
| 53 // Cancel potentially delayed tasks that relate to the previous 'session'. | 59 // Cancel potentially delayed tasks that relate to the previous 'session'. |
| 54 weak_ptr_factory_.InvalidateWeakPtrs(); | 60 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 55 state_ = State::READY; | 61 state_ = State::READY; |
| 56 current_page_quality_ = PageQuality::POOR; | 62 current_page_quality_ = PageQuality::POOR; |
| 57 } | 63 } |
| 58 | 64 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 94 current_page_quality_ = updated_page_quality; | 100 current_page_quality_ = updated_page_quality; |
| 95 state_ = State::SNAPSHOT_PENDING; | 101 state_ = State::SNAPSHOT_PENDING; |
| 96 client_->StartSnapshot(); | 102 client_->StartSnapshot(); |
| 97 } | 103 } |
| 98 | 104 |
| 99 void SnapshotController::MaybeStartSnapshotThenStop() { | 105 void SnapshotController::MaybeStartSnapshotThenStop() { |
| 100 MaybeStartSnapshot(PageQuality::HIGH); | 106 MaybeStartSnapshot(PageQuality::HIGH); |
| 101 Stop(); | 107 Stop(); |
| 102 } | 108 } |
| 103 | 109 |
| 104 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | 110 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { |
| 105 return delay_after_document_available_ms_; | 111 return delay_after_document_available_ms_; |
| 106 } | 112 } |
| 107 | 113 |
| 108 size_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { | 114 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { |
| 109 return delay_after_document_on_load_completed_ms_; | 115 return delay_after_document_on_load_completed_ms_; |
| 110 } | 116 } |
| 111 | 117 |
| 112 } // namespace offline_pages | 118 } // namespace offline_pages |
| OLD | NEW |