| 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 #ifndef COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ | 5 #ifndef COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
| 6 #define COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ | 6 #define COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| 11 | 11 |
| 12 namespace offline_pages { | 12 namespace offline_pages { |
| 13 | 13 |
| 14 // Takes various signals and produces StartSnapshot calls following a specific | 14 // Takes various signals and produces StartSnapshot calls following a specific |
| 15 // policy. Can request snapshots multiple times per 'session'. Session can be | 15 // policy. Can request snapshots multiple times per 'session'. Session can be |
| 16 // ended and another one started by calling Reset(). | 16 // ended and another one started by calling Reset(). |
| 17 // Main invariants: | 17 // Main invariants: |
| 18 // - It never starts overlapping snapshots, Client reports when previous | 18 // - It never starts overlapping snapshots, Client reports when previous |
| 19 // snapshot is done. | 19 // snapshot is done. |
| 20 // - The currently worked on (pending) snapshot is always allowed to complete, | 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. | 21 // the new attempts to start a snapshot are ignored until it does. |
| 22 // - Some signals prevent more snapshots to be taken. | 22 // - Some signals prevent more snapshots to be taken. |
| 23 // OnLoad is currently such signal. | 23 // OnLoad is currently such signal. |
| 24 // - Once Reset() is called on the SnapshotController, the delayed tasks are |
| 25 // reset so no StartSnapshot calls is made 'cross-session'. |
| 24 class SnapshotController { | 26 class SnapshotController { |
| 25 public: | 27 public: |
| 26 // kStateReady - listening to input, will start snapshot when needed. | 28 // kStateReady - listening to input, will start snapshot when needed. |
| 27 // kStateSnapshotPending - snapshot is in progress, don't start another. | 29 // kStateSnapshotPending - snapshot is in progress, don't start another. |
| 28 // kStateStopped - terminal state, no snapshots until reset. | 30 // kStateStopped - terminal state, no snapshots until reset. |
| 29 enum class State { kReady, kSnapshotPending, kStopped }; | 31 enum class State { kReady, kSnapshotPending, kStopped }; |
| 30 | 32 |
| 31 // Client of the SnapshotController. | 33 // Client of the SnapshotController. |
| 32 class Client { | 34 class Client { |
| 33 public: | 35 public: |
| 34 // Invoked at a good moment to start a snapshot. May be invoked multiple | 36 // Invoked at a good moment to start a snapshot. May be invoked multiple |
| 35 // times, but not in overlapping manner - waits until | 37 // times, but not in overlapping manner - waits until |
| 36 // PreviousSnapshotCompleted() before the next StatrSnapshot(). | 38 // PreviousSnapshotCompleted() before the next StatrSnapshot(). |
| 37 // Client should overwrite the result of previous snapshot with the new one, | 39 // Client should overwrite the result of previous snapshot with the new one, |
| 38 // it is assumed that later snapshots are better then previous. | 40 // it is assumed that later snapshots are better then previous. |
| 39 // Returns true if the snapshot actually started. | 41 virtual void StartSnapshot() = 0; |
| 40 virtual bool StartSnapshot() = 0; | |
| 41 | 42 |
| 42 protected: | 43 protected: |
| 43 virtual ~Client() {} | 44 virtual ~Client() {} |
| 44 }; | 45 }; |
| 45 | 46 |
| 46 SnapshotController( | 47 SnapshotController( |
| 47 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 48 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 48 SnapshotController::Client* client); | 49 SnapshotController::Client* client); |
| 49 virtual ~SnapshotController(); | 50 virtual ~SnapshotController(); |
| 50 | 51 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 77 SnapshotController::State state_; | 78 SnapshotController::State state_; |
| 78 | 79 |
| 79 base::WeakPtrFactory<SnapshotController> weak_ptr_factory_; | 80 base::WeakPtrFactory<SnapshotController> weak_ptr_factory_; |
| 80 | 81 |
| 81 DISALLOW_COPY_AND_ASSIGN(SnapshotController); | 82 DISALLOW_COPY_AND_ASSIGN(SnapshotController); |
| 82 }; | 83 }; |
| 83 | 84 |
| 84 } // namespace offline_pages | 85 } // namespace offline_pages |
| 85 | 86 |
| 86 #endif // COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ | 87 #endif // COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ |
| OLD | NEW |