Chromium Code Reviews| 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 // - If a snapshot is in process (pending), another snapshot attempt is | |
| 21 // abandoned w/o waiting for the completion of the current one. | |
|
fgorski
2016/04/25 04:28:45
The part staring with "w/o" is not clear.
I presum
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 22 // - Some signals cause 'last' snapshot - no signals after those will cause | |
|
fgorski
2016/04/25 04:28:45
some signals prevent more snapshots to be taken.
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 23 // another snapshot. 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 { kStateReady, kStateSnapshotPending, kStateStopped }; | |
| 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(). | |
|
fgorski
2016/04/25 04:28:46
Is this meant to be called by the client? (I think
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 56 void Stop(); | |
| 57 | |
| 58 // The way for Client to report that previously started snapshot is | |
| 59 // now completed (so the next one can be started). | |
| 60 void PreviousSnapshotCompleted(); | |
|
fgorski
2016/04/25 04:28:45
Since your state is kStateSnapshotPending I sugges
Dmitry Titov
2016/04/26 01:45:29
Done.
| |
| 61 | |
| 62 // Invoked from WebContentObserver::DocumentAvailableInMainFrame | |
| 63 void DocumentAvailableInMainFrame(); | |
| 64 | |
| 65 // Invoked from WebContentObserver::DocumentOnLoadCompletedInMainFrame | |
| 66 void DocumentOnLoadCompletedInMainFrame(); | |
| 67 | |
| 68 size_t GetDelayAfterDocumentAvailableForTest(); | |
| 69 | |
| 70 private: | |
| 71 void MaybeStartSnapshot(); | |
| 72 | |
| 73 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
| 74 // Client owns this class. | |
| 75 SnapshotController::Client* client_; | |
| 76 SnapshotController::State state_; | |
| 77 | |
| 78 base::WeakPtrFactory<SnapshotController> weak_ptr_factory_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(SnapshotController); | |
| 81 }; | |
| 82 | |
| 83 } // namespace offline_pages | |
| 84 | |
| 85 #endif // COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ | |
| OLD | NEW |