Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / apps / app_window_geometry_cache.h
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.
4
5 #ifndef APPS_APP_WINDOW_GEOMETRY_CACHE_H_
6 #define APPS_APP_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/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"
24
25 class Profile;
26
27 namespace extensions {
28 class ExtensionPrefs;
29 }
30
31 namespace apps {
32
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
35 // geometry change.
36 class AppWindowGeometryCache : public KeyedService,
37                                public content::NotificationObserver {
38  public:
39   class Factory : public BrowserContextKeyedServiceFactory {
40    public:
41     static AppWindowGeometryCache* GetForContext(
42         content::BrowserContext* context,
43         bool create);
44
45     static Factory* GetInstance();
46
47    private:
48     friend struct DefaultSingletonTraits<Factory>;
49
50     Factory();
51     virtual ~Factory();
52
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;
59   };
60
61   class Observer {
62    public:
63     virtual void OnGeometryCacheChanged(const std::string& extension_id,
64                                         const std::string& window_id,
65                                         const gfx::Rect& bounds) = 0;
66
67    protected:
68     virtual ~Observer() {}
69   };
70
71   AppWindowGeometryCache(Profile* profile, extensions::ExtensionPrefs* prefs);
72
73   virtual ~AppWindowGeometryCache();
74
75   // Returns the instance for the given browsing context.
76   static AppWindowGeometryCache* Get(content::BrowserContext* context);
77
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);
84
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,
90                    gfx::Rect* bounds,
91                    gfx::Rect* screen_bounds,
92                    ui::WindowShowState* state);
93
94   // KeyedService
95   virtual void Shutdown() OVERRIDE;
96
97   void AddObserver(Observer* observer);
98   void RemoveObserver(Observer* observer);
99
100   // Maximum number of windows we'll cache the geometry for per app.
101   static const size_t kMaxCachedWindows = 100;
102
103  protected:
104   friend class AppWindowGeometryCacheTest;
105
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);
110
111  private:
112   // Data stored for each window.
113   struct WindowData {
114     WindowData();
115     ~WindowData();
116     gfx::Rect bounds;
117     gfx::Rect screen_bounds;
118     ui::WindowShowState window_state;
119     base::Time last_change;
120   };
121
122   // Data stored for each extension.
123   typedef std::map<std::string, WindowData> ExtensionData;
124
125   // content::NotificationObserver
126   virtual void Observe(int type,
127                        const content::NotificationSource& source,
128                        const content::NotificationDetails& details) OVERRIDE;
129
130   void LoadGeometryFromStorage(const std::string& extension_id);
131   void OnExtensionUnloaded(const std::string& extension_id);
132   void SyncToStorage();
133
134   // Preferences storage.
135   extensions::ExtensionPrefs* prefs_;
136
137   // Cached data
138   std::map<std::string, ExtensionData> cache_;
139
140   // Data that still needs saving
141   std::set<std::string> unsynced_extensions_;
142
143   // The timer used to save the data
144   base::OneShotTimer<AppWindowGeometryCache> sync_timer_;
145
146   // The timeout value we'll use for |sync_timer_|.
147   base::TimeDelta sync_delay_;
148
149   content::NotificationRegistrar registrar_;
150   ObserverList<Observer> observers_;
151 };
152
153 }  // namespace apps
154
155 #endif  // APPS_APP_WINDOW_GEOMETRY_CACHE_H_