Refactoring of SessionService into a component part 3.
[chromium-blink-merge.git] / chrome / browser / sessions / base_session_service_commands.cc
blob7e3fd5f89e1fcfa82ed251c83ea430a717fb7919
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 "chrome/browser/sessions/base_session_service_commands.h"
7 #include "base/pickle.h"
8 #include "chrome/browser/sessions/session_backend.h"
9 #include "chrome/browser/sessions/session_types.h"
11 // BaseSessionService ---------------------------------------------------------
13 namespace {
15 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to
16 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|).
17 // |bytes_written| is incremented to reflect the data written.
18 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes,
19 const std::string& str) {
20 int num_bytes = str.size() * sizeof(char);
21 if (*bytes_written + num_bytes < max_bytes) {
22 *bytes_written += num_bytes;
23 pickle.WriteString(str);
24 } else {
25 pickle.WriteString(std::string());
29 } // namespace
31 SessionCommand* CreateUpdateTabNavigationCommand(
32 SessionID::id_type command_id,
33 SessionID::id_type tab_id,
34 const sessions::SerializedNavigationEntry& navigation) {
35 // Use pickle to handle marshalling.
36 Pickle pickle;
37 pickle.WriteInt(tab_id);
38 // We only allow navigations up to 63k (which should be completely
39 // reasonable).
40 static const size_t max_state_size =
41 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
42 navigation.WriteToPickle(max_state_size, &pickle);
43 return new SessionCommand(command_id, pickle);
46 SessionCommand* CreateSetTabExtensionAppIDCommand(
47 SessionID::id_type command_id,
48 SessionID::id_type tab_id,
49 const std::string& extension_id) {
50 // Use pickle to handle marshalling.
51 Pickle pickle;
52 pickle.WriteInt(tab_id);
54 // Enforce a max for ids. They should never be anywhere near this size.
55 static const SessionCommand::size_type max_id_size =
56 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
58 int bytes_written = 0;
60 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id);
62 return new SessionCommand(command_id, pickle);
65 SessionCommand* CreateSetTabUserAgentOverrideCommand(
66 SessionID::id_type command_id,
67 SessionID::id_type tab_id,
68 const std::string& user_agent_override) {
69 // Use pickle to handle marshalling.
70 Pickle pickle;
71 pickle.WriteInt(tab_id);
73 // Enforce a max for the user agent length. They should never be anywhere
74 // near this size.
75 static const SessionCommand::size_type max_user_agent_size =
76 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
78 int bytes_written = 0;
80 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size,
81 user_agent_override);
83 return new SessionCommand(command_id, pickle);
86 SessionCommand* CreateSetWindowAppNameCommand(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 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
113 pickle->ReadInt(&iterator, tab_id) &&
114 navigation->ReadFromPickle(&iterator);
117 bool RestoreSetTabExtensionAppIDCommand(const SessionCommand& command,
118 SessionID::id_type* tab_id,
119 std::string* extension_app_id) {
120 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
121 if (!pickle.get())
122 return false;
124 PickleIterator iterator(*pickle);
125 return pickle->ReadInt(&iterator, tab_id) &&
126 pickle->ReadString(&iterator, extension_app_id);
129 bool RestoreSetTabUserAgentOverrideCommand(const SessionCommand& command,
130 SessionID::id_type* tab_id,
131 std::string* user_agent_override) {
132 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
133 if (!pickle.get())
134 return false;
136 PickleIterator iterator(*pickle);
137 return pickle->ReadInt(&iterator, tab_id) &&
138 pickle->ReadString(&iterator, user_agent_override);
141 bool RestoreSetWindowAppNameCommand(const SessionCommand& command,
142 SessionID::id_type* window_id,
143 std::string* app_name) {
144 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
145 if (!pickle.get())
146 return false;
148 PickleIterator iterator(*pickle);
149 return pickle->ReadInt(&iterator, window_id) &&
150 pickle->ReadString(&iterator, app_name);