Lazily build the app list model.
[chromium-blink-merge.git] / components / sessions / session_backend.h
blob138fc5848d63c9c74795943327325e6faa5d71e9
1 // Copyright 2011 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.
5 #ifndef COMPONENTS_SESSIONS_SESSION_BACKEND_H_
6 #define COMPONENTS_SESSIONS_SESSION_BACKEND_H_
8 #include <vector>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/task/cancelable_task_tracker.h"
13 #include "components/sessions/base_session_service.h"
14 #include "components/sessions/session_command.h"
16 namespace base {
17 class File;
20 namespace sessions {
21 // SessionBackend -------------------------------------------------------------
23 // SessionBackend is the backend used by BaseSessionService. It is responsible
24 // for maintaining two files:
25 // . The current file, which is the file commands passed to AppendCommands
26 // get written to.
27 // . The last file. When created the current file is moved to the last
28 // file.
30 // Each file contains an arbitrary set of commands supplied from
31 // BaseSessionService. A command consists of a unique id and a stream of bytes.
32 // SessionBackend does not use the id in anyway, that is used by
33 // BaseSessionService.
34 class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> {
35 public:
36 typedef sessions::SessionCommand::id_type id_type;
37 typedef sessions::SessionCommand::size_type size_type;
39 // Initial size of the buffer used in reading the file. This is exposed
40 // for testing.
41 static const int kFileReadBufferSize;
43 // Creates a SessionBackend. This method is invoked on the MAIN thread,
44 // and does no IO. The real work is done from Init, which is invoked on
45 // the file thread.
47 // |path_to_dir| gives the path the files are written two, and |type|
48 // indicates which service is using this backend. |type| is used to determine
49 // the name of the files to use as well as for logging.
50 SessionBackend(sessions::BaseSessionService::SessionType type,
51 const base::FilePath& path_to_dir);
53 // Moves the current file to the last file, and recreates the current file.
55 // NOTE: this is invoked before every command, and does nothing if we've
56 // already Init'ed.
57 void Init();
58 bool inited() const { return inited_; }
60 // Appends the specified commands to the current file. If reset_first is
61 // true the the current file is recreated.
62 void AppendCommands(ScopedVector<sessions::SessionCommand> commands,
63 bool reset_first);
65 // Invoked from the service to read the commands that make up the last
66 // session, invokes ReadLastSessionCommandsImpl to do the work.
67 void ReadLastSessionCommands(
68 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
69 const sessions::BaseSessionService::GetCommandsCallback& callback);
71 // Reads the commands from the last file.
73 // On success, the read commands are added to commands.
74 bool ReadLastSessionCommandsImpl(
75 ScopedVector<sessions::SessionCommand>* commands);
77 // Deletes the file containing the commands for the last session.
78 void DeleteLastSession();
80 // Moves the current session to the last and resets the current. This is
81 // called during startup and if the user launchs the app and no tabbed
82 // browsers are running.
83 void MoveCurrentSessionToLastSession();
85 // Reads the commands from the current file.
87 // On success, the read commands are added to commands. It is up to the
88 // caller to delete the commands.
89 bool ReadCurrentSessionCommandsImpl(
90 ScopedVector<sessions::SessionCommand>* commands);
92 private:
93 friend class base::RefCountedThreadSafe<SessionBackend>;
95 ~SessionBackend();
97 // If current_session_file_ is open, it is truncated so that it is essentially
98 // empty (only contains the header). If current_session_file_ isn't open, it
99 // is is opened and the header is written to it. After this
100 // current_session_file_ contains no commands.
101 // NOTE: current_session_file_ may be NULL if the file couldn't be opened or
102 // the header couldn't be written.
103 void ResetFile();
105 // Opens the current file and writes the header. On success a handle to
106 // the file is returned.
107 base::File* OpenAndWriteHeader(const base::FilePath& path);
109 // Appends the specified commands to the specified file.
110 bool AppendCommandsToFile(
111 base::File* file,
112 const ScopedVector<sessions::SessionCommand>& commands);
114 const sessions::BaseSessionService::SessionType type_;
116 // Returns the path to the last file.
117 base::FilePath GetLastSessionPath();
119 // Returns the path to the current file.
120 base::FilePath GetCurrentSessionPath();
122 // Directory files are relative to.
123 const base::FilePath path_to_dir_;
125 // Whether the previous target file is valid.
126 bool last_session_valid_;
128 // Handle to the target file.
129 scoped_ptr<base::File> current_session_file_;
131 // Whether we've inited. Remember, the constructor is run on the
132 // Main thread, all others on the IO thread, hence lazy initialization.
133 bool inited_;
135 // If true, the file is empty (no commands have been added to it).
136 bool empty_file_;
138 DISALLOW_COPY_AND_ASSIGN(SessionBackend);
141 } // namespace sessions
143 #endif // COMPONENTS_SESSIONS_SESSION_BACKEND_H_