Port Android relocation packer to chromium build
[chromium-blink-merge.git] / components / sessions / base_session_service_commands.cc
blob09eab437287d28a19b8cf0f238ceb5aaf5bc2af9
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.
5 #include "components/sessions/base_session_service_commands.h"
7 #include "base/pickle.h"
8 #include "components/sessions/session_backend.h"
9 #include "components/sessions/session_types.h"
11 namespace sessions {
12 namespace {
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(Pickle& pickle, int* bytes_written, int max_bytes,
18 const std::string& str) {
19 int num_bytes = str.size() * sizeof(char);
20 if (*bytes_written + num_bytes < max_bytes) {
21 *bytes_written += num_bytes;
22 pickle.WriteString(str);
23 } else {
24 pickle.WriteString(std::string());
28 } // namespace
30 scoped_ptr<SessionCommand> CreateUpdateTabNavigationCommand(
31 SessionID::id_type command_id,
32 SessionID::id_type tab_id,
33 const sessions::SerializedNavigationEntry& navigation) {
34 // Use pickle to handle marshalling.
35 Pickle pickle;
36 pickle.WriteInt(tab_id);
37 // We only allow navigations up to 63k (which should be completely
38 // reasonable).
39 static const size_t max_state_size =
40 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
41 navigation.WriteToPickle(max_state_size, &pickle);
42 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
45 scoped_ptr<SessionCommand> CreateSetTabExtensionAppIDCommand(
46 SessionID::id_type command_id,
47 SessionID::id_type tab_id,
48 const std::string& extension_id) {
49 // Use pickle to handle marshalling.
50 Pickle pickle;
51 pickle.WriteInt(tab_id);
53 // Enforce a max for ids. They should never be anywhere near this size.
54 static const SessionCommand::size_type max_id_size =
55 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
57 int bytes_written = 0;
59 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id);
61 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
64 scoped_ptr<SessionCommand> CreateSetTabUserAgentOverrideCommand(
65 SessionID::id_type command_id,
66 SessionID::id_type tab_id,
67 const std::string& user_agent_override) {
68 // Use pickle to handle marshalling.
69 Pickle pickle;
70 pickle.WriteInt(tab_id);
72 // Enforce a max for the user agent length. They should never be anywhere
73 // near this size.
74 static const SessionCommand::size_type max_user_agent_size =
75 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
77 int bytes_written = 0;
79 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size,
80 user_agent_override);
82 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
85 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand(
86 SessionID::id_type command_id,
87 SessionID::id_type window_id,
88 const std::string& app_name) {
89 // Use pickle to handle marshalling.
90 Pickle pickle;
91 pickle.WriteInt(window_id);
93 // Enforce a max for ids. They should never be anywhere near this size.
94 static const SessionCommand::size_type max_id_size =
95 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
97 int bytes_written = 0;
99 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name);
101 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
104 bool RestoreUpdateTabNavigationCommand(
105 const SessionCommand& command,
106 sessions::SerializedNavigationEntry* navigation,
107 SessionID::id_type* tab_id) {
108 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
109 if (!pickle.get())
110 return false;
111 PickleIterator iterator(*pickle);
112 return iterator.ReadInt(tab_id) && navigation->ReadFromPickle(&iterator);
115 bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command,
116 SessionID::id_type* tab_id,
117 std::string* extension_app_id) {
118 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
119 if (!pickle.get())
120 return false;
122 PickleIterator iterator(*pickle);
123 return iterator.ReadInt(tab_id) && iterator.ReadString(extension_app_id);
126 bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command,
127 SessionID::id_type* tab_id,
128 std::string* user_agent_override) {
129 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
130 if (!pickle.get())
131 return false;
133 PickleIterator iterator(*pickle);
134 return iterator.ReadInt(tab_id) && iterator.ReadString(user_agent_override);
137 bool RestoreSetWindowAppNameCommand(const SessionCommand& command,
138 SessionID::id_type* window_id,
139 std::string* app_name) {
140 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
141 if (!pickle.get())
142 return false;
144 PickleIterator iterator(*pickle);
145 return iterator.ReadInt(window_id) && iterator.ReadString(app_name);
148 } // namespace sessions