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.
5 #ifndef APPS_APP_WINDOW_GEOMETRY_CACHE_H_
6 #define APPS_APP_WINDOW_GEOMETRY_CACHE_H_
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"
25 namespace extensions {
27 class ExtensionRegistry;
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
35 class AppWindowGeometryCache : public KeyedService,
36 public extensions::ExtensionRegistryObserver {
38 class Factory : public BrowserContextKeyedServiceFactory {
40 static AppWindowGeometryCache* GetForContext(
41 content::BrowserContext* context,
44 static Factory* GetInstance();
47 friend struct DefaultSingletonTraits<Factory>;
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;
62 virtual void OnGeometryCacheChanged(const std::string& extension_id,
63 const std::string& window_id,
64 const gfx::Rect& bounds) = 0;
67 virtual ~Observer() {}
70 AppWindowGeometryCache(content::BrowserContext* context,
71 extensions::ExtensionPrefs* prefs);
73 virtual ~AppWindowGeometryCache();
75 // Returns the instance for the given browsing context.
76 static AppWindowGeometryCache* Get(content::BrowserContext* context);
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);
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,
91 gfx::Rect* screen_bounds,
92 ui::WindowShowState* state);
95 virtual void Shutdown() OVERRIDE;
97 void AddObserver(Observer* observer);
98 void RemoveObserver(Observer* observer);
100 // Maximum number of windows we'll cache the geometry for per app.
101 static const size_t kMaxCachedWindows = 100;
104 friend class AppWindowGeometryCacheTest;
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);
112 // Data stored for each window.
117 gfx::Rect screen_bounds;
118 ui::WindowShowState window_state;
119 base::Time last_change;
122 // Data stored for each extension.
123 typedef std::map<std::string, WindowData> ExtensionData;
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;
134 void LoadGeometryFromStorage(const std::string& extension_id);
135 void SyncToStorage();
137 // Preferences storage.
138 extensions::ExtensionPrefs* prefs_;
141 std::map<std::string, ExtensionData> cache_;
143 // Data that still needs saving.
144 std::set<std::string> unsynced_extensions_;
146 // The timer used to save the data.
147 base::OneShotTimer<AppWindowGeometryCache> sync_timer_;
149 // The timeout value we'll use for |sync_timer_|.
150 base::TimeDelta sync_delay_;
152 // Listen to extension load, unloaded notifications.
153 ScopedObserver<extensions::ExtensionRegistry,
154 extensions::ExtensionRegistryObserver>
155 extension_registry_observer_;
157 ObserverList<Observer> observers_;
162 #endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_