OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "components/sessions/base_session_service_commands.h" | 5 #include "components/sessions/base_session_service_commands.h" |
6 | 6 |
7 #include "base/pickle.h" | 7 #include "base/pickle.h" |
8 #include "components/sessions/session_backend.h" | 8 #include "components/sessions/session_backend.h" |
9 #include "components/sessions/session_types.h" | 9 #include "components/sessions/session_types.h" |
10 | 10 |
11 namespace sessions { | 11 namespace sessions { |
12 namespace { | 12 namespace { |
13 | 13 |
14 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to | 14 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to |
15 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). | 15 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). |
16 // |bytes_written| is incremented to reflect the data written. | 16 // |bytes_written| is incremented to reflect the data written. |
17 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes, | 17 void WriteStringToPickle(base::Pickle& pickle, |
| 18 int* bytes_written, |
| 19 int max_bytes, |
18 const std::string& str) { | 20 const std::string& str) { |
19 int num_bytes = str.size() * sizeof(char); | 21 int num_bytes = str.size() * sizeof(char); |
20 if (*bytes_written + num_bytes < max_bytes) { | 22 if (*bytes_written + num_bytes < max_bytes) { |
21 *bytes_written += num_bytes; | 23 *bytes_written += num_bytes; |
22 pickle.WriteString(str); | 24 pickle.WriteString(str); |
23 } else { | 25 } else { |
24 pickle.WriteString(std::string()); | 26 pickle.WriteString(std::string()); |
25 } | 27 } |
26 } | 28 } |
27 | 29 |
28 } // namespace | 30 } // namespace |
29 | 31 |
30 scoped_ptr<SessionCommand> CreateUpdateTabNavigationCommand( | 32 scoped_ptr<SessionCommand> CreateUpdateTabNavigationCommand( |
31 SessionID::id_type command_id, | 33 SessionID::id_type command_id, |
32 SessionID::id_type tab_id, | 34 SessionID::id_type tab_id, |
33 const sessions::SerializedNavigationEntry& navigation) { | 35 const sessions::SerializedNavigationEntry& navigation) { |
34 // Use pickle to handle marshalling. | 36 // Use pickle to handle marshalling. |
35 Pickle pickle; | 37 base::Pickle pickle; |
36 pickle.WriteInt(tab_id); | 38 pickle.WriteInt(tab_id); |
37 // We only allow navigations up to 63k (which should be completely | 39 // We only allow navigations up to 63k (which should be completely |
38 // reasonable). | 40 // reasonable). |
39 static const size_t max_state_size = | 41 static const size_t max_state_size = |
40 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | 42 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
41 navigation.WriteToPickle(max_state_size, &pickle); | 43 navigation.WriteToPickle(max_state_size, &pickle); |
42 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); | 44 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); |
43 } | 45 } |
44 | 46 |
45 scoped_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand( | 47 scoped_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand( |
46 SessionID::id_type command_id, | 48 SessionID::id_type command_id, |
47 SessionID::id_type tab_id, | 49 SessionID::id_type tab_id, |
48 const std::string& extension_id) { | 50 const std::string& extension_id) { |
49 // Use pickle to handle marshalling. | 51 // Use pickle to handle marshalling. |
50 Pickle pickle; | 52 base::Pickle pickle; |
51 pickle.WriteInt(tab_id); | 53 pickle.WriteInt(tab_id); |
52 | 54 |
53 // Enforce a max for ids. They should never be anywhere near this size. | 55 // Enforce a max for ids. They should never be anywhere near this size. |
54 static const SessionCommand::size_type max_id_size = | 56 static const SessionCommand::size_type max_id_size = |
55 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | 57 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
56 | 58 |
57 int bytes_written = 0; | 59 int bytes_written = 0; |
58 | 60 |
59 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id); | 61 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id); |
60 | 62 |
61 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); | 63 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); |
62 } | 64 } |
63 | 65 |
64 scoped_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand( | 66 scoped_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand( |
65 SessionID::id_type command_id, | 67 SessionID::id_type command_id, |
66 SessionID::id_type tab_id, | 68 SessionID::id_type tab_id, |
67 const std::string& user_agent_override) { | 69 const std::string& user_agent_override) { |
68 // Use pickle to handle marshalling. | 70 // Use pickle to handle marshalling. |
69 Pickle pickle; | 71 base::Pickle pickle; |
70 pickle.WriteInt(tab_id); | 72 pickle.WriteInt(tab_id); |
71 | 73 |
72 // Enforce a max for the user agent length. They should never be anywhere | 74 // Enforce a max for the user agent length. They should never be anywhere |
73 // near this size. | 75 // near this size. |
74 static const SessionCommand::size_type max_user_agent_size = | 76 static const SessionCommand::size_type max_user_agent_size = |
75 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | 77 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
76 | 78 |
77 int bytes_written = 0; | 79 int bytes_written = 0; |
78 | 80 |
79 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size, | 81 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size, |
80 user_agent_override); | 82 user_agent_override); |
81 | 83 |
82 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); | 84 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); |
83 } | 85 } |
84 | 86 |
85 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand( | 87 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand( |
86 SessionID::id_type command_id, | 88 SessionID::id_type command_id, |
87 SessionID::id_type window_id, | 89 SessionID::id_type window_id, |
88 const std::string& app_name) { | 90 const std::string& app_name) { |
89 // Use pickle to handle marshalling. | 91 // Use pickle to handle marshalling. |
90 Pickle pickle; | 92 base::Pickle pickle; |
91 pickle.WriteInt(window_id); | 93 pickle.WriteInt(window_id); |
92 | 94 |
93 // Enforce a max for ids. They should never be anywhere near this size. | 95 // Enforce a max for ids. They should never be anywhere near this size. |
94 static const SessionCommand::size_type max_id_size = | 96 static const SessionCommand::size_type max_id_size = |
95 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | 97 std::numeric_limits<SessionCommand::size_type>::max() - 1024; |
96 | 98 |
97 int bytes_written = 0; | 99 int bytes_written = 0; |
98 | 100 |
99 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name); | 101 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name); |
100 | 102 |
101 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); | 103 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle)); |
102 } | 104 } |
103 | 105 |
104 bool RestoreUpdateTabNavigationCommand( | 106 bool RestoreUpdateTabNavigationCommand( |
105 const SessionCommand& command, | 107 const SessionCommand& command, |
106 sessions::SerializedNavigationEntry* navigation, | 108 sessions::SerializedNavigationEntry* navigation, |
107 SessionID::id_type* tab_id) { | 109 SessionID::id_type* tab_id) { |
108 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | 110 scoped_ptr<base::Pickle> pickle(command.PayloadAsPickle()); |
109 if (!pickle.get()) | 111 if (!pickle.get()) |
110 return false; | 112 return false; |
111 PickleIterator iterator(*pickle); | 113 base::PickleIterator iterator(*pickle); |
112 return iterator.ReadInt(tab_id) && navigation->ReadFromPickle(&iterator); | 114 return iterator.ReadInt(tab_id) && navigation->ReadFromPickle(&iterator); |
113 } | 115 } |
114 | 116 |
115 bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command, | 117 bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command, |
116 SessionID::id_type* tab_id, | 118 SessionID::id_type* tab_id, |
117 std::string* extension_app_id) { | 119 std::string* extension_app_id) { |
118 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | 120 scoped_ptr<base::Pickle> pickle(command.PayloadAsPickle()); |
119 if (!pickle.get()) | 121 if (!pickle.get()) |
120 return false; | 122 return false; |
121 | 123 |
122 PickleIterator iterator(*pickle); | 124 base::PickleIterator iterator(*pickle); |
123 return iterator.ReadInt(tab_id) && iterator.ReadString(extension_app_id); | 125 return iterator.ReadInt(tab_id) && iterator.ReadString(extension_app_id); |
124 } | 126 } |
125 | 127 |
126 bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command, | 128 bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command, |
127 SessionID::id_type* tab_id, | 129 SessionID::id_type* tab_id, |
128 std::string* user_agent_override) { | 130 std::string* user_agent_override) { |
129 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | 131 scoped_ptr<base::Pickle> pickle(command.PayloadAsPickle()); |
130 if (!pickle.get()) | 132 if (!pickle.get()) |
131 return false; | 133 return false; |
132 | 134 |
133 PickleIterator iterator(*pickle); | 135 base::PickleIterator iterator(*pickle); |
134 return iterator.ReadInt(tab_id) && iterator.ReadString(user_agent_override); | 136 return iterator.ReadInt(tab_id) && iterator.ReadString(user_agent_override); |
135 } | 137 } |
136 | 138 |
137 bool RestoreSetWindowAppNameCommand(const SessionCommand& command, | 139 bool RestoreSetWindowAppNameCommand(const SessionCommand& command, |
138 SessionID::id_type* window_id, | 140 SessionID::id_type* window_id, |
139 std::string* app_name) { | 141 std::string* app_name) { |
140 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | 142 scoped_ptr<base::Pickle> pickle(command.PayloadAsPickle()); |
141 if (!pickle.get()) | 143 if (!pickle.get()) |
142 return false; | 144 return false; |
143 | 145 |
144 PickleIterator iterator(*pickle); | 146 base::PickleIterator iterator(*pickle); |
145 return iterator.ReadInt(window_id) && iterator.ReadString(app_name); | 147 return iterator.ReadInt(window_id) && iterator.ReadString(app_name); |
146 } | 148 } |
147 | 149 |
148 } // namespace sessions | 150 } // namespace sessions |
OLD | NEW |