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