OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ash/launcher/browser_status_monitor.h" | 5 #include "chrome/browser/ui/ash/launcher/browser_status_monitor.h" |
6 | 6 |
7 #include "ash/shell.h" | 7 #include "ash/shell.h" |
8 #include "ash/wm/window_util.h" | 8 #include "ash/wm/window_util.h" |
9 #include "base/stl_util.h" | |
9 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll er.h" | 10 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll er.h" |
10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 11 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
11 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 13 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/browser_list.h" | 14 #include "chrome/browser/ui/browser_list.h" |
14 #include "chrome/browser/ui/browser_window.h" | 15 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
16 #include "chrome/browser/web_applications/web_app.h" | 17 #include "chrome/browser/web_applications/web_app.h" |
17 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_contents_view.h" | |
19 #include "ui/aura/client/activation_client.h" | 19 #include "ui/aura/client/activation_client.h" |
20 #include "ui/aura/root_window.h" | 20 #include "ui/aura/root_window.h" |
21 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
22 #include "ui/gfx/screen.h" | 22 #include "ui/gfx/screen.h" |
23 | 23 |
24 BrowserStatusMonitor::LocalWebContentsObserver::LocalWebContentsObserver( | |
25 content::WebContents* contents, | |
26 BrowserStatusMonitor* monitor) | |
27 : content::WebContentsObserver(contents), | |
28 monitor_(monitor) { | |
29 } | |
30 | |
31 BrowserStatusMonitor::LocalWebContentsObserver::~LocalWebContentsObserver() { | |
32 } | |
33 | |
34 void BrowserStatusMonitor::LocalWebContentsObserver::DidNavigateMainFrame( | |
35 const content::LoadCommittedDetails& details, | |
36 const content::FrameNavigateParams& params) { | |
37 Browser* browser = chrome::FindBrowserWithWebContents(web_contents()); | |
38 ChromeLauncherController::AppState state = | |
39 ChromeLauncherController::APP_STATE_ACTIVE; | |
40 if (browser->window()->IsActive() && | |
41 browser->tab_strip_model()->GetActiveWebContents() == web_contents()) | |
42 state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; | |
43 | |
44 monitor_->UpdateAppItemState(web_contents(), state); | |
45 monitor_->UpdateBrowserItemState(); | |
46 } | |
47 | |
24 BrowserStatusMonitor::BrowserStatusMonitor( | 48 BrowserStatusMonitor::BrowserStatusMonitor( |
25 ChromeLauncherController* launcher_controller) | 49 ChromeLauncherController* launcher_controller) |
26 : launcher_controller_(launcher_controller), | 50 : launcher_controller_(launcher_controller), |
27 observed_activation_clients_(this), | 51 observed_activation_clients_(this), |
28 observed_root_windows_(this) { | 52 observed_root_windows_(this) { |
29 DCHECK(launcher_controller_); | 53 DCHECK(launcher_controller_); |
30 BrowserList::AddObserver(this); | 54 BrowserList::AddObserver(this); |
31 | 55 |
32 // This check needs for win7_aura. Without this, all tests in | 56 // This check needs for win7_aura. Without this, all tests in |
33 // ChromeLauncherController will fail in win7_aura. | 57 // ChromeLauncherController will fail in win7_aura. |
(...skipping 21 matching lines...) Expand all Loading... | |
55 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this); | 79 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this); |
56 | 80 |
57 BrowserList::RemoveObserver(this); | 81 BrowserList::RemoveObserver(this); |
58 | 82 |
59 BrowserList* browser_list = | 83 BrowserList* browser_list = |
60 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); | 84 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
61 for (BrowserList::const_iterator i = browser_list->begin(); | 85 for (BrowserList::const_iterator i = browser_list->begin(); |
62 i != browser_list->end(); ++i) { | 86 i != browser_list->end(); ++i) { |
63 OnBrowserRemoved(*i); | 87 OnBrowserRemoved(*i); |
64 } | 88 } |
89 | |
90 STLDeleteContainerPairSecondPointers(webcontents_to_observer_map_.begin(), | |
91 webcontents_to_observer_map_.end()); | |
92 } | |
93 | |
94 void BrowserStatusMonitor::UpdateAppItemState( | |
95 content::WebContents* contents, | |
96 ChromeLauncherController::AppState app_state) { | |
97 DCHECK(contents); | |
98 launcher_controller_->UpdateAppState(contents, app_state); | |
99 } | |
100 | |
101 void BrowserStatusMonitor::UpdateBrowserItemState() { | |
102 launcher_controller_->GetBrowserShortcutLauncherItemController()-> | |
103 UpdateBrowserItemState(); | |
65 } | 104 } |
66 | 105 |
67 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, | 106 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, |
68 aura::Window* lost_active) { | 107 aura::Window* lost_active) { |
69 Browser* browser = chrome::FindBrowserWithWindow(lost_active); | 108 Browser* browser = NULL; |
70 if (browser) { | 109 content::WebContents* contents_from_gained = NULL; |
71 UpdateAppAndBrowserState( | 110 content::WebContents* contents_from_lost = NULL; |
72 browser->tab_strip_model()->GetActiveWebContents()); | 111 // Update active webcontents's app item state of |lost_active|, if existed. |
112 if (lost_active) { | |
113 browser = chrome::FindBrowserWithWindow(lost_active); | |
114 if (browser) | |
115 contents_from_lost = browser->tab_strip_model()->GetActiveWebContents(); | |
116 if (contents_from_lost) { | |
117 UpdateAppItemState( | |
118 contents_from_lost, | |
119 ChromeLauncherController::APP_STATE_INACTIVE); | |
120 } | |
73 } | 121 } |
74 | 122 |
75 browser = chrome::FindBrowserWithWindow(gained_active); | 123 // Update active webcontents's app item state of |gained_active|, if existed. |
76 if (browser) { | 124 if (gained_active) { |
77 UpdateAppAndBrowserState( | 125 browser = chrome::FindBrowserWithWindow(gained_active); |
78 browser->tab_strip_model()->GetActiveWebContents()); | 126 if (browser) |
127 contents_from_gained = browser->tab_strip_model()->GetActiveWebContents(); | |
128 if (contents_from_gained) { | |
129 UpdateAppItemState( | |
130 contents_from_gained, | |
131 ChromeLauncherController::APP_STATE_WINDOW_ACTIVE); | |
132 } | |
79 } | 133 } |
134 | |
135 if (contents_from_lost || contents_from_gained) | |
136 UpdateBrowserItemState(); | |
80 } | 137 } |
81 | 138 |
82 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { | 139 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { |
83 // Remove RootWindow and its ActivationClient from observed list. | 140 // Remove RootWindow and its ActivationClient from observed list. |
84 observed_root_windows_.Remove(window); | 141 observed_root_windows_.Remove(window); |
85 observed_activation_clients_.Remove(aura::client::GetActivationClient( | 142 observed_activation_clients_.Remove(aura::client::GetActivationClient( |
86 static_cast<aura::RootWindow*>(window))); | 143 static_cast<aura::RootWindow*>(window))); |
87 } | 144 } |
88 | 145 |
89 void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) { | 146 void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
138 // Instead, we can remove RootWindow and its ActivationClient in the | 195 // Instead, we can remove RootWindow and its ActivationClient in the |
139 // OnWindowRemoved(). | 196 // OnWindowRemoved(). |
140 // Do nothing here. | 197 // Do nothing here. |
141 } | 198 } |
142 | 199 |
143 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, | 200 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, |
144 content::WebContents* new_contents, | 201 content::WebContents* new_contents, |
145 int index, | 202 int index, |
146 int reason) { | 203 int reason) { |
147 Browser* browser = NULL; | 204 Browser* browser = NULL; |
148 if (old_contents) | 205 // Use |new_contents|. |old_contents| could be NULL. |
149 browser = chrome::FindBrowserWithWebContents(old_contents); | 206 DCHECK(new_contents); |
Mr4D (OOO till 08-26)
2013/10/01 03:20:33
Hmm. What happens when a browser goes away? As far
simonhong_
2013/10/01 05:10:43
I checked that only TabClosingAt() was called when
| |
207 browser = chrome::FindBrowserWithWebContents(new_contents); | |
150 | 208 |
151 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | 209 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
152 return; | 210 return; |
153 | 211 |
212 ChromeLauncherController::AppState state = | |
213 ChromeLauncherController::APP_STATE_INACTIVE; | |
214 | |
154 // Update immediately on a tab change. | 215 // Update immediately on a tab change. |
155 if (browser && | 216 if (old_contents && |
156 (TabStripModel::kNoTab != | 217 (TabStripModel::kNoTab != |
157 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { | 218 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) |
158 launcher_controller_->UpdateAppState( | 219 UpdateAppItemState(old_contents, state); |
159 old_contents, | 220 |
160 ChromeLauncherController::APP_STATE_INACTIVE); | 221 if (new_contents) { |
222 state = browser->window()->IsActive() ? | |
223 ChromeLauncherController::APP_STATE_WINDOW_ACTIVE : | |
224 ChromeLauncherController::APP_STATE_ACTIVE; | |
225 UpdateAppItemState(new_contents, state); | |
226 UpdateBrowserItemState(); | |
161 } | 227 } |
162 | |
163 UpdateAppAndBrowserState(new_contents); | |
164 } | |
165 | |
166 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, | |
167 int index, | |
168 bool foreground) { | |
169 UpdateAppAndBrowserState(contents); | |
170 } | |
171 | |
172 void BrowserStatusMonitor::TabDetachedAt(content::WebContents* contents, | |
173 int index) { | |
174 launcher_controller_->UpdateAppState( | |
175 contents, ChromeLauncherController::APP_STATE_REMOVED); | |
176 UpdateBrowserItemState(); | |
177 } | |
178 | |
179 void BrowserStatusMonitor::TabChangedAt( | |
180 content::WebContents* contents, | |
181 int index, | |
182 TabStripModelObserver::TabChangeType change_type) { | |
183 UpdateAppAndBrowserState(contents); | |
184 } | 228 } |
185 | 229 |
186 void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model, | 230 void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model, |
187 content::WebContents* old_contents, | 231 content::WebContents* old_contents, |
188 content::WebContents* new_contents, | 232 content::WebContents* new_contents, |
189 int index) { | 233 int index) { |
190 launcher_controller_->UpdateAppState( | 234 DCHECK(old_contents && new_contents); |
191 old_contents, | 235 Browser* browser = chrome::FindBrowserWithWebContents(new_contents); |
192 ChromeLauncherController::APP_STATE_REMOVED); | 236 |
193 UpdateAppAndBrowserState(new_contents); | 237 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
238 return; | |
239 | |
240 UpdateAppItemState(old_contents, | |
241 ChromeLauncherController::APP_STATE_REMOVED); | |
242 RemoveWebContentsObserver(old_contents); | |
243 | |
244 ChromeLauncherController::AppState state = | |
245 ChromeLauncherController::APP_STATE_ACTIVE; | |
246 if (browser->window()->IsActive() && | |
247 (tab_strip_model->GetActiveWebContents() == new_contents)) | |
248 state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; | |
249 UpdateAppItemState(new_contents, state); | |
250 CreateWebContentsObserver(new_contents); | |
194 } | 251 } |
195 | 252 |
196 void BrowserStatusMonitor::UpdateAppAndBrowserState( | 253 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, |
197 content::WebContents* contents) { | 254 int index, |
198 if (contents) { | 255 bool foreground) { |
199 ChromeLauncherController::AppState app_state = | 256 // An inserted tab is not active - ActiveTabChanged() will be called to |
200 ChromeLauncherController::APP_STATE_INACTIVE; | 257 // activate. We initialize therefore with |APP_STATE_INACTIVE|. |
201 | 258 UpdateAppItemState(contents, |
202 Browser* browser = chrome::FindBrowserWithWebContents(contents); | 259 ChromeLauncherController::APP_STATE_INACTIVE); |
203 DCHECK(browser); | 260 CreateWebContentsObserver(contents); |
204 if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | |
205 return; | |
206 if (browser->tab_strip_model()->GetActiveWebContents() == contents) { | |
207 if (browser->window()->IsActive()) | |
208 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; | |
209 else | |
210 app_state = ChromeLauncherController::APP_STATE_ACTIVE; | |
211 } | |
212 | |
213 launcher_controller_->UpdateAppState(contents, app_state); | |
214 } | |
215 UpdateBrowserItemState(); | |
216 } | 261 } |
217 | 262 |
218 void BrowserStatusMonitor::UpdateBrowserItemState() { | 263 void BrowserStatusMonitor::TabClosingAt(TabStripModel* tab_strip_mode, |
219 launcher_controller_->GetBrowserShortcutLauncherItemController()-> | 264 content::WebContents* contents, |
220 UpdateBrowserItemState(); | 265 int index) { |
266 UpdateAppItemState(contents, | |
267 ChromeLauncherController::APP_STATE_REMOVED); | |
268 RemoveWebContentsObserver(contents); | |
221 } | 269 } |
270 | |
271 void BrowserStatusMonitor::CreateWebContentsObserver( | |
Mr4D (OOO till 08-26)
2013/10/01 03:20:33
That sounds odd. This should be something like "Ad
simonhong_
2013/10/01 05:10:43
Done.
| |
272 content::WebContents* contents) { | |
273 if (webcontents_to_observer_map_.find(contents) == | |
274 webcontents_to_observer_map_.end()) { | |
275 webcontents_to_observer_map_[contents] = | |
276 new LocalWebContentsObserver(contents, this); | |
277 } | |
278 } | |
279 | |
280 void BrowserStatusMonitor::RemoveWebContentsObserver( | |
281 content::WebContents* contents) { | |
282 DCHECK(webcontents_to_observer_map_.find(contents) != | |
283 webcontents_to_observer_map_.end()); | |
284 delete webcontents_to_observer_map_[contents]; | |
285 webcontents_to_observer_map_.erase(contents); | |
286 } | |
OLD | NEW |