1 // Copyright (c) 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 CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_
6 #define CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/task/cancelable_task_tracker.h"
13 #include "chrome/browser/sessions/base_session_service.h"
14 #include "chrome/browser/sessions/session_command.h"
20 // SessionBackend -------------------------------------------------------------
22 // SessionBackend is the backend used by BaseSessionService. It is responsible
23 // for maintaining two files:
24 // . The current file, which is the file commands passed to AppendCommands
26 // . The last file. When created the current file is moved to the last
29 // Each file contains an arbitrary set of commands supplied from
30 // BaseSessionService. A command consists of a unique id and a stream of bytes.
31 // SessionBackend does not use the id in anyway, that is used by
32 // BaseSessionService.
33 class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> {
35 typedef SessionCommand::id_type id_type;
36 typedef SessionCommand::size_type size_type;
38 // Initial size of the buffer used in reading the file. This is exposed
40 static const int kFileReadBufferSize;
42 // Creates a SessionBackend. This method is invoked on the MAIN thread,
43 // and does no IO. The real work is done from Init, which is invoked on
46 // |path_to_dir| gives the path the files are written two, and |type|
47 // indicates which service is using this backend. |type| is used to determine
48 // the name of the files to use as well as for logging.
49 SessionBackend(BaseSessionService::SessionType type,
50 const base::FilePath& path_to_dir);
52 // Moves the current file to the last file, and recreates the current file.
54 // NOTE: this is invoked before every command, and does nothing if we've
57 bool inited() const { return inited_; }
59 // Appends the specified commands to the current file. If reset_first is
60 // true the the current file is recreated.
61 void AppendCommands(ScopedVector<SessionCommand> commands, bool reset_first);
63 // Invoked from the service to read the commands that make up the last
64 // session, invokes ReadLastSessionCommandsImpl to do the work.
65 void ReadLastSessionCommands(
66 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled,
67 const BaseSessionService::GetCommandsCallback& callback);
69 // Reads the commands from the last file.
71 // On success, the read commands are added to commands.
72 bool ReadLastSessionCommandsImpl(ScopedVector<SessionCommand>* commands);
74 // Deletes the file containing the commands for the last session.
75 void DeleteLastSession();
77 // Moves the current session to the last and resets the current. This is
78 // called during startup and if the user launchs the app and no tabbed
79 // browsers are running.
80 void MoveCurrentSessionToLastSession();
82 // Reads the commands from the current file.
84 // On success, the read commands are added to commands. It is up to the
85 // caller to delete the commands.
86 bool ReadCurrentSessionCommandsImpl(ScopedVector<SessionCommand>* commands);
89 friend class base::RefCountedThreadSafe<SessionBackend>;
93 // If current_session_file_ is open, it is truncated so that it is essentially
94 // empty (only contains the header). If current_session_file_ isn't open, it
95 // is is opened and the header is written to it. After this
96 // current_session_file_ contains no commands.
97 // NOTE: current_session_file_ may be NULL if the file couldn't be opened or
98 // the header couldn't be written.
101 // Opens the current file and writes the header. On success a handle to
102 // the file is returned.
103 base::File* OpenAndWriteHeader(const base::FilePath& path);
105 // Appends the specified commands to the specified file.
106 bool AppendCommandsToFile(base::File* file,
107 const ScopedVector<SessionCommand>& commands);
109 const BaseSessionService::SessionType type_;
111 // Returns the path to the last file.
112 base::FilePath GetLastSessionPath();
114 // Returns the path to the current file.
115 base::FilePath GetCurrentSessionPath();
117 // Directory files are relative to.
118 const base::FilePath path_to_dir_;
120 // Whether the previous target file is valid.
121 bool last_session_valid_;
123 // Handle to the target file.
124 scoped_ptr<base::File> current_session_file_;
126 // Whether we've inited. Remember, the constructor is run on the
127 // Main thread, all others on the IO thread, hence lazy initialization.
130 // If true, the file is empty (no commands have been added to it).
133 DISALLOW_COPY_AND_ASSIGN(SessionBackend);
136 #endif // CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_