blob: 744077cb2dd3eb3f73d674763d7365de01cd7946 [file] [log] [blame]
[email protected]cb610dc2012-08-31 17:16:561// Copyright (c) 2012 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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_
6#define CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/memory/scoped_ptr.h"
13#include "base/memory/weak_ptr.h"
[email protected]af3920f2012-12-13 00:49:2414#include "base/time.h"
[email protected]cb610dc2012-08-31 17:16:5615#include "base/timer.h"
16#include "base/values.h"
17#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
19#include "ui/gfx/rect.h"
20
21class Profile;
22
23namespace extensions {
24
[email protected]af3920f2012-12-13 00:49:2425class ExtensionPrefs;
[email protected]cb610dc2012-08-31 17:16:5626
27// A cache for persisted geometry of shell windows, both to not have to wait
28// for IO when creating a new window, and to not cause IO on every window
29// geometry change.
30class ShellWindowGeometryCache
31 : public base::SupportsWeakPtr<ShellWindowGeometryCache>,
32 public content::NotificationObserver {
33 public:
[email protected]af3920f2012-12-13 00:49:2434 ShellWindowGeometryCache(Profile* profile,
35 ExtensionPrefs* prefs);
[email protected]cb610dc2012-08-31 17:16:5636
37 virtual ~ShellWindowGeometryCache();
38
39 void SaveGeometry(const std::string& extension_id,
[email protected]2b0fc08a2012-11-07 23:54:2640 const std::string& window_id,
41 const gfx::Rect& bounds);
[email protected]cb610dc2012-08-31 17:16:5642
43 bool GetGeometry(const std::string& extension_id,
[email protected]2b0fc08a2012-11-07 23:54:2644 const std::string& window_id,
45 gfx::Rect* bounds) const;
46
[email protected]af3920f2012-12-13 00:49:2447 // Maximum number of windows we'll cache the geometry for per app.
48 static const size_t kMaxCachedWindows = 100;
49
[email protected]2b0fc08a2012-11-07 23:54:2650 protected:
51 friend class ShellWindowGeometryCacheTest;
52
53 // For tests, this modifies the timeout delay for saving changes from calls
54 // to SaveGeometry. (Note that even if this is set to 0, you still need to
55 // run the message loop to see the results of any SyncToStorage call).
56 void SetSyncDelayForTests(int timeout_ms);
[email protected]cb610dc2012-08-31 17:16:5657
58 private:
[email protected]af3920f2012-12-13 00:49:2459 // Data stored for each window.
60 struct WindowData {
61 gfx::Rect bounds;
62 base::Time last_change;
63 };
64
65 // Data stored for each extension.
66 typedef std::map<std::string, WindowData> ExtensionData;
67
[email protected]cb610dc2012-08-31 17:16:5668 // content::NotificationObserver
69 virtual void Observe(int type,
70 const content::NotificationSource& source,
71 const content::NotificationDetails& details) OVERRIDE;
72
73 void OnExtensionLoaded(const std::string& extension_id);
74 void OnExtensionUnloaded(const std::string& extension_id);
[email protected]cb610dc2012-08-31 17:16:5675 void SyncToStorage();
76
[email protected]af3920f2012-12-13 00:49:2477 // Preferences storage.
78 ExtensionPrefs* prefs_;
[email protected]cb610dc2012-08-31 17:16:5679
80 // Cached data
[email protected]af3920f2012-12-13 00:49:2481 std::map<std::string, ExtensionData> cache_;
[email protected]cb610dc2012-08-31 17:16:5682
83 // Data that still needs saving
84 std::set<std::string> unsynced_extensions_;
85
86 // The timer used to save the data
87 base::OneShotTimer<ShellWindowGeometryCache> sync_timer_;
88
[email protected]2b0fc08a2012-11-07 23:54:2689 // The timeout value we'll use for |sync_timer_|.
90 base::TimeDelta sync_delay_;
91
[email protected]cb610dc2012-08-31 17:16:5692 content::NotificationRegistrar registrar_;
93};
94
95} // namespace extensions
96
97#endif // CHROME_BROWSER_EXTENSIONS_SHELL_WINDOW_GEOMETRY_CACHE_H_