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

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

Issue 42209: Merge 11262 - Fixes possible crash in SessionBackend. I believe what's happen... (Closed) Base URL: svn://chrome-svn.corp.google.com/chrome/branches/169/src/
Patch Set: '' Created 11 years, 9 months 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
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/file_util.h" 9 #include "base/file_util.h"
10 #include "base/histogram.h" 10 #include "base/histogram.h"
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // Create the directory for session info. 206 // Create the directory for session info.
207 file_util::CreateDirectory(path_to_dir_); 207 file_util::CreateDirectory(path_to_dir_);
208 208
209 MoveCurrentSessionToLastSession(); 209 MoveCurrentSessionToLastSession();
210 } 210 }
211 211
212 void SessionBackend::AppendCommands( 212 void SessionBackend::AppendCommands(
213 std::vector<SessionCommand*>* commands, 213 std::vector<SessionCommand*>* commands,
214 bool reset_first) { 214 bool reset_first) {
215 Init(); 215 Init();
216 if ((reset_first && !empty_file_) || !current_session_file_->IsOpen()) 216 // Make sure and check current_session_file_, if opening the file failed
217 // current_session_file_ will be NULL.
218 if ((reset_first && !empty_file_) || !current_session_file_.get() ||
219 !current_session_file_->IsOpen()) {
217 ResetFile(); 220 ResetFile();
218 if (current_session_file_->IsOpen() && 221 }
222 // Need to check current_session_file_ again, ResetFile may fail.
223 if (current_session_file_.get() && current_session_file_->IsOpen() &&
219 !AppendCommandsToFile(current_session_file_.get(), *commands)) { 224 !AppendCommandsToFile(current_session_file_.get(), *commands)) {
220 current_session_file_.reset(NULL); 225 current_session_file_.reset(NULL);
221 } 226 }
222 empty_file_ = false; 227 empty_file_ = false;
223 STLDeleteElements(commands); 228 STLDeleteElements(commands);
224 delete commands; 229 delete commands;
225 } 230 }
226 231
227 void SessionBackend::ReadLastSessionCommands( 232 void SessionBackend::ReadLastSessionCommands(
228 scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) { 233 scoped_refptr<BaseSessionService::InternalGetCommandsRequest> request) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
306 if (wrote != content_size) { 311 if (wrote != content_size) {
307 NOTREACHED() << "error writing"; 312 NOTREACHED() << "error writing";
308 return false; 313 return false;
309 } 314 }
310 } 315 }
311 return true; 316 return true;
312 } 317 }
313 318
314 void SessionBackend::ResetFile() { 319 void SessionBackend::ResetFile() {
315 DCHECK(inited_); 320 DCHECK(inited_);
316 current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath())); 321 if (current_session_file_.get()) {
322 // File is already open, truncate it. We truncate instead of closing and
323 // reopening to avoid the possibility of scanners locking the file out
324 // from under us once we close it. If truncation fails, we'll try to
325 // recreate.
326 if (current_session_file_->Truncate(sizeof_header()) != sizeof_header())
327 current_session_file_.reset(NULL);
328 }
329 if (!current_session_file_.get())
330 current_session_file_.reset(OpenAndWriteHeader(GetCurrentSessionPath()));
317 empty_file_ = true; 331 empty_file_ = true;
318 } 332 }
319 333
320 net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) { 334 net::FileStream* SessionBackend::OpenAndWriteHeader(const FilePath& path) {
321 DCHECK(!path.empty()); 335 DCHECK(!path.empty());
322 net::FileStream* file = new net::FileStream(); 336 net::FileStream* file = new net::FileStream();
323 file->Open(path, base::PLATFORM_FILE_CREATE_ALWAYS | 337 file->Open(path, base::PLATFORM_FILE_CREATE_ALWAYS |
324 base::PLATFORM_FILE_WRITE); 338 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_EXCLUSIVE_WRITE |
339 base::PLATFORM_FILE_EXCLUSIVE_READ);
325 if (!file->IsOpen()) 340 if (!file->IsOpen())
326 return NULL; 341 return NULL;
327 int32 header[2]; 342 int32 header[2];
328 header[0] = kFileSignature; 343 header[0] = kFileSignature;
329 header[1] = kFileCurrentVersion; 344 header[1] = kFileCurrentVersion;
330 int wrote = file->Write(reinterpret_cast<char*>(&header), 345 int wrote = file->Write(reinterpret_cast<char*>(&header),
331 sizeof(header), NULL); 346 sizeof(header), NULL);
332 if (wrote != sizeof(header)) 347 if (wrote != sizeof_header())
333 return NULL; 348 return NULL;
334 return file; 349 return file;
335 } 350 }
336 351
337 FilePath SessionBackend::GetLastSessionPath() { 352 FilePath SessionBackend::GetLastSessionPath() {
338 FilePath path = path_to_dir_; 353 FilePath path = path_to_dir_;
339 if (type_ == BaseSessionService::TAB_RESTORE) 354 if (type_ == BaseSessionService::TAB_RESTORE)
340 path = path.AppendASCII(kLastTabSessionFileName); 355 path = path.AppendASCII(kLastTabSessionFileName);
341 else 356 else
342 path = path.AppendASCII(kLastSessionFileName); 357 path = path.AppendASCII(kLastSessionFileName);
343 return path; 358 return path;
344 } 359 }
345 360
346 FilePath SessionBackend::GetCurrentSessionPath() { 361 FilePath SessionBackend::GetCurrentSessionPath() {
347 FilePath path = path_to_dir_; 362 FilePath path = path_to_dir_;
348 if (type_ == BaseSessionService::TAB_RESTORE) 363 if (type_ == BaseSessionService::TAB_RESTORE)
349 path = path.AppendASCII(kCurrentTabSessionFileName); 364 path = path.AppendASCII(kCurrentTabSessionFileName);
350 else 365 else
351 path = path.AppendASCII(kCurrentSessionFileName); 366 path = path.AppendASCII(kCurrentSessionFileName);
352 return path; 367 return path;
353 } 368 }
OLDNEW
« no previous file with comments | « chrome/browser/sessions/session_backend.h ('k') | chrome/browser/sessions/session_backend_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698