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