OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
| 7 |
| 8 #include <set> |
| 9 |
| 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "components/keyed_service/core/keyed_service.h" |
| 13 #include "content/public/browser/notification_observer.h" |
| 14 #include "content/public/browser/notification_registrar.h" |
| 15 #include "extensions/browser/extension_registry_observer.h" |
| 16 |
| 17 namespace content { |
| 18 class BrowserContext; |
| 19 } |
| 20 |
| 21 namespace gfx { |
| 22 class Image; |
| 23 } |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 class Extension; |
| 28 class StorageEventObserver; |
| 29 |
| 30 // ExtensionStorageMonitor monitors the storage usage of extensions and apps |
| 31 // that are granted unlimited storage and displays notifications when high |
| 32 // usage is detected. |
| 33 class ExtensionStorageMonitor : public KeyedService, |
| 34 public content::NotificationObserver, |
| 35 public ExtensionRegistryObserver { |
| 36 public: |
| 37 static ExtensionStorageMonitor* Get(content::BrowserContext* context); |
| 38 |
| 39 // Indices of buttons in the notification. Exposed for testing. |
| 40 enum ButtonIndex { |
| 41 BUTTON_DISABLE_NOTIFICATION = 0 |
| 42 }; |
| 43 |
| 44 explicit ExtensionStorageMonitor(content::BrowserContext* context); |
| 45 virtual ~ExtensionStorageMonitor(); |
| 46 |
| 47 private: |
| 48 // content::NotificationObserver overrides: |
| 49 virtual void Observe(int type, |
| 50 const content::NotificationSource& source, |
| 51 const content::NotificationDetails& details) OVERRIDE; |
| 52 |
| 53 // ExtensionRegistryObserver overrides: |
| 54 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 55 const Extension* extension) OVERRIDE; |
| 56 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 57 const Extension* extension) OVERRIDE; |
| 58 |
| 59 std::string GetNotificationId(const std::string& extension_id); |
| 60 |
| 61 void OnStorageThresholdExceeded(const std::string& extension_id, |
| 62 int64 next_threshold, |
| 63 int64 current_usage); |
| 64 void OnImageLoaded(const std::string& extension_id, |
| 65 int64 current_usage, |
| 66 const gfx::Image& image); |
| 67 void OnNotificationButtonClick(const std::string& extension_id, |
| 68 int button_index); |
| 69 |
| 70 void DisableStorageMonitoring(const std::string& extension_id); |
| 71 void StartMonitoringStorage(const Extension* extension); |
| 72 void StopMonitoringStorage(const std::string& extension_id); |
| 73 void StopMonitoringAll(); |
| 74 void RemoveNotificationForExtension(const std::string& extension_id); |
| 75 void RemoveAllNotifications(); |
| 76 |
| 77 // Initially, monitoring will only be applied to ephemeral apps. This flag |
| 78 // is set by tests to enable for all extensions and apps. |
| 79 bool enable_for_all_extensions_; |
| 80 |
| 81 // A lower threshold is set by tests. |
| 82 int64 initial_extension_threshold_; |
| 83 int64 initial_ephemeral_threshold_; |
| 84 int observer_rate_; |
| 85 |
| 86 std::set<std::string> notified_extension_ids_; |
| 87 |
| 88 content::BrowserContext* context_; |
| 89 content::NotificationRegistrar registrar_; |
| 90 scoped_refptr<StorageEventObserver> storage_observer_; |
| 91 base::WeakPtrFactory<ExtensionStorageMonitor> weak_ptr_factory_; |
| 92 |
| 93 friend class StorageEventObserver; |
| 94 friend class ExtensionStorageMonitorTest; |
| 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(ExtensionStorageMonitor); |
| 97 }; |
| 98 |
| 99 } // namespace extensions |
| 100 |
| 101 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
OLD | NEW |