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 #ifndef COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/single_thread_task_runner.h" |
| 11 |
| 12 namespace offline_pages { |
| 13 |
| 14 // Takes various signals and produces StartSnapshot calls following a specific |
| 15 // policy. Can request snapshots multiple times per 'session'. Session can be |
| 16 // ended and another one started by calling Reset(). |
| 17 // Main invariants: |
| 18 // - It never starts overlapping snapshots, Client reports when previous |
| 19 // snapshot is done. |
| 20 // - The currently worked on (pending) snapshot is always allowed to complete, |
| 21 // the new attempts to start a snapshot are ignored until it does. |
| 22 // - Some signals prevent more snapshots to be taken. |
| 23 // OnLoad is currently such signal. |
| 24 class SnapshotController { |
| 25 public: |
| 26 // kStateReady - listening to input, will start snapshot when needed. |
| 27 // kStateSnapshotPending - snapshot is in progress, don't start another. |
| 28 // kStateStopped - terminal state, no snapshots until reset. |
| 29 enum class State { kReady, kSnapshotPending, kStopped }; |
| 30 |
| 31 // Client of the SnapshotController. |
| 32 class Client { |
| 33 public: |
| 34 // Invoked at a good moment to start a snapshot. May be invoked multiple |
| 35 // times, but not in overlapping manner - waits until |
| 36 // PreviousSnapshotCompleted() before the next StatrSnapshot(). |
| 37 // Client should overwrite the result of previous snapshot with the new one, |
| 38 // it is assumed that later snapshots are better then previous. |
| 39 // Returns true if the snapshot actually started. |
| 40 virtual bool StartSnapshot() = 0; |
| 41 |
| 42 protected: |
| 43 virtual ~Client() {} |
| 44 }; |
| 45 |
| 46 SnapshotController( |
| 47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 48 SnapshotController::Client* client); |
| 49 virtual ~SnapshotController(); |
| 50 |
| 51 // Resets the 'session', returning controller to initial state. |
| 52 void Reset(); |
| 53 |
| 54 // Stops current session, no more Client::StartSnapshot calls will be |
| 55 // invoked from the SnapshotController until current session is Reset(). |
| 56 // Called by Client, for example when it encounters an error loading the page. |
| 57 void Stop(); |
| 58 |
| 59 // The way for Client to report that previously started snapshot is |
| 60 // now completed (so the next one can be started). |
| 61 void PendingSnapshotCompleted(); |
| 62 |
| 63 // Invoked from WebContentObserver::DocumentAvailableInMainFrame |
| 64 void DocumentAvailableInMainFrame(); |
| 65 |
| 66 // Invoked from WebContentObserver::DocumentOnLoadCompletedInMainFrame |
| 67 void DocumentOnLoadCompletedInMainFrame(); |
| 68 |
| 69 size_t GetDelayAfterDocumentAvailableForTest(); |
| 70 |
| 71 private: |
| 72 void MaybeStartSnapshot(); |
| 73 |
| 74 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 75 // Client owns this class. |
| 76 SnapshotController::Client* client_; |
| 77 SnapshotController::State state_; |
| 78 |
| 79 base::WeakPtrFactory<SnapshotController> weak_ptr_factory_; |
| 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(SnapshotController); |
| 82 }; |
| 83 |
| 84 } // namespace offline_pages |
| 85 |
| 86 #endif // COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
OLD | NEW |