| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/offline_pages/snapshot_controller.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/time/time.h" | |
| 11 | |
| 12 namespace { | |
| 13 // 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 // OnLoad delay elapses first to trigger a final snapshot. | |
| 16 const size_t kDefaultDelayAfterDocumentAvailableMs = 7000; | |
| 17 | |
| 18 // Default delay, in milliseconds, between the main document OnLoad event and | |
| 19 // snapshot. | |
| 20 const size_t kDelayAfterDocumentOnLoadCompletedMs = 1000; | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 namespace offline_pages { | |
| 25 | |
| 26 SnapshotController::SnapshotController( | |
| 27 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_( | |
| 33 kDefaultDelayAfterDocumentAvailableMs), | |
| 34 delay_after_document_on_load_completed_ms_( | |
| 35 kDelayAfterDocumentOnLoadCompletedMs), | |
| 36 weak_ptr_factory_(this) {} | |
| 37 | |
| 38 SnapshotController::SnapshotController( | |
| 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 40 SnapshotController::Client* client, | |
| 41 size_t delay_after_document_available_ms, | |
| 42 size_t delay_after_document_on_load_completed_ms) | |
| 43 : task_runner_(task_runner), | |
| 44 client_(client), | |
| 45 state_(State::READY), | |
| 46 delay_after_document_available_ms_( | |
| 47 delay_after_document_available_ms), | |
| 48 delay_after_document_on_load_completed_ms_( | |
| 49 delay_after_document_on_load_completed_ms), | |
| 50 weak_ptr_factory_(this) {} | |
| 51 | |
| 52 SnapshotController::~SnapshotController() {} | |
| 53 | |
| 54 void SnapshotController::Reset() { | |
| 55 // Cancel potentially delayed tasks that relate to the previous 'session'. | |
| 56 weak_ptr_factory_.InvalidateWeakPtrs(); | |
| 57 state_ = State::READY; | |
| 58 } | |
| 59 | |
| 60 void SnapshotController::Stop() { | |
| 61 state_ = State::STOPPED; | |
| 62 } | |
| 63 | |
| 64 void SnapshotController::PendingSnapshotCompleted() { | |
| 65 // Unless the controller is "stopped", enable the subsequent snapshots. | |
| 66 // Stopped state prevents any further snapshots form being started. | |
| 67 if (state_ == State::STOPPED) | |
| 68 return; | |
| 69 state_ = State::READY; | |
| 70 } | |
| 71 | |
| 72 void SnapshotController::DocumentAvailableInMainFrame() { | |
| 73 // Post a delayed task to snapshot. | |
| 74 task_runner_->PostDelayedTask( | |
| 75 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshot, | |
| 76 weak_ptr_factory_.GetWeakPtr()), | |
| 77 base::TimeDelta::FromMilliseconds( | |
| 78 delay_after_document_available_ms_)); | |
| 79 } | |
| 80 | |
| 81 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | |
| 82 // Post a delayed task to snapshot and then stop this controller. | |
| 83 task_runner_->PostDelayedTask( | |
| 84 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop, | |
| 85 weak_ptr_factory_.GetWeakPtr()), | |
| 86 base::TimeDelta::FromMilliseconds( | |
| 87 delay_after_document_on_load_completed_ms_)); | |
| 88 } | |
| 89 | |
| 90 void SnapshotController::MaybeStartSnapshot() { | |
| 91 if (state_ != State::READY) | |
| 92 return; | |
| 93 state_ = State::SNAPSHOT_PENDING; | |
| 94 client_->StartSnapshot(); | |
| 95 } | |
| 96 | |
| 97 void SnapshotController::MaybeStartSnapshotThenStop() { | |
| 98 MaybeStartSnapshot(); | |
| 99 Stop(); | |
| 100 } | |
| 101 | |
| 102 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | |
| 103 return delay_after_document_available_ms_; | |
| 104 } | |
| 105 | |
| 106 size_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { | |
| 107 return delay_after_document_on_load_completed_ms_; | |
| 108 } | |
| 109 | |
| 110 | |
| 111 } // namespace offline_pages | |
| OLD | NEW |