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.
5 #include "apps/shell_window_geometry_cache.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "chrome/browser/extensions/extension_prefs.h"
11 #include "chrome/browser/extensions/extension_prefs_factory.h"
12 #include "chrome/browser/profiles/incognito_helpers.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/profiles/profile_dependency_manager.h"
15 #include "chrome/common/chrome_notification_types.h"
16 #include "chrome/common/extensions/extension.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/browser/notification_types.h"
22 // The timeout in milliseconds before we'll persist window geometry to the
24 const int kSyncTimeoutMilliseconds
= 1000;
30 ShellWindowGeometryCache::ShellWindowGeometryCache(
31 Profile
* profile
, extensions::ExtensionPrefs
* 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
));
40 ShellWindowGeometryCache::~ShellWindowGeometryCache() {
44 ShellWindowGeometryCache
* ShellWindowGeometryCache::Get(
45 content::BrowserContext
* context
) {
46 return Factory::GetForContext(context
, true /* create */);
49 void ShellWindowGeometryCache::SaveGeometry(
50 const std::string
& extension_id
,
51 const std::string
& window_id
,
52 const gfx::Rect
& bounds
,
53 ui::WindowShowState window_state
) {
54 ExtensionData
& extension_data
= cache_
[extension_id
];
56 // If we don't have any unsynced changes and this is a duplicate of what's
57 // already in the cache, just ignore it.
58 if (extension_data
[window_id
].bounds
== bounds
&&
59 extension_data
[window_id
].window_state
== window_state
&&
60 !ContainsKey(unsynced_extensions_
, extension_id
))
63 base::Time now
= base::Time::Now();
65 extension_data
[window_id
].bounds
= bounds
;
66 extension_data
[window_id
].window_state
= window_state
;
67 extension_data
[window_id
].last_change
= now
;
69 if (extension_data
.size() > kMaxCachedWindows
) {
70 ExtensionData::iterator oldest
= extension_data
.end();
71 // Too many windows in the cache, find the oldest one to remove.
72 for (ExtensionData::iterator it
= extension_data
.begin();
73 it
!= extension_data
.end(); ++it
) {
74 // Don't expunge the window that was just added.
75 if (it
->first
== window_id
) continue;
77 // If time is in the future, reset it to now to minimize weirdness.
78 if (it
->second
.last_change
> now
)
79 it
->second
.last_change
= now
;
81 if (oldest
== extension_data
.end() ||
82 it
->second
.last_change
< oldest
->second
.last_change
)
85 extension_data
.erase(oldest
);
88 unsynced_extensions_
.insert(extension_id
);
90 // We don't use Reset() because the timer may not yet be running.
91 // (In that case Stop() is a no-op.)
93 sync_timer_
.Start(FROM_HERE
, sync_delay_
, this,
94 &ShellWindowGeometryCache::SyncToStorage
);
97 void ShellWindowGeometryCache::SyncToStorage() {
98 std::set
<std::string
> tosync
;
99 tosync
.swap(unsynced_extensions_
);
100 for (std::set
<std::string
>::const_iterator it
= tosync
.begin(),
101 eit
= tosync
.end(); it
!= eit
; ++it
) {
102 const std::string
& extension_id
= *it
;
103 const ExtensionData
& extension_data
= cache_
[extension_id
];
105 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
106 for (ExtensionData::const_iterator it
= extension_data
.begin(),
107 eit
= extension_data
.end(); it
!= eit
; ++it
) {
108 base::DictionaryValue
* value
= new base::DictionaryValue
;
109 const gfx::Rect
& bounds
= it
->second
.bounds
;
110 value
->SetInteger("x", bounds
.x());
111 value
->SetInteger("y", bounds
.y());
112 value
->SetInteger("w", bounds
.width());
113 value
->SetInteger("h", bounds
.height());
114 value
->SetInteger("state", it
->second
.window_state
);
116 "ts", base::Int64ToString(it
->second
.last_change
.ToInternalValue()));
117 dict
->SetWithoutPathExpansion(it
->first
, value
);
119 prefs_
->SetGeometryCache(extension_id
, dict
.Pass());
123 bool ShellWindowGeometryCache::GetGeometry(
124 const std::string
& extension_id
,
125 const std::string
& window_id
,
127 ui::WindowShowState
* window_state
) const {
129 std::map
<std::string
, ExtensionData
>::const_iterator
130 extension_data_it
= cache_
.find(extension_id
);
132 // Not in the map means loading data for the extension didn't finish yet.
133 if (extension_data_it
== cache_
.end())
136 ExtensionData::const_iterator window_data
= extension_data_it
->second
.find(
139 if (window_data
== extension_data_it
->second
.end())
143 *bounds
= window_data
->second
.bounds
;
145 *window_state
= window_data
->second
.window_state
;
149 void ShellWindowGeometryCache::Shutdown() {
153 void ShellWindowGeometryCache::Observe(
154 int type
, const content::NotificationSource
& source
,
155 const content::NotificationDetails
& details
) {
157 case chrome::NOTIFICATION_EXTENSION_LOADED
: {
158 std::string extension_id
=
159 content::Details
<const extensions::Extension
>(details
).ptr()->id();
160 OnExtensionLoaded(extension_id
);
163 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
164 std::string extension_id
=
165 content::Details
<const extensions::UnloadedExtensionInfo
>(details
).
166 ptr()->extension
->id();
167 OnExtensionUnloaded(extension_id
);
176 void ShellWindowGeometryCache::SetSyncDelayForTests(int timeout_ms
) {
177 sync_delay_
= base::TimeDelta::FromMilliseconds(timeout_ms
);
180 void ShellWindowGeometryCache::OnExtensionLoaded(
181 const std::string
& extension_id
) {
182 ExtensionData
& extension_data
= cache_
[extension_id
];
184 const base::DictionaryValue
* stored_windows
=
185 prefs_
->GetGeometryCache(extension_id
);
189 for (base::DictionaryValue::Iterator
it(*stored_windows
); !it
.IsAtEnd();
191 // If the cache already contains geometry for this window, don't
192 // overwrite that information since it is probably the result of an
193 // application starting up very quickly.
194 const std::string
& window_id
= it
.key();
195 ExtensionData::iterator cached_window
= extension_data
.find(window_id
);
196 if (cached_window
== extension_data
.end()) {
197 const base::DictionaryValue
* stored_window
;
198 if (it
.value().GetAsDictionary(&stored_window
)) {
199 WindowData
& window_data
= extension_data
[it
.key()];
202 if (stored_window
->GetInteger("x", &i
))
203 window_data
.bounds
.set_x(i
);
204 if (stored_window
->GetInteger("y", &i
))
205 window_data
.bounds
.set_y(i
);
206 if (stored_window
->GetInteger("w", &i
))
207 window_data
.bounds
.set_width(i
);
208 if (stored_window
->GetInteger("h", &i
))
209 window_data
.bounds
.set_height(i
);
210 if (stored_window
->GetInteger("state", &i
)) {
211 window_data
.window_state
=
212 static_cast<ui::WindowShowState
>(i
);
214 std::string ts_as_string
;
215 if (stored_window
->GetString("ts", &ts_as_string
)) {
217 if (base::StringToInt64(ts_as_string
, &ts
)) {
218 window_data
.last_change
= base::Time::FromInternalValue(ts
);
226 void ShellWindowGeometryCache::OnExtensionUnloaded(
227 const std::string
& extension_id
) {
229 cache_
.erase(extension_id
);
232 ///////////////////////////////////////////////////////////////////////////////
233 // Factory boilerplate
236 ShellWindowGeometryCache
* ShellWindowGeometryCache::Factory::GetForContext(
237 content::BrowserContext
* context
, bool create
) {
238 return static_cast<ShellWindowGeometryCache
*>(
239 GetInstance()->GetServiceForProfile(context
, create
));
242 ShellWindowGeometryCache::Factory
*
243 ShellWindowGeometryCache::Factory::GetInstance() {
244 return Singleton
<ShellWindowGeometryCache::Factory
>::get();
247 ShellWindowGeometryCache::Factory::Factory()
248 : ProfileKeyedServiceFactory("ShellWindowGeometryCache",
249 ProfileDependencyManager::GetInstance()) {
250 DependsOn(extensions::ExtensionPrefsFactory::GetInstance());
253 ShellWindowGeometryCache::Factory::~Factory() {
257 ShellWindowGeometryCache::Factory::BuildServiceInstanceFor(
258 content::BrowserContext
* context
) const {
259 Profile
* profile
= Profile::FromBrowserContext(context
);
260 return new ShellWindowGeometryCache(
262 extensions::ExtensionPrefs::Get(profile
));
265 bool ShellWindowGeometryCache::Factory::ServiceIsNULLWhileTesting() const {
269 content::BrowserContext
*
270 ShellWindowGeometryCache::Factory::GetBrowserContextToUse(
271 content::BrowserContext
* context
) const {
272 return chrome::GetBrowserContextRedirectedInIncognito(context
);