| 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 "storage/browser/quota/storage_monitor.h" | 5 #include "storage/browser/quota/storage_monitor.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/trace_event/trace_event.h" |
| 10 #include "net/base/net_util.h" | 11 #include "net/base/net_util.h" |
| 11 #include "storage/browser/quota/quota_manager.h" | 12 #include "storage/browser/quota/quota_manager.h" |
| 12 #include "storage/common/quota/quota_status_code.h" | 13 #include "storage/common/quota/quota_status_code.h" |
| 13 | 14 |
| 14 namespace storage { | 15 namespace storage { |
| 15 | 16 |
| 16 // StorageObserverList: | 17 // StorageObserverList: |
| 17 | 18 |
| 18 StorageObserverList::ObserverState::ObserverState() | 19 StorageObserverList::ObserverState::ObserverState() |
| 19 : requires_update(false) { | 20 : requires_update(false) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 32 | 33 |
| 33 void StorageObserverList::RemoveObserver(StorageObserver* observer) { | 34 void StorageObserverList::RemoveObserver(StorageObserver* observer) { |
| 34 observers_.erase(observer); | 35 observers_.erase(observer); |
| 35 } | 36 } |
| 36 | 37 |
| 37 int StorageObserverList::ObserverCount() const { | 38 int StorageObserverList::ObserverCount() const { |
| 38 return observers_.size(); | 39 return observers_.size(); |
| 39 } | 40 } |
| 40 | 41 |
| 41 void StorageObserverList::OnStorageChange(const StorageObserver::Event& event) { | 42 void StorageObserverList::OnStorageChange(const StorageObserver::Event& event) { |
| 43 // crbug.com/349708 |
| 44 TRACE_EVENT0("io", |
| 45 "HostStorageObserversStorageObserverList::OnStorageChange"); |
| 46 |
| 42 for (StorageObserverStateMap::iterator it = observers_.begin(); | 47 for (StorageObserverStateMap::iterator it = observers_.begin(); |
| 43 it != observers_.end(); ++it) { | 48 it != observers_.end(); ++it) { |
| 44 it->second.requires_update = true; | 49 it->second.requires_update = true; |
| 45 } | 50 } |
| 46 | 51 |
| 47 MaybeDispatchEvent(event); | 52 MaybeDispatchEvent(event); |
| 48 } | 53 } |
| 49 | 54 |
| 50 void StorageObserverList::MaybeDispatchEvent( | 55 void StorageObserverList::MaybeDispatchEvent( |
| 51 const StorageObserver::Event& event) { | 56 const StorageObserver::Event& event) { |
| 57 // crbug.com/349708 |
| 58 TRACE_EVENT0("io", "StorageObserverList::MaybeDispatchEvent"); |
| 59 |
| 52 notification_timer_.Stop(); | 60 notification_timer_.Stop(); |
| 53 base::TimeDelta min_delay = base::TimeDelta::Max(); | 61 base::TimeDelta min_delay = base::TimeDelta::Max(); |
| 54 bool all_observers_notified = true; | 62 bool all_observers_notified = true; |
| 55 | 63 |
| 56 for (StorageObserverStateMap::iterator it = observers_.begin(); | 64 for (StorageObserverStateMap::iterator it = observers_.begin(); |
| 57 it != observers_.end(); ++it) { | 65 it != observers_.end(); ++it) { |
| 58 if (!it->second.requires_update) | 66 if (!it->second.requires_update) |
| 59 continue; | 67 continue; |
| 60 | 68 |
| 61 base::TimeTicks current_time = base::TimeTicks::Now(); | 69 base::TimeTicks current_time = base::TimeTicks::Now(); |
| 62 base::TimeDelta delta = current_time - it->second.last_notification_time; | 70 base::TimeDelta delta = current_time - it->second.last_notification_time; |
| 63 if (it->second.last_notification_time.is_null() || | 71 if (it->second.last_notification_time.is_null() || |
| 64 delta >= it->second.rate) { | 72 delta >= it->second.rate) { |
| 65 it->second.requires_update = false; | 73 it->second.requires_update = false; |
| 66 it->second.last_notification_time = current_time; | 74 it->second.last_notification_time = current_time; |
| 67 | 75 |
| 68 if (it->second.origin == event.filter.origin) { | 76 if (it->second.origin == event.filter.origin) { |
| 77 // crbug.com/349708 |
| 78 TRACE_EVENT0("io", |
| 79 "StorageObserverList::MaybeDispatchEvent OnStorageEvent1"); |
| 80 |
| 69 it->first->OnStorageEvent(event); | 81 it->first->OnStorageEvent(event); |
| 70 } else { | 82 } else { |
| 71 // When the quota and usage of an origin is requested, QuotaManager | 83 // When the quota and usage of an origin is requested, QuotaManager |
| 72 // returns the quota and usage of the host. Multiple origins can map to | 84 // returns the quota and usage of the host. Multiple origins can map to |
| 73 // to the same host, so ensure the |origin| field in the dispatched | 85 // to the same host, so ensure the |origin| field in the dispatched |
| 74 // event matches the |origin| specified by the observer when it was | 86 // event matches the |origin| specified by the observer when it was |
| 75 // registered. | 87 // registered. |
| 76 StorageObserver::Event dispatch_event(event); | 88 StorageObserver::Event dispatch_event(event); |
| 77 dispatch_event.filter.origin = it->second.origin; | 89 dispatch_event.filter.origin = it->second.origin; |
| 90 |
| 91 // crbug.com/349708 |
| 92 TRACE_EVENT0("io", |
| 93 "StorageObserverList::MaybeDispatchEvent OnStorageEvent2"); |
| 94 |
| 78 it->first->OnStorageEvent(dispatch_event); | 95 it->first->OnStorageEvent(dispatch_event); |
| 79 } | 96 } |
| 80 } else { | 97 } else { |
| 81 all_observers_notified = false; | 98 all_observers_notified = false; |
| 82 base::TimeDelta delay = it->second.rate - delta; | 99 base::TimeDelta delay = it->second.rate - delta; |
| 83 if (delay < min_delay) | 100 if (delay < min_delay) |
| 84 min_delay = delay; | 101 min_delay = delay; |
| 85 } | 102 } |
| 86 } | 103 } |
| 87 | 104 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 return; | 193 return; |
| 177 } | 194 } |
| 178 | 195 |
| 179 StartInitialization(filter); | 196 StartInitialization(filter); |
| 180 } | 197 } |
| 181 | 198 |
| 182 void HostStorageObservers::StartInitialization( | 199 void HostStorageObservers::StartInitialization( |
| 183 const StorageObserver::Filter& filter) { | 200 const StorageObserver::Filter& filter) { |
| 184 if (initialized_ || initializing_) | 201 if (initialized_ || initializing_) |
| 185 return; | 202 return; |
| 203 // crbug.com/349708 |
| 204 TRACE_EVENT0("io", "HostStorageObservers::StartInitialization"); |
| 186 | 205 |
| 187 initializing_ = true; | 206 initializing_ = true; |
| 188 quota_manager_->GetUsageAndQuotaForWebApps( | 207 quota_manager_->GetUsageAndQuotaForWebApps( |
| 189 filter.origin, | 208 filter.origin, |
| 190 filter.storage_type, | 209 filter.storage_type, |
| 191 base::Bind(&HostStorageObservers::GotHostUsageAndQuota, | 210 base::Bind(&HostStorageObservers::GotHostUsageAndQuota, |
| 192 weak_factory_.GetWeakPtr(), | 211 weak_factory_.GetWeakPtr(), |
| 193 filter)); | 212 filter)); |
| 194 } | 213 } |
| 195 | 214 |
| 196 void HostStorageObservers::GotHostUsageAndQuota( | 215 void HostStorageObservers::GotHostUsageAndQuota( |
| 197 const StorageObserver::Filter& filter, | 216 const StorageObserver::Filter& filter, |
| 198 QuotaStatusCode status, | 217 QuotaStatusCode status, |
| 199 int64 usage, | 218 int64 usage, |
| 200 int64 quota) { | 219 int64 quota) { |
| 201 initializing_ = false; | 220 initializing_ = false; |
| 202 if (status != kQuotaStatusOk) | 221 if (status != kQuotaStatusOk) |
| 203 return; | 222 return; |
| 204 | |
| 205 initialized_ = true; | 223 initialized_ = true; |
| 206 cached_quota_ = quota; | 224 cached_quota_ = quota; |
| 207 cached_usage_ = usage + usage_deltas_during_init_; | 225 cached_usage_ = usage + usage_deltas_during_init_; |
| 208 DispatchEvent(filter, event_occurred_before_init_); | 226 DispatchEvent(filter, event_occurred_before_init_); |
| 209 } | 227 } |
| 210 | 228 |
| 211 void HostStorageObservers::DispatchEvent( | 229 void HostStorageObservers::DispatchEvent( |
| 212 const StorageObserver::Filter& filter, bool is_update) { | 230 const StorageObserver::Filter& filter, bool is_update) { |
| 213 StorageObserver::Event event(filter, | 231 StorageObserver::Event event(filter, |
| 214 std::max<int64>(cached_usage_, 0), | 232 std::max<int64>(cached_usage_, 0), |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 | 388 |
| 371 StorageTypeObserversMap::iterator it = | 389 StorageTypeObserversMap::iterator it = |
| 372 storage_type_observers_map_.find(filter.storage_type); | 390 storage_type_observers_map_.find(filter.storage_type); |
| 373 if (it == storage_type_observers_map_.end()) | 391 if (it == storage_type_observers_map_.end()) |
| 374 return; | 392 return; |
| 375 | 393 |
| 376 it->second->NotifyUsageChange(filter, delta); | 394 it->second->NotifyUsageChange(filter, delta); |
| 377 } | 395 } |
| 378 | 396 |
| 379 } // namespace storage | 397 } // namespace storage |
| OLD | NEW |