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

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

Issue 1936613002: Implementing recent pages snapshot capture. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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/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::kReady),
28 session_counter_(0),
28 weak_ptr_factory_(this) { 29 weak_ptr_factory_(this) {
29 } 30 }
30 31
31 SnapshotController::~SnapshotController() {} 32 SnapshotController::~SnapshotController() {}
32 33
33 void SnapshotController::Reset() { 34 void SnapshotController::Reset() {
35 session_counter_++;
34 state_ = State::kReady; 36 state_ = State::kReady;
35 } 37 }
36 38
37 void SnapshotController::Stop() { 39 void SnapshotController::Stop() {
38 state_ = State::kStopped; 40 state_ = State::kStopped;
39 } 41 }
40 42
41 void SnapshotController::PendingSnapshotCompleted() { 43 void SnapshotController::PendingSnapshotCompleted() {
42 // Unless the controller is "stopped", enable the subsequent snapshots. 44 // Unless the controller is "stopped", enable the subsequent snapshots.
43 // Stopped state prevents any further snapshots form being started. 45 // Stopped state prevents any further snapshots form being started.
44 if (state_ == State::kStopped) 46 if (state_ == State::kStopped)
45 return; 47 return;
46 DCHECK(state_ == State::kSnapshotPending); 48 DCHECK(state_ == State::kSnapshotPending);
47 state_ = State::kReady; 49 state_ = State::kReady;
48 } 50 }
49 51
50 void SnapshotController::DocumentAvailableInMainFrame() { 52 void SnapshotController::DocumentAvailableInMainFrame() {
51 // Post a delayed task. The snapshot will happen either when the delay 53 // 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. 54 // is up, or if the "load" event is dispatched in the main frame.
53 task_runner_->PostDelayedTask( 55 task_runner_->PostDelayedTask(
54 FROM_HERE, 56 FROM_HERE,
55 base::Bind(&SnapshotController::MaybeStartSnapshot, 57 base::Bind(&SnapshotController::MaybeStartSnapshot,
56 weak_ptr_factory_.GetWeakPtr()), 58 weak_ptr_factory_.GetWeakPtr(),
59 session_counter_),
57 base::TimeDelta::FromMilliseconds(kDelayAfterDocumentAvailable)); 60 base::TimeDelta::FromMilliseconds(kDelayAfterDocumentAvailable));
58 } 61 }
59 62
60 void SnapshotController::DocumentOnLoadCompletedInMainFrame() { 63 void SnapshotController::DocumentOnLoadCompletedInMainFrame() {
61 MaybeStartSnapshot(); 64 MaybeStartSnapshot(session_counter_);
62 // No more snapshots after onLoad (there still can be other events 65 // No more snapshots after onLoad (there still can be other events
63 // or delayed tasks that can try to start another snapshot) 66 // or delayed tasks that can try to start another snapshot)
64 Stop(); 67 Stop();
65 } 68 }
66 69
67 void SnapshotController::MaybeStartSnapshot() { 70 void SnapshotController::MaybeStartSnapshot(size_t session_id) {
68 if (state_ != State::kReady) 71 if (state_ != State::kReady)
69 return; 72 return;
70 if (client_->StartSnapshot()) 73 // This task was posted for previous session, ignore it.
71 state_ = State::kSnapshotPending; 74 if (session_id != session_counter_)
75 return;
76 state_ = State::kSnapshotPending;
77 client_->StartSnapshot();
72 } 78 }
73 79
74 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() { 80 size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() {
75 return kDelayAfterDocumentAvailable; 81 return kDelayAfterDocumentAvailable;
76 } 82 }
77 83
78 84
79 } // namespace offline_pages 85 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698