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

Unified Diff: components/offline_pages/snapshot_controller.cc

Issue 1920603002: SnapshotController implementation. It will be used in WebContentsObservers for Offline Pages - to d… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: feedback addressed Created 4 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 side-by-side diff with in-line comments
Download patch
Index: components/offline_pages/snapshot_controller.cc
diff --git a/components/offline_pages/snapshot_controller.cc b/components/offline_pages/snapshot_controller.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ab5d1c970dc183e1c3e926c3bf00cb01b0b354bf
--- /dev/null
+++ b/components/offline_pages/snapshot_controller.cc
@@ -0,0 +1,79 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/offline_pages/snapshot_controller.h"
+
+#include "base/bind.h"
+#include "base/location.h"
+#include "base/logging.h"
+#include "base/time/time.h"
+
+namespace {
+// Delay, in milliseconds, between the main document parsed event and snapshot.
+// Note if the "load" event fires before this delay is up, then the snapshot
+// is taken immediately.
+const size_t DELAY_AFTER_DOCUMENT_AVAILABLE = 7000;
fgorski 2016/04/26 02:45:57 kDelayAfterDocumentAvailable -- I believe style g
+
+} // namespace
+
+namespace offline_pages {
+
+SnapshotController::SnapshotController(
+ const scoped_refptr<base::SingleThreadTaskRunner>& task_runner,
+ SnapshotController::Client* client)
+ : task_runner_(task_runner),
+ client_(client),
+ state_(State::kReady),
+ weak_ptr_factory_(this) {
+}
+
+SnapshotController::~SnapshotController() {}
+
+void SnapshotController::Reset() {
+ state_ = State::kReady;
+}
+
+void SnapshotController::Stop() {
+ state_ = State::kStopped;
+}
+
+void SnapshotController::PendingSnapshotCompleted() {
+ // Unless the controller is "stopped", enable the subsequent snapshots.
+ // Stopped state prevents any further snapshots form being started.
+ if (state_ == State::kStopped)
+ return;
+ DCHECK(state_ == State::kSnapshotPending);
+ state_ = State::kReady;
+}
+
+void SnapshotController::DocumentAvailableInMainFrame() {
+ // Post a delayed task. The snapshot will happen either when the delay
+ // is up, or if the "load" event is dispatched in the main frame.
+ task_runner_->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&SnapshotController::MaybeStartSnapshot,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::TimeDelta::FromMilliseconds(DELAY_AFTER_DOCUMENT_AVAILABLE));
+}
+
+void SnapshotController::DocumentOnLoadCompletedInMainFrame() {
+ MaybeStartSnapshot();
+ // No more snapshots after onLoad (there still can be other events
+ // or delayed tasks that can try to start another snapshot)
+ Stop();
+}
+
+void SnapshotController::MaybeStartSnapshot() {
+ if (state_ != State::kReady)
+ return;
+ if (client_->StartSnapshot())
+ state_ = State::kSnapshotPending;
+}
+
+size_t SnapshotController::GetDelayAfterDocumentAvailableForTest() {
+ return DELAY_AFTER_DOCUMENT_AVAILABLE;
+}
+
+
+} // namespace offline_pages
« no previous file with comments | « components/offline_pages/snapshot_controller.h ('k') | components/offline_pages/snapshot_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698