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