Extensions cleanup: Merge IsSyncableApp+Extension, ShouldSyncApp+Extension
[chromium-blink-merge.git] / chrome / browser / extensions / extension_storage_monitor.h
blobd739851fca6d05a9de405735075dcc41f3c348ef
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_
8 #include <set>
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "chrome/browser/extensions/extension_uninstall_dialog.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/extension_registry_observer.h"
19 namespace content {
20 class BrowserContext;
23 namespace gfx {
24 class Image;
27 namespace extensions {
29 class Extension;
30 class ExtensionPrefs;
31 class ExtensionRegistry;
32 class StorageEventObserver;
34 // ExtensionStorageMonitor monitors the storage usage of extensions and apps
35 // that are granted unlimited storage and displays notifications when high
36 // usage is detected.
37 class ExtensionStorageMonitor : public KeyedService,
38 public content::NotificationObserver,
39 public ExtensionRegistryObserver,
40 public ExtensionUninstallDialog::Delegate {
41 public:
42 static ExtensionStorageMonitor* Get(content::BrowserContext* context);
44 // Indices of buttons in the notification. Exposed for testing.
45 enum ButtonIndex {
46 BUTTON_DISABLE_NOTIFICATION = 0,
47 BUTTON_UNINSTALL
50 explicit ExtensionStorageMonitor(content::BrowserContext* context);
51 ~ExtensionStorageMonitor() override;
53 private:
54 // content::NotificationObserver overrides:
55 void Observe(int type,
56 const content::NotificationSource& source,
57 const content::NotificationDetails& details) override;
59 // ExtensionRegistryObserver overrides:
60 void OnExtensionLoaded(content::BrowserContext* browser_context,
61 const Extension* extension) override;
62 void OnExtensionUnloaded(content::BrowserContext* browser_context,
63 const Extension* extension,
64 UnloadedExtensionInfo::Reason reason) override;
65 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
66 const Extension* extension,
67 bool is_update,
68 bool from_ephemeral,
69 const std::string& old_name) override;
70 void OnExtensionUninstalled(content::BrowserContext* browser_context,
71 const Extension* extension,
72 extensions::UninstallReason reason) override;
74 // Overridden from ExtensionUninstallDialog::Delegate:
75 void OnExtensionUninstallDialogClosed(bool did_start_uninstall,
76 const base::string16& error) override;
78 std::string GetNotificationId(const std::string& extension_id);
80 void OnStorageThresholdExceeded(const std::string& extension_id,
81 int64 next_threshold,
82 int64 current_usage);
83 void OnImageLoaded(const std::string& extension_id,
84 int64 current_usage,
85 const gfx::Image& image);
86 void OnNotificationButtonClick(const std::string& extension_id,
87 int button_index);
89 void DisableStorageMonitoring(const std::string& extension_id);
90 void StartMonitoringStorage(const Extension* extension);
91 void StopMonitoringStorage(const std::string& extension_id);
92 void StopMonitoringAll();
94 void RemoveNotificationForExtension(const std::string& extension_id);
95 void RemoveAllNotifications();
97 // Displays the prompt for uninstalling the extension.
98 void ShowUninstallPrompt(const std::string& extension_id);
100 // Returns/sets the next threshold for displaying a notification if an
101 // extension or app consumes excessive disk space.
102 int64 GetNextStorageThreshold(const std::string& extension_id) const;
103 void SetNextStorageThreshold(const std::string& extension_id,
104 int64 next_threshold);
106 // Returns the raw next storage threshold value stored in prefs. Returns 0 if
107 // the initial threshold has not yet been reached.
108 int64 GetNextStorageThresholdFromPrefs(const std::string& extension_id) const;
110 // Returns/sets whether notifications should be shown if an extension or app
111 // consumes too much disk space.
112 bool IsStorageNotificationEnabled(const std::string& extension_id) const;
113 void SetStorageNotificationEnabled(const std::string& extension_id,
114 bool enable_notifications);
116 // Initially, monitoring will only be applied to ephemeral apps. This flag
117 // is set by tests to enable for all extensions and apps.
118 bool enable_for_all_extensions_;
120 // The first notification is shown after the initial threshold is exceeded.
121 // Ephemeral apps have a lower threshold than fully installed extensions.
122 // A lower threshold is set by tests.
123 int64 initial_extension_threshold_;
124 int64 initial_ephemeral_threshold_;
126 // The rate at which we would like to receive storage updates
127 // from QuotaManager. Overridden in tests.
128 base::TimeDelta observer_rate_;
130 // IDs of extensions that notifications were shown for.
131 std::set<std::string> notified_extension_ids_;
133 content::BrowserContext* context_;
134 extensions::ExtensionPrefs* extension_prefs_;
136 content::NotificationRegistrar registrar_;
137 ScopedObserver<extensions::ExtensionRegistry,
138 extensions::ExtensionRegistryObserver>
139 extension_registry_observer_;
141 // StorageEventObserver monitors storage for extensions on the IO thread.
142 scoped_refptr<StorageEventObserver> storage_observer_;
144 // Modal dialog used to confirm removal of an extension.
145 scoped_ptr<ExtensionUninstallDialog> uninstall_dialog_;
147 // The ID of the extension that is the subject of the uninstall confirmation
148 // dialog.
149 std::string uninstall_extension_id_;
151 base::WeakPtrFactory<ExtensionStorageMonitor> weak_ptr_factory_;
153 friend class StorageEventObserver;
154 friend class ExtensionStorageMonitorTest;
156 DISALLOW_COPY_AND_ASSIGN(ExtensionStorageMonitor);
159 } // namespace extensions
161 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_STORAGE_MONITOR_H_