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