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. 4 5 #ifndef APPS_APP_WINDOW_GEOMETRY_CACHE_H_ 6 #define APPS_APP_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/scoped_observer.h" 16 #include "base/time/time.h" 17 #include "base/timer/timer.h" 18 #include "base/values.h" 19 #include "components/keyed_service/content/browser_context_keyed_service_factory.h" 20 #include "components/keyed_service/core/keyed_service.h" 21 #include "extensions/browser/extension_registry_observer.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 class ExtensionRegistry; 30 } 31 32 namespace apps { 33 34 // A cache for persisted geometry of app windows, both to not have to wait 35 // for IO when creating a new window, and to not cause IO on every window 36 // geometry change. 37 class AppWindowGeometryCache : public KeyedService, 38 public extensions::ExtensionRegistryObserver { 39 public: 40 class Factory : public BrowserContextKeyedServiceFactory { 41 public: 42 static AppWindowGeometryCache* GetForContext( 43 content::BrowserContext* context, 44 bool create); 45 46 static Factory* GetInstance(); 47 48 private: 49 friend struct DefaultSingletonTraits<Factory>; 50 51 Factory(); 52 virtual ~Factory(); 53 54 // BrowserContextKeyedServiceFactory 55 virtual KeyedService* BuildServiceInstanceFor( 56 content::BrowserContext* context) const OVERRIDE; 57 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE; 58 virtual content::BrowserContext* GetBrowserContextToUse( 59 content::BrowserContext* context) const OVERRIDE; 60 }; 61 62 class Observer { 63 public: 64 virtual void OnGeometryCacheChanged(const std::string& extension_id, 65 const std::string& window_id, 66 const gfx::Rect& bounds) = 0; 67 68 protected: 69 virtual ~Observer() {} 70 }; 71 72 AppWindowGeometryCache(Profile* profile, extensions::ExtensionPrefs* prefs); 73 74 virtual ~AppWindowGeometryCache(); 75 76 // Returns the instance for the given browsing context. 77 static AppWindowGeometryCache* 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 // KeyedService 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 AppWindowGeometryCacheTest; 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 // ExtensionRegistryObserver implementation. 127 virtual void OnExtensionLoaded( 128 content::BrowserContext* browser_context, 129 const extensions::Extension* extension) OVERRIDE; 130 virtual void OnExtensionUnloaded( 131 content::BrowserContext* browser_context, 132 const extensions::Extension* extension, 133 extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; 134 135 void LoadGeometryFromStorage(const std::string& extension_id); 136 void SyncToStorage(); 137 138 // Preferences storage. 139 extensions::ExtensionPrefs* prefs_; 140 141 // Cached data. 142 std::map<std::string, ExtensionData> cache_; 143 144 // Data that still needs saving. 145 std::set<std::string> unsynced_extensions_; 146 147 // The timer used to save the data. 148 base::OneShotTimer<AppWindowGeometryCache> sync_timer_; 149 150 // The timeout value we'll use for |sync_timer_|. 151 base::TimeDelta sync_delay_; 152 153 // Listen to extension load, unloaded notifications. 154 ScopedObserver<extensions::ExtensionRegistry, 155 extensions::ExtensionRegistryObserver> 156 extension_registry_observer_; 157 158 ObserverList<Observer> observers_; 159 }; 160 161 } // namespace apps 162 163 #endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_ 164