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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_GEOMETRY_CACHE_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_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/geometry/rect.h"
25 namespace extensions
{
28 class ExtensionRegistry
;
30 // A cache for persisted geometry of app windows, both to not have to wait
31 // for IO when creating a new window, and to not cause IO on every window
33 class AppWindowGeometryCache
: public KeyedService
,
34 public ExtensionRegistryObserver
{
36 class Factory
: public BrowserContextKeyedServiceFactory
{
38 static AppWindowGeometryCache
* GetForContext(
39 content::BrowserContext
* context
,
42 static Factory
* GetInstance();
45 friend struct base::DefaultSingletonTraits
<Factory
>;
50 // BrowserContextKeyedServiceFactory
51 KeyedService
* BuildServiceInstanceFor(
52 content::BrowserContext
* context
) const override
;
53 bool ServiceIsNULLWhileTesting() const override
;
54 content::BrowserContext
* GetBrowserContextToUse(
55 content::BrowserContext
* context
) const override
;
60 virtual void OnGeometryCacheChanged(const std::string
& extension_id
,
61 const std::string
& window_id
,
62 const gfx::Rect
& bounds
) = 0;
65 virtual ~Observer() {}
68 AppWindowGeometryCache(content::BrowserContext
* context
,
69 ExtensionPrefs
* prefs
);
71 ~AppWindowGeometryCache() override
;
73 // Returns the instance for the given browsing context.
74 static AppWindowGeometryCache
* Get(content::BrowserContext
* context
);
76 // Save the geometry and state associated with |extension_id| and |window_id|.
77 void SaveGeometry(const std::string
& extension_id
,
78 const std::string
& window_id
,
79 const gfx::Rect
& bounds
,
80 const gfx::Rect
& screen_bounds
,
81 ui::WindowShowState state
);
83 // Get any saved geometry and state associated with |extension_id| and
84 // |window_id|. If saved data exists, sets |bounds|, |screen_bounds| and
85 // |state| if not NULL and returns true.
86 bool GetGeometry(const std::string
& extension_id
,
87 const std::string
& window_id
,
89 gfx::Rect
* screen_bounds
,
90 ui::WindowShowState
* state
);
93 void Shutdown() override
;
95 void AddObserver(Observer
* observer
);
96 void RemoveObserver(Observer
* observer
);
98 // Maximum number of windows we'll cache the geometry for per app.
99 static const size_t kMaxCachedWindows
= 100;
102 friend class AppWindowGeometryCacheTest
;
104 // For tests, this modifies the timeout delay for saving changes from calls
105 // to SaveGeometry. (Note that even if this is set to 0, you still need to
106 // run the message loop to see the results of any SyncToStorage call).
107 void SetSyncDelayForTests(int timeout_ms
);
110 // Data stored for each window.
115 gfx::Rect screen_bounds
;
116 ui::WindowShowState window_state
;
117 base::Time last_change
;
120 // Data stored for each extension.
121 typedef std::map
<std::string
, WindowData
> ExtensionData
;
123 // ExtensionRegistryObserver implementation.
124 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
125 const Extension
* extension
) override
;
126 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
127 const Extension
* extension
,
128 UnloadedExtensionInfo::Reason reason
) override
;
130 void LoadGeometryFromStorage(const std::string
& extension_id
);
131 void SyncToStorage();
133 // Preferences storage.
134 ExtensionPrefs
* prefs_
;
137 std::map
<std::string
, ExtensionData
> cache_
;
139 // Data that still needs saving.
140 std::set
<std::string
> unsynced_extensions_
;
142 // The timer used to save the data.
143 base::OneShotTimer
<AppWindowGeometryCache
> sync_timer_
;
145 // The timeout value we'll use for |sync_timer_|.
146 base::TimeDelta sync_delay_
;
148 // Listen to extension load, unloaded notifications.
149 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
150 extension_registry_observer_
;
152 base::ObserverList
<Observer
> observers_
;
155 } // namespace extensions
157 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_GEOMETRY_CACHE_H_