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"
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
,
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
);
26 pickle
.WriteString(std::string());
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.
38 pickle
.WriteInt(tab_id
);
39 // We only allow navigations up to 63k (which should be completely
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
));
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.
53 pickle
.WriteInt(tab_id
);
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;
59 int bytes_written
= 0;
61 WriteStringToPickle(pickle
, &bytes_written
, max_id_size
, extension_id
);
63 return scoped_ptr
<SessionCommand
>(new SessionCommand(command_id
, pickle
));
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.
72 pickle
.WriteInt(tab_id
);
74 // Enforce a max for the user agent length. They should never be anywhere
76 static const SessionCommand::size_type max_user_agent_size
=
77 std::numeric_limits
<SessionCommand::size_type
>::max() - 1024;
79 int bytes_written
= 0;
81 WriteStringToPickle(pickle
, &bytes_written
, max_user_agent_size
,
84 return scoped_ptr
<SessionCommand
>(new SessionCommand(command_id
, pickle
));
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.
93 pickle
.WriteInt(window_id
);
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;
99 int bytes_written
= 0;
101 WriteStringToPickle(pickle
, &bytes_written
, max_id_size
, app_name
);
103 return scoped_ptr
<SessionCommand
>(new SessionCommand(command_id
, pickle
));
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());
113 base::PickleIterator
iterator(*pickle
);
114 return iterator
.ReadInt(tab_id
) && navigation
->ReadFromPickle(&iterator
);
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());
124 base::PickleIterator
iterator(*pickle
);
125 return iterator
.ReadInt(tab_id
) && iterator
.ReadString(extension_app_id
);
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());
135 base::PickleIterator
iterator(*pickle
);
136 return iterator
.ReadInt(tab_id
) && iterator
.ReadString(user_agent_override
);
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());
146 base::PickleIterator
iterator(*pickle
);
147 return iterator
.ReadInt(window_id
) && iterator
.ReadString(app_name
);
150 } // namespace sessions