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