| 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/snapshot_controller.h" | 5 #include "components/offline_pages/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 | 11 |
| 12 namespace { | 12 namespace { |
| 13 // Delay, in milliseconds, between the main document parsed event and snapshot. | 13 // Delay, in milliseconds, between the main document parsed event and snapshot. |
| 14 // Note if the "load" event fires before this delay is up, then the snapshot | 14 // Note if the "load" event fires before this delay is up, then the snapshot |
| 15 // is taken immediately. | 15 // is taken immediately. |
| 16 const size_t kDelayAfterDocumentAvailable = 7000; | 16 const size_t kDelayAfterDocumentAvailable = 7000; |
| 17 | 17 |
| 18 } // namespace | 18 } // namespace |
| 19 | 19 |
| 20 namespace offline_pages { | 20 namespace offline_pages { |
| 21 | 21 |
| 22 SnapshotController::SnapshotController( | 22 SnapshotController::SnapshotController( |
| 23 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 23 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 24 SnapshotController::Client* client) | 24 SnapshotController::Client* client) |
| 25 : task_runner_(task_runner), | 25 : task_runner_(task_runner), |
| 26 client_(client), | 26 client_(client), |
| 27 state_(State::kReady), | 27 state_(State::READY), |
| 28 weak_ptr_factory_(this) { | 28 weak_ptr_factory_(this) { |
| 29 } | 29 } |
| 30 | 30 |
| 31 SnapshotController::~SnapshotController() {} | 31 SnapshotController::~SnapshotController() {} |
| 32 | 32 |
| 33 void SnapshotController::Reset() { | 33 void SnapshotController::Reset() { |
| 34 // Cancel potentially delayed tasks that relate to the previous 'session'. | 34 // Cancel potentially delayed tasks that relate to the previous 'session'. |
| 35 weak_ptr_factory_.InvalidateWeakPtrs(); | 35 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 36 state_ = State::kReady; | 36 state_ = State::READY; |
| 37 } | 37 } |
| 38 | 38 |
| 39 void SnapshotController::Stop() { | 39 void SnapshotController::Stop() { |
| 40 state_ = State::kStopped; | 40 state_ = State::STOPPED; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SnapshotController::PendingSnapshotCompleted() { | 43 void SnapshotController::PendingSnapshotCompleted() { |
| 44 // Unless the controller is "stopped", enable the subsequent snapshots. | 44 // Unless the controller is "stopped", enable the subsequent snapshots. |
| 45 // Stopped state prevents any further snapshots form being started. | 45 // Stopped state prevents any further snapshots form being started. |
| 46 if (state_ == State::kStopped) | 46 if (state_ == State::STOPPED) |
| 47 return; | 47 return; |
| 48 state_ = State::kReady; | 48 state_ = State::READY; |
| 49 } | 49 } |
| 50 | 50 |
| 51 void SnapshotController::DocumentAvailableInMainFrame() { | 51 void SnapshotController::DocumentAvailableInMainFrame() { |
| 52 // Post a delayed task. The snapshot will happen either when the delay | 52 // Post a delayed task. The snapshot will happen either when the delay |
| 53 // is up, or if the "load" event is dispatched in the main frame. | 53 // is up, or if the "load" event is dispatched in the main frame. |
| 54 task_runner_->PostDelayedTask( | 54 task_runner_->PostDelayedTask( |
| 55 FROM_HERE, | 55 FROM_HERE, |
| 56 base::Bind(&SnapshotController::MaybeStartSnapshot, | 56 base::Bind(&SnapshotController::MaybeStartSnapshot, |
| 57 weak_ptr_factory_.GetWeakPtr()), | 57 weak_ptr_factory_.GetWeakPtr()), |
| 58 base::TimeDelta::FromMilliseconds(kDelayAfterDocumentAvailable)); | 58 base::TimeDelta::FromMilliseconds(kDelayAfterDocumentAvailable)); |
| 59 } | 59 } |
| 60 | 60 |
| 61 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | 61 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { |
| 62 MaybeStartSnapshot(); | 62 MaybeStartSnapshot(); |
| 63 // No more snapshots after onLoad (there still can be other events | 63 // No more snapshots after onLoad (there still can be other events |
| 64 // or delayed tasks that can try to start another snapshot) | 64 // or delayed tasks that can try to start another snapshot) |
| 65 Stop(); | 65 Stop(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void SnapshotController::MaybeStartSnapshot() { | 68 void SnapshotController::MaybeStartSnapshot() { |
| 69 if (state_ != State::kReady) | 69 if (state_ != State::READY) |
| 70 return; | 70 return; |
| 71 state_ = State::kSnapshotPending; | 71 state_ = State::SNAPSHOT_PENDING; |
| 72 client_->StartSnapshot(); | 72 client_->StartSnapshot(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | 75 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { |
| 76 return kDelayAfterDocumentAvailable; | 76 return kDelayAfterDocumentAvailable; |
| 77 } | 77 } |
| 78 | 78 |
| 79 | 79 |
| 80 } // namespace offline_pages | 80 } // namespace offline_pages |
| OLD | NEW |