diff options
-rw-r--r-- | net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java | 72 | ||||
-rw-r--r-- | net/android/net_jni_registrar.cc | 5 | ||||
-rw-r--r-- | net/android/simple_cache_activity_status_notifier.cc | 54 | ||||
-rw-r--r-- | net/android/simple_cache_activity_status_notifier.h | 62 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index.cc | 51 | ||||
-rw-r--r-- | net/disk_cache/simple/simple_index.h | 18 | ||||
-rw-r--r-- | net/net.gyp | 3 |
7 files changed, 24 insertions, 241 deletions
diff --git a/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java b/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java deleted file mode 100644 index c9882fd..0000000 --- a/net/android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -package org.chromium.net; - -import android.util.Log; - -import org.chromium.base.ActivityStatus; -import org.chromium.base.CalledByNative; -import org.chromium.base.JNINamespace; -import org.chromium.base.NativeClassQualifiedName; - -import android.os.Handler; -import android.os.Looper; - -/** - * Used by the SimpleIndex in net/disk_cache/simple/ to listen to changes in the android app state - * such as the app going to the background or foreground. - */ -public class SimpleCacheActivityStatusNotifier implements ActivityStatus.StateListener { - private int mNativePtr = 0; - private final Looper mIoLooper; - - @CalledByNative - public static SimpleCacheActivityStatusNotifier newInstance(int nativePtr) { - return new SimpleCacheActivityStatusNotifier(nativePtr); - } - - private SimpleCacheActivityStatusNotifier(int nativePtr) { - this.mIoLooper = Looper.myLooper(); - this.mNativePtr = nativePtr; - // Call the singleton's ActivityStatus in the UI thread. - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { - ActivityStatus.registerStateListener(SimpleCacheActivityStatusNotifier.this); - } - }); - } - - @CalledByNative - public void prepareToBeDestroyed() { - this.mNativePtr = 0; - // Call the singleton's ActivityStatus in the UI thread. - new Handler(Looper.getMainLooper()).post(new Runnable() { - @Override - public void run() { - ActivityStatus.unregisterStateListener(SimpleCacheActivityStatusNotifier.this); - } - }); - } - - // ActivityStatus.StateListener - @Override - public void onActivityStateChange(final int state) { - if (state == ActivityStatus.RESUMED || - state == ActivityStatus.STOPPED) { - new Handler(mIoLooper).post(new Runnable() { - @Override - public void run() { - if (SimpleCacheActivityStatusNotifier.this.mNativePtr != 0) - nativeNotifyActivityStatusChanged( - SimpleCacheActivityStatusNotifier.this.mNativePtr, state); - } - }); - } - } - - @NativeClassQualifiedName("net::SimpleCacheActivityStatusNotifier") - private native void nativeNotifyActivityStatusChanged(int nativePtr, int newActivityStatus); -} diff --git a/net/android/net_jni_registrar.cc b/net/android/net_jni_registrar.cc index 39ab899..a6e09b6 100644 --- a/net/android/net_jni_registrar.cc +++ b/net/android/net_jni_registrar.cc @@ -4,14 +4,13 @@ #include "net/android/net_jni_registrar.h" +#include "base/basictypes.h" #include "base/android/jni_android.h" #include "base/android/jni_registrar.h" -#include "base/basictypes.h" #include "net/android/gurl_utils.h" #include "net/android/keystore.h" #include "net/android/network_change_notifier_android.h" #include "net/android/network_library.h" -#include "net/android/simple_cache_activity_status_notifier.h" #include "net/proxy/proxy_config_service_android.h" namespace net { @@ -24,8 +23,6 @@ static base::android::RegistrationMethod kNetRegisteredMethods[] = { { "NetworkChangeNotifierAndroid", net::NetworkChangeNotifierAndroid::Register }, { "ProxyConfigService", net::ProxyConfigServiceAndroid::Register }, - { "SimpleCacheActivityStatusNotifier", - net::SimpleCacheActivityStatusNotifier::Register }, }; bool RegisterJni(JNIEnv* env) { diff --git a/net/android/simple_cache_activity_status_notifier.cc b/net/android/simple_cache_activity_status_notifier.cc deleted file mode 100644 index a13400c..0000000 --- a/net/android/simple_cache_activity_status_notifier.cc +++ /dev/null @@ -1,54 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "net/android/simple_cache_activity_status_notifier.h" - -#include "base/android/jni_android.h" -#include "base/basictypes.h" -#include "base/bind.h" -#include "base/bind_helpers.h" -#include "base/callback.h" -#include "base/logging.h" -#include "base/message_loop.h" -#include "base/message_loop_proxy.h" -#include "base/task_runner.h" -#include "jni/SimpleCacheActivityStatusNotifier_jni.h" - -namespace net { - -SimpleCacheActivityStatusNotifier::SimpleCacheActivityStatusNotifier( - const ActivityStatusChangedCallback& notify_callback) - : notify_callback_(notify_callback) { - JNIEnv* env = base::android::AttachCurrentThread(); - DCHECK(env); - java_obj_.Reset( - Java_SimpleCacheActivityStatusNotifier_newInstance( - env, reinterpret_cast<jint>(this))); -} - -SimpleCacheActivityStatusNotifier::~SimpleCacheActivityStatusNotifier() { - DCHECK(thread_checker_.CalledOnValidThread()); - JNIEnv* env = base::android::AttachCurrentThread(); - DCHECK(env); - Java_SimpleCacheActivityStatusNotifier_prepareToBeDestroyed( - env, java_obj_.obj()); -} - -void SimpleCacheActivityStatusNotifier::NotifyActivityStatusChanged( - JNIEnv* env, - jobject obj, - jint j_new_activity_status) { - DCHECK(thread_checker_.CalledOnValidThread()); - ActivityStatus new_activity_status = - static_cast<ActivityStatus>(j_new_activity_status); - DCHECK(!notify_callback_.is_null()); - notify_callback_.Run(new_activity_status); -} - -// static -bool SimpleCacheActivityStatusNotifier::Register(JNIEnv* env) { - return RegisterNativesImpl(env); -} - -} // namespace net diff --git a/net/android/simple_cache_activity_status_notifier.h b/net/android/simple_cache_activity_status_notifier.h deleted file mode 100644 index 023c112..0000000 --- a/net/android/simple_cache_activity_status_notifier.h +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef NET_ANDROID_SIMPLE_CACHE_ACTIVITY_STATUS_NOTIFIER_H_ -#define NET_ANDROID_SIMPLE_CACHE_ACTIVITY_STATUS_NOTIFIER_H_ - -#include <jni.h> - -#include "base/android/jni_android.h" -#include "base/basictypes.h" -#include "base/callback.h" -#include "base/memory/ref_counted.h" -#include "base/threading/thread_checker.h" - -namespace base { -class SingleThreadTaskRunner; -} - -namespace net { - -// This class is the native twin of the class with same name in -// SimpleCacheActivityStatusNotifier.java -// This is used by the SimpleIndex in net/disk_cache/simple/ to listens to -// changes in the android app state such as the app going to the background or -// foreground. -class SimpleCacheActivityStatusNotifier { -public: - // This enum must match the constants defined in - // ./base/android/java/src/org/chromium/base/ActivityStatus.java - enum ActivityStatus { - CREATED = 1, - STARTED = 2, - RESUMED = 3, - PAUSED = 4, - STOPPED = 5, - DESTROYED = 6 - }; - - typedef base::Callback<void(ActivityStatus activity_status)> - ActivityStatusChangedCallback; - - SimpleCacheActivityStatusNotifier( - const ActivityStatusChangedCallback& notify_callback); - - ~SimpleCacheActivityStatusNotifier(); - - void NotifyActivityStatusChanged(JNIEnv* env, - jobject obj, - jint new_activity_status); - - static bool Register(JNIEnv* env); - -private: - base::android::ScopedJavaGlobalRef<jobject> java_obj_; - const ActivityStatusChangedCallback notify_callback_; - base::ThreadChecker thread_checker_; -}; - -} // namespace net - -#endif // NET_ANDROID_SIMPLE_CACHE_ACTIVITY_STATUS_NOTIFIER_H_ diff --git a/net/disk_cache/simple/simple_index.cc b/net/disk_cache/simple/simple_index.cc index 4be61e8..d7d6408 100644 --- a/net/disk_cache/simple/simple_index.cc +++ b/net/disk_cache/simple/simple_index.cc @@ -32,8 +32,10 @@ namespace { // How many seconds we delay writing the index to disk since the last cache // operation has happened. -const int kWriteToDiskDelayMSecs = 20000; -const int kWriteToDiskOnBackgroundDelayMSecs = 100; +const int kWriteToDiskDelaySecs = 20; + +// WriteToDisk at lest every 5 minutes. +const int kMaxWriteToDiskDelaySecs = 300; // Divides the cache space into this amount of parts to evict when only one part // is left. @@ -164,12 +166,7 @@ SimpleIndex::SimpleIndex( initialized_(false), index_filename_(path.AppendASCII("the-real-index")), cache_thread_(cache_thread), - io_thread_(io_thread), -#if defined(OS_ANDROID) - activity_status_notifier_( - base::Bind(&SimpleIndex::ActivityStatusChanged, AsWeakPtr())), -#endif - app_on_background_(false) { + io_thread_(io_thread) { } SimpleIndex::~SimpleIndex() { @@ -377,17 +374,19 @@ void SimpleIndex::InsertInEntrySet( void SimpleIndex::PostponeWritingToDisk() { if (!initialized_) return; - int delay = kWriteToDiskDelayMSecs; - if (app_on_background_) { - // When the app is in the background we can write the index much more - // frequently. We could even write it to disk on every operation if we - // wanted to. - delay = kWriteToDiskOnBackgroundDelayMSecs; + 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; } + // If the timer is already active, Start() will just Reset it, postponing it. write_to_disk_timer_.Start( FROM_HERE, - base::TimeDelta::FromMilliseconds(delay), + base::TimeDelta::FromSeconds(kWriteToDiskDelaySecs), base::Bind(&SimpleIndex::WriteToDisk, AsWeakPtr())); } @@ -453,6 +452,8 @@ 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); scoped_ptr<EntrySet> index_file_entries(new EntrySet()); // TODO(felipeg,gavinp): Fix this once we have a one-file per entry format. @@ -478,6 +479,7 @@ 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; } @@ -544,6 +546,7 @@ 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(); @@ -562,23 +565,6 @@ void SimpleIndex::MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, to_run_when_initialized_.clear(); } -#if defined(OS_ANDROID) -void SimpleIndex::ActivityStatusChanged( - net::SimpleCacheActivityStatusNotifier::ActivityStatus activity_status) { - 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 (activity_status == net::SimpleCacheActivityStatusNotifier::RESUMED) { - app_on_background_ = false; - } else if (activity_status == - net::SimpleCacheActivityStatusNotifier::STOPPED) { - app_on_background_ = true; - WriteToDisk(); - } -} -#endif - void SimpleIndex::WriteToDisk() { DCHECK(io_thread_checker_.CalledOnValidThread()); if (!initialized_) @@ -586,6 +572,7 @@ void SimpleIndex::WriteToDisk() { UMA_HISTOGRAM_CUSTOM_COUNTS("SimpleCache.IndexNumEntriesOnWrite", entries_set_.size(), 0, 100000, 50); const base::TimeTicks start = base::TimeTicks::Now(); + last_write_to_disk_ = base::Time::Now(); SimpleIndexFile::IndexMetadata index_metadata(entries_set_.size(), cache_size_); scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(index_metadata, diff --git a/net/disk_cache/simple/simple_index.h b/net/disk_cache/simple/simple_index.h index e8205ef..e684be9 100644 --- a/net/disk_cache/simple/simple_index.h +++ b/net/disk_cache/simple/simple_index.h @@ -23,10 +23,6 @@ #include "net/base/completion_callback.h" #include "net/base/net_export.h" -#if defined(OS_ANDROID) -#include "net/android/simple_cache_activity_status_notifier.h" -#endif - class Pickle; class PickleIterator; @@ -158,11 +154,6 @@ class NET_EXPORT_PRIVATE SimpleIndex void MergeInitializingSet(scoped_ptr<EntrySet> index_file_entries, bool force_index_flush); -#if defined(OS_ANDROID) - void ActivityStatusChanged( - net::SimpleCacheActivityStatusNotifier::ActivityStatus activity_status); -#endif - EntrySet entries_set_; uint64 cache_size_; // Total cache storage size in bytes. uint64 max_size_; @@ -185,15 +176,14 @@ class NET_EXPORT_PRIVATE SimpleIndex // thread, in all cases. |io_thread_checker_| documents and enforces this. base::ThreadChecker io_thread_checker_; + // Timestamp of the last time we wrote the index to disk. + // PostponeWritingToDisk() may give up postponing and allow the write if it + // has been a while since last time we wrote. + base::Time last_write_to_disk_; base::OneShotTimer<SimpleIndex> write_to_disk_timer_; typedef std::list<net::CompletionCallback> CallbackList; CallbackList to_run_when_initialized_; - -#if defined(OS_ANDROID) - net::SimpleCacheActivityStatusNotifier activity_status_notifier_; -#endif - bool app_on_background_; }; } // namespace disk_cache diff --git a/net/net.gyp b/net/net.gyp index 3a78c4d..c319a68 100644 --- a/net/net.gyp +++ b/net/net.gyp @@ -78,8 +78,6 @@ 'android/network_change_notifier_factory_android.h', 'android/network_library.cc', 'android/network_library.h', - 'android/simple_cache_activity_status_notifier.cc', - 'android/simple_cache_activity_status_notifier.h', 'base/address_family.h', 'base/address_list.cc', 'base/address_list.h', @@ -2655,7 +2653,6 @@ 'android/java/src/org/chromium/net/GURLUtils.java', 'android/java/src/org/chromium/net/NetworkChangeNotifier.java', 'android/java/src/org/chromium/net/ProxyChangeListener.java', - 'android/java/src/org/chromium/net/SimpleCacheActivityStatusNotifier.java', ], 'variables': { 'jni_gen_package': 'net', |