OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/ui/window_sizer.h" | 5 #include "chrome/browser/ui/window_sizer.h" |
6 | 6 |
7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/browser/ui/browser_list.h" | 11 #include "chrome/browser/ui/browser_list.h" |
11 #include "chrome/browser/ui/browser_window.h" | 12 #include "chrome/browser/ui/browser_window.h" |
12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
13 | 14 |
14 /////////////////////////////////////////////////////////////////////////////// | 15 /////////////////////////////////////////////////////////////////////////////// |
15 // An implementation of WindowSizer::StateProvider that gets the last active | 16 // An implementation of WindowSizer::StateProvider that gets the last active |
16 // and persistent state from the browser window and the user's profile. | 17 // and persistent state from the browser window and the user's profile. |
17 class DefaultStateProvider : public WindowSizer::StateProvider { | 18 class DefaultStateProvider : public WindowSizer::StateProvider { |
18 public: | 19 public: |
19 explicit DefaultStateProvider(const std::string& app_name, Browser* browser) | 20 explicit DefaultStateProvider(const std::string& app_name, |
20 : app_name_(app_name), | 21 const Browser* browser) : app_name_(app_name), |
21 browser_(browser) { | 22 browser_(browser) { |
22 } | 23 } |
23 | 24 |
24 // Overridden from WindowSizer::StateProvider: | 25 // Overridden from WindowSizer::StateProvider: |
25 virtual bool GetPersistentState(gfx::Rect* bounds, | 26 virtual bool GetPersistentState(gfx::Rect* bounds, |
26 bool* maximized, | 27 bool* maximized, |
27 gfx::Rect* work_area) const { | 28 gfx::Rect* work_area) const { |
28 DCHECK(bounds && maximized); | 29 DCHECK(bounds && maximized); |
29 | 30 |
30 std::string key(prefs::kBrowserWindowPlacement); | 31 std::string key(prefs::kBrowserWindowPlacement); |
31 if (!app_name_.empty()) { | 32 if (!app_name_.empty()) { |
32 key.append("_"); | 33 key.append("_"); |
33 key.append(app_name_); | 34 key.append(app_name_); |
34 } | 35 } |
35 | 36 |
36 if (!g_browser_process->local_state()) | 37 if (!browser_->profile()->GetPrefs()) |
37 return false; | 38 return false; |
38 | 39 |
39 const DictionaryValue* wp_pref = | 40 const DictionaryValue* wp_pref = |
40 g_browser_process->local_state()->GetDictionary(key.c_str()); | 41 browser_->profile()->GetPrefs()->GetDictionary(key.c_str()); |
41 int top = 0, left = 0, bottom = 0, right = 0; | 42 int top = 0, left = 0, bottom = 0, right = 0; |
42 bool has_prefs = | 43 bool has_prefs = |
43 wp_pref && | 44 wp_pref && |
44 wp_pref->GetInteger("top", &top) && | 45 wp_pref->GetInteger("top", &top) && |
45 wp_pref->GetInteger("left", &left) && | 46 wp_pref->GetInteger("left", &left) && |
46 wp_pref->GetInteger("bottom", &bottom) && | 47 wp_pref->GetInteger("bottom", &bottom) && |
47 wp_pref->GetInteger("right", &right) && | 48 wp_pref->GetInteger("right", &right) && |
48 wp_pref->GetBoolean("maximized", maximized); | 49 wp_pref->GetBoolean("maximized", maximized); |
49 bounds->SetRect(left, top, std::max(0, right - left), | 50 bounds->SetRect(left, top, std::max(0, right - left), |
50 std::max(0, bottom - top)); | 51 std::max(0, bottom - top)); |
(...skipping 16 matching lines...) Expand all Loading... |
67 } | 68 } |
68 | 69 |
69 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { | 70 virtual bool GetLastActiveWindowState(gfx::Rect* bounds) const { |
70 // Applications are always restored with the same position. | 71 // Applications are always restored with the same position. |
71 if (!app_name_.empty()) | 72 if (!app_name_.empty()) |
72 return false; | 73 return false; |
73 | 74 |
74 // If a reference browser is set, use its window. Otherwise find last | 75 // If a reference browser is set, use its window. Otherwise find last |
75 // active. | 76 // active. |
76 BrowserWindow* window = NULL; | 77 BrowserWindow* window = NULL; |
77 if (browser_) { | 78 // Window may be null if browser is just starting up. |
| 79 if (browser_ && browser_->window()) { |
78 window = browser_->window(); | 80 window = browser_->window(); |
79 DCHECK(window); | |
80 } else { | 81 } else { |
81 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); | 82 BrowserList::const_reverse_iterator it = BrowserList::begin_last_active(); |
82 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); | 83 BrowserList::const_reverse_iterator end = BrowserList::end_last_active(); |
83 for (; (it != end); ++it) { | 84 for (; (it != end); ++it) { |
84 Browser* last_active = *it; | 85 Browser* last_active = *it; |
85 if (last_active && last_active->type() == Browser::TYPE_NORMAL) { | 86 if (last_active && last_active->type() == Browser::TYPE_NORMAL) { |
86 window = last_active->window(); | 87 window = last_active->window(); |
87 DCHECK(window); | 88 DCHECK(window); |
88 break; | 89 break; |
89 } | 90 } |
90 } | 91 } |
91 } | 92 } |
92 | 93 |
93 if (window) { | 94 if (window) { |
94 *bounds = window->GetRestoredBounds(); | 95 *bounds = window->GetRestoredBounds(); |
95 return true; | 96 return true; |
96 } | 97 } |
97 | 98 |
98 return false; | 99 return false; |
99 } | 100 } |
100 | 101 |
101 private: | 102 private: |
102 std::string app_name_; | 103 std::string app_name_; |
103 | 104 |
104 // If set, is used as the reference browser for GetLastActiveWindowState. | 105 // If set, is used as the reference browser for GetLastActiveWindowState. |
105 Browser* browser_; | 106 const Browser* browser_; |
106 DISALLOW_COPY_AND_ASSIGN(DefaultStateProvider); | 107 DISALLOW_COPY_AND_ASSIGN(DefaultStateProvider); |
107 }; | 108 }; |
108 | 109 |
109 /////////////////////////////////////////////////////////////////////////////// | 110 /////////////////////////////////////////////////////////////////////////////// |
110 // MonitorInfoProvider, public: | 111 // MonitorInfoProvider, public: |
111 | 112 |
112 WindowSizer::MonitorInfoProvider::MonitorInfoProvider() {} | 113 WindowSizer::MonitorInfoProvider::MonitorInfoProvider() {} |
113 | 114 |
114 WindowSizer::MonitorInfoProvider::~MonitorInfoProvider() {} | 115 WindowSizer::MonitorInfoProvider::~MonitorInfoProvider() {} |
115 | 116 |
116 /////////////////////////////////////////////////////////////////////////////// | 117 /////////////////////////////////////////////////////////////////////////////// |
117 // WindowSizer, public: | 118 // WindowSizer, public: |
118 | 119 |
119 WindowSizer::WindowSizer( | 120 WindowSizer::WindowSizer( |
120 StateProvider* state_provider, | 121 StateProvider* state_provider, |
121 MonitorInfoProvider* monitor_info_provider) { | 122 MonitorInfoProvider* monitor_info_provider) { |
122 Init(state_provider, monitor_info_provider); | 123 Init(state_provider, monitor_info_provider); |
123 } | 124 } |
124 | 125 |
125 WindowSizer::~WindowSizer() { | 126 WindowSizer::~WindowSizer() { |
126 if (state_provider_) | 127 if (state_provider_) |
127 delete state_provider_; | 128 delete state_provider_; |
128 if (monitor_info_provider_) | 129 if (monitor_info_provider_) |
129 delete monitor_info_provider_; | 130 delete monitor_info_provider_; |
130 } | 131 } |
131 | 132 |
132 // static | 133 // static |
133 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, | 134 void WindowSizer::GetBrowserWindowBounds(const std::string& app_name, |
134 const gfx::Rect& specified_bounds, | 135 const gfx::Rect& specified_bounds, |
135 Browser* browser, | 136 const Browser* browser, |
136 gfx::Rect* window_bounds, | 137 gfx::Rect* window_bounds, |
137 bool* maximized) { | 138 bool* maximized) { |
138 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), | 139 const WindowSizer sizer(new DefaultStateProvider(app_name, browser), |
139 CreateDefaultMonitorInfoProvider()); | 140 CreateDefaultMonitorInfoProvider()); |
140 sizer.DetermineWindowBounds(specified_bounds, window_bounds, maximized); | 141 sizer.DetermineWindowBounds(specified_bounds, window_bounds, maximized); |
141 } | 142 } |
142 | 143 |
143 /////////////////////////////////////////////////////////////////////////////// | 144 /////////////////////////////////////////////////////////////////////////////// |
144 // WindowSizer, private: | 145 // WindowSizer, private: |
145 | 146 |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // On non-Mac platforms, we are less aggressive about repositioning. Simply | 328 // On non-Mac platforms, we are less aggressive about repositioning. Simply |
328 // ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible. | 329 // ensure that at least kMinVisibleWidth * kMinVisibleHeight is visible. |
329 const int min_y = work_area.y() + kMinVisibleHeight - bounds->height(); | 330 const int min_y = work_area.y() + kMinVisibleHeight - bounds->height(); |
330 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); | 331 const int min_x = work_area.x() + kMinVisibleWidth - bounds->width(); |
331 const int max_y = work_area.bottom() - kMinVisibleHeight; | 332 const int max_y = work_area.bottom() - kMinVisibleHeight; |
332 const int max_x = work_area.right() - kMinVisibleWidth; | 333 const int max_x = work_area.right() - kMinVisibleWidth; |
333 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); | 334 bounds->set_y(std::max(min_y, std::min(max_y, bounds->y()))); |
334 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); | 335 bounds->set_x(std::max(min_x, std::min(max_x, bounds->x()))); |
335 #endif // defined(OS_MACOSX) | 336 #endif // defined(OS_MACOSX) |
336 } | 337 } |
OLD | NEW |