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/singleton.h" 14 #include "base/observer_list.h" 15 #include "base/time/time.h" 16 #include "base/timer/timer.h" 17 #include "base/values.h" 18 #include "components/browser_context_keyed_service/browser_context_keyed_service.h" 19 #include "components/browser_context_keyed_service/browser_context_keyed_service_factory.h" 20 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_registrar.h" 22 #include "ui/base/ui_base_types.h" 23 #include "ui/gfx/rect.h" 24 25 class Profile; 26 27 namespace extensions { 28 class ExtensionPrefs; 29 } 30 31 namespace apps { 32 33 // A cache for persisted geometry of shell windows, both to not have to wait 34 // for IO when creating a new window, and to not cause IO on every window 35 // geometry change. 36 class ShellWindowGeometryCache 37 : public BrowserContextKeyedService, 38 public content::NotificationObserver { 39 public: 40 class Factory : public BrowserContextKeyedServiceFactory { 41 public: 42 static ShellWindowGeometryCache* GetForContext( 43 content::BrowserContext* context, 44 bool create); 45 46 static Factory* GetInstance(); 47 private: 48 friend struct DefaultSingletonTraits<Factory>; 49 50 Factory(); 51 virtual ~Factory(); 52 53 // BrowserContextKeyedServiceFactory 54 virtual BrowserContextKeyedService* BuildServiceInstanceFor( 55 content::BrowserContext* context) const OVERRIDE; 56 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; 57 virtual content::BrowserContext* GetBrowserContextToUse( 58 content::BrowserContext* context) const OVERRIDE; 59 }; 60 61 class Observer { 62 public: 63 virtual void OnGeometryCacheChanged(const std::string& extension_id, 64 const std::string& window_id, 65 const gfx::Rect& bounds) = 0; 66 67 protected: 68 virtual ~Observer() {}; 69 }; 70 71 ShellWindowGeometryCache(Profile* profile, 72 extensions::ExtensionPrefs* prefs); 73 74 virtual ~ShellWindowGeometryCache(); 75 76 // Returns the instance for the given browsing context. 77 static ShellWindowGeometryCache* Get(content::BrowserContext* context); 78 79 // Save the geometry and state associated with |extension_id| and |window_id|. 80 void SaveGeometry(const std::string& extension_id, 81 const std::string& window_id, 82 const gfx::Rect& bounds, 83 const gfx::Rect& screen_bounds, 84 ui::WindowShowState state); 85 86 // Get any saved geometry and state associated with |extension_id| and 87 // |window_id|. If saved data exists, sets |bounds|, |screen_bounds| and 88 // |state| if not NULL and returns true. 89 bool GetGeometry(const std::string& extension_id, 90 const std::string& window_id, 91 gfx::Rect* bounds, 92 gfx::Rect* screen_bounds, 93 ui::WindowShowState* state); 94 95 // BrowserContextKeyedService 96 virtual void Shutdown() OVERRIDE; 97 98 void AddObserver(Observer* observer); 99 void RemoveObserver(Observer* observer); 100 101 // Maximum number of windows we'll cache the geometry for per app. 102 static const size_t kMaxCachedWindows = 100; 103 104 protected: 105 friend class ShellWindowGeometryCacheTest; 106 107 // For tests, this modifies the timeout delay for saving changes from calls 108 // to SaveGeometry. (Note that even if this is set to 0, you still need to 109 // run the message loop to see the results of any SyncToStorage call). 110 void SetSyncDelayForTests(int timeout_ms); 111 112 private: 113 // Data stored for each window. 114 struct WindowData { 115 WindowData(); 116 ~WindowData(); 117 gfx::Rect bounds; 118 gfx::Rect screen_bounds; 119 ui::WindowShowState window_state; 120 base::Time last_change; 121 }; 122 123 // Data stored for each extension. 124 typedef std::map<std::string, WindowData> ExtensionData; 125 126 // content::NotificationObserver 127 virtual void Observe(int type, 128 const content::NotificationSource& source, 129 const content::NotificationDetails& details) OVERRIDE; 130 131 void LoadGeometryFromStorage(const std::string& extension_id); 132 void OnExtensionUnloaded(const std::string& extension_id); 133 void SyncToStorage(); 134 135 // Preferences storage. 136 extensions::ExtensionPrefs* prefs_; 137 138 // Cached data 139 std::map<std::string, ExtensionData> cache_; 140 141 // Data that still needs saving 142 std::set<std::string> unsynced_extensions_; 143 144 // The timer used to save the data 145 base::OneShotTimer<ShellWindowGeometryCache> sync_timer_; 146 147 // The timeout value we'll use for |sync_timer_|. 148 base::TimeDelta sync_delay_; 149 150 content::NotificationRegistrar registrar_; 151 ObserverList<Observer> observers_; 152 }; 153 154 } // namespace apps 155 156 #endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_ 157