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