Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/common/important_file_writer.h" | 5 #include "chrome/common/important_file_writer.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/file_path.h" | 11 #include "base/file_path.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/message_loop_proxy.h" | 14 #include "base/message_loop_proxy.h" |
| 15 #include "base/rand_util.h" | |
| 15 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 16 #include "base/task.h" | 17 #include "base/task.h" |
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 18 #include "base/time.h" | 19 #include "base/time.h" |
| 19 | 20 |
| 20 using base::TimeDelta; | |
| 21 | |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 const int kDefaultCommitIntervalMs = 10000; | 23 base::TimeDelta EstimateDesiredCommitInterval() { |
| 24 // 2 seconds interval provides reasonable optimization by coalescing bursts of | |
| 25 // writes yet this value is reasonably low to be safe. | |
| 26 const int kSafeCommitIntervalMs = 2000; | |
| 27 | |
| 28 static int commit_interval = 0; | |
| 29 if (commit_interval == 0) { | |
| 30 commit_interval = kSafeCommitIntervalMs; | |
| 31 | |
| 32 #if !defined(OFFICIAL_BUILD) && !defined(GOOGLE_CHROME_BUILD) | |
| 33 // For debugging purpose we may want to increase commit interval in order to | |
| 34 // ease manifestation of bugs caused by some exit path failing to commit | |
| 35 // pending writes (as in http://crosbug.com/13102). | |
| 36 const int kDebugCommitIntervalMs = 60000; | |
| 37 | |
| 38 // Leave 50% chance to match behaviour of official build. | |
| 39 if (base::RandInt(0, 1)) | |
|
Paweł Hajdan Jr.
2011/03/21 13:27:43
I'm strongly against any randomness in this code.
| |
| 40 commit_interval = kDebugCommitIntervalMs; | |
| 41 #endif | |
| 42 } | |
| 43 return base::TimeDelta::FromMilliseconds(commit_interval); | |
| 44 } | |
| 25 | 45 |
| 26 class WriteToDiskTask : public Task { | 46 class WriteToDiskTask : public Task { |
| 27 public: | 47 public: |
| 28 WriteToDiskTask(const FilePath& path, const std::string& data) | 48 WriteToDiskTask(const FilePath& path, const std::string& data) |
| 29 : path_(path), | 49 : path_(path), |
| 30 data_(data) { | 50 data_(data) { |
| 31 } | 51 } |
| 32 | 52 |
| 33 virtual void Run() { | 53 virtual void Run() { |
| 34 // Write the data to a temp file then rename to avoid data loss if we crash | 54 // Write the data to a temp file then rename to avoid data loss if we crash |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 75 DISALLOW_COPY_AND_ASSIGN(WriteToDiskTask); | 95 DISALLOW_COPY_AND_ASSIGN(WriteToDiskTask); |
| 76 }; | 96 }; |
| 77 | 97 |
| 78 } // namespace | 98 } // namespace |
| 79 | 99 |
| 80 ImportantFileWriter::ImportantFileWriter( | 100 ImportantFileWriter::ImportantFileWriter( |
| 81 const FilePath& path, base::MessageLoopProxy* file_message_loop_proxy) | 101 const FilePath& path, base::MessageLoopProxy* file_message_loop_proxy) |
| 82 : path_(path), | 102 : path_(path), |
| 83 file_message_loop_proxy_(file_message_loop_proxy), | 103 file_message_loop_proxy_(file_message_loop_proxy), |
| 84 serializer_(NULL), | 104 serializer_(NULL), |
| 85 commit_interval_(TimeDelta::FromMilliseconds( | 105 commit_interval_(EstimateDesiredCommitInterval()) { |
| 86 kDefaultCommitIntervalMs)) { | |
| 87 DCHECK(CalledOnValidThread()); | 106 DCHECK(CalledOnValidThread()); |
| 88 DCHECK(file_message_loop_proxy_.get()); | 107 DCHECK(file_message_loop_proxy_.get()); |
| 89 } | 108 } |
| 90 | 109 |
| 91 ImportantFileWriter::~ImportantFileWriter() { | 110 ImportantFileWriter::~ImportantFileWriter() { |
| 92 // We're usually a member variable of some other object, which also tends | 111 // We're usually a member variable of some other object, which also tends |
| 93 // to be our serializer. It may not be safe to call back to the parent object | 112 // to be our serializer. It may not be safe to call back to the parent object |
| 94 // being destructed. | 113 // being destructed. |
| 95 DCHECK(!HasPendingWrite()); | 114 DCHECK(!HasPendingWrite()); |
| 96 } | 115 } |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 140 DCHECK(serializer_); | 159 DCHECK(serializer_); |
| 141 std::string data; | 160 std::string data; |
| 142 if (serializer_->SerializeData(&data)) { | 161 if (serializer_->SerializeData(&data)) { |
| 143 WriteNow(data); | 162 WriteNow(data); |
| 144 } else { | 163 } else { |
| 145 LOG(WARNING) << "failed to serialize data to be saved in " | 164 LOG(WARNING) << "failed to serialize data to be saved in " |
| 146 << path_.value(); | 165 << path_.value(); |
| 147 } | 166 } |
| 148 serializer_ = NULL; | 167 serializer_ = NULL; |
| 149 } | 168 } |
| OLD | NEW |