| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/app_window_geometry_cache.h" | 5 #include "apps/app_window_geometry_cache.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "chrome/browser/chrome_notification_types.h" | |
| 11 #include "chrome/browser/profiles/incognito_helpers.h" | 10 #include "chrome/browser/profiles/incognito_helpers.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 13 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 12 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 #include "content/public/browser/notification_types.h" | |
| 16 #include "extensions/browser/extension_prefs.h" | 13 #include "extensions/browser/extension_prefs.h" |
| 17 #include "extensions/browser/extension_prefs_factory.h" | 14 #include "extensions/browser/extension_prefs_factory.h" |
| 15 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/browser/extensions_browser_client.h" | 16 #include "extensions/browser/extensions_browser_client.h" |
| 19 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 20 | 18 |
| 21 namespace { | 19 namespace { |
| 22 | 20 |
| 23 // The timeout in milliseconds before we'll persist window geometry to the | 21 // The timeout in milliseconds before we'll persist window geometry to the |
| 24 // StateStore. | 22 // StateStore. |
| 25 const int kSyncTimeoutMilliseconds = 1000; | 23 const int kSyncTimeoutMilliseconds = 1000; |
| 26 | 24 |
| 27 } // namespace | 25 } // namespace |
| 28 | 26 |
| 29 namespace apps { | 27 namespace apps { |
| 30 | 28 |
| 31 AppWindowGeometryCache::AppWindowGeometryCache( | 29 AppWindowGeometryCache::AppWindowGeometryCache( |
| 32 Profile* profile, | 30 Profile* profile, |
| 33 extensions::ExtensionPrefs* prefs) | 31 extensions::ExtensionPrefs* prefs) |
| 34 : prefs_(prefs), | 32 : prefs_(prefs), |
| 35 sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)) { | 33 sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)), |
| 36 registrar_.Add(this, | 34 extension_registry_observer_(this) { |
| 37 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 35 extension_registry_observer_.Add(extensions::ExtensionRegistry::Get(profile)); |
| 38 content::Source<Profile>(profile)); | |
| 39 registrar_.Add(this, | |
| 40 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
| 41 content::Source<Profile>(profile)); | |
| 42 } | 36 } |
| 43 | 37 |
| 44 AppWindowGeometryCache::~AppWindowGeometryCache() {} | 38 AppWindowGeometryCache::~AppWindowGeometryCache() {} |
| 45 | 39 |
| 46 // static | 40 // static |
| 47 AppWindowGeometryCache* AppWindowGeometryCache::Get( | 41 AppWindowGeometryCache* AppWindowGeometryCache::Get( |
| 48 content::BrowserContext* context) { | 42 content::BrowserContext* context) { |
| 49 return Factory::GetForContext(context, true /* create */); | 43 return Factory::GetForContext(context, true /* create */); |
| 50 } | 44 } |
| 51 | 45 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 return true; | 179 return true; |
| 186 } | 180 } |
| 187 | 181 |
| 188 void AppWindowGeometryCache::Shutdown() { SyncToStorage(); } | 182 void AppWindowGeometryCache::Shutdown() { SyncToStorage(); } |
| 189 | 183 |
| 190 AppWindowGeometryCache::WindowData::WindowData() | 184 AppWindowGeometryCache::WindowData::WindowData() |
| 191 : window_state(ui::SHOW_STATE_DEFAULT) {} | 185 : window_state(ui::SHOW_STATE_DEFAULT) {} |
| 192 | 186 |
| 193 AppWindowGeometryCache::WindowData::~WindowData() {} | 187 AppWindowGeometryCache::WindowData::~WindowData() {} |
| 194 | 188 |
| 195 void AppWindowGeometryCache::Observe( | 189 void AppWindowGeometryCache::OnExtensionLoaded( |
| 196 int type, | 190 content::BrowserContext* browser_context, |
| 197 const content::NotificationSource& source, | 191 const extensions::Extension* extension) { |
| 198 const content::NotificationDetails& details) { | 192 LoadGeometryFromStorage(extension->id()); |
| 199 switch (type) { | 193 } |
| 200 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | 194 |
| 201 std::string extension_id = | 195 void AppWindowGeometryCache::OnExtensionUnloaded( |
| 202 content::Details<const extensions::Extension>(details).ptr()->id(); | 196 content::BrowserContext* browser_context, |
| 203 LoadGeometryFromStorage(extension_id); | 197 const extensions::Extension* extension, |
| 204 break; | 198 extensions::UnloadedExtensionInfo::Reason reason) { |
| 205 } | 199 SyncToStorage(); |
| 206 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 200 cache_.erase(extension->id()); |
| 207 std::string extension_id = | |
| 208 content::Details<const extensions::UnloadedExtensionInfo>(details) | |
| 209 .ptr() | |
| 210 ->extension->id(); | |
| 211 OnExtensionUnloaded(extension_id); | |
| 212 break; | |
| 213 } | |
| 214 default: | |
| 215 NOTREACHED(); | |
| 216 return; | |
| 217 } | |
| 218 } | 201 } |
| 219 | 202 |
| 220 void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) { | 203 void AppWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) { |
| 221 sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms); | 204 sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms); |
| 222 } | 205 } |
| 223 | 206 |
| 224 void AppWindowGeometryCache::LoadGeometryFromStorage( | 207 void AppWindowGeometryCache::LoadGeometryFromStorage( |
| 225 const std::string& extension_id) { | 208 const std::string& extension_id) { |
| 226 ExtensionData& extension_data = cache_[extension_id]; | 209 ExtensionData& extension_data = cache_[extension_id]; |
| 227 | 210 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 int64 ts; | 250 int64 ts; |
| 268 if (base::StringToInt64(ts_as_string, &ts)) { | 251 if (base::StringToInt64(ts_as_string, &ts)) { |
| 269 window_data.last_change = base::Time::FromInternalValue(ts); | 252 window_data.last_change = base::Time::FromInternalValue(ts); |
| 270 } | 253 } |
| 271 } | 254 } |
| 272 } | 255 } |
| 273 } | 256 } |
| 274 } | 257 } |
| 275 } | 258 } |
| 276 | 259 |
| 277 void AppWindowGeometryCache::OnExtensionUnloaded( | |
| 278 const std::string& extension_id) { | |
| 279 SyncToStorage(); | |
| 280 cache_.erase(extension_id); | |
| 281 } | |
| 282 | |
| 283 /////////////////////////////////////////////////////////////////////////////// | 260 /////////////////////////////////////////////////////////////////////////////// |
| 284 // Factory boilerplate | 261 // Factory boilerplate |
| 285 | 262 |
| 286 // static | 263 // static |
| 287 AppWindowGeometryCache* AppWindowGeometryCache::Factory::GetForContext( | 264 AppWindowGeometryCache* AppWindowGeometryCache::Factory::GetForContext( |
| 288 content::BrowserContext* context, | 265 content::BrowserContext* context, |
| 289 bool create) { | 266 bool create) { |
| 290 return static_cast<AppWindowGeometryCache*>( | 267 return static_cast<AppWindowGeometryCache*>( |
| 291 GetInstance()->GetServiceForBrowserContext(context, create)); | 268 GetInstance()->GetServiceForBrowserContext(context, create)); |
| 292 } | 269 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 | 302 |
| 326 void AppWindowGeometryCache::AddObserver(Observer* observer) { | 303 void AppWindowGeometryCache::AddObserver(Observer* observer) { |
| 327 observers_.AddObserver(observer); | 304 observers_.AddObserver(observer); |
| 328 } | 305 } |
| 329 | 306 |
| 330 void AppWindowGeometryCache::RemoveObserver(Observer* observer) { | 307 void AppWindowGeometryCache::RemoveObserver(Observer* observer) { |
| 331 observers_.RemoveObserver(observer); | 308 observers_.RemoveObserver(observer); |
| 332 } | 309 } |
| 333 | 310 |
| 334 } // namespace apps | 311 } // namespace apps |
| OLD | NEW |