Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: components/offline_pages/core/snapshot_controller.cc

Issue 2797013002: [Offline pages] Update background loader to override the WebContentsObserver methods expected (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/core/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 #include "components/offline_pages/core/offline_page_feature.h" 11 #include "components/offline_pages/core/offline_page_feature.h"
12 12
13 namespace { 13 namespace {
14 // Default delay, in milliseconds, between the main document parsed event and 14 // Default delay, in milliseconds, between the main document parsed event and
15 // snapshot. Note: this snapshot might not occur if the OnLoad event and 15 // snapshot. Note: this snapshot might not occur if the OnLoad event and
16 // OnLoad delay elapses first to trigger a final snapshot. 16 // OnLoad delay elapses first to trigger a final snapshot.
17 const int64_t kDefaultDelayAfterDocumentAvailableMs = 7000; 17 const int64_t kDefaultDelayAfterDocumentLoadedMs = 7000;
18 18
19 // Default delay, in milliseconds, between the main document OnLoad event and 19 // Default delay, in milliseconds, between the main document OnLoad event and
20 // snapshot. 20 // snapshot.
21 const int64_t kDelayAfterDocumentOnLoadCompletedMs = 1000; 21 const int64_t kDelayAfterLoadCompletedMs = 1000;
Pete Williamson 2017/04/05 00:26:21 Shouldn't this be 2000? I was thinking all along
chili 2017/04/05 00:34:33 This is the default number. Both offliners pass i
22 22
23 // Delay for testing to keep polling times reasonable. 23 // Delay for testing to keep polling times reasonable.
24 const int64_t kDelayForTests = 0; 24 const int64_t kDelayForTests = 0;
25 25
26 } // namespace 26 } // namespace
27 27
28 namespace offline_pages { 28 namespace offline_pages {
29 29
30 SnapshotController::SnapshotController( 30 SnapshotController::SnapshotController(
31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 31 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
32 SnapshotController::Client* client) 32 SnapshotController::Client* client)
33 : SnapshotController(task_runner, 33 : SnapshotController(task_runner,
34 client, 34 client,
35 kDefaultDelayAfterDocumentAvailableMs, 35 kDefaultDelayAfterDocumentLoadedMs,
36 kDelayAfterDocumentOnLoadCompletedMs) {} 36 kDelayAfterLoadCompletedMs) {}
37 37
38 SnapshotController::SnapshotController( 38 SnapshotController::SnapshotController(
39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 39 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
40 SnapshotController::Client* client, 40 SnapshotController::Client* client,
41 int64_t delay_after_document_available_ms, 41 int64_t delay_after_document_loaded_ms,
42 int64_t delay_after_document_on_load_completed_ms) 42 int64_t delay_after_load_completed_ms)
43 : task_runner_(task_runner), 43 : task_runner_(task_runner),
44 client_(client), 44 client_(client),
45 state_(State::READY), 45 state_(State::READY),
46 delay_after_document_available_ms_(delay_after_document_available_ms), 46 delay_after_document_loaded_ms_(delay_after_document_loaded_ms),
47 delay_after_document_on_load_completed_ms_( 47 delay_after_load_completed_ms_(delay_after_load_completed_ms),
48 delay_after_document_on_load_completed_ms),
49 weak_ptr_factory_(this) { 48 weak_ptr_factory_(this) {
50 if (offline_pages::ShouldUseTestingSnapshotDelay()) { 49 if (offline_pages::ShouldUseTestingSnapshotDelay()) {
51 delay_after_document_available_ms_ = kDelayForTests; 50 delay_after_document_loaded_ms_ = kDelayForTests;
52 delay_after_document_on_load_completed_ms_ = kDelayForTests; 51 delay_after_load_completed_ms_ = kDelayForTests;
53 } 52 }
54 } 53 }
55 54
56 SnapshotController::~SnapshotController() {} 55 SnapshotController::~SnapshotController() {}
57 56
58 void SnapshotController::Reset() { 57 void SnapshotController::Reset() {
59 // Cancel potentially delayed tasks that relate to the previous 'session'. 58 // Cancel potentially delayed tasks that relate to the previous 'session'.
60 weak_ptr_factory_.InvalidateWeakPtrs(); 59 weak_ptr_factory_.InvalidateWeakPtrs();
61 state_ = State::READY; 60 state_ = State::READY;
62 current_page_quality_ = PageQuality::POOR; 61 current_page_quality_ = PageQuality::POOR;
63 } 62 }
64 63
65 void SnapshotController::Stop() { 64 void SnapshotController::Stop() {
66 state_ = State::STOPPED; 65 state_ = State::STOPPED;
67 } 66 }
68 67
69 void SnapshotController::PendingSnapshotCompleted() { 68 void SnapshotController::PendingSnapshotCompleted() {
70 // Unless the controller is "stopped", enable the subsequent snapshots. 69 // Unless the controller is "stopped", enable the subsequent snapshots.
71 // Stopped state prevents any further snapshots form being started. 70 // Stopped state prevents any further snapshots form being started.
72 if (state_ == State::STOPPED) 71 if (state_ == State::STOPPED)
73 return; 72 return;
74 state_ = State::READY; 73 state_ = State::READY;
75 } 74 }
76 75
77 void SnapshotController::DocumentAvailableInMainFrame() { 76 void SnapshotController::DocumentLoadedInMainFrame() {
78 DCHECK_EQ(PageQuality::POOR, current_page_quality_); 77 DCHECK_EQ(PageQuality::POOR, current_page_quality_);
79 // Post a delayed task to snapshot. 78 // Post a delayed task to snapshot.
80 task_runner_->PostDelayedTask( 79 task_runner_->PostDelayedTask(
81 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshot, 80 FROM_HERE,
82 weak_ptr_factory_.GetWeakPtr(), 81 base::Bind(&SnapshotController::MaybeStartSnapshot,
83 PageQuality::FAIR_AND_IMPROVING), 82 weak_ptr_factory_.GetWeakPtr(),
84 base::TimeDelta::FromMilliseconds(delay_after_document_available_ms_)); 83 PageQuality::FAIR_AND_IMPROVING),
84 base::TimeDelta::FromMilliseconds(delay_after_document_loaded_ms_));
85 }
86
87 void SnapshotController::DidStopLoading() {
88 // Post a delayed task to snapshot and then stop this controller.
89 task_runner_->PostDelayedTask(
90 FROM_HERE,
91 base::Bind(&SnapshotController::MaybeStartSnapshotThenStop,
92 weak_ptr_factory_.GetWeakPtr()),
93 base::TimeDelta::FromMilliseconds(delay_after_load_completed_ms_));
94 }
95
96 void SnapshotController::DocumentAvailableInMainFrame() {
97 // Maps to DocumentLoadedInMainFrame for now.
98 DocumentLoadedInMainFrame();
85 } 99 }
86 100
87 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { 101 void SnapshotController::DocumentOnLoadCompletedInMainFrame() {
88 // Post a delayed task to snapshot and then stop this controller. 102 // Maps to DidStopLoading for now.
89 task_runner_->PostDelayedTask( 103 DidStopLoading();
90 FROM_HERE, base::Bind(&SnapshotController::MaybeStartSnapshotThenStop,
91 weak_ptr_factory_.GetWeakPtr()),
92 base::TimeDelta::FromMilliseconds(
93 delay_after_document_on_load_completed_ms_));
94 } 104 }
95 105
96 void SnapshotController::MaybeStartSnapshot(PageQuality updated_page_quality) { 106 void SnapshotController::MaybeStartSnapshot(PageQuality updated_page_quality) {
97 if (state_ != State::READY) 107 if (state_ != State::READY)
98 return; 108 return;
99 DCHECK_LT(current_page_quality_, updated_page_quality); 109 DCHECK_LT(current_page_quality_, updated_page_quality);
100 current_page_quality_ = updated_page_quality; 110 current_page_quality_ = updated_page_quality;
101 state_ = State::SNAPSHOT_PENDING; 111 state_ = State::SNAPSHOT_PENDING;
102 client_->StartSnapshot(); 112 client_->StartSnapshot();
103 } 113 }
104 114
105 void SnapshotController::MaybeStartSnapshotThenStop() { 115 void SnapshotController::MaybeStartSnapshotThenStop() {
106 MaybeStartSnapshot(PageQuality::HIGH); 116 MaybeStartSnapshot(PageQuality::HIGH);
107 Stop(); 117 Stop();
108 } 118 }
109 119
110 int64_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { 120 int64_t SnapshotController::GetDelayAfterDocumentLoadedForTest() {
111 return delay_after_document_available_ms_; 121 return delay_after_document_loaded_ms_;
112 } 122 }
113 123
114 int64_t SnapshotController::GetDelayAfterDocumentOnLoadCompletedForTest() { 124 int64_t SnapshotController::GetDelayAfterLoadCompletedForTest() {
115 return delay_after_document_on_load_completed_ms_; 125 return delay_after_load_completed_ms_;
116 } 126 }
117 127
118 } // namespace offline_pages 128 } // namespace offline_pages
OLDNEW
« no previous file with comments | « components/offline_pages/core/snapshot_controller.h ('k') | components/offline_pages/core/snapshot_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698