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/time/time.h"
16 #include "base/timer/timer.h"
17 #include "base/values.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "ui/base/ui_base_types.h"
23 #include "ui/gfx/rect.h"
27 namespace extensions
{
33 // A cache for persisted geometry of app windows, both to not have to wait
34 // for IO when creating a new window, and to not cause IO on every window
36 class AppWindowGeometryCache
: public KeyedService
,
37 public content::NotificationObserver
{
39 class Factory
: public BrowserContextKeyedServiceFactory
{
41 static AppWindowGeometryCache
* GetForContext(
42 content::BrowserContext
* context
,
45 static Factory
* GetInstance();
48 friend struct DefaultSingletonTraits
<Factory
>;
53 // BrowserContextKeyedServiceFactory
54 virtual KeyedService
* BuildServiceInstanceFor(
55 content::BrowserContext
* context
) const OVERRIDE
;
56 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE
;
57 virtual content::BrowserContext
* GetBrowserContextToUse(
58 content::BrowserContext
* context
) const OVERRIDE
;
63 virtual void OnGeometryCacheChanged(const std::string
& extension_id
,
64 const std::string
& window_id
,
65 const gfx::Rect
& bounds
) = 0;
68 virtual ~Observer() {}
71 AppWindowGeometryCache(Profile
* profile
, 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 // content::NotificationObserver
126 virtual void Observe(int type
,
127 const content::NotificationSource
& source
,
128 const content::NotificationDetails
& details
) OVERRIDE
;
130 void LoadGeometryFromStorage(const std::string
& extension_id
);
131 void OnExtensionUnloaded(const std::string
& extension_id
);
132 void SyncToStorage();
134 // Preferences storage.
135 extensions::ExtensionPrefs
* prefs_
;
138 std::map
<std::string
, ExtensionData
> cache_
;
140 // Data that still needs saving
141 std::set
<std::string
> unsynced_extensions_
;
143 // The timer used to save the data
144 base::OneShotTimer
<AppWindowGeometryCache
> sync_timer_
;
146 // The timeout value we'll use for |sync_timer_|.
147 base::TimeDelta sync_delay_
;
149 content::NotificationRegistrar registrar_
;
150 ObserverList
<Observer
> observers_
;
155 #endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_