Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / components / sessions / base_session_service_commands.cc
blobc0ae0a3d0707e340713742d40b5ce4faf5a93faf
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 pickle->ReadInt(&iterator, tab_id) &&
113 navigation->ReadFromPickle(&iterator);
116 bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command,
117 SessionID::id_type* tab_id,
118 std::string* extension_app_id) {
119 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
120 if (!pickle.get())
121 return false;
123 PickleIterator iterator(*pickle);
124 return pickle->ReadInt(&iterator, tab_id) &&
125 pickle->ReadString(&iterator, extension_app_id);
128 bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command,
129 SessionID::id_type* tab_id,
130 std::string* user_agent_override) {
131 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
132 if (!pickle.get())
133 return false;
135 PickleIterator iterator(*pickle);
136 return pickle->ReadInt(&iterator, tab_id) &&
137 pickle->ReadString(&iterator, user_agent_override);
140 bool RestoreSetWindowAppNameCommand(const SessionCommand& command,
141 SessionID::id_type* window_id,
142 std::string* app_name) {
143 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
144 if (!pickle.get())
145 return false;
147 PickleIterator iterator(*pickle);
148 return pickle->ReadInt(&iterator, window_id) &&
149 pickle->ReadString(&iterator, app_name);
152 } // namespace sessions