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/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/sessions/session_backend.h" | 14 #include "chrome/browser/sessions/session_backend.h" |
15 #include "chrome/browser/sessions/session_types.h" | 15 #include "chrome/browser/sessions/session_types.h" |
16 #include "chrome/common/chrome_switches.h" | 16 #include "chrome/common/chrome_switches.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/navigation_entry.h" | 18 #include "content/public/browser/navigation_entry.h" |
19 #include "content/public/common/referrer.h" | 19 #include "content/public/common/referrer.h" |
20 #include "webkit/glue/webkit_glue.h" | 20 #include "webkit/glue/webkit_glue.h" |
21 | 21 |
22 using content::BrowserThread; | 22 using content::BrowserThread; |
23 using content::NavigationEntry; | 23 using content::NavigationEntry; |
24 | 24 |
25 // InternalGetCommandsRequest ------------------------------------------------- | |
26 | |
27 BaseSessionService::InternalGetCommandsRequest::InternalGetCommandsRequest( | |
28 const CallbackType& callback) | |
29 : CancelableRequest<InternalGetCommandsCallback>(callback) { | |
30 } | |
31 | |
32 BaseSessionService::InternalGetCommandsRequest::~InternalGetCommandsRequest() { | |
33 STLDeleteElements(&commands); | |
34 } | |
35 | |
36 // BaseSessionService --------------------------------------------------------- | 25 // BaseSessionService --------------------------------------------------------- |
37 | 26 |
38 namespace { | 27 namespace { |
39 | 28 |
40 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to | 29 // Helper used by CreateUpdateTabNavigationCommand(). It writes |str| to |
41 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). | 30 // |pickle|, if and only if |str| fits within (|max_bytes| - |*bytes_written|). |
42 // |bytes_written| is incremented to reflect the data written. | 31 // |bytes_written| is incremented to reflect the data written. |
43 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes, | 32 void WriteStringToPickle(Pickle& pickle, int* bytes_written, int max_bytes, |
44 const std::string& str) { | 33 const std::string& str) { |
45 int num_bytes = str.size() * sizeof(char); | 34 int num_bytes = str.size() * sizeof(char); |
46 if (*bytes_written + num_bytes < max_bytes) { | 35 if (*bytes_written + num_bytes < max_bytes) { |
47 *bytes_written += num_bytes; | 36 *bytes_written += num_bytes; |
48 pickle.WriteString(str); | 37 pickle.WriteString(str); |
49 } else { | 38 } else { |
50 pickle.WriteString(std::string()); | 39 pickle.WriteString(std::string()); |
51 } | 40 } |
52 } | 41 } |
53 | 42 |
| 43 // Helper used by ScheduleGetLastSessionCommands. It runs callback on TaskRunner |
| 44 // thread if it's not canceled. |
| 45 void RunIfNotCanceled( |
| 46 const CancelableTaskTracker::IsCanceledCallback& is_canceled, |
| 47 base::TaskRunner* task_runner, |
| 48 const BaseSessionService::InternalGetCommandsCallback& callback, |
| 49 ScopedVector<SessionCommand> commands) { |
| 50 if (is_canceled.Run()) |
| 51 return; |
| 52 |
| 53 if (task_runner->RunsTasksOnCurrentThread()) { |
| 54 callback.Run(commands.Pass()); |
| 55 } else { |
| 56 task_runner->PostTask(FROM_HERE, |
| 57 base::Bind(callback, base::Passed(&commands))); |
| 58 } |
| 59 } |
| 60 |
54 } // namespace | 61 } // namespace |
55 | 62 |
56 // Delay between when a command is received, and when we save it to the | 63 // Delay between when a command is received, and when we save it to the |
57 // backend. | 64 // backend. |
58 static const int kSaveDelayMS = 2500; | 65 static const int kSaveDelayMS = 2500; |
59 | 66 |
60 // static | 67 // static |
61 const int BaseSessionService::max_persist_navigation_count = 6; | 68 const int BaseSessionService::max_persist_navigation_count = 6; |
62 | 69 |
63 BaseSessionService::BaseSessionService(SessionType type, | 70 BaseSessionService::BaseSessionService(SessionType type, |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 259 |
253 PickleIterator iterator(*pickle); | 260 PickleIterator iterator(*pickle); |
254 return pickle->ReadInt(&iterator, window_id) && | 261 return pickle->ReadInt(&iterator, window_id) && |
255 pickle->ReadString(&iterator, app_name); | 262 pickle->ReadString(&iterator, app_name); |
256 } | 263 } |
257 | 264 |
258 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { | 265 bool BaseSessionService::ShouldTrackEntry(const GURL& url) { |
259 return url.is_valid(); | 266 return url.is_valid(); |
260 } | 267 } |
261 | 268 |
262 BaseSessionService::Handle BaseSessionService::ScheduleGetLastSessionCommands( | 269 CancelableTaskTracker::TaskId |
263 InternalGetCommandsRequest* request, | 270 BaseSessionService::ScheduleGetLastSessionCommands( |
264 CancelableRequestConsumerBase* consumer) { | 271 const InternalGetCommandsCallback& callback, |
265 scoped_refptr<InternalGetCommandsRequest> request_wrapper(request); | 272 CancelableTaskTracker* tracker) { |
266 AddRequest(request, consumer); | 273 CancelableTaskTracker::IsCanceledCallback is_canceled; |
| 274 CancelableTaskTracker::TaskId id = tracker->NewTrackedTaskId(&is_canceled); |
| 275 |
| 276 InternalGetCommandsCallback callback_runner = |
| 277 base::Bind(&RunIfNotCanceled, |
| 278 is_canceled, base::MessageLoopProxy::current(), callback); |
| 279 |
267 RunTaskOnBackendThread( | 280 RunTaskOnBackendThread( |
268 FROM_HERE, | 281 FROM_HERE, |
269 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), | 282 base::Bind(&SessionBackend::ReadLastSessionCommands, backend(), |
270 request_wrapper)); | 283 is_canceled, callback_runner)); |
271 return request->handle(); | 284 return id; |
272 } | 285 } |
273 | 286 |
274 bool BaseSessionService::RunTaskOnBackendThread( | 287 bool BaseSessionService::RunTaskOnBackendThread( |
275 const tracked_objects::Location& from_here, | 288 const tracked_objects::Location& from_here, |
276 const base::Closure& task) { | 289 const base::Closure& task) { |
277 if (profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) { | 290 if (profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE)) { |
278 return BrowserThread::PostTask(BrowserThread::FILE, from_here, task); | 291 return BrowserThread::PostTask(BrowserThread::FILE, from_here, task); |
279 } else { | 292 } else { |
280 // Fall back to executing on the main thread if the file thread | 293 // Fall back to executing on the main thread if the file thread |
281 // has gone away (around shutdown time) or if we're running as | 294 // has gone away (around shutdown time) or if we're running as |
282 // part of a unit test that does not set profile_. | 295 // part of a unit test that does not set profile_. |
283 task.Run(); | 296 task.Run(); |
284 return true; | 297 return true; |
285 } | 298 } |
286 } | 299 } |
287 | 300 |
288 bool BaseSessionService::RunningInProduction() const { | 301 bool BaseSessionService::RunningInProduction() const { |
289 return profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE); | 302 return profile_ && BrowserThread::IsMessageLoopValid(BrowserThread::FILE); |
290 } | 303 } |
OLD | NEW |