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/session_backend.h" | 5 #include "chrome/browser/sessions/session_backend.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
| 9 #include "base/command_line.h" |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
10 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/string_number_conversions.h" |
12 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 19 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/browser_thread.h" |
13 #include "net/base/file_stream.h" | 21 #include "net/base/file_stream.h" |
14 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
15 | 23 |
16 using base::TimeTicks; | 24 using base::TimeTicks; |
17 | 25 |
18 // File version number. | 26 // File version number. |
19 static const int32 kFileCurrentVersion = 1; | 27 static const int32 kFileCurrentVersion = 1; |
20 | 28 |
21 // The signature at the beginning of the file = SSNS (Sessions). | 29 // The signature at the beginning of the file = SSNS (Sessions). |
22 static const int32 kFileSignature = 0x53534E53; | 30 static const int32 kFileSignature = 0x53534E53; |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 268 } |
261 | 269 |
262 void SessionBackend::DeleteLastSession() { | 270 void SessionBackend::DeleteLastSession() { |
263 Init(); | 271 Init(); |
264 file_util::Delete(GetLastSessionPath(), false); | 272 file_util::Delete(GetLastSessionPath(), false); |
265 } | 273 } |
266 | 274 |
267 void SessionBackend::MoveCurrentSessionToLastSession() { | 275 void SessionBackend::MoveCurrentSessionToLastSession() { |
268 Init(); | 276 Init(); |
269 current_session_file_.reset(NULL); | 277 current_session_file_.reset(NULL); |
270 | |
271 const FilePath current_session_path = GetCurrentSessionPath(); | 278 const FilePath current_session_path = GetCurrentSessionPath(); |
272 const FilePath last_session_path = GetLastSessionPath(); | 279 const FilePath last_session_path = GetLastSessionPath(); |
| 280 if (type_ == BaseSessionService::SESSION_RESTORE) { |
| 281 // TODO(marja): This is only for debugging session restore failures; remove |
| 282 // when debugging is done. |
| 283 content::BrowserThread::PostTask( |
| 284 content::BrowserThread::UI, FROM_HERE, |
| 285 base::Bind(&SessionBackend::WriteDebugData, this)); |
| 286 } |
273 if (file_util::PathExists(last_session_path)) | 287 if (file_util::PathExists(last_session_path)) |
274 file_util::Delete(last_session_path, false); | 288 file_util::Delete(last_session_path, false); |
275 if (file_util::PathExists(current_session_path)) { | 289 if (file_util::PathExists(current_session_path)) { |
276 int64 file_size; | 290 int64 file_size; |
277 if (file_util::GetFileSize(current_session_path, &file_size)) { | 291 if (file_util::GetFileSize(current_session_path, &file_size)) { |
278 if (type_ == BaseSessionService::TAB_RESTORE) { | 292 if (type_ == BaseSessionService::TAB_RESTORE) { |
279 UMA_HISTOGRAM_COUNTS("TabRestore.last_session_file_size", | 293 UMA_HISTOGRAM_COUNTS("TabRestore.last_session_file_size", |
280 static_cast<int>(file_size / 1024)); | 294 static_cast<int>(file_size / 1024)); |
281 } else { | 295 } else { |
282 UMA_HISTOGRAM_COUNTS("SessionRestore.last_session_file_size", | 296 UMA_HISTOGRAM_COUNTS("SessionRestore.last_session_file_size", |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 } | 412 } |
399 | 413 |
400 FilePath SessionBackend::GetCurrentSessionPath() { | 414 FilePath SessionBackend::GetCurrentSessionPath() { |
401 FilePath path = path_to_dir_; | 415 FilePath path = path_to_dir_; |
402 if (type_ == BaseSessionService::TAB_RESTORE) | 416 if (type_ == BaseSessionService::TAB_RESTORE) |
403 path = path.AppendASCII(kCurrentTabSessionFileName); | 417 path = path.AppendASCII(kCurrentTabSessionFileName); |
404 else | 418 else |
405 path = path.AppendASCII(kCurrentSessionFileName); | 419 path = path.AppendASCII(kCurrentSessionFileName); |
406 return path; | 420 return path; |
407 } | 421 } |
| 422 |
| 423 void SessionBackend::WriteDebugData() { |
| 424 PrefService* prefs = g_browser_process->local_state(); |
| 425 if (!prefs) |
| 426 return; |
| 427 ListPrefUpdate update(prefs, prefs::kSessionRestoreFilesCycled); |
| 428 ListValue* list = update.Get(); |
| 429 while (list->GetSize() > 10) |
| 430 list->Remove(0, NULL); |
| 431 char time_string[32]; |
| 432 time_t now = base::Time::Now().ToTimeT(); |
| 433 strftime(time_string, 32, "%d.%m.%Y %H:%M:%S", localtime(&now)); |
| 434 list->Append(new StringValue( |
| 435 #if defined(OS_POSIX) |
| 436 base::IntToString(getpid()) + " " + |
| 437 #endif |
| 438 std::string(time_string) + " " + |
| 439 #if defined(OS_WIN) |
| 440 WideToUTF8(path_to_dir_.BaseName().value()) + " " + |
| 441 WideToUTF8(CommandLine::ForCurrentProcess()->GetCommandLineString()))); |
| 442 #else |
| 443 path_to_dir_.BaseName().value() + " " + |
| 444 CommandLine::ForCurrentProcess()->GetCommandLineString())); |
| 445 #endif |
| 446 } |
OLD | NEW |