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 // - Once Reset() is called on the SnapshotController, the delayed tasks are | |
25 // reset so no StartSnapshot calls is made 'cross-session'. | |
26 class SnapshotController { | |
27 public: | |
28 enum class State { | |
29 READY, // Listening to input, will start snapshot when needed. | |
30 SNAPSHOT_PENDING, // Snapshot is in progress, don't start another. | |
31 STOPPED, // Terminal state, no snapshots until reset. | |
32 }; | |
33 | |
34 // Client of the SnapshotController. | |
35 class Client { | |
36 public: | |
37 // Invoked at a good moment to start a snapshot. May be invoked multiple | |
38 // times, but not in overlapping manner - waits until | |
39 // PreviousSnapshotCompleted() before the next StatrSnapshot(). | |
40 // Client should overwrite the result of previous snapshot with the new one, | |
41 // it is assumed that later snapshots are better then previous. | |
42 virtual void StartSnapshot() = 0; | |
43 | |
44 protected: | |
45 virtual ~Client() {} | |
46 }; | |
47 | |
48 SnapshotController( | |
49 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
50 SnapshotController::Client* client); | |
51 SnapshotController( | |
52 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
53 SnapshotController::Client* client, | |
54 size_t delay_after_document_available_ms, | |
55 size_t delay_after_document_on_load_completed_ms); | |
56 virtual ~SnapshotController(); | |
57 | |
58 // Resets the 'session', returning controller to initial state. | |
59 void Reset(); | |
60 | |
61 // Stops current session, no more Client::StartSnapshot calls will be | |
62 // invoked from the SnapshotController until current session is Reset(). | |
63 // Called by Client, for example when it encounters an error loading the page. | |
64 void Stop(); | |
65 | |
66 // The way for Client to report that previously started snapshot is | |
67 // now completed (so the next one can be started). | |
68 void PendingSnapshotCompleted(); | |
69 | |
70 // Invoked from WebContentObserver::DocumentAvailableInMainFrame | |
71 void DocumentAvailableInMainFrame(); | |
72 | |
73 // Invoked from WebContentObserver::DocumentOnLoadCompletedInMainFrame | |
74 void DocumentOnLoadCompletedInMainFrame(); | |
75 | |
76 size_t GetDelayAfterDocumentAvailableForTest(); | |
77 size_t GetDelayAfterDocumentOnLoadCompletedForTest(); | |
78 | |
79 private: | |
80 void MaybeStartSnapshot(); | |
81 void MaybeStartSnapshotThenStop(); | |
82 | |
83 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
84 // Client owns this class. | |
85 SnapshotController::Client* client_; | |
86 SnapshotController::State state_; | |
87 size_t delay_after_document_available_ms_; | |
88 size_t delay_after_document_on_load_completed_ms_; | |
89 | |
90 base::WeakPtrFactory<SnapshotController> weak_ptr_factory_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(SnapshotController); | |
93 }; | |
94 | |
95 } // namespace offline_pages | |
96 | |
97 #endif // COMPONENTS_OFFLINE_PAGES_SNAPSHOT_CONTROLLER_H_ | |
OLD | NEW |