OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sessions/base_session_service.h" | 5 #include "chrome/browser/sessions/base_session_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/pickle.h" | |
9 #include "base/threading/thread.h" | 8 #include "base/threading/thread.h" |
10 #include "chrome/browser/sessions/base_session_service_delegate.h" | 9 #include "chrome/browser/sessions/base_session_service_delegate.h" |
11 #include "chrome/browser/sessions/session_backend.h" | 10 #include "chrome/browser/sessions/session_backend.h" |
12 #include "chrome/browser/sessions/session_types.h" | |
13 | 11 |
14 // BaseSessionService --------------------------------------------------------- | 12 // BaseSessionService --------------------------------------------------------- |
15 | 13 |
16 namespace { | 14 namespace { |
17 | 15 |
18 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to | |
19 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). | |
20 // |bytes_written| is incremented to reflect the data written. | |
21 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes, | |
22 const std::string& str) { | |
23 int num_bytes = str.size() * sizeof(char); | |
24 if (*bytes_written + num_bytes < max_bytes) { | |
25 *bytes_written += num_bytes; | |
26 pickle.WriteString(str); | |
27 } else { | |
28 pickle.WriteString(std::string()); | |
29 } | |
30 } | |
31 | |
32 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner | 16 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner |
33 // thread if it's not canceled. | 17 // thread if it's not canceled. |
34 void RunIfNotCanceled( | 18 void RunIfNotCanceled( |
35 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, | 19 const base::CancelableTaskTracker::IsCanceledCallback& is_canceled, |
36 const BaseSessionService::InternalGetCommandsCallback& callback, | 20 const BaseSessionService::InternalGetCommandsCallback& callback, |
37 ScopedVector<SessionCommand> commands) { | 21 ScopedVector<SessionCommand> commands) { |
38 if (is_canceled.Run()) | 22 if (is_canceled.Run()) |
39 return; | 23 return; |
40 callback.Run(commands.Pass()); | 24 callback.Run(commands.Pass()); |
41 } | 25 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 99 |
116 // Backend took ownership of commands. | 100 // Backend took ownership of commands. |
117 pending_commands_.clear(); | 101 pending_commands_.clear(); |
118 | 102 |
119 if (pending_reset_) { | 103 if (pending_reset_) { |
120 commands_since_reset_ = 0; | 104 commands_since_reset_ = 0; |
121 pending_reset_ = false; | 105 pending_reset_ = false; |
122 } | 106 } |
123 } | 107 } |
124 | 108 |
125 SessionCommand* BaseSessionService::CreateUpdateTabNavigationCommand( | |
126 SessionID::id_type command_id, | |
127 SessionID::id_type tab_id, | |
128 const sessions::SerializedNavigationEntry& navigation) { | |
129 // Use pickle to handle marshalling. | |
130 Pickle pickle; | |
131 pickle.WriteInt(tab_id); | |
132 // We only allow navigations up to 63k (which should be completely | |
133 // reasonable). | |
134 static const size_t max_state_size = | |
135 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | |
136 navigation.WriteToPickle(max_state_size, &pickle); | |
137 return new SessionCommand(command_id, pickle); | |
138 } | |
139 | |
140 SessionCommand* BaseSessionService::CreateSetTabExtensionAppIDCommand( | |
141 SessionID::id_type command_id, | |
142 SessionID::id_type tab_id, | |
143 const std::string& extension_id) { | |
144 // Use pickle to handle marshalling. | |
145 Pickle pickle; | |
146 pickle.WriteInt(tab_id); | |
147 | |
148 // Enforce a max for ids. They should never be anywhere near this size. | |
149 static const SessionCommand::size_type max_id_size = | |
150 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | |
151 | |
152 int bytes_written = 0; | |
153 | |
154 WriteStringToPickle(pickle, &bytes_written, max_id_size, extension_id); | |
155 | |
156 return new SessionCommand(command_id, pickle); | |
157 } | |
158 | |
159 SessionCommand* BaseSessionService::CreateSetTabUserAgentOverrideCommand( | |
160 SessionID::id_type command_id, | |
161 SessionID::id_type tab_id, | |
162 const std::string& user_agent_override) { | |
163 // Use pickle to handle marshalling. | |
164 Pickle pickle; | |
165 pickle.WriteInt(tab_id); | |
166 | |
167 // Enforce a max for the user agent length. They should never be anywhere | |
168 // near this size. | |
169 static const SessionCommand::size_type max_user_agent_size = | |
170 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | |
171 | |
172 int bytes_written = 0; | |
173 | |
174 WriteStringToPickle(pickle, &bytes_written, max_user_agent_size, | |
175 user_agent_override); | |
176 | |
177 return new SessionCommand(command_id, pickle); | |
178 } | |
179 | |
180 SessionCommand* BaseSessionService::CreateSetWindowAppNameCommand( | |
181 SessionID::id_type command_id, | |
182 SessionID::id_type window_id, | |
183 const std::string& app_name) { | |
184 // Use pickle to handle marshalling. | |
185 Pickle pickle; | |
186 pickle.WriteInt(window_id); | |
187 | |
188 // Enforce a max for ids. They should never be anywhere near this size. | |
189 static const SessionCommand::size_type max_id_size = | |
190 std::numeric_limits<SessionCommand::size_type>::max() - 1024; | |
191 | |
192 int bytes_written = 0; | |
193 | |
194 WriteStringToPickle(pickle, &bytes_written, max_id_size, app_name); | |
195 | |
196 return new SessionCommand(command_id, pickle); | |
197 } | |
198 | |
199 bool BaseSessionService::RestoreUpdateTabNavigationCommand( | |
200 const SessionCommand& command, | |
201 sessions::SerializedNavigationEntry* navigation, | |
202 SessionID::id_type* tab_id) { | |
203 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | |
204 if (!pickle.get()) | |
205 return false; | |
206 PickleIterator iterator(*pickle); | |
207 return | |
208 pickle->ReadInt(&iterator, tab_id) && | |
209 navigation->ReadFromPickle(&iterator); | |
210 } | |
211 | |
212 bool BaseSessionService::RestoreSetTabExtensionAppIDCommand( | |
213 const SessionCommand& command, | |
214 SessionID::id_type* tab_id, | |
215 std::string* extension_app_id) { | |
216 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | |
217 if (!pickle.get()) | |
218 return false; | |
219 | |
220 PickleIterator iterator(*pickle); | |
221 return pickle->ReadInt(&iterator, tab_id) && | |
222 pickle->ReadString(&iterator, extension_app_id); | |
223 } | |
224 | |
225 bool BaseSessionService::RestoreSetTabUserAgentOverrideCommand( | |
226 const SessionCommand& command, | |
227 SessionID::id_type* tab_id, | |
228 std::string* user_agent_override) { | |
229 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | |
230 if (!pickle.get()) | |
231 return false; | |
232 | |
233 PickleIterator iterator(*pickle); | |
234 return pickle->ReadInt(&iterator, tab_id) && | |
235 pickle->ReadString(&iterator, user_agent_override); | |
236 } | |
237 | |
238 bool BaseSessionService::RestoreSetWindowAppNameCommand( | |
239 const SessionCommand& command, | |
240 SessionID::id_type* window_id, | |
241 std::string* app_name) { | |
242 scoped_ptr<Pickle> pickle(command.PayloadAsPickle()); | |
243 if (!pickle.get()) | |
244 return false; | |
245 | |
246 PickleIterator iterator(*pickle); | |
247 return pickle->ReadInt(&iterator, window_id) && | |
248 pickle->ReadString(&iterator, app_name); | |
249 } | |
250 | |
251 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { | 109 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { |
252 return url.is_valid() && delegate_->ShouldTrackEntry(url); | 110 return url.is_valid() && delegate_->ShouldTrackEntry(url); |
253 } | 111 } |
254 | 112 |
255 base::CancelableTaskTracker::TaskId | 113 base::CancelableTaskTracker::TaskId |
256 BaseSessionService::ScheduleGetLastSessionCommands( | 114 BaseSessionService::ScheduleGetLastSessionCommands( |
257 const InternalGetCommandsCallback& callback, | 115 const InternalGetCommandsCallback& callback, |
258 base::CancelableTaskTracker* tracker) { | 116 base::CancelableTaskTracker* tracker) { |
259 base::CancelableTaskTracker::IsCanceledCallback is_canceled; | 117 base::CancelableTaskTracker::IsCanceledCallback is_canceled; |
260 base::CancelableTaskTracker::TaskId id = | 118 base::CancelableTaskTracker::TaskId id = |
(...skipping 19 matching lines...) Expand all Loading... |
280 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); | 138 base::SequencedWorkerPool* pool = delegate_->GetBlockingPool(); |
281 if (!pool->IsShutdownInProgress()) { | 139 if (!pool->IsShutdownInProgress()) { |
282 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); | 140 pool->PostSequencedWorkerTask(sequence_token_, from_here, task); |
283 } else { | 141 } else { |
284 // Fall back to executing on the main thread if the sequence | 142 // Fall back to executing on the main thread if the sequence |
285 // worker pool has been requested to shutdown (around shutdown | 143 // worker pool has been requested to shutdown (around shutdown |
286 // time). | 144 // time). |
287 task.Run(); | 145 task.Run(); |
288 } | 146 } |
289 } | 147 } |
OLD | NEW |