Chromium Code Reviews| Index: net/disk_cache/simple/simple_index.cc |
| diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc |
| index d7d6408cadff790bb71453c48dc2a5feecbdedf9..1a69347fe804120180d92fbfbf7f9b5de9b3ca54 100644 |
| --- a/net/disk_cache/simple/simple_index.cc |
| +++ b/net/disk_cache/simple/simple_index.cc |
| @@ -32,10 +32,8 @@ namespace { |
| // How many seconds we delay writing the index to disk since the last cache |
| // operation has happened. |
| -const int kWriteToDiskDelaySecs = 20; |
| - |
| -// WriteToDisk at lest every 5 minutes. |
| -const int kMaxWriteToDiskDelaySecs = 300; |
| +const int kWriteToDiskDelayMSecs = 20000; |
| +const int kWriteToDiskOnBackgroundDelayMSecs = 100; |
| // Divides the cache space into this amount of parts to evict when only one part |
| // is left. |
| @@ -157,8 +155,12 @@ void EntryMetadata::MergeWith(const EntryMetadata& from) { |
| SimpleIndex::SimpleIndex( |
| base::SingleThreadTaskRunner* cache_thread, |
| base::SingleThreadTaskRunner* io_thread, |
| - const base::FilePath& path) |
| - : cache_size_(0), |
| + const base::FilePath& path) : |
| +#if defined(OS_ANDROID) |
| + activity_status_listener_( |
| + base::Bind(&SimpleIndex::OnActivityStateChange, AsWeakPtr())), |
|
pasko-google - do not use
2013/05/03 10:30:54
this operation feels a bit too non-trivial for a c
gavinp
2013/05/03 12:16:53
Done.
|
| +#endif |
| + cache_size_(0), |
| max_size_(0), |
| high_watermark_(0), |
| low_watermark_(0), |
| @@ -166,7 +168,8 @@ SimpleIndex::SimpleIndex( |
| initialized_(false), |
| index_filename_(path.AppendASCII("the-real-index")), |
| cache_thread_(cache_thread), |
| - io_thread_(io_thread) { |
| + io_thread_(io_thread), |
| + app_on_background_(false) { |
| } |
| SimpleIndex::~SimpleIndex() { |
| @@ -374,19 +377,12 @@ void SimpleIndex::InsertInEntrySet( |
| void SimpleIndex::PostponeWritingToDisk() { |
| if (!initialized_) |
| return; |
| - const base::TimeDelta file_age = base::Time::Now() - last_write_to_disk_; |
| - if (file_age > base::TimeDelta::FromSeconds(kMaxWriteToDiskDelaySecs) && |
| - write_to_disk_timer_.IsRunning()) { |
| - // If the index file is too old and there is a timer programmed to run a |
| - // WriteToDisk soon, we don't postpone it, so we always WriteToDisk |
| - // approximately every kMaxWriteToDiskDelaySecs. |
| - return; |
| - } |
| - |
| + const int delay = app_on_background_ ? kWriteToDiskOnBackgroundDelayMSecs |
| + : kWriteToDiskDelayMSecs; |
| // If the timer is already active, Start() will just Reset it, postponing it. |
| write_to_disk_timer_.Start( |
| FROM_HERE, |
| - base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), |
| + base::TimeDelta::FromMilliseconds(delay), |
|
pasko-google - do not use
2013/05/03 10:30:54
So at maximum it can write the index every 100ms,
gavinp
2013/05/03 12:16:53
I think in a followup; we currently measure index
|
| base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); |
| } |
| @@ -546,7 +542,6 @@ void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, |
| cache_size_ += it->second.GetEntrySize(); |
| } |
| } |
| - last_write_to_disk_ = base::Time::Now(); |
| initialized_ = true; |
| removed_entries_.clear(); |
| @@ -565,6 +560,22 @@ void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, |
| to_run_when_initialized_.clear(); |
| } |
| +#if defined(OS_ANDROID) |
| +void SimpleIndex::OnActivityStateChange( |
| + base::android::ActivityState state) { |
| + DCHECK(io_thread_checker_.CalledOnValidThread()); |
| + // For more info about android activities, see: |
| + // developer.android.com/training/basics/activity-lifecycle/pausing.html |
| + // These values are defined in the file ActivityStatus.java |
| + if (state == base::android::ACTIVITY_STATE_RESUMED) { |
| + app_on_background_ = false; |
| + } else if (state == base::android::ACTIVITY_STATE_STOPPED) { |
|
felipeg
2013/05/03 10:03:00
I know Philippe told you that STATE_STOPPED is eno
Philippe
2013/05/03 10:11:47
STOPPED is supposed to be enough. Its support was
|
| + app_on_background_ = true; |
| + WriteToDisk(); |
| + } |
| +} |
| +#endif |
| + |
| void SimpleIndex::WriteToDisk() { |
| DCHECK(io_thread_checker_.CalledOnValidThread()); |
| if (!initialized_) |