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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/scoped_observer.h" |
12 #include "components/keyed_service/core/keyed_service.h" | 13 #include "components/keyed_service/core/keyed_service.h" |
13 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
15 #include "extensions/browser/extension_registry_observer.h" | 16 #include "extensions/browser/extension_registry_observer.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 class BrowserContext; | 19 class BrowserContext; |
19 } | 20 } |
20 | 21 |
21 namespace gfx { | 22 namespace gfx { |
22 class Image; | 23 class Image; |
23 } | 24 } |
24 | 25 |
25 namespace extensions { | 26 namespace extensions { |
26 | 27 |
27 class Extension; | 28 class Extension; |
| 29 class ExtensionPrefs; |
| 30 class ExtensionRegistry; |
28 class StorageEventObserver; | 31 class StorageEventObserver; |
29 | 32 |
30 // ExtensionStorageMonitor monitors the storage usage of extensions and apps | 33 // ExtensionStorageMonitor monitors the storage usage of extensions and apps |
31 // that are granted unlimited storage and displays notifications when high | 34 // that are granted unlimited storage and displays notifications when high |
32 // usage is detected. | 35 // usage is detected. |
33 class ExtensionStorageMonitor : public KeyedService, | 36 class ExtensionStorageMonitor : public KeyedService, |
34 public content::NotificationObserver, | 37 public content::NotificationObserver, |
35 public ExtensionRegistryObserver { | 38 public ExtensionRegistryObserver { |
36 public: | 39 public: |
37 static ExtensionStorageMonitor* Get(content::BrowserContext* context); | 40 static ExtensionStorageMonitor* Get(content::BrowserContext* context); |
38 | 41 |
39 // Indices of buttons in the notification. Exposed for testing. | 42 // Indices of buttons in the notification. Exposed for testing. |
40 enum ButtonIndex { | 43 enum ButtonIndex { |
41 BUTTON_DISABLE_NOTIFICATION = 0 | 44 BUTTON_DISABLE_NOTIFICATION = 0 |
42 }; | 45 }; |
43 | 46 |
44 explicit ExtensionStorageMonitor(content::BrowserContext* context); | 47 explicit ExtensionStorageMonitor(content::BrowserContext* context); |
45 virtual ~ExtensionStorageMonitor(); | 48 virtual ~ExtensionStorageMonitor(); |
46 | 49 |
47 private: | 50 private: |
48 // content::NotificationObserver overrides: | 51 // content::NotificationObserver overrides: |
49 virtual void Observe(int type, | 52 virtual void Observe(int type, |
50 const content::NotificationSource& source, | 53 const content::NotificationSource& source, |
51 const content::NotificationDetails& details) OVERRIDE; | 54 const content::NotificationDetails& details) OVERRIDE; |
52 | 55 |
53 // ExtensionRegistryObserver overrides: | 56 // ExtensionRegistryObserver overrides: |
54 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, | 57 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
55 const Extension* extension) OVERRIDE; | 58 const Extension* extension) OVERRIDE; |
56 virtual void OnExtensionUnloaded(content::BrowserContext* browser_context, | 59 virtual void OnExtensionUnloaded( |
57 const Extension* extension, | 60 content::BrowserContext* browser_context, |
58 UnloadedExtensionInfo::Reason reason) | 61 const Extension* extension, |
59 OVERRIDE; | 62 UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 63 virtual void OnExtensionWillBeInstalled( |
| 64 content::BrowserContext* browser_context, |
| 65 const Extension* extension, |
| 66 bool is_update, |
| 67 bool from_ephemeral, |
| 68 const std::string& old_name) OVERRIDE; |
| 69 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 70 const Extension* extension) OVERRIDE; |
60 | 71 |
61 std::string GetNotificationId(const std::string& extension_id); | 72 std::string GetNotificationId(const std::string& extension_id); |
62 | 73 |
63 void OnStorageThresholdExceeded(const std::string& extension_id, | 74 void OnStorageThresholdExceeded(const std::string& extension_id, |
64 int64 next_threshold, | 75 int64 next_threshold, |
65 int64 current_usage); | 76 int64 current_usage); |
66 void OnImageLoaded(const std::string& extension_id, | 77 void OnImageLoaded(const std::string& extension_id, |
67 int64 current_usage, | 78 int64 current_usage, |
68 const gfx::Image& image); | 79 const gfx::Image& image); |
69 void OnNotificationButtonClick(const std::string& extension_id, | 80 void OnNotificationButtonClick(const std::string& extension_id, |
70 int button_index); | 81 int button_index); |
71 | 82 |
72 void DisableStorageMonitoring(const std::string& extension_id); | 83 void DisableStorageMonitoring(const std::string& extension_id); |
73 void StartMonitoringStorage(const Extension* extension); | 84 void StartMonitoringStorage(const Extension* extension); |
74 void StopMonitoringStorage(const std::string& extension_id); | 85 void StopMonitoringStorage(const std::string& extension_id); |
75 void StopMonitoringAll(); | 86 void StopMonitoringAll(); |
| 87 |
76 void RemoveNotificationForExtension(const std::string& extension_id); | 88 void RemoveNotificationForExtension(const std::string& extension_id); |
77 void RemoveAllNotifications(); | 89 void RemoveAllNotifications(); |
78 | 90 |
| 91 // Returns/sets the next threshold for displaying a notification if an |
| 92 // extension or app consumes excessive disk space. |
| 93 int64 GetNextStorageThreshold(const std::string& extension_id) const; |
| 94 void SetNextStorageThreshold(const std::string& extension_id, |
| 95 int64 next_threshold); |
| 96 |
| 97 // Returns the raw next storage threshold value stored in prefs. Returns 0 if |
| 98 // the initial threshold has not yet been reached. |
| 99 int64 GetNextStorageThresholdFromPrefs(const std::string& extension_id) const; |
| 100 |
| 101 // Returns/sets whether notifications should be shown if an extension or app |
| 102 // consumes too much disk space. |
| 103 bool IsStorageNotificationEnabled(const std::string& extension_id) const; |
| 104 void SetStorageNotificationEnabled(const std::string& extension_id, |
| 105 bool enable_notifications); |
| 106 |
79 // Initially, monitoring will only be applied to ephemeral apps. This flag | 107 // Initially, monitoring will only be applied to ephemeral apps. This flag |
80 // is set by tests to enable for all extensions and apps. | 108 // is set by tests to enable for all extensions and apps. |
81 bool enable_for_all_extensions_; | 109 bool enable_for_all_extensions_; |
82 | 110 |
| 111 // The first notification is shown after the initial threshold is exceeded. |
| 112 // Ephemeral apps have a lower threshold than fully installed extensions. |
83 // A lower threshold is set by tests. | 113 // A lower threshold is set by tests. |
84 int64 initial_extension_threshold_; | 114 int64 initial_extension_threshold_; |
85 int64 initial_ephemeral_threshold_; | 115 int64 initial_ephemeral_threshold_; |
| 116 |
| 117 // The rate (in seconds) at which we would like to receive storage updates |
| 118 // from QuotaManager. Overridden in tests. |
86 int observer_rate_; | 119 int observer_rate_; |
87 | 120 |
| 121 // IDs of extensions that notifications were shown for. |
88 std::set<std::string> notified_extension_ids_; | 122 std::set<std::string> notified_extension_ids_; |
89 | 123 |
90 content::BrowserContext* context_; | 124 content::BrowserContext* context_; |
| 125 extensions::ExtensionPrefs* extension_prefs_; |
| 126 |
91 content::NotificationRegistrar registrar_; | 127 content::NotificationRegistrar registrar_; |
| 128 ScopedObserver<extensions::ExtensionRegistry, |
| 129 extensions::ExtensionRegistryObserver> |
| 130 extension_registry_observer_; |
| 131 |
| 132 // StorageEventObserver monitors storage for extensions on the IO thread. |
92 scoped_refptr<StorageEventObserver> storage_observer_; | 133 scoped_refptr<StorageEventObserver> storage_observer_; |
| 134 |
93 base::WeakPtrFactory<ExtensionStorageMonitor> weak_ptr_factory_; | 135 base::WeakPtrFactory<ExtensionStorageMonitor> weak_ptr_factory_; |
94 | 136 |
95 friend class StorageEventObserver; | 137 friend class StorageEventObserver; |
96 friend class ExtensionStorageMonitorTest; | 138 friend class ExtensionStorageMonitorTest; |
97 | 139 |
98 DISALLOW_COPY_AND_ASSIGN(ExtensionStorageMonitor); | 140 DISALLOW_COPY_AND_ASSIGN(ExtensionStorageMonitor); |
99 }; | 141 }; |
100 | 142 |
101 } // namespace extensions | 143 } // namespace extensions |
102 | 144 |
103 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ | 145 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_ |
OLD | NEW |