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