OLD | NEW |
| (Empty) |
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" | |
14 #include "base/time.h" | |
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/base/ui_base_types.h" | |
20 #include "ui/gfx/rect.h" | |
21 | |
22 class Profile; | |
23 | |
24 namespace extensions { | |
25 | |
26 class ExtensionPrefs; | |
27 | |
28 // A cache for persisted geometry of shell windows, both to not have to wait | |
29 // for IO when creating a new window, and to not cause IO on every window | |
30 // geometry change. | |
31 class ShellWindowGeometryCache | |
32 : public base::SupportsWeakPtr<ShellWindowGeometryCache>, | |
33 public content::NotificationObserver { | |
34 public: | |
35 ShellWindowGeometryCache(Profile* profile, | |
36 ExtensionPrefs* prefs); | |
37 | |
38 virtual ~ShellWindowGeometryCache(); | |
39 | |
40 // Save the geometry and state associated with |extension_id| and |window_id|. | |
41 void SaveGeometry(const std::string& extension_id, | |
42 const std::string& window_id, | |
43 const gfx::Rect& bounds, | |
44 ui::WindowShowState state); | |
45 | |
46 // Get any saved geometry and state associated with |extension_id| and | |
47 // |window_id|. If saved data exists, sets |bounds| and |state| if not NULL | |
48 // and returns true. | |
49 bool GetGeometry(const std::string& extension_id, | |
50 const std::string& window_id, | |
51 gfx::Rect* bounds, | |
52 ui::WindowShowState* state) const; | |
53 | |
54 // Maximum number of windows we'll cache the geometry for per app. | |
55 static const size_t kMaxCachedWindows = 100; | |
56 | |
57 protected: | |
58 friend class ShellWindowGeometryCacheTest; | |
59 | |
60 // For tests, this modifies the timeout delay for saving changes from calls | |
61 // to SaveGeometry. (Note that even if this is set to 0, you still need to | |
62 // run the message loop to see the results of any SyncToStorage call). | |
63 void SetSyncDelayForTests(int timeout_ms); | |
64 | |
65 private: | |
66 // Data stored for each window. | |
67 struct WindowData { | |
68 WindowData() : window_state(ui::SHOW_STATE_DEFAULT) {} | |
69 gfx::Rect bounds; | |
70 ui::WindowShowState window_state; | |
71 base::Time last_change; | |
72 }; | |
73 | |
74 // Data stored for each extension. | |
75 typedef std::map<std::string, WindowData> ExtensionData; | |
76 | |
77 // content::NotificationObserver | |
78 virtual void Observe(int type, | |
79 const content::NotificationSource& source, | |
80 const content::NotificationDetails& details) OVERRIDE; | |
81 | |
82 void OnExtensionLoaded(const std::string& extension_id); | |
83 void OnExtensionUnloaded(const std::string& extension_id); | |
84 void SyncToStorage(); | |
85 | |
86 // Preferences storage. | |
87 ExtensionPrefs* prefs_; | |
88 | |
89 // Cached data | |
90 std::map<std::string, ExtensionData> cache_; | |
91 | |
92 // Data that still needs saving | |
93 std::set<std::string> unsynced_extensions_; | |
94 | |
95 // The timer used to save the data | |
96 base::OneShotTimer<ShellWindowGeometryCache> sync_timer_; | |
97 | |
98 // The timeout value we'll use for |sync_timer_|. | |
99 base::TimeDelta sync_delay_; | |
100 | |
101 content::NotificationRegistrar registrar_; | |
102 }; | |
103 | |
104 } // namespace extensions | |
105 | |
106 #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ | |
OLD | NEW |