| 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 "chrome/browser/extensions/extension_storage_monitor.h" | 5 #include "chrome/browser/extensions/extension_storage_monitor.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 } | 115 } |
| 116 | 116 |
| 117 // Register as an observer for the extension's storage events. | 117 // Register as an observer for the extension's storage events. |
| 118 void StartObservingForExtension( | 118 void StartObservingForExtension( |
| 119 scoped_refptr<storage::QuotaManager> quota_manager, | 119 scoped_refptr<storage::QuotaManager> quota_manager, |
| 120 const std::string& extension_id, | 120 const std::string& extension_id, |
| 121 const GURL& site_url, | 121 const GURL& site_url, |
| 122 int64 next_threshold, | 122 int64 next_threshold, |
| 123 const base::TimeDelta& rate, | 123 const base::TimeDelta& rate, |
| 124 bool should_uma) { | 124 bool should_uma) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 DCHECK(quota_manager.get()); | 126 DCHECK(quota_manager.get()); |
| 127 | 127 |
| 128 GURL origin = site_url.GetOrigin(); | 128 GURL origin = site_url.GetOrigin(); |
| 129 StorageState& state = origin_state_map_[origin]; | 129 StorageState& state = origin_state_map_[origin]; |
| 130 state.quota_manager = quota_manager; | 130 state.quota_manager = quota_manager; |
| 131 state.extension_id = extension_id; | 131 state.extension_id = extension_id; |
| 132 state.next_threshold = next_threshold; | 132 state.next_threshold = next_threshold; |
| 133 state.should_uma = should_uma; | 133 state.should_uma = should_uma; |
| 134 | 134 |
| 135 // We always observe persistent storage usage. | 135 // We always observe persistent storage usage. |
| 136 storage::StorageObserver::MonitorParams params( | 136 storage::StorageObserver::MonitorParams params( |
| 137 storage::kStorageTypePersistent, origin, rate, false); | 137 storage::kStorageTypePersistent, origin, rate, false); |
| 138 quota_manager->AddStorageObserver(this, params); | 138 quota_manager->AddStorageObserver(this, params); |
| 139 if (should_uma) { | 139 if (should_uma) { |
| 140 // And if this is for uma, we also observe temporary storage usage. | 140 // And if this is for uma, we also observe temporary storage usage. |
| 141 MonitorParams temporary_params( | 141 MonitorParams temporary_params( |
| 142 storage::kStorageTypeTemporary, origin, rate, false); | 142 storage::kStorageTypeTemporary, origin, rate, false); |
| 143 quota_manager->AddStorageObserver(this, temporary_params); | 143 quota_manager->AddStorageObserver(this, temporary_params); |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 // Updates the threshold for an extension already being monitored. | 147 // Updates the threshold for an extension already being monitored. |
| 148 void UpdateThresholdForExtension(const std::string& extension_id, | 148 void UpdateThresholdForExtension(const std::string& extension_id, |
| 149 int64 next_threshold) { | 149 int64 next_threshold) { |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 | 151 |
| 152 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); | 152 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); |
| 153 it != origin_state_map_.end(); | 153 it != origin_state_map_.end(); |
| 154 ++it) { | 154 ++it) { |
| 155 if (it->second.extension_id == extension_id) { | 155 if (it->second.extension_id == extension_id) { |
| 156 it->second.next_threshold = next_threshold; | 156 it->second.next_threshold = next_threshold; |
| 157 break; | 157 break; |
| 158 } | 158 } |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 | 161 |
| 162 // Deregister as an observer for the extension's storage events. | 162 // Deregister as an observer for the extension's storage events. |
| 163 void StopObservingForExtension(const std::string& extension_id) { | 163 void StopObservingForExtension(const std::string& extension_id) { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 164 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 165 | 165 |
| 166 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); | 166 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); |
| 167 it != origin_state_map_.end(); ) { | 167 it != origin_state_map_.end(); ) { |
| 168 if (it->second.extension_id == extension_id) { | 168 if (it->second.extension_id == extension_id) { |
| 169 storage::StorageObserver::Filter filter( | 169 storage::StorageObserver::Filter filter( |
| 170 storage::kStorageTypePersistent, it->first); | 170 storage::kStorageTypePersistent, it->first); |
| 171 it->second.quota_manager->RemoveStorageObserverForFilter(this, filter); | 171 it->second.quota_manager->RemoveStorageObserverForFilter(this, filter); |
| 172 // We also need to unregister temporary storage observation, if this was | 172 // We also need to unregister temporary storage observation, if this was |
| 173 // being tracked for uma. | 173 // being tracked for uma. |
| 174 if (it->second.should_uma) { | 174 if (it->second.should_uma) { |
| 175 storage::StorageObserver::Filter temporary_filter( | 175 storage::StorageObserver::Filter temporary_filter( |
| 176 storage::kStorageTypeTemporary, it->first); | 176 storage::kStorageTypeTemporary, it->first); |
| 177 it->second.quota_manager->RemoveStorageObserverForFilter(this, | 177 it->second.quota_manager->RemoveStorageObserverForFilter(this, |
| 178 filter); | 178 filter); |
| 179 } | 179 } |
| 180 origin_state_map_.erase(it++); | 180 origin_state_map_.erase(it++); |
| 181 } else { | 181 } else { |
| 182 ++it; | 182 ++it; |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Stop observing all storage events. Called during shutdown. | 187 // Stop observing all storage events. Called during shutdown. |
| 188 void StopObserving() { | 188 void StopObserving() { |
| 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 190 | 190 |
| 191 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); | 191 for (OriginStorageStateMap::iterator it = origin_state_map_.begin(); |
| 192 it != origin_state_map_.end(); ++it) { | 192 it != origin_state_map_.end(); ++it) { |
| 193 it->second.quota_manager->RemoveStorageObserver(this); | 193 it->second.quota_manager->RemoveStorageObserver(this); |
| 194 } | 194 } |
| 195 origin_state_map_.clear(); | 195 origin_state_map_.clear(); |
| 196 } | 196 } |
| 197 | 197 |
| 198 private: | 198 private: |
| 199 friend class base::DeleteHelper<StorageEventObserver>; | 199 friend class base::DeleteHelper<StorageEventObserver>; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 placeholders.push_back(context_->GetPath().BaseName().MaybeAsASCII()); | 394 placeholders.push_back(context_->GetPath().BaseName().MaybeAsASCII()); |
| 395 placeholders.push_back(extension_id); | 395 placeholders.push_back(extension_id); |
| 396 | 396 |
| 397 return ReplaceStringPlaceholders(kNotificationIdFormat, placeholders, NULL); | 397 return ReplaceStringPlaceholders(kNotificationIdFormat, placeholders, NULL); |
| 398 } | 398 } |
| 399 | 399 |
| 400 void ExtensionStorageMonitor::OnStorageThresholdExceeded( | 400 void ExtensionStorageMonitor::OnStorageThresholdExceeded( |
| 401 const std::string& extension_id, | 401 const std::string& extension_id, |
| 402 int64 next_threshold, | 402 int64 next_threshold, |
| 403 int64 current_usage) { | 403 int64 current_usage) { |
| 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 404 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 405 | 405 |
| 406 const Extension* extension = GetExtensionById(context_, extension_id); | 406 const Extension* extension = GetExtensionById(context_, extension_id); |
| 407 if (!extension) | 407 if (!extension) |
| 408 return; | 408 return; |
| 409 | 409 |
| 410 if (GetNextStorageThreshold(extension->id()) < next_threshold) | 410 if (GetNextStorageThreshold(extension->id()) < next_threshold) |
| 411 SetNextStorageThreshold(extension->id(), next_threshold); | 411 SetNextStorageThreshold(extension->id(), next_threshold); |
| 412 | 412 |
| 413 const int kIconSize = message_center::kNotificationIconSize; | 413 const int kIconSize = message_center::kNotificationIconSize; |
| 414 ExtensionResource resource = IconsInfo::GetIconResource( | 414 ExtensionResource resource = IconsInfo::GetIconResource( |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 678 void ExtensionStorageMonitor::SetStorageNotificationEnabled( | 678 void ExtensionStorageMonitor::SetStorageNotificationEnabled( |
| 679 const std::string& extension_id, | 679 const std::string& extension_id, |
| 680 bool enable_notifications) { | 680 bool enable_notifications) { |
| 681 extension_prefs_->UpdateExtensionPref( | 681 extension_prefs_->UpdateExtensionPref( |
| 682 extension_id, | 682 extension_id, |
| 683 kPrefDisableStorageNotifications, | 683 kPrefDisableStorageNotifications, |
| 684 enable_notifications ? NULL : new base::FundamentalValue(true)); | 684 enable_notifications ? NULL : new base::FundamentalValue(true)); |
| 685 } | 685 } |
| 686 | 686 |
| 687 } // namespace extensions | 687 } // namespace extensions |
| OLD | NEW |