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 02c1453ad4742684562194c157fbd98667a917b1..f70676a8d6fdf3d1ea2c37858a7d06ff6b19d94f 100644 |
--- a/net/disk_cache/simple/simple_index.cc |
+++ b/net/disk_cache/simple/simple_index.cc |
@@ -23,10 +23,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. |
gavinp
2013/04/19 15:58:55
Did this really land without the spelling correcti
felipeg
2013/04/19 17:10:15
Done.
|
-const int kMaxWriteToDiskDelaySecs = 300; |
gavinp
2013/04/19 15:58:55
Yeah, I guess we can lose this if we don't expect
felipeg
2013/04/19 17:10:15
Done.
|
+const int kWriteToDiskDelay_ms = 20000; |
+const int kWriteToDiskOnBackgroundDelay_ms = 100; |
} // namespace |
@@ -86,7 +84,13 @@ SimpleIndex::SimpleIndex( |
initialized_(false), |
index_filename_(path.AppendASCII("simple-index")), |
cache_thread_(cache_thread), |
- io_thread_(io_thread) { |
+ io_thread_(io_thread), |
+#if defined(OS_ANDROID) |
+ activity_status_notifier_( |
+ io_thread, |
+ base::Bind(&SimpleIndex::ActivityStatusChanged, AsWeakPtr())), |
+#endif |
+ app_on_background_(false) { |
} |
SimpleIndex::~SimpleIndex() { |
@@ -214,19 +218,19 @@ void SimpleIndex::InsertInEntrySet( |
} |
void SimpleIndex::PostponeWritingToDisk() { |
- 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. |
+ if (!initialized_) |
return; |
+ int delay = kWriteToDiskDelay_ms; |
+ if (app_on_background_) { |
+ // When the app is in the background we can afford to write the index much |
gavinp
2013/04/19 15:58:55
"can afford" or "do" ? We're just writing it more
felipeg
2013/04/19 17:10:15
Done.
|
+ // more frequently. We could even write it to disk on every operation if we |
+ // wanted to. |
+ delay = kWriteToDiskOnBackgroundDelay_ms; |
} |
- |
// 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), |
base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); |
} |
@@ -257,8 +261,6 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk( |
const base::FilePath& index_filename) { |
using file_util::FileEnumerator; |
LOG(INFO) << "Simple Cache Index is being restored from disk."; |
- |
- file_util::Delete(index_filename, /* recursive = */ false); |
gavinp
2013/04/19 15:58:55
The changes in here seem unrelated.
felipeg
2013/04/19 17:10:15
I realized we should not delete the file, this jus
|
scoped_ptr<EntrySet> index_file_entries(new EntrySet()); |
// TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. |
@@ -284,7 +286,6 @@ scoped_ptr<SimpleIndex::EntrySet> SimpleIndex::RestoreFromDisk( |
hash_key_string, &hash_key)) { |
LOG(WARNING) << "Invalid Entry Hash Key filename while restoring " |
<< "Simple Index from disk: " << hash_name; |
- // TODO(felipeg): Should we delete the invalid file here ? |
continue; |
} |
@@ -346,7 +347,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(); |
@@ -363,11 +363,25 @@ void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, |
to_run_when_initialized_.clear(); |
} |
+void SimpleIndex::ActivityStatusChanged(int activity_status) { |
+ DCHECK(io_thread_checker_.CalledOnValidThread()); |
+ // These values are defined in the file ActivityStatus.java |
+ if (activity_status == 3 /* RESUMED */) { |
+ app_on_background_ = false; |
+ } else if (activity_status == 4 /* PAUSED */) { |
+ app_on_background_ = true; |
+ WriteToDisk(); |
+ } else if (activity_status == 5 /* STOPPED */) { |
+ WriteToDisk(); |
+ } else if (activity_status == 6 /* DESTROYED */) { |
+ WriteToDisk(); |
+ } |
+} |
+ |
void SimpleIndex::WriteToDisk() { |
DCHECK(io_thread_checker_.CalledOnValidThread()); |
if (!initialized_) |
return; |
- last_write_to_disk_ = base::Time::Now(); |
SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), |
cache_size_); |
scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, |