Chromium Code Reviews| Index: chrome/browser/android/thumbnail/thumbnail_cache.cc |
| diff --git a/chrome/browser/android/thumbnail/thumbnail_cache.cc b/chrome/browser/android/thumbnail/thumbnail_cache.cc |
| index 307bbe382983e3f57b8e360b8947001577c54a96..b5904c7eebe93ee480e1231f05ffa38f32c5e0e6 100644 |
| --- a/chrome/browser/android/thumbnail/thumbnail_cache.cc |
| +++ b/chrome/browser/android/thumbnail/thumbnail_cache.cc |
| @@ -8,6 +8,7 @@ |
| #include <cmath> |
| #include <utility> |
| +#include "base/android/application_status_listener.h" |
| #include "base/android/path_utils.h" |
| #include "base/big_endian.h" |
| #include "base/files/file.h" |
| @@ -131,6 +132,8 @@ ThumbnailCache::ThumbnailCache(size_t default_cache_size, |
| ui_resource_provider_(NULL), |
| weak_factory_(this) { |
| DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + memory_pressure_.reset(new base::MemoryPressureListener( |
| + base::Bind(&ThumbnailCache::OnMemoryPressure, base::Unretained(this)))); |
| } |
| ThumbnailCache::~ThumbnailCache() { |
| @@ -445,8 +448,23 @@ void ThumbnailCache::RemoveFromReadQueue(TabId tab_id) { |
| } |
| void ThumbnailCache::OnUIResourcesWereEvicted() { |
| - cache_.Clear(); |
| - approximation_cache_.Clear(); |
| + TabId last_tab = visible_ids_.empty() ? 0 : visible_ids_.front(); |
|
David Trainor- moved to gerrit
2016/08/16 02:47:29
IIRC think 0 is a valid id. -1?
no sievers
2016/08/26 19:00:20
Done.
|
| + if (!last_tab) { |
| + cache_.Clear(); |
| + approximation_cache_.Clear(); |
| + } else { |
| + std::unique_ptr<Thumbnail> thumbnail = cache_.Remove(last_tab); |
| + cache_.Clear(); |
| + std::unique_ptr<Thumbnail> approximation = |
| + approximation_cache_.Remove(last_tab); |
| + approximation_cache_.Clear(); |
| + |
| + // Keep the thumbnail for app resume if it wasn't uploaded yet. |
| + if (thumbnail.get() && !thumbnail->ui_resource_id()) |
| + cache_.Put(last_tab, std::move(thumbnail)); |
| + if (approximation.get() && !approximation->ui_resource_id()) |
| + approximation_cache_.Put(last_tab, std::move(approximation)); |
| + } |
| } |
| void ThumbnailCache::InvalidateCachedThumbnail(Thumbnail* thumbnail) { |
| @@ -613,7 +631,12 @@ void ThumbnailCache::PostCompressionTask( |
| if (thumbnail->time_stamp() != time_stamp) |
| return; |
| thumbnail->SetCompressedBitmap(compressed_data, content_size); |
| - thumbnail->CreateUIResource(); |
| + // Don't upload the texture if we are being paused/stopped because |
| + // the context will go away anyways. |
| + if (base::android::ApplicationStatusListener::GetState() == |
| + base::android::APPLICATION_STATE_HAS_RUNNING_ACTIVITIES) { |
| + thumbnail->CreateUIResource(); |
| + } |
| } |
| WriteThumbnailIfNecessary(tab_id, std::move(compressed_data), scale, |
| content_size); |
| @@ -909,3 +932,11 @@ std::pair<SkBitmap, float> ThumbnailCache::CreateApproximation( |
| return std::make_pair(dst_bitmap, new_scale * scale); |
| } |
| + |
| +void ThumbnailCache::OnMemoryPressure( |
| + base::MemoryPressureListener::MemoryPressureLevel level) { |
| + if (level == base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) { |
| + cache_.Clear(); |
| + approximation_cache_.Clear(); |
| + } |
| +} |