Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1018)

Side by Side Diff: apps/shell_window_geometry_cache.cc

Issue 17564005: Place newly created app windows on screen. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comment; Fix name of CallAdjust... Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « apps/shell_window_geometry_cache.h ('k') | apps/shell_window_geometry_cache_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/shell_window_geometry_cache.h" 5 #include "apps/shell_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/extensions/extension_prefs.h" 10 #include "chrome/browser/extensions/extension_prefs.h"
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 // static 43 // static
44 ShellWindowGeometryCache* ShellWindowGeometryCache::Get( 44 ShellWindowGeometryCache* ShellWindowGeometryCache::Get(
45 content::BrowserContext* context) { 45 content::BrowserContext* context) {
46 return Factory::GetForContext(context, true /* create */); 46 return Factory::GetForContext(context, true /* create */);
47 } 47 }
48 48
49 void ShellWindowGeometryCache::SaveGeometry( 49 void ShellWindowGeometryCache::SaveGeometry(
50 const std::string& extension_id, 50 const std::string& extension_id,
51 const std::string& window_id, 51 const std::string& window_id,
52 const gfx::Rect& bounds, 52 const gfx::Rect& bounds,
53 const gfx::Rect& screen_bounds,
53 ui::WindowShowState window_state) { 54 ui::WindowShowState window_state) {
54 ExtensionData& extension_data = cache_[extension_id]; 55 ExtensionData& extension_data = cache_[extension_id];
55 56
56 // If we don't have any unsynced changes and this is a duplicate of what's 57 // 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 // already in the cache, just ignore it.
58 if (extension_data[window_id].bounds == bounds && 59 if (extension_data[window_id].bounds == bounds &&
59 extension_data[window_id].window_state == window_state && 60 extension_data[window_id].window_state == window_state &&
61 extension_data[window_id].screen_bounds == screen_bounds &&
60 !ContainsKey(unsynced_extensions_, extension_id)) 62 !ContainsKey(unsynced_extensions_, extension_id))
61 return; 63 return;
62 64
63 base::Time now = base::Time::Now(); 65 base::Time now = base::Time::Now();
64 66
65 extension_data[window_id].bounds = bounds; 67 extension_data[window_id].bounds = bounds;
68 extension_data[window_id].screen_bounds = screen_bounds;
66 extension_data[window_id].window_state = window_state; 69 extension_data[window_id].window_state = window_state;
67 extension_data[window_id].last_change = now; 70 extension_data[window_id].last_change = now;
68 71
69 if (extension_data.size() > kMaxCachedWindows) { 72 if (extension_data.size() > kMaxCachedWindows) {
70 ExtensionData::iterator oldest = extension_data.end(); 73 ExtensionData::iterator oldest = extension_data.end();
71 // Too many windows in the cache, find the oldest one to remove. 74 // Too many windows in the cache, find the oldest one to remove.
72 for (ExtensionData::iterator it = extension_data.begin(); 75 for (ExtensionData::iterator it = extension_data.begin();
73 it != extension_data.end(); ++it) { 76 it != extension_data.end(); ++it) {
74 // Don't expunge the window that was just added. 77 // Don't expunge the window that was just added.
75 if (it->first == window_id) continue; 78 if (it->first == window_id) continue;
(...skipping 24 matching lines...) Expand all
100 for (std::set<std::string>::const_iterator it = tosync.begin(), 103 for (std::set<std::string>::const_iterator it = tosync.begin(),
101 eit = tosync.end(); it != eit; ++it) { 104 eit = tosync.end(); it != eit; ++it) {
102 const std::string& extension_id = *it; 105 const std::string& extension_id = *it;
103 const ExtensionData& extension_data = cache_[extension_id]; 106 const ExtensionData& extension_data = cache_[extension_id];
104 107
105 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 108 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
106 for (ExtensionData::const_iterator it = extension_data.begin(), 109 for (ExtensionData::const_iterator it = extension_data.begin(),
107 eit = extension_data.end(); it != eit; ++it) { 110 eit = extension_data.end(); it != eit; ++it) {
108 base::DictionaryValue* value = new base::DictionaryValue; 111 base::DictionaryValue* value = new base::DictionaryValue;
109 const gfx::Rect& bounds = it->second.bounds; 112 const gfx::Rect& bounds = it->second.bounds;
113 const gfx::Rect& screen_bounds = it->second.screen_bounds;
110 value->SetInteger("x", bounds.x()); 114 value->SetInteger("x", bounds.x());
111 value->SetInteger("y", bounds.y()); 115 value->SetInteger("y", bounds.y());
112 value->SetInteger("w", bounds.width()); 116 value->SetInteger("w", bounds.width());
113 value->SetInteger("h", bounds.height()); 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());
114 value->SetInteger("state", it->second.window_state); 122 value->SetInteger("state", it->second.window_state);
115 value->SetString( 123 value->SetString(
116 "ts", base::Int64ToString(it->second.last_change.ToInternalValue())); 124 "ts", base::Int64ToString(it->second.last_change.ToInternalValue()));
117 dict->SetWithoutPathExpansion(it->first, value); 125 dict->SetWithoutPathExpansion(it->first, value);
118 } 126 }
119 prefs_->SetGeometryCache(extension_id, dict.Pass()); 127 prefs_->SetGeometryCache(extension_id, dict.Pass());
120 } 128 }
121 } 129 }
122 130
123 bool ShellWindowGeometryCache::GetGeometry( 131 bool ShellWindowGeometryCache::GetGeometry(
124 const std::string& extension_id, 132 const std::string& extension_id,
125 const std::string& window_id, 133 const std::string& window_id,
126 gfx::Rect* bounds, 134 gfx::Rect* bounds,
135 gfx::Rect* screen_bounds,
127 ui::WindowShowState* window_state) { 136 ui::WindowShowState* window_state) {
128 137
129 std::map<std::string, ExtensionData>::const_iterator 138 std::map<std::string, ExtensionData>::const_iterator
130 extension_data_it = cache_.find(extension_id); 139 extension_data_it = cache_.find(extension_id);
131 140
132 // Not in the map means loading data for the extension didn't finish yet or 141 // Not in the map means loading data for the extension didn't finish yet or
133 // the cache was not constructed until after the extension was loaded. 142 // the cache was not constructed until after the extension was loaded.
134 // Attempt to load from sync to address the latter case. 143 // Attempt to load from sync to address the latter case.
135 if (extension_data_it == cache_.end()) { 144 if (extension_data_it == cache_.end()) {
136 LoadGeometryFromStorage(extension_id); 145 LoadGeometryFromStorage(extension_id);
137 extension_data_it = cache_.find(extension_id); 146 extension_data_it = cache_.find(extension_id);
138 DCHECK(extension_data_it != cache_.end()); 147 DCHECK(extension_data_it != cache_.end());
139 } 148 }
140 149
141 ExtensionData::const_iterator window_data = extension_data_it->second.find( 150 ExtensionData::const_iterator window_data = extension_data_it->second.find(
142 window_id); 151 window_id);
143 152
144 if (window_data == extension_data_it->second.end()) 153 if (window_data == extension_data_it->second.end())
145 return false; 154 return false;
146 155
147 if (bounds) 156 if (bounds)
148 *bounds = window_data->second.bounds; 157 *bounds = window_data->second.bounds;
158 if (screen_bounds)
159 *screen_bounds = window_data->second.screen_bounds;
149 if (window_state) 160 if (window_state)
150 *window_state = window_data->second.window_state; 161 *window_state = window_data->second.window_state;
151 return true; 162 return true;
152 } 163 }
153 164
154 void ShellWindowGeometryCache::Shutdown() { 165 void ShellWindowGeometryCache::Shutdown() {
155 SyncToStorage(); 166 SyncToStorage();
156 } 167 }
157 168
169
170 ShellWindowGeometryCache::WindowData::WindowData()
171 : window_state(ui::SHOW_STATE_DEFAULT) {
172 }
173
174 ShellWindowGeometryCache::WindowData::~WindowData() {
175 }
176
158 void ShellWindowGeometryCache::Observe( 177 void ShellWindowGeometryCache::Observe(
159 int type, const content::NotificationSource& source, 178 int type, const content::NotificationSource& source,
160 const content::NotificationDetails& details) { 179 const content::NotificationDetails& details) {
161 switch (type) { 180 switch (type) {
162 case chrome::NOTIFICATION_EXTENSION_LOADED: { 181 case chrome::NOTIFICATION_EXTENSION_LOADED: {
163 std::string extension_id = 182 std::string extension_id =
164 content::Details<const extensions::Extension>(details).ptr()->id(); 183 content::Details<const extensions::Extension>(details).ptr()->id();
165 LoadGeometryFromStorage(extension_id); 184 LoadGeometryFromStorage(extension_id);
166 break; 185 break;
167 } 186 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 224
206 int i; 225 int i;
207 if (stored_window->GetInteger("x", &i)) 226 if (stored_window->GetInteger("x", &i))
208 window_data.bounds.set_x(i); 227 window_data.bounds.set_x(i);
209 if (stored_window->GetInteger("y", &i)) 228 if (stored_window->GetInteger("y", &i))
210 window_data.bounds.set_y(i); 229 window_data.bounds.set_y(i);
211 if (stored_window->GetInteger("w", &i)) 230 if (stored_window->GetInteger("w", &i))
212 window_data.bounds.set_width(i); 231 window_data.bounds.set_width(i);
213 if (stored_window->GetInteger("h", &i)) 232 if (stored_window->GetInteger("h", &i))
214 window_data.bounds.set_height(i); 233 window_data.bounds.set_height(i);
234 if (stored_window->GetInteger("screen_bounds_x", &i))
235 window_data.screen_bounds.set_x(i);
236 if (stored_window->GetInteger("screen_bounds_y", &i))
237 window_data.screen_bounds.set_y(i);
238 if (stored_window->GetInteger("screen_bounds_w", &i))
239 window_data.screen_bounds.set_width(i);
240 if (stored_window->GetInteger("screen_bounds_h", &i))
241 window_data.screen_bounds.set_height(i);
215 if (stored_window->GetInteger("state", &i)) { 242 if (stored_window->GetInteger("state", &i)) {
216 window_data.window_state = 243 window_data.window_state =
217 static_cast<ui::WindowShowState>(i); 244 static_cast<ui::WindowShowState>(i);
218 } 245 }
219 std::string ts_as_string; 246 std::string ts_as_string;
220 if (stored_window->GetString("ts", &ts_as_string)) { 247 if (stored_window->GetString("ts", &ts_as_string)) {
221 int64 ts; 248 int64 ts;
222 if (base::StringToInt64(ts_as_string, &ts)) { 249 if (base::StringToInt64(ts_as_string, &ts)) {
223 window_data.last_change = base::Time::FromInternalValue(ts); 250 window_data.last_change = base::Time::FromInternalValue(ts);
224 } 251 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 return false; 299 return false;
273 } 300 }
274 301
275 content::BrowserContext* 302 content::BrowserContext*
276 ShellWindowGeometryCache::Factory::GetBrowserContextToUse( 303 ShellWindowGeometryCache::Factory::GetBrowserContextToUse(
277 content::BrowserContext* context) const { 304 content::BrowserContext* context) const {
278 return chrome::GetBrowserContextRedirectedInIncognito(context); 305 return chrome::GetBrowserContextRedirectedInIncognito(context);
279 } 306 }
280 307
281 } // namespace apps 308 } // namespace apps
OLDNEW
« no previous file with comments | « apps/shell_window_geometry_cache.h ('k') | apps/shell_window_geometry_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698