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 #include "apps/app_window_geometry_cache.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"
19 // The timeout in milliseconds before we'll persist window geometry to the
21 const int kSyncTimeoutMilliseconds = 1000;
27 AppWindowGeometryCache::AppWindowGeometryCache(
28 content::BrowserContext* context,
29 extensions::ExtensionPrefs* prefs)
31 sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)),
32 extension_registry_observer_(this) {
33 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(context));
36 AppWindowGeometryCache::~AppWindowGeometryCache() {}
39 AppWindowGeometryCache* AppWindowGeometryCache::Get(
40 content::BrowserContext* context) {
41 return Factory::GetForContext(context, true /* create */);
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];
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))
59 base::Time now = base::Time::Now();
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;
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();
72 // Don't expunge the window that was just added.
73 if (it->first == window_id)
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;
80 if (oldest == extension_data.end() ||
81 it->second.last_change < oldest->second.last_change)
84 extension_data.erase(oldest);
87 unsynced_extensions_.insert(extension_id);
89 // We don't use Reset() because the timer may not yet be running.
90 // (In that case Stop() is a no-op.)
93 FROM_HERE, sync_delay_, this, &AppWindowGeometryCache::SyncToStorage);
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(),
103 const std::string& extension_id = *it;
104 const ExtensionData& extension_data = cache_[extension_id];
106 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
107 for (ExtensionData::const_iterator it = extension_data.begin(),
108 eit = extension_data.end();
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);
127 "ts", base::Int64ToString(it->second.last_change.ToInternalValue()));
128 dict->SetWithoutPathExpansion(it->first, value);
133 OnGeometryCacheChanged(extension_id, it->first, bounds));
136 prefs_->SetGeometryCache(extension_id, dict.Pass());
140 bool AppWindowGeometryCache::GetGeometry(const std::string& extension_id,
141 const std::string& window_id,
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);
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());
157 ExtensionData::const_iterator window_data_it =
158 extension_data_it->second.find(window_id);
160 if (window_data_it == extension_data_it->second.end())
163 const WindowData& window_data = window_data_it->second;
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))
172 *bounds = window_data.bounds;
174 *screen_bounds = window_data.screen_bounds;
176 *window_state = window_data.window_state;
180 void AppWindowGeometryCache::Shutdown() { SyncToStorage(); }
182 AppWindowGeometryCache::WindowData::WindowData()
183 : window_state(ui::SHOW_STATE_DEFAULT) {}
185 AppWindowGeometryCache::WindowData::~WindowData() {}
187 void AppWindowGeometryCache::OnExtensionLoaded(
188 content::BrowserContext* browser_context,
189 const extensions::Extension* extension) {
190 LoadGeometryFromStorage(extension->id());
193 void AppWindowGeometryCache::OnExtensionUnloaded(
194 content::BrowserContext* browser_context,
195 const extensions::Extension* extension,
196 extensions::UnloadedExtensionInfo::Reason reason) {
198 cache_.erase(extension->id());
201 void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) {
202 sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms);
205 void AppWindowGeometryCache::LoadGeometryFromStorage(
206 const std::string& extension_id) {
207 ExtensionData& extension_data = cache_[extension_id];
209 const base::DictionaryValue* stored_windows =
210 prefs_->GetGeometryCache(extension_id);
214 for (base::DictionaryValue::Iterator it(*stored_windows); !it.IsAtEnd();
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()];
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);
246 std::string ts_as_string;
247 if (stored_window->GetString("ts", &ts_as_string)) {
249 if (base::StringToInt64(ts_as_string, &ts)) {
250 window_data.last_change = base::Time::FromInternalValue(ts);
258 ///////////////////////////////////////////////////////////////////////////////
259 // Factory boilerplate
262 AppWindowGeometryCache* AppWindowGeometryCache::Factory::GetForContext(
263 content::BrowserContext* context,
265 return static_cast<AppWindowGeometryCache*>(
266 GetInstance()->GetServiceForBrowserContext(context, create));
269 AppWindowGeometryCache::Factory*
270 AppWindowGeometryCache::Factory::GetInstance() {
271 return Singleton<AppWindowGeometryCache::Factory>::get();
274 AppWindowGeometryCache::Factory::Factory()
275 : BrowserContextKeyedServiceFactory(
276 "AppWindowGeometryCache",
277 BrowserContextDependencyManager::GetInstance()) {
278 DependsOn(extensions::ExtensionPrefsFactory::GetInstance());
281 AppWindowGeometryCache::Factory::~Factory() {}
283 KeyedService* AppWindowGeometryCache::Factory::BuildServiceInstanceFor(
284 content::BrowserContext* context) const {
285 return new AppWindowGeometryCache(context,
286 extensions::ExtensionPrefs::Get(context));
289 bool AppWindowGeometryCache::Factory::ServiceIsNULLWhileTesting() const {
293 content::BrowserContext*
294 AppWindowGeometryCache::Factory::GetBrowserContextToUse(
295 content::BrowserContext* context) const {
296 return extensions::ExtensionsBrowserClient::Get()->GetOriginalContext(
300 void AppWindowGeometryCache::AddObserver(Observer* observer) {
301 observers_.AddObserver(observer);
304 void AppWindowGeometryCache::RemoveObserver(Observer* observer) {
305 observers_.RemoveObserver(observer);