Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(974)

Side by Side Diff: chrome/browser/sessions/base_session_service_commands.cc

Issue 672083002: Refactoring of SessionService to get componentized. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: One more self nit Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/sessions/base_session_service_commands.h"
6
7 #include "base/pickle.h"
8 #include "chrome/browser/sessions/session_backend.h"
9 #include "chrome/browser/sessions/session_types.h"
10
11 namespace BaseSessionServiceCommands {
12
13 namespace {
14
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());
26 }
27 }
28
29 } // namespace
30
31 scoped_ptr<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 scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
44 }
45
46 scoped_ptr<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);
53
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;
57
58 int bytes_written = 0;
59
60 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id);
61
62 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
63 }
64
65 scoped_ptr<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);
72
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;
77
78 int bytes_written = 0;
79
80 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size,
81 user_agent_override);
82
83 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
84 }
85
86 scoped_ptr<SessionCommand> CreateSetWindowAppNameCommand(
87 SessionID::id_type command_id,
88 SessionID::id_type window_id,
89 const std::string& app_name) {
90 // Use pickle to handle marshalling.
91 Pickle pickle;
92 pickle.WriteInt(window_id);
93
94 // Enforce a max for ids. They should never be anywhere near this size.
95 static const SessionCommand::size_type max_id_size =
96 std::numeric_limits<SessionCommand::size_type>::max() - 1024;
97
98 int bytes_written = 0;
99
100 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name);
101
102 return scoped_ptr<SessionCommand>(new SessionCommand(command_id, pickle));
103 }
104
105 bool RestoreUpdateTabNavigationCommand(
106 const SessionCommand& command,
107 sessions::SerializedNavigationEntry* navigation,
108 SessionID::id_type* tab_id) {
109 scoped_ptr<Pickle> pickle(command.PayloadAsPickle());
110 if (!pickle.get())
111 return false;
112 PickleIterator iterator(*pickle);
113 return pickle->ReadInt(&iterator, tab_id) &&
114 navigation->ReadFromPickle(&iterator);
115 }
116
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;
123
124 PickleIterator iterator(*pickle);
125 return pickle->ReadInt(&iterator, tab_id) &&
126 pickle->ReadString(&iterator, extension_app_id);
127 }
128
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;
135
136 PickleIterator iterator(*pickle);
137 return pickle->ReadInt(&iterator, tab_id) &&
138 pickle->ReadString(&iterator, user_agent_override);
139 }
140
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;
147
148 PickleIterator iterator(*pickle);
149 return pickle->ReadInt(&iterator, window_id) &&
150 pickle->ReadString(&iterator, app_name);
151 }
152
153 } // namespace BaseSessionServiceCommands
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698