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 OnExtensionUnloaded(const Extension* extension) OVERRIDE; |
| 55 |
| 56 std::string GetNotificationId(const std::string& extension_id); |
| 57 |
| 58 void OnStorageThresholdExceeded(const std::string& extension_id, |
| 59 int64 next_threshold, |
| 60 int64 current_usage); |
| 61 void OnImageLoaded(const std::string& extension_id, |
| 62 int64 current_usage, |
| 63 const gfx::Image& image); |
| 64 void OnNotificationButtonClick(const std::string& extension_id, |
| 65 int button_index); |
| 66 |
| 67 void DisableStorageMonitoring(const std::string& extension_id); |
| 68 void StartMonitoringStorage(const Extension* extension); |
| 69 void StopMonitoringStorage(const std::string& extension_id); |
| 70 void StopMonitoringAll(); |
| 71 void RemoveNotificationForExtension(const std::string& extension_id); |
| 72 void RemoveAllNotifications(); |
| 73 |
| 74 // Initially, monitoring will only be applied to ephemeral apps. This flag |
| 75 // is set by tests to enable for all extensions and apps. |
| 76 bool enable_for_all_extensions_; |
| 77 |
| 78 // A lower threshold is set by tests. |
| 79 int64 initial_extension_threshold_; |
| 80 int64 initial_ephemeral_threshold_; |
| 81 int observer_rate_; |
| 82 |
| 83 std::set<std::string> notified_extension_ids_; |
| 84 |
| 85 content::BrowserContext* context_; |
| 86 content::NotificationRegistrar registrar_; |
| 87 scoped_refptr<StorageEventObserver> storage_observer_; |
| 88 base::WeakPtrFactory<ExtensionStorageMonitor> weak_ptr_factory_; |
| 89 |
| 90 friend class StorageEventObserver; |
| 91 friend class ExtensionStorageMonitorTest; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(ExtensionStorageMonitor); |
| 94 }; |
| 95 |
| 96 } // namespace extensions |
| 97 |
| 98 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
OLD | NEW |