[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 1 | // Copyright (c) 2012 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_SHELL_WINDOW_GEOMETRY_CACHE_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "base/memory/scoped_ptr.h" |
| 13 | #include "base/memory/weak_ptr.h" |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 14 | #include "base/time.h" |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 15 | #include "base/timer.h" |
| 16 | #include "base/values.h" |
| 17 | #include "content/public/browser/notification_observer.h" |
| 18 | #include "content/public/browser/notification_registrar.h" |
| 19 | #include "ui/gfx/rect.h" |
| 20 | |
| 21 | class Profile; |
| 22 | |
| 23 | namespace extensions { |
| 24 | |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 25 | class ExtensionPrefs; |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 26 | |
| 27 | // A cache for persisted geometry of shell windows, both to not have to wait |
| 28 | // for IO when creating a new window, and to not cause IO on every window |
| 29 | // geometry change. |
| 30 | class ShellWindowGeometryCache |
| 31 | : public base::SupportsWeakPtr<ShellWindowGeometryCache>, |
| 32 | public content::NotificationObserver { |
| 33 | public: |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 34 | ShellWindowGeometryCache(Profile* profile, |
| 35 | ExtensionPrefs* prefs); |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 36 | |
| 37 | virtual ~ShellWindowGeometryCache(); |
| 38 | |
| 39 | void SaveGeometry(const std::string& extension_id, |
[email protected] | 2b0fc08a | 2012-11-07 23:54:26 | [diff] [blame] | 40 | const std::string& window_id, |
| 41 | const gfx::Rect& bounds); |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 42 | |
| 43 | bool GetGeometry(const std::string& extension_id, |
[email protected] | 2b0fc08a | 2012-11-07 23:54:26 | [diff] [blame] | 44 | const std::string& window_id, |
| 45 | gfx::Rect* bounds) const; |
| 46 | |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 47 | // Maximum number of windows we'll cache the geometry for per app. |
| 48 | static const size_t kMaxCachedWindows = 100; |
| 49 | |
[email protected] | 2b0fc08a | 2012-11-07 23:54:26 | [diff] [blame] | 50 | protected: |
| 51 | friend class ShellWindowGeometryCacheTest; |
| 52 | |
| 53 | // For tests, this modifies the timeout delay for saving changes from calls |
| 54 | // to SaveGeometry. (Note that even if this is set to 0, you still need to |
| 55 | // run the message loop to see the results of any SyncToStorage call). |
| 56 | void SetSyncDelayForTests(int timeout_ms); |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 57 | |
| 58 | private: |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 59 | // Data stored for each window. |
| 60 | struct WindowData { |
| 61 | gfx::Rect bounds; |
| 62 | base::Time last_change; |
| 63 | }; |
| 64 | |
| 65 | // Data stored for each extension. |
| 66 | typedef std::map<std::string, WindowData> ExtensionData; |
| 67 | |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 68 | // content::NotificationObserver |
| 69 | virtual void Observe(int type, |
| 70 | const content::NotificationSource& source, |
| 71 | const content::NotificationDetails& details) OVERRIDE; |
| 72 | |
| 73 | void OnExtensionLoaded(const std::string& extension_id); |
| 74 | void OnExtensionUnloaded(const std::string& extension_id); |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 75 | void SyncToStorage(); |
| 76 | |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 77 | // Preferences storage. |
| 78 | ExtensionPrefs* prefs_; |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 79 | |
| 80 | // Cached data |
[email protected] | af3920f | 2012-12-13 00:49:24 | [diff] [blame] | 81 | std::map<std::string, ExtensionData> cache_; |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 82 | |
| 83 | // Data that still needs saving |
| 84 | std::set<std::string> unsynced_extensions_; |
| 85 | |
| 86 | // The timer used to save the data |
| 87 | base::OneShotTimer<ShellWindowGeometryCache> sync_timer_; |
| 88 | |
[email protected] | 2b0fc08a | 2012-11-07 23:54:26 | [diff] [blame] | 89 | // The timeout value we'll use for |sync_timer_|. |
| 90 | base::TimeDelta sync_delay_; |
| 91 | |
[email protected] | cb610dc | 2012-08-31 17:16:56 | [diff] [blame] | 92 | content::NotificationRegistrar registrar_; |
| 93 | }; |
| 94 | |
| 95 | } // namespace extensions |
| 96 | |
| 97 | #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ |