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