OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/sdch/sdch_owner.h" | 5 #include "net/sdch/sdch_owner.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/memory_coordinator_client_registry.h" |
13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
14 #include "base/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/time/default_clock.h" | 17 #include "base/time/default_clock.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
18 #include "net/base/sdch_manager.h" | 19 #include "net/base/sdch_manager.h" |
19 #include "net/base/sdch_net_log_params.h" | 20 #include "net/base/sdch_net_log_params.h" |
20 #include "net/log/net_log_event_type.h" | 21 #include "net/log/net_log_event_type.h" |
21 | 22 |
22 namespace net { | 23 namespace net { |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 // Because |memory_pressure_listener_| is owned by | 304 // Because |memory_pressure_listener_| is owned by |
304 // SdchOwner, the SdchOwner object will be available | 305 // SdchOwner, the SdchOwner object will be available |
305 // for the lifetime of |memory_pressure_listener_|. | 306 // for the lifetime of |memory_pressure_listener_|. |
306 base::Unretained(this))), | 307 base::Unretained(this))), |
307 in_memory_pref_store_(new ValueMapPrefStorage()), | 308 in_memory_pref_store_(new ValueMapPrefStorage()), |
308 external_pref_store_(nullptr), | 309 external_pref_store_(nullptr), |
309 pref_store_(in_memory_pref_store_.get()), | 310 pref_store_(in_memory_pref_store_.get()), |
310 creation_time_(clock_->Now()) { | 311 creation_time_(clock_->Now()) { |
311 manager_->AddObserver(this); | 312 manager_->AddObserver(this); |
312 InitializePrefStore(pref_store_); | 313 InitializePrefStore(pref_store_); |
| 314 base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this); |
313 } | 315 } |
314 | 316 |
315 SdchOwner::~SdchOwner() { | 317 SdchOwner::~SdchOwner() { |
316 for (DictionaryPreferenceIterator it(pref_store_); !it.IsAtEnd(); | 318 for (DictionaryPreferenceIterator it(pref_store_); !it.IsAtEnd(); |
317 it.Advance()) { | 319 it.Advance()) { |
318 int new_uses = it.use_count() - use_counts_at_load_[it.server_hash()]; | 320 int new_uses = it.use_count() - use_counts_at_load_[it.server_hash()]; |
319 DictionaryFate fate = IsPersistingDictionaries() ? | 321 DictionaryFate fate = IsPersistingDictionaries() ? |
320 DICTIONARY_FATE_UNLOAD_FOR_DESTRUCTION : | 322 DICTIONARY_FATE_UNLOAD_FOR_DESTRUCTION : |
321 DICTIONARY_FATE_EVICT_FOR_DESTRUCTION; | 323 DICTIONARY_FATE_EVICT_FOR_DESTRUCTION; |
322 RecordDictionaryEvictionOrUnload(it.server_hash(), it.size(), new_uses, | 324 RecordDictionaryEvictionOrUnload(it.server_hash(), it.size(), new_uses, |
323 fate); | 325 fate); |
324 } | 326 } |
325 manager_->RemoveObserver(this); | 327 manager_->RemoveObserver(this); |
326 | 328 |
327 // This object only observes the external store during loading, | 329 // This object only observes the external store during loading, |
328 // i.e. before it's made the default preferences store. | 330 // i.e. before it's made the default preferences store. |
329 if (external_pref_store_ && pref_store_ != external_pref_store_.get()) | 331 if (external_pref_store_ && pref_store_ != external_pref_store_.get()) |
330 external_pref_store_->StopObservingInit(); | 332 external_pref_store_->StopObservingInit(); |
331 | 333 |
332 int64_t object_lifetime = (clock_->Now() - creation_time_).InMilliseconds(); | 334 int64_t object_lifetime = (clock_->Now() - creation_time_).InMilliseconds(); |
333 for (const auto& val : consumed_byte_seconds_) { | 335 for (const auto& val : consumed_byte_seconds_) { |
334 if (object_lifetime > 0) { | 336 if (object_lifetime > 0) { |
335 // Objects that are created and immediately destroyed don't add any memory | 337 // Objects that are created and immediately destroyed don't add any memory |
336 // pressure over time (and also cause a crash here). | 338 // pressure over time (and also cause a crash here). |
337 UMA_HISTOGRAM_MEMORY_KB("Sdch3.TimeWeightedMemoryUse", | 339 UMA_HISTOGRAM_MEMORY_KB("Sdch3.TimeWeightedMemoryUse", |
338 val / object_lifetime); | 340 val / object_lifetime); |
339 } | 341 } |
340 } | 342 } |
| 343 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); |
341 } | 344 } |
342 | 345 |
343 void SdchOwner::EnablePersistentStorage( | 346 void SdchOwner::EnablePersistentStorage( |
344 std::unique_ptr<PrefStorage> pref_store) { | 347 std::unique_ptr<PrefStorage> pref_store) { |
345 DCHECK(!external_pref_store_); | 348 DCHECK(!external_pref_store_); |
346 DCHECK(pref_store); | 349 DCHECK(pref_store); |
347 external_pref_store_ = std::move(pref_store); | 350 external_pref_store_ = std::move(pref_store); |
348 external_pref_store_->StartObservingInit(this); | 351 external_pref_store_->StartObservingInit(this); |
349 | 352 |
350 if (external_pref_store_->IsInitializationComplete()) | 353 if (external_pref_store_->IsInitializationComplete()) |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
666 } | 669 } |
667 | 670 |
668 void SdchOwner::SetFetcherForTesting( | 671 void SdchOwner::SetFetcherForTesting( |
669 std::unique_ptr<SdchDictionaryFetcher> fetcher) { | 672 std::unique_ptr<SdchDictionaryFetcher> fetcher) { |
670 fetcher_ = std::move(fetcher); | 673 fetcher_ = std::move(fetcher); |
671 } | 674 } |
672 | 675 |
673 void SdchOwner::OnMemoryPressure( | 676 void SdchOwner::OnMemoryPressure( |
674 base::MemoryPressureListener::MemoryPressureLevel level) { | 677 base::MemoryPressureListener::MemoryPressureLevel level) { |
675 DCHECK_NE(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, level); | 678 DCHECK_NE(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, level); |
| 679 ClearData(); |
| 680 } |
676 | 681 |
| 682 void SdchOwner::OnMemoryStateChange(base::MemoryState state) { |
| 683 // TODO(hajimehoshi): When the state changes, adjust the sizes of the caches |
| 684 // to reduce the limits. SdchOwner doesn't have the ability to limit at |
| 685 // present. |
| 686 switch (state) { |
| 687 case base::MemoryState::NORMAL: |
| 688 break; |
| 689 case base::MemoryState::THROTTLED: |
| 690 ClearData(); |
| 691 break; |
| 692 case base::MemoryState::SUSPENDED: |
| 693 // Note: Not supported at present. Fall through. |
| 694 case base::MemoryState::UNKNOWN: |
| 695 NOTREACHED(); |
| 696 break; |
| 697 } |
| 698 } |
| 699 |
| 700 void SdchOwner::ClearData() { |
677 for (DictionaryPreferenceIterator it(pref_store_); !it.IsAtEnd(); | 701 for (DictionaryPreferenceIterator it(pref_store_); !it.IsAtEnd(); |
678 it.Advance()) { | 702 it.Advance()) { |
679 int new_uses = it.use_count() - use_counts_at_load_[it.server_hash()]; | 703 int new_uses = it.use_count() - use_counts_at_load_[it.server_hash()]; |
680 RecordDictionaryEvictionOrUnload(it.server_hash(), | 704 RecordDictionaryEvictionOrUnload(it.server_hash(), |
681 it.size(), | 705 it.size(), |
682 new_uses, | 706 new_uses, |
683 DICTIONARY_FATE_EVICT_FOR_MEMORY); | 707 DICTIONARY_FATE_EVICT_FOR_MEMORY); |
684 } | 708 } |
685 | 709 |
686 // TODO(rdsmith): Make a distinction between moderate and critical | 710 // TODO(rdsmith): Make a distinction between moderate and critical |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 } | 763 } |
740 | 764 |
741 return true; | 765 return true; |
742 } | 766 } |
743 | 767 |
744 bool SdchOwner::IsPersistingDictionaries() const { | 768 bool SdchOwner::IsPersistingDictionaries() const { |
745 return in_memory_pref_store_.get() != nullptr; | 769 return in_memory_pref_store_.get() != nullptr; |
746 } | 770 } |
747 | 771 |
748 } // namespace net | 772 } // namespace net |
OLD | NEW |