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"
27 namespace extensions
{
29 class ExtensionRegistry
;
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
37 class AppWindowGeometryCache
: public KeyedService
,
38 public extensions::ExtensionRegistryObserver
{
40 class Factory
: public BrowserContextKeyedServiceFactory
{
42 static AppWindowGeometryCache
* GetForContext(
43 content::BrowserContext
* context
,
46 static Factory
* GetInstance();
49 friend struct DefaultSingletonTraits
<Factory
>;
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
;
64 virtual void OnGeometryCacheChanged(const std::string
& extension_id
,
65 const std::string
& window_id
,
66 const gfx::Rect
& bounds
) = 0;
69 virtual ~Observer() {}
72 AppWindowGeometryCache(Profile
* profile
, extensions::ExtensionPrefs
* prefs
);
74 virtual ~AppWindowGeometryCache();
76 // Returns the instance for the given browsing context.
77 static AppWindowGeometryCache
* Get(content::BrowserContext
* context
);
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
);
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
,
92 gfx::Rect
* screen_bounds
,
93 ui::WindowShowState
* state
);
96 virtual void Shutdown() OVERRIDE
;
98 void AddObserver(Observer
* observer
);
99 void RemoveObserver(Observer
* observer
);
101 // Maximum number of windows we'll cache the geometry for per app.
102 static const size_t kMaxCachedWindows
= 100;
105 friend class AppWindowGeometryCacheTest
;
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
);
113 // Data stored for each window.
118 gfx::Rect screen_bounds
;
119 ui::WindowShowState window_state
;
120 base::Time last_change
;
123 // Data stored for each extension.
124 typedef std::map
<std::string
, WindowData
> ExtensionData
;
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
;
135 void LoadGeometryFromStorage(const std::string
& extension_id
);
136 void SyncToStorage();
138 // Preferences storage.
139 extensions::ExtensionPrefs
* prefs_
;
142 std::map
<std::string
, ExtensionData
> cache_
;
144 // Data that still needs saving.
145 std::set
<std::string
> unsynced_extensions_
;
147 // The timer used to save the data.
148 base::OneShotTimer
<AppWindowGeometryCache
> sync_timer_
;
150 // The timeout value we'll use for |sync_timer_|.
151 base::TimeDelta sync_delay_
;
153 // Listen to extension load, unloaded notifications.
154 ScopedObserver
<extensions::ExtensionRegistry
,
155 extensions::ExtensionRegistryObserver
>
156 extension_registry_observer_
;
158 ObserverList
<Observer
> observers_
;
163 #endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_