summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sessions/session_backend.h
diff options
context:
space:
mode:
authorsky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-06 19:30:19 +0000
committersky@google.com <sky@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-06 19:30:19 +0000
commit169627b81ce036a7014476c366b060e050b5ff70 (patch)
tree6231d8cfaa065513b4742d99204b25da4178037a /chrome/browser/sessions/session_backend.h
parentee824a436efbdeed4ca78efc4dd2aa4976ba43a9 (diff)
downloadchromium_src-169627b81ce036a7014476c366b060e050b5ff70.zip
chromium_src-169627b81ce036a7014476c366b060e050b5ff70.tar.gz
chromium_src-169627b81ce036a7014476c366b060e050b5ff70.tar.bz2
Makes the tab restore service persist closed tabs/windows to disk and
reload them when asked. Sorry for largish looking change. It's made big by refactoring common code between TabRestoreService and SessionService into a common superclass. At the same time I removed some dead code and shuffled the session related classes into a single directory for easier perusal. BUG=384 TEST=close the browser, start the browser and make sure the new tab page shows closed windows/tabs from the previous session. Review URL: http://codereview.chromium.org/13152 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@6490 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sessions/session_backend.h')
-rw-r--r--chrome/browser/sessions/session_backend.h122
1 files changed, 122 insertions, 0 deletions
diff --git a/chrome/browser/sessions/session_backend.h b/chrome/browser/sessions/session_backend.h
new file mode 100644
index 0000000..1519d3f
--- /dev/null
+++ b/chrome/browser/sessions/session_backend.h
@@ -0,0 +1,122 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_
+#define CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_
+
+#include <vector>
+
+#include "base/ref_counted.h"
+#include "base/scoped_handle.h"
+#include "chrome/browser/sessions/base_session_service.h"
+#include "chrome/browser/sessions/session_command.h"
+
+class Pickle;
+
+// SessionBackend -------------------------------------------------------------
+
+// SessionBackend is the backend used by BaseSessionService. It is responsible
+// for maintaining two files:
+// . The current file, which is the file commands passed to AppendCommands
+// get written to.
+// . The last file. When created the current file is moved to the last
+// file.
+//
+// Each file contains an arbitrary set of commands supplied from
+// BaseSessionService. A command consists of a unique id and a stream of bytes.
+// SessionBackend does not use the id in anyway, that is used by
+// BaseSessionService.
+class SessionBackend : public base::RefCountedThreadSafe<SessionBackend> {
+ public:
+ typedef SessionCommand::id_type id_type;
+ typedef SessionCommand::size_type size_type;
+
+ // Initial size of the buffer used in reading the file. This is exposed
+ // for testing.
+ static const int kFileReadBufferSize;
+
+ // Creates a SessionBackend. This method is invoked on the MAIN thread,
+ // and does no IO. The real work is done from Init, which is invoked on
+ // the file thread.
+ //
+ // |path_to_dir| gives the path the files are written two, and |type|
+ // indicates which service is using this backend. |type| is used to determine
+ // the name of the files to use as well as for logging.
+ SessionBackend(BaseSessionService::SessionType type,
+ const std::wstring& path_to_dir);
+
+ // Moves the current file to the last file, and recreates the current file.
+ //
+ // NOTE: this is invoked before every command, and does nothing if we've
+ // already Init'ed.
+ void Init();
+
+ // Appends the specified commands to the current file. If reset_first is
+ // true the the current file is recreated.
+ //
+ // NOTE: this deletes SessionCommands in commands as well as the supplied
+ // vector.
+ void AppendCommands(std::vector<SessionCommand*>* commands,
+ bool reset_first);
+
+ // Invoked from the service to read the commands that make up the last
+ // session, invokes ReadSessionImpl to do the work.
+ void ReadLastSessionCommands(
+ scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request);
+
+ // Reads the commands from the last file.
+ //
+ // On success, the read commands are added to commands. It is up to the
+ // caller to delete the commands.
+ bool ReadLastSessionCommandsImpl(std::vector<SessionCommand*>* commands);
+
+ // Deletes the file containing the commands for the last session.
+ void DeleteLastSession();
+
+ // Moves the current session to the last and resets the current. This is
+ // called during startup and if the user launchs the app and no tabbed
+ // browsers are running.
+ void MoveCurrentSessionToLastSession();
+
+ private:
+ // Recreates the current file such that it only contains the header and
+ // NO commands.
+ void ResetFile();
+
+ // Opens the current file and writes the header. On success a handle to
+ // the file is returned.
+ HANDLE OpenAndWriteHeader(const std::wstring& path);
+
+ // Appends the specified commands to the specified file.
+ bool AppendCommandsToFile(HANDLE handle,
+ const std::vector<SessionCommand*>& commands);
+
+ const BaseSessionService::SessionType type_;
+
+ // Returns the path to the last file.
+ std::wstring GetLastSessionPath();
+
+ // Returns the path to the current file.
+ std::wstring GetCurrentSessionPath();
+
+ // Directory files are relative to.
+ const std::wstring path_to_dir_;
+
+ // Whether the previous target file is valid.
+ bool last_session_valid_;
+
+ // Handle to the target file.
+ ScopedHandle current_session_handle_;
+
+ // Whether we've inited. Remember, the constructor is run on the
+ // Main thread, all others on the IO thread, hence lazy initialization.
+ bool inited_;
+
+ // If true, the file is empty (no commands have been added to it).
+ bool empty_file_;
+
+ DISALLOW_COPY_AND_ASSIGN(SessionBackend);
+};
+
+#endif // #define CHROME_BROWSER_SESSIONS_SESSION_BACKEND_H_