blob: 8813eea2b08156be6131e370d4e9dbc87fa42468 [file] [log] [blame]
[email protected]dbb03fb2014-02-15 05:36:331// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]cb610dc2012-08-31 17:16:562// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]dbb03fb2014-02-15 05:36:335#ifndef APPS_APP_WINDOW_GEOMETRY_CACHE_H_
6#define APPS_APP_WINDOW_GEOMETRY_CACHE_H_
[email protected]cb610dc2012-08-31 17:16:567
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/memory/scoped_ptr.h"
[email protected]71e0c302013-05-17 09:26:1513#include "base/memory/singleton.h"
[email protected]6349da02013-11-29 17:08:1814#include "base/observer_list.h"
[email protected]829f0462014-05-10 04:40:0015#include "base/scoped_observer.h"
[email protected]1e84c632013-06-27 23:12:2116#include "base/time/time.h"
17#include "base/timer/timer.h"
[email protected]cb610dc2012-08-31 17:16:5618#include "base/values.h"
[email protected]12b7af32014-03-13 05:28:2019#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
20#include "components/keyed_service/core/keyed_service.h"
[email protected]829f0462014-05-10 04:40:0021#include "extensions/browser/extension_registry_observer.h"
[email protected]68eeede2013-05-09 06:10:5722#include "ui/base/ui_base_types.h"
[email protected]cb610dc2012-08-31 17:16:5623#include "ui/gfx/rect.h"
24
25class Profile;
26
27namespace extensions {
[email protected]af3920f2012-12-13 00:49:2428class ExtensionPrefs;
[email protected]829f0462014-05-10 04:40:0029class ExtensionRegistry;
[email protected]71e0c302013-05-17 09:26:1530}
31
32namespace apps {
[email protected]cb610dc2012-08-31 17:16:5633
[email protected]dbb03fb2014-02-15 05:36:3334// A cache for persisted geometry of app windows, both to not have to wait
[email protected]cb610dc2012-08-31 17:16:5635// for IO when creating a new window, and to not cause IO on every window
36// geometry change.
[email protected]12b7af32014-03-13 05:28:2037class AppWindowGeometryCache : public KeyedService,
[email protected]829f0462014-05-10 04:40:0038 public extensions::ExtensionRegistryObserver {
[email protected]cb610dc2012-08-31 17:16:5639 public:
[email protected]f1484c52013-05-22 23:25:4440 class Factory : public BrowserContextKeyedServiceFactory {
[email protected]71e0c302013-05-17 09:26:1541 public:
[email protected]dbb03fb2014-02-15 05:36:3342 static AppWindowGeometryCache* GetForContext(
[email protected]71e0c302013-05-17 09:26:1543 content::BrowserContext* context,
44 bool create);
45
46 static Factory* GetInstance();
[email protected]dbb03fb2014-02-15 05:36:3347
[email protected]71e0c302013-05-17 09:26:1548 private:
49 friend struct DefaultSingletonTraits<Factory>;
50
51 Factory();
52 virtual ~Factory();
53
[email protected]f1484c52013-05-22 23:25:4454 // BrowserContextKeyedServiceFactory
[email protected]12b7af32014-03-13 05:28:2055 virtual KeyedService* BuildServiceInstanceFor(
[email protected]71e0c302013-05-17 09:26:1556 content::BrowserContext* context) const OVERRIDE;
57 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
58 virtual content::BrowserContext* GetBrowserContextToUse(
59 content::BrowserContext* context) const OVERRIDE;
60 };
61
[email protected]6349da02013-11-29 17:08:1862 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;
67
68 protected:
[email protected]2919a5e2014-04-24 08:34:0569 virtual ~Observer() {}
[email protected]6349da02013-11-29 17:08:1870 };
71
[email protected]dbb03fb2014-02-15 05:36:3372 AppWindowGeometryCache(Profile* profile, extensions::ExtensionPrefs* prefs);
[email protected]cb610dc2012-08-31 17:16:5673
[email protected]dbb03fb2014-02-15 05:36:3374 virtual ~AppWindowGeometryCache();
[email protected]cb610dc2012-08-31 17:16:5675
[email protected]71e0c302013-05-17 09:26:1576 // Returns the instance for the given browsing context.
[email protected]dbb03fb2014-02-15 05:36:3377 static AppWindowGeometryCache* Get(content::BrowserContext* context);
[email protected]71e0c302013-05-17 09:26:1578
[email protected]68eeede2013-05-09 06:10:5779 // Save the geometry and state associated with |extension_id| and |window_id|.
[email protected]cb610dc2012-08-31 17:16:5680 void SaveGeometry(const std::string& extension_id,
[email protected]2b0fc08a2012-11-07 23:54:2681 const std::string& window_id,
[email protected]68eeede2013-05-09 06:10:5782 const gfx::Rect& bounds,
[email protected]36e44c62013-06-23 14:20:5883 const gfx::Rect& screen_bounds,
[email protected]68eeede2013-05-09 06:10:5784 ui::WindowShowState state);
[email protected]cb610dc2012-08-31 17:16:5685
[email protected]68eeede2013-05-09 06:10:5786 // Get any saved geometry and state associated with |extension_id| and
[email protected]837af562013-08-01 20:12:4287 // |window_id|. If saved data exists, sets |bounds|, |screen_bounds| and
88 // |state| if not NULL and returns true.
[email protected]cb610dc2012-08-31 17:16:5689 bool GetGeometry(const std::string& extension_id,
[email protected]2b0fc08a2012-11-07 23:54:2690 const std::string& window_id,
[email protected]68eeede2013-05-09 06:10:5791 gfx::Rect* bounds,
[email protected]36e44c62013-06-23 14:20:5892 gfx::Rect* screen_bounds,
[email protected]e9f195e2013-06-13 06:37:4293 ui::WindowShowState* state);
[email protected]2b0fc08a2012-11-07 23:54:2694
[email protected]12b7af32014-03-13 05:28:2095 // KeyedService
[email protected]71e0c302013-05-17 09:26:1596 virtual void Shutdown() OVERRIDE;
97
[email protected]6349da02013-11-29 17:08:1898 void AddObserver(Observer* observer);
99 void RemoveObserver(Observer* observer);
100
[email protected]af3920f2012-12-13 00:49:24101 // Maximum number of windows we'll cache the geometry for per app.
102 static const size_t kMaxCachedWindows = 100;
103
[email protected]2b0fc08a2012-11-07 23:54:26104 protected:
[email protected]dbb03fb2014-02-15 05:36:33105 friend class AppWindowGeometryCacheTest;
[email protected]2b0fc08a2012-11-07 23:54:26106
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);
[email protected]cb610dc2012-08-31 17:16:56111
112 private:
[email protected]af3920f2012-12-13 00:49:24113 // Data stored for each window.
114 struct WindowData {
[email protected]36e44c62013-06-23 14:20:58115 WindowData();
116 ~WindowData();
[email protected]af3920f2012-12-13 00:49:24117 gfx::Rect bounds;
[email protected]36e44c62013-06-23 14:20:58118 gfx::Rect screen_bounds;
[email protected]68eeede2013-05-09 06:10:57119 ui::WindowShowState window_state;
[email protected]af3920f2012-12-13 00:49:24120 base::Time last_change;
121 };
122
123 // Data stored for each extension.
124 typedef std::map<std::string, WindowData> ExtensionData;
125
[email protected]829f0462014-05-10 04:40:00126 // 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;
[email protected]cb610dc2012-08-31 17:16:56134
[email protected]e9f195e2013-06-13 06:37:42135 void LoadGeometryFromStorage(const std::string& extension_id);
[email protected]cb610dc2012-08-31 17:16:56136 void SyncToStorage();
137
[email protected]af3920f2012-12-13 00:49:24138 // Preferences storage.
[email protected]71e0c302013-05-17 09:26:15139 extensions::ExtensionPrefs* prefs_;
[email protected]cb610dc2012-08-31 17:16:56140
[email protected]829f0462014-05-10 04:40:00141 // Cached data.
[email protected]af3920f2012-12-13 00:49:24142 std::map<std::string, ExtensionData> cache_;
[email protected]cb610dc2012-08-31 17:16:56143
[email protected]829f0462014-05-10 04:40:00144 // Data that still needs saving.
[email protected]cb610dc2012-08-31 17:16:56145 std::set<std::string> unsynced_extensions_;
146
[email protected]829f0462014-05-10 04:40:00147 // The timer used to save the data.
[email protected]dbb03fb2014-02-15 05:36:33148 base::OneShotTimer<AppWindowGeometryCache> sync_timer_;
[email protected]cb610dc2012-08-31 17:16:56149
[email protected]2b0fc08a2012-11-07 23:54:26150 // The timeout value we'll use for |sync_timer_|.
151 base::TimeDelta sync_delay_;
152
[email protected]829f0462014-05-10 04:40:00153 // Listen to extension load, unloaded notifications.
154 ScopedObserver<extensions::ExtensionRegistry,
155 extensions::ExtensionRegistryObserver>
156 extension_registry_observer_;
157
[email protected]6349da02013-11-29 17:08:18158 ObserverList<Observer> observers_;
[email protected]cb610dc2012-08-31 17:16:56159};
160
[email protected]71e0c302013-05-17 09:26:15161} // namespace apps
[email protected]cb610dc2012-08-31 17:16:56162
[email protected]dbb03fb2014-02-15 05:36:33163#endif // APPS_APP_WINDOW_GEOMETRY_CACHE_H_