OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
6 #define CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 6 #define CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 21 matching lines...) Expand all Loading... | |
32 // backend to determine the name of the files. | 32 // backend to determine the name of the files. |
33 enum SessionType { | 33 enum SessionType { |
34 SESSION_RESTORE, | 34 SESSION_RESTORE, |
35 TAB_RESTORE | 35 TAB_RESTORE |
36 }; | 36 }; |
37 | 37 |
38 typedef base::Callback<void(ScopedVector<SessionCommand>)> | 38 typedef base::Callback<void(ScopedVector<SessionCommand>)> |
39 GetCommandsCallback; | 39 GetCommandsCallback; |
40 | 40 |
41 // Creates a new BaseSessionService. After creation you need to invoke | 41 // Creates a new BaseSessionService. After creation you need to invoke |
42 // Init. | 42 // Init. |delegate| will remain owned by the creator and it is guaranteed |
43 // that its lifetime surpasses this class. | |
43 // |type| gives the type of session service, |path| the path to save files to. | 44 // |type| gives the type of session service, |path| the path to save files to. |
44 BaseSessionService(SessionType type, | 45 BaseSessionService(SessionType type, |
45 const base::FilePath& path, | 46 const base::FilePath& path, |
46 scoped_ptr<BaseSessionServiceDelegate> delegate); | 47 BaseSessionServiceDelegate* delegate); |
48 virtual ~BaseSessionService(); | |
sky
2014/10/31 22:36:16
No one subclasses this anymore, right? Can this be
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Done.
| |
49 | |
50 // Moves the current session to the last session. | |
51 void MoveCurrentSessionToLastSession(); | |
47 | 52 |
48 // Deletes the last session. | 53 // Deletes the last session. |
49 void DeleteLastSession(); | 54 void DeleteLastSession(); |
50 | 55 |
51 typedef base::Callback<void(ScopedVector<SessionCommand>)> | |
52 InternalGetCommandsCallback; | |
53 | |
54 protected: | |
55 virtual ~BaseSessionService(); | |
56 | |
57 // Returns the backend. | |
58 SessionBackend* backend() const { return backend_.get(); } | |
59 | |
60 // Returns the set of commands which were scheduled to be written. Once | 56 // Returns the set of commands which were scheduled to be written. Once |
61 // committed to the backend, the commands are removed from here. | 57 // committed to the backend, the commands are removed from here. |
62 ScopedVector<SessionCommand>& pending_commands() { | 58 ScopedVector<SessionCommand>& pending_commands() { |
sky
2014/10/31 22:36:16
I don't like exposing pending_commands_ like this,
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
That is not enough. The class ReplacePendingComman
sky
2014/11/03 15:53:22
I don't like exposing the detail of the commands.
Mr4D (OOO till 08-26)
2014/11/03 22:05:50
More was needed - Done.
| |
63 return pending_commands_; | 59 return pending_commands_; |
64 } | 60 } |
65 | 61 |
66 // Whether the next save resets the file before writing to it. | 62 // Whether the next save resets the file before writing to it. |
67 void set_pending_reset(bool value) { pending_reset_ = value; } | 63 void set_pending_reset(bool value) { pending_reset_ = value; } |
68 bool pending_reset() const { return pending_reset_; } | 64 bool pending_reset() const { return pending_reset_; } |
69 | 65 |
70 // Returns the number of commands sent down since the last reset. | 66 // Returns the number of commands sent down since the last reset. |
71 int commands_since_reset() const { return commands_since_reset_; } | 67 int commands_since_reset() const { return commands_since_reset_; } |
72 | 68 |
73 // Schedules a command. This adds |command| to pending_commands_ and | 69 // Schedules a command. This adds |command| to pending_commands_ and |
74 // invokes StartSaveTimer to start a timer that invokes Save at a later | 70 // invokes StartSaveTimer to start a timer that invokes Save at a later |
75 // time. | 71 // time. |
76 virtual void ScheduleCommand(scoped_ptr<SessionCommand> command); | 72 virtual void ScheduleCommand(scoped_ptr<SessionCommand> command); |
77 | 73 |
78 // Starts the timer that invokes Save (if timer isn't already running). | 74 // Starts the timer that invokes Save (if timer isn't already running). |
79 void StartSaveTimer(); | 75 void StartSaveTimer(); |
80 | 76 |
81 // Saves pending commands to the backend. This is invoked from the timer | 77 // Passes all pending commands to the backend for saving. Does nothing if |
82 // scheduled by StartSaveTimer. | 78 // no backend exists |
83 virtual void Save(); | 79 virtual void SaveNow(); |
84 | 80 |
85 // Returns true if the entry at specified |url| should be written to disk. | 81 // Uses the backend to load the last session commands from disc. |callback| |
86 bool ShouldTrackEntry(const GURL& url); | 82 // gets called once the data has arrived. |
83 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | |
84 const GetCommandsCallback& callback, | |
85 base::CancelableTaskTracker* tracker); | |
87 | 86 |
88 // Invokes SessionBackend::ReadLastSessionCommands with callback on the | 87 // Returns true if any commands got processed yet (used by unit tests). |
89 // backend thread. | 88 bool ProcessedAnyCommandsForTest(); |
sky
2014/10/31 22:36:16
Don't make this public. Access it through SessionS
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Done.
| |
90 // If testing, SessionBackend::ReadLastSessionCommands is invoked directly. | 89 |
91 base::CancelableTaskTracker::TaskId ScheduleGetLastSessionCommands( | 90 // Max number of navigation entries in each direction we'll persist. |
92 const InternalGetCommandsCallback& callback, | 91 static const int max_persist_navigation_count; |
93 base::CancelableTaskTracker* tracker); | 92 |
93 private: | |
94 friend class BetterSessionRestoreCrashTest; | |
95 friend class SessionServiceTestHelper; | |
96 | |
97 // Saves pending commands to the backend immediately. This gets invoked by the | |
98 // timer scheduled by StartSaveTimer - or via SaveNow(). | |
99 void SaveInternal(); | |
100 | |
101 // Returns the backend. | |
102 SessionBackend* backend() const { return backend_.get(); } | |
sky
2014/10/31 22:36:16
Any reason you can't use backend_ directly?
Mr4D (OOO till 08-26)
2014/11/01 01:28:23
Yes, there was still one more reference from a uni
| |
94 | 103 |
95 // This posts the task to the SequencedWorkerPool, or run immediately | 104 // This posts the task to the SequencedWorkerPool, or run immediately |
96 // if the SequencedWorkerPool has been shutdown. | 105 // if the SequencedWorkerPool has been shutdown. |
97 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, | 106 void RunTaskOnBackendThread(const tracked_objects::Location& from_here, |
98 const base::Closure& task); | 107 const base::Closure& task); |
99 | 108 |
100 // Max number of navigation entries in each direction we'll persist. | 109 // The backend object which reads and saves commands. |
101 static const int max_persist_navigation_count; | |
102 | |
103 private: | |
104 friend class BetterSessionRestoreCrashTest; | |
105 | |
106 // The backend. | |
107 scoped_refptr<SessionBackend> backend_; | 110 scoped_refptr<SessionBackend> backend_; |
108 | 111 |
109 // Commands we need to send over to the backend. | 112 // Commands we need to send over to the backend. |
110 ScopedVector<SessionCommand> pending_commands_; | 113 ScopedVector<SessionCommand> pending_commands_; |
111 | 114 |
112 // Whether the backend file should be recreated the next time we send | 115 // Whether the backend file should be recreated the next time we send |
113 // over the commands. | 116 // over the commands. |
114 bool pending_reset_; | 117 bool pending_reset_; |
115 | 118 |
116 // The number of commands sent to the backend before doing a reset. | 119 // The number of commands sent to the backend before doing a reset. |
117 int commands_since_reset_; | 120 int commands_since_reset_; |
118 | 121 |
119 scoped_ptr<BaseSessionServiceDelegate> delegate_; | 122 BaseSessionServiceDelegate* delegate_; |
120 | 123 |
121 // A token to make sure that all tasks will be serialized. | 124 // A token to make sure that all tasks will be serialized. |
122 base::SequencedWorkerPool::SequenceToken sequence_token_; | 125 base::SequencedWorkerPool::SequenceToken sequence_token_; |
123 | 126 |
124 // Used to invoke Save. | 127 // Used to invoke Save. |
125 base::WeakPtrFactory<BaseSessionService> weak_factory_; | 128 base::WeakPtrFactory<BaseSessionService> weak_factory_; |
126 | 129 |
127 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); | 130 DISALLOW_COPY_AND_ASSIGN(BaseSessionService); |
128 }; | 131 }; |
129 | 132 |
130 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ | 133 #endif // CHROME_BROWSER_SESSIONS_BASE_SESSION_SERVICE_H_ |
OLD | NEW |