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/files/file_util.h" | |
|
fgorski
2016/04/25 04:28:45
do you use it here?
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/time/time.h" | |
| 12 | |
| 13 namespace { | |
| 14 // Delay, in milliseconds, between the main document parsed and snapshot. | |
|
fgorski
2016/04/25 04:28:45
nit: main document parsed event and
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 15 // Note if the "load" event fires before this delay is up, then the snapshot | |
| 16 // is taken immediately. | |
| 17 const size_t DELAY_AFTER_DOCUMENT_AVAILABLE = 7000; | |
| 18 | |
| 19 } // namespace | |
| 20 | |
| 21 namespace offline_pages { | |
| 22 | |
| 23 SnapshotController::SnapshotController( | |
| 24 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
| 25 SnapshotController::Client* client) | |
| 26 : task_runner_(task_runner), | |
| 27 client_(client), | |
| 28 state_(State::kStateReady), | |
|
fgorski
2016/04/25 04:28:45
I see a peculiarity here, since this is an enum cl
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 29 weak_ptr_factory_(this) { | |
| 30 } | |
| 31 | |
| 32 SnapshotController::~SnapshotController() {} | |
| 33 | |
| 34 void SnapshotController::Reset() { | |
| 35 state_ = State::kStateReady; | |
| 36 } | |
| 37 | |
| 38 void SnapshotController::Stop() { | |
| 39 state_ = State::kStateStopped; | |
| 40 } | |
| 41 | |
| 42 void SnapshotController::PreviousSnapshotCompleted() { | |
| 43 if (state_ != State::kStateStopped) | |
|
fgorski
2016/04/25 04:28:45
It feels a bit weird that you can have a snapshot
Dmitry Titov
2016/04/26 01:45:29
Stopped is the terminal state which prevents new s
| |
| 44 state_ = State::kStateReady; | |
| 45 } | |
| 46 | |
| 47 void SnapshotController::DocumentAvailableInMainFrame() { | |
| 48 // Post a delayed task. The snapshot will happen either when the delay | |
| 49 // is up, or if the "load" event is dispatched in the main frame. | |
| 50 task_runner_->PostDelayedTask( | |
| 51 FROM_HERE, | |
| 52 base::Bind(&SnapshotController::MaybeStartSnapshot, | |
| 53 weak_ptr_factory_.GetWeakPtr()), | |
| 54 base::TimeDelta::FromMilliseconds(DELAY_AFTER_DOCUMENT_AVAILABLE)); | |
| 55 } | |
| 56 | |
| 57 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { | |
| 58 MaybeStartSnapshot(); | |
| 59 // No more snapshots after onLoad (there still can be other events | |
| 60 // or delayed tasks that can try to start another snapshot) | |
| 61 Stop(); | |
| 62 } | |
| 63 | |
| 64 void SnapshotController::MaybeStartSnapshot() { | |
| 65 if (state_ != State::kStateReady) | |
| 66 return; | |
| 67 if (client_->StartSnapshot()) | |
| 68 state_ = State::kStateSnapshotPending; | |
| 69 } | |
| 70 | |
| 71 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { | |
| 72 return DELAY_AFTER_DOCUMENT_AVAILABLE; | |
| 73 } | |
| 74 | |
| 75 | |
| 76 } // namespace offline_pages | |
| OLD | NEW |