Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / apps / app_window_geometry_cache.cc
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 #include "apps/app_window_geometry_cache.h"
6
7 #include "base/bind.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "components/keyed_service/content/browser_context_dependency_manager.h"
11 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_prefs_factory.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extensions_browser_client.h"
15 #include "extensions/common/extension.h"
16
17 namespace {
18
19 // The timeout in milliseconds before we'll persist window geometry to the
20 // StateStore.
21 const int kSyncTimeoutMilliseconds = 1000;
22
23 }  // namespace
24
25 namespace apps {
26
27 AppWindowGeometryCache::AppWindowGeometryCache(
28     content::BrowserContext* context,
29     extensions::ExtensionPrefs* prefs)
30     : prefs_(prefs),
31       sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)),
32       extension_registry_observer_(this) {
33   extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(context));
34 }
35
36 AppWindowGeometryCache::~AppWindowGeometryCache() {}
37
38 // static
39 AppWindowGeometryCache* AppWindowGeometryCache::Get(
40     content::BrowserContext* context) {
41   return Factory::GetForContext(context, true /* create */);
42 }
43
44 void AppWindowGeometryCache::SaveGeometry(const std::string& extension_id,
45                                           const std::string& window_id,
46                                           const gfx::Rect& bounds,
47                                           const gfx::Rect& screen_bounds,
48                                           ui::WindowShowState window_state) {
49   ExtensionData& extension_data = cache_[extension_id];
50
51   // If we don't have any unsynced changes and this is a duplicate of what's
52   // already in the cache, just ignore it.
53   if (extension_data[window_id].bounds == bounds &&
54       extension_data[window_id].window_state == window_state &&
55       extension_data[window_id].screen_bounds == screen_bounds &&
56       !ContainsKey(unsynced_extensions_, extension_id))
57     return;
58
59   base::Time now = base::Time::Now();
60
61   extension_data[window_id].bounds = bounds;
62   extension_data[window_id].screen_bounds = screen_bounds;
63   extension_data[window_id].window_state = window_state;
64   extension_data[window_id].last_change = now;
65
66   if (extension_data.size() > kMaxCachedWindows) {
67     ExtensionData::iterator oldest = extension_data.end();
68     // Too many windows in the cache, find the oldest one to remove.
69     for (ExtensionData::iterator it = extension_data.begin();
70          it != extension_data.end();
71          ++it) {
72       // Don't expunge the window that was just added.
73       if (it->first == window_id)
74         continue;
75
76       // If time is in the future, reset it to now to minimize weirdness.
77       if (it->second.last_change > now)
78         it->second.last_change = now;
79
80       if (oldest == extension_data.end() ||
81           it->second.last_change < oldest->second.last_change)
82         oldest = it;
83     }
84     extension_data.erase(oldest);
85   }
86
87   unsynced_extensions_.insert(extension_id);
88
89   // We don't use Reset() because the timer may not yet be running.
90   // (In that case Stop() is a no-op.)
91   sync_timer_.Stop();
92   sync_timer_.Start(
93       FROM_HERE, sync_delay_, this, &AppWindowGeometryCache::SyncToStorage);
94 }
95
96 void AppWindowGeometryCache::SyncToStorage() {
97   std::set<std::string> tosync;
98   tosync.swap(unsynced_extensions_);
99   for (std::set<std::string>::const_iterator it = tosync.begin(),
100                                              eit = tosync.end();
101        it != eit;
102        ++it) {
103     const std::string& extension_id = *it;
104     const ExtensionData& extension_data = cache_[extension_id];
105
106     scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
107     for (ExtensionData::const_iterator it = extension_data.begin(),
108                                        eit = extension_data.end();
109          it != eit;
110          ++it) {
111       base::DictionaryValue* value = new base::DictionaryValue;
112       const gfx::Rect& bounds = it->second.bounds;
113       const gfx::Rect& screen_bounds = it->second.screen_bounds;
114       DCHECK(!bounds.IsEmpty());
115       DCHECK(!screen_bounds.IsEmpty());
116       DCHECK(it->second.window_state != ui::SHOW_STATE_DEFAULT);
117       value->SetInteger("x", bounds.x());
118       value->SetInteger("y", bounds.y());
119       value->SetInteger("w", bounds.width());
120       value->SetInteger("h", bounds.height());
121       value->SetInteger("screen_bounds_x", screen_bounds.x());
122       value->SetInteger("screen_bounds_y", screen_bounds.y());
123       value->SetInteger("screen_bounds_w", screen_bounds.width());
124       value->SetInteger("screen_bounds_h", screen_bounds.height());
125       value->SetInteger("state", it->second.window_state);
126       value->SetString(
127           "ts", base::Int64ToString(it->second.last_change.ToInternalValue()));
128       dict->SetWithoutPathExpansion(it->first, value);
129
130       FOR_EACH_OBSERVER(
131           Observer,
132           observers_,
133           OnGeometryCacheChanged(extension_id, it->first, bounds));
134     }
135
136     prefs_->SetGeometryCache(extension_id, dict.Pass());
137   }
138 }
139
140 bool AppWindowGeometryCache::GetGeometry(const std::string& extension_id,
141                                          const std::string& window_id,
142                                          gfx::Rect* bounds,
143                                          gfx::Rect* screen_bounds,
144                                          ui::WindowShowState* window_state) {
145   std::map<std::string, ExtensionData>::const_iterator extension_data_it =
146       cache_.find(extension_id);
147
148   // Not in the map means loading data for the extension didn't finish yet or
149   // the cache was not constructed until after the extension was loaded.
150   // Attempt to load from sync to address the latter case.
151   if (extension_data_it == cache_.end()) {
152     LoadGeometryFromStorage(extension_id);
153     extension_data_it = cache_.find(extension_id);
154     DCHECK(extension_data_it != cache_.end());
155   }
156
157   ExtensionData::const_iterator window_data_it =
158       extension_data_it->second.find(window_id);
159
160   if (window_data_it == extension_data_it->second.end())
161     return false;
162
163   const WindowData& window_data = window_data_it->second;
164
165   // Check for and do not return corrupt data.
166   if ((bounds && window_data.bounds.IsEmpty()) ||
167       (screen_bounds && window_data.screen_bounds.IsEmpty()) ||
168       (window_state && window_data.window_state == ui::SHOW_STATE_DEFAULT))
169     return false;
170
171   if (bounds)
172     *bounds = window_data.bounds;
173   if (screen_bounds)
174     *screen_bounds = window_data.screen_bounds;
175   if (window_state)
176     *window_state = window_data.window_state;
177   return true;
178 }
179
180 void AppWindowGeometryCache::Shutdown() { SyncToStorage(); }
181
182 AppWindowGeometryCache::WindowData::WindowData()
183     : window_state(ui::SHOW_STATE_DEFAULT) {}
184
185 AppWindowGeometryCache::WindowData::~WindowData() {}
186
187 void AppWindowGeometryCache::OnExtensionLoaded(
188     content::BrowserContext* browser_context,
189     const extensions::Extension* extension) {
190   LoadGeometryFromStorage(extension->id());
191 }
192
193 void AppWindowGeometryCache::OnExtensionUnloaded(
194     content::BrowserContext* browser_context,
195     const extensions::Extension* extension,
196     extensions::UnloadedExtensionInfo::Reason reason) {
197   SyncToStorage();
198   cache_.erase(extension->id());
199 }
200
201 void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) {
202   sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms);
203 }
204
205 void AppWindowGeometryCache::LoadGeometryFromStorage(
206     const std::string& extension_id) {
207   ExtensionData& extension_data = cache_[extension_id];
208
209   const base::DictionaryValue* stored_windows =
210       prefs_->GetGeometryCache(extension_id);
211   if (!stored_windows)
212     return;
213
214   for (base::DictionaryValue::Iterator it(*stored_windows); !it.IsAtEnd();
215        it.Advance()) {
216     // If the cache already contains geometry for this window, don't
217     // overwrite that information since it is probably the result of an
218     // application starting up very quickly.
219     const std::string& window_id = it.key();
220     ExtensionData::iterator cached_window = extension_data.find(window_id);
221     if (cached_window == extension_data.end()) {
222       const base::DictionaryValue* stored_window;
223       if (it.value().GetAsDictionary(&stored_window)) {
224         WindowData& window_data = extension_data[it.key()];
225
226         int i;
227         if (stored_window->GetInteger("x", &i))
228           window_data.bounds.set_x(i);
229         if (stored_window->GetInteger("y", &i))
230           window_data.bounds.set_y(i);
231         if (stored_window->GetInteger("w", &i))
232           window_data.bounds.set_width(i);
233         if (stored_window->GetInteger("h", &i))
234           window_data.bounds.set_height(i);
235         if (stored_window->GetInteger("screen_bounds_x", &i))
236           window_data.screen_bounds.set_x(i);
237         if (stored_window->GetInteger("screen_bounds_y", &i))
238           window_data.screen_bounds.set_y(i);
239         if (stored_window->GetInteger("screen_bounds_w", &i))
240           window_data.screen_bounds.set_width(i);
241         if (stored_window->GetInteger("screen_bounds_h", &i))
242           window_data.screen_bounds.set_height(i);
243         if (stored_window->GetInteger("state", &i)) {
244           window_data.window_state = static_cast<ui::WindowShowState>(i);
245         }
246         std::string ts_as_string;
247         if (stored_window->GetString("ts", &ts_as_string)) {
248           int64 ts;
249           if (base::StringToInt64(ts_as_string, &ts)) {
250             window_data.last_change = base::Time::FromInternalValue(ts);
251           }
252         }
253       }
254     }
255   }
256 }
257
258 ///////////////////////////////////////////////////////////////////////////////
259 // Factory boilerplate
260
261 // static
262 AppWindowGeometryCache* AppWindowGeometryCache::Factory::GetForContext(
263     content::BrowserContext* context,
264     bool create) {
265   return static_cast<AppWindowGeometryCache*>(
266       GetInstance()->GetServiceForBrowserContext(context, create));
267 }
268
269 AppWindowGeometryCache::Factory*
270 AppWindowGeometryCache::Factory::GetInstance() {
271   return Singleton<AppWindowGeometryCache::Factory>::get();
272 }
273
274 AppWindowGeometryCache::Factory::Factory()
275     : BrowserContextKeyedServiceFactory(
276           "AppWindowGeometryCache",
277           BrowserContextDependencyManager::GetInstance()) {
278   DependsOn(extensions::ExtensionPrefsFactory::GetInstance());
279 }
280
281 AppWindowGeometryCache::Factory::~Factory() {}
282
283 KeyedService* AppWindowGeometryCache::Factory::BuildServiceInstanceFor(
284     content::BrowserContext* context) const {
285   return new AppWindowGeometryCache(context,
286                                     extensions::ExtensionPrefs::Get(context));
287 }
288
289 bool AppWindowGeometryCache::Factory::ServiceIsNULLWhileTesting() const {
290   return false;
291 }
292
293 content::BrowserContext*
294 AppWindowGeometryCache::Factory::GetBrowserContextToUse(
295     content::BrowserContext* context) const {
296   return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(
297       context);
298 }
299
300 void AppWindowGeometryCache::AddObserver(Observer* observer) {
301   observers_.AddObserver(observer);
302 }
303
304 void AppWindowGeometryCache::RemoveObserver(Observer* observer) {
305   observers_.RemoveObserver(observer);
306 }
307
308 }  // namespace apps