OLD | NEW |
1 // Copyright 2006 The Chromium Authors. All rights reserved. | 1 // Copyright 2006 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 COMPONENTS_SESSIONS_SESSION_COMMAND_H_ | 5 #ifndef COMPONENTS_SESSIONS_SESSION_COMMAND_H_ |
6 #define COMPONENTS_SESSIONS_SESSION_COMMAND_H_ | 6 #define COMPONENTS_SESSIONS_SESSION_COMMAND_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "components/sessions/sessions_export.h" | 11 #include "components/sessions/sessions_export.h" |
12 | 12 |
| 13 namespace base { |
13 class Pickle; | 14 class Pickle; |
| 15 } |
14 | 16 |
15 namespace sessions { | 17 namespace sessions { |
16 | 18 |
17 // SessionCommand contains a command id and arbitrary chunk of data. The id | 19 // SessionCommand contains a command id and arbitrary chunk of data. The id |
18 // and chunk of data are specific to the service creating them. | 20 // and chunk of data are specific to the service creating them. |
19 // | 21 // |
20 // Both TabRestoreService and SessionService use SessionCommands to represent | 22 // Both TabRestoreService and SessionService use SessionCommands to represent |
21 // state on disk. | 23 // state on disk. |
22 // | 24 // |
23 // There are two ways to create a SessionCommand: | 25 // There are two ways to create a SessionCommand: |
24 // . Specifiy the size of the data block to create. This is useful for | 26 // . Specifiy the size of the data block to create. This is useful for |
25 // commands that have a fixed size. | 27 // commands that have a fixed size. |
26 // . From a pickle, this is useful for commands whose length varies. | 28 // . From a pickle, this is useful for commands whose length varies. |
27 class SESSIONS_EXPORT SessionCommand { | 29 class SESSIONS_EXPORT SessionCommand { |
28 public: | 30 public: |
29 // These get written to disk, so we define types for them. | 31 // These get written to disk, so we define types for them. |
30 // Type for the identifier. | 32 // Type for the identifier. |
31 typedef uint8 id_type; | 33 typedef uint8 id_type; |
32 | 34 |
33 // Type for writing the size. | 35 // Type for writing the size. |
34 typedef uint16 size_type; | 36 typedef uint16 size_type; |
35 | 37 |
36 // Creates a session command with the specified id. This allocates a buffer | 38 // Creates a session command with the specified id. This allocates a buffer |
37 // of size |size| that must be filled via contents(). | 39 // of size |size| that must be filled via contents(). |
38 SessionCommand(id_type id, size_type size); | 40 SessionCommand(id_type id, size_type size); |
39 | 41 |
40 // Convenience constructor that creates a session command with the specified | 42 // Convenience constructor that creates a session command with the specified |
41 // id whose contents is populated from the contents of pickle. | 43 // id whose contents is populated from the contents of pickle. |
42 SessionCommand(id_type id, const Pickle& pickle); | 44 SessionCommand(id_type id, const base::Pickle& pickle); |
43 | 45 |
44 // The contents of the command. | 46 // The contents of the command. |
45 char* contents() { return const_cast<char*>(contents_.c_str()); } | 47 char* contents() { return const_cast<char*>(contents_.c_str()); } |
46 const char* contents() const { return contents_.c_str(); } | 48 const char* contents() const { return contents_.c_str(); } |
47 | 49 |
48 // Identifier for the command. | 50 // Identifier for the command. |
49 id_type id() const { return id_; } | 51 id_type id() const { return id_; } |
50 | 52 |
51 // Size of data. | 53 // Size of data. |
52 size_type size() const { return static_cast<size_type>(contents_.size()); } | 54 size_type size() const { return static_cast<size_type>(contents_.size()); } |
53 | 55 |
54 // Convenience for extracting the data to a target. Returns false if | 56 // Convenience for extracting the data to a target. Returns false if |
55 // count is not equal to the size of data this command contains. | 57 // count is not equal to the size of data this command contains. |
56 bool GetPayload(void* dest, size_t count) const; | 58 bool GetPayload(void* dest, size_t count) const; |
57 | 59 |
58 // Returns the contents as a pickle. It is up to the caller to delete the | 60 // Returns the contents as a pickle. It is up to the caller to delete the |
59 // returned Pickle. The returned Pickle references the underlying data of | 61 // returned Pickle. The returned Pickle references the underlying data of |
60 // this SessionCommand. If you need it to outlive the command, copy the | 62 // this SessionCommand. If you need it to outlive the command, copy the |
61 // pickle. | 63 // pickle. |
62 Pickle* PayloadAsPickle() const; | 64 base::Pickle* PayloadAsPickle() const; |
63 | 65 |
64 private: | 66 private: |
65 const id_type id_; | 67 const id_type id_; |
66 std::string contents_; | 68 std::string contents_; |
67 | 69 |
68 DISALLOW_COPY_AND_ASSIGN(SessionCommand); | 70 DISALLOW_COPY_AND_ASSIGN(SessionCommand); |
69 }; | 71 }; |
70 | 72 |
71 } // namespace sessions | 73 } // namespace sessions |
72 | 74 |
73 #endif // COMPONENTS_SESSIONS_SESSION_COMMAND_H_ | 75 #endif // COMPONENTS_SESSIONS_SESSION_COMMAND_H_ |
OLD | NEW |