Files.app: Override overflow specifying to use the original scroll bar.
[chromium-blink-merge.git] / apps / app_window_geometry_cache.h
blob8813eea2b08156be6131e370d4e9dbc87fa42468
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_
8 #include <map>
9 #include <set>
10 #include <string>
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 class Profile;
27 namespace extensions {
28 class ExtensionPrefs;
29 class ExtensionRegistry;
32 namespace apps {
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
36 // geometry change.
37 class AppWindowGeometryCache : public KeyedService,
38 public extensions::ExtensionRegistryObserver {
39 public:
40 class Factory : public BrowserContextKeyedServiceFactory {
41 public:
42 static AppWindowGeometryCache* GetForContext(
43 content::BrowserContext* context,
44 bool create);
46 static Factory* GetInstance();
48 private:
49 friend struct DefaultSingletonTraits<Factory>;
51 Factory();
52 virtual ~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;
62 class Observer {
63 public:
64 virtual void OnGeometryCacheChanged(const std::string& extension_id,
65 const std::string& window_id,
66 const gfx::Rect& bounds) = 0;
68 protected:
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,
91 gfx::Rect* bounds,
92 gfx::Rect* screen_bounds,
93 ui::WindowShowState* state);
95 // KeyedService
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;
104 protected:
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);
112 private:
113 // Data stored for each window.
114 struct WindowData {
115 WindowData();
116 ~WindowData();
117 gfx::Rect bounds;
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_;
141 // Cached data.
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_;
161 } // namespace apps
163 #endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_