1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/shell_window_geometry_cache.h"
#include "base/bind.h"
#include "base/stl_util.h"
#include "chrome/browser/extensions/state_store.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
namespace {
// Keys for serialization to and from Value to store in the preferences.
const char kWindowGeometryKey[] = "window_geometry";
// The timeout in milliseconds before we'll persist window geometry to the
// StateStore.
const int kSyncTimeoutMilliseconds = 1000;
} // namespace
namespace extensions {
ShellWindowGeometryCache::ShellWindowGeometryCache(Profile* profile,
StateStore* state_store)
: store_(state_store),
sync_delay_(base::TimeDelta::FromMilliseconds(kSyncTimeoutMilliseconds)) {
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
content::Source<Profile>(profile));
registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
content::Source<Profile>(profile));
store_->RegisterKey(kWindowGeometryKey);
}
ShellWindowGeometryCache::~ShellWindowGeometryCache() {
SyncToStorage();
}
void ShellWindowGeometryCache::SaveGeometry(
const std::string& extension_id,
const std::string& window_id,
const gfx::Rect& bounds) {
std::map<std::string, gfx::Rect>& geometry = cache_[extension_id];
// If we don't have any unsynced changes and this is a duplicate of what's
// already in the cache, just ignore it.
if (geometry[window_id] == bounds &&
!ContainsKey(unsynced_extensions_, extension_id))
return;
geometry[window_id] = bounds;
unsynced_extensions_.insert(extension_id);
// We don't use Reset() because the timer may not yet be running.
// (In that case Stop() is a no-op.)
sync_timer_.Stop();
sync_timer_.Start(FROM_HERE, sync_delay_, this,
&ShellWindowGeometryCache::SyncToStorage);
}
void ShellWindowGeometryCache::SyncToStorage() {
std::set<std::string> tosync;
tosync.swap(unsynced_extensions_);
for (std::set<std::string>::const_iterator it = tosync.begin(),
eit = tosync.end(); it != eit; ++it) {
const std::string& extension_id = *it;
const std::map<std::string, gfx::Rect>& geometry = cache_[extension_id];
scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
for (std::map<std::string, gfx::Rect>::const_iterator it =
geometry.begin(), eit = geometry.end(); it != eit; ++it) {
base::DictionaryValue* value = new base::DictionaryValue;
const gfx::Rect& bounds = it->second;
value->SetInteger("x", bounds.x());
value->SetInteger("y", bounds.y());
value->SetInteger("w", bounds.width());
value->SetInteger("h", bounds.height());
dict->SetWithoutPathExpansion(it->first, value);
}
store_->SetExtensionValue(extension_id, kWindowGeometryKey,
scoped_ptr<base::Value>(dict.release()));
}
}
bool ShellWindowGeometryCache::GetGeometry(
const std::string& extension_id,
const std::string& window_id,
gfx::Rect* bounds) const {
std::map<std::string, std::map<std::string, gfx::Rect> >::const_iterator
ext = cache_.find(extension_id);
// Not in the map means loading data for the extension didn't finish yet.
if (ext == cache_.end())
return false;
std::map<std::string, gfx::Rect>::const_iterator win = ext->second.find(
window_id);
if (win == ext->second.end())
return false;
*bounds = win->second;
return true;
}
void ShellWindowGeometryCache::Observe(
int type, const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case chrome::NOTIFICATION_EXTENSION_LOADED: {
std::string extension_id =
content::Details<const Extension>(details).ptr()->id();
OnExtensionLoaded(extension_id);
break;
}
case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
std::string extension_id =
content::Details<const UnloadedExtensionInfo>(details).
ptr()->extension->id();
OnExtensionUnloaded(extension_id);
break;
}
default:
NOTREACHED();
return;
}
}
void ShellWindowGeometryCache::SetSyncDelayForTests(int timeout_ms) {
sync_delay_ = base::TimeDelta::FromMilliseconds(timeout_ms);
}
void ShellWindowGeometryCache::OnExtensionLoaded(
const std::string& extension_id)
{
store_->GetExtensionValue(extension_id, kWindowGeometryKey,
base::Bind(&ShellWindowGeometryCache::GeometryFromStorage,
AsWeakPtr(), extension_id));
}
void ShellWindowGeometryCache::OnExtensionUnloaded(
const std::string& extension_id)
{
SyncToStorage();
cache_.erase(extension_id);
}
void ShellWindowGeometryCache::GeometryFromStorage(
const std::string& extension_id, scoped_ptr<base::Value> value) {
// TODO(mek): What should happen if an extension is already unloaded by the
// time this code is executed.
std::map<std::string, gfx::Rect>& geometry = cache_[extension_id];
base::DictionaryValue* dict;
if (value.get() && value->GetAsDictionary(&dict)) {
for (base::DictionaryValue::Iterator it(*dict); it.HasNext();
it.Advance()) {
// If the cache already contains geometry for this window, don't
// overwrite that information since it is probably the result of an
// application starting up very quickly.
std::map<std::string, gfx::Rect>::iterator geomIt =
geometry.find(it.key());
if (geomIt == geometry.end()) {
const base::DictionaryValue* geom;
if (it.value().GetAsDictionary(&geom)) {
gfx::Rect& bounds = geometry[it.key()];
int i;
if (geom->GetInteger("x", &i)) bounds.set_x(i);
if (geom->GetInteger("y", &i)) bounds.set_y(i);
if (geom->GetInteger("w", &i)) bounds.set_width(i);
if (geom->GetInteger("h", &i)) bounds.set_height(i);
}
}
}
}
}
} // namespace extensions
|