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 = NULL; |
71 UpdateAppAndBrowserState( | 110 // Update active webcontents's app item state of |lost_active|, if existed. |
72 browser->tab_strip_model()->GetActiveWebContents()); | 111 if (lost_active) { |
112 browser = chrome::FindBrowserWithWindow(lost_active); | |
113 if (browser) | |
114 contents = browser->tab_strip_model()->GetActiveWebContents(); | |
115 if (contents) { | |
116 UpdateAppItemState( | |
117 contents, | |
118 ChromeLauncherController::APP_STATE_INACTIVE); | |
119 } | |
73 } | 120 } |
74 | 121 |
75 browser = chrome::FindBrowserWithWindow(gained_active); | 122 // Update active webcontents's app item state of |gained_active|, if existed. |
76 if (browser) { | 123 if (gained_active) { |
77 UpdateAppAndBrowserState( | 124 browser = chrome::FindBrowserWithWindow(gained_active); |
78 browser->tab_strip_model()->GetActiveWebContents()); | 125 if (browser) |
126 contents = browser->tab_strip_model()->GetActiveWebContents(); | |
127 if (contents) { | |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
since contents was already set in 114 - you could
simonhong_
2013/09/18 13:38:38
Done.
| |
128 UpdateAppItemState( | |
129 contents, | |
130 ChromeLauncherController::APP_STATE_WINDOW_ACTIVE); | |
131 } | |
79 } | 132 } |
133 | |
134 if (contents) | |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
I am not sure which contents you are referring to
simonhong_
2013/09/18 13:38:38
My purpose was updating browser item state only if
| |
135 UpdateBrowserItemState(); | |
80 } | 136 } |
81 | 137 |
82 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { | 138 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { |
83 // Remove RootWindow and its ActivationClient from observed list. | 139 // Remove RootWindow and its ActivationClient from observed list. |
84 observed_root_windows_.Remove(window); | 140 observed_root_windows_.Remove(window); |
85 observed_activation_clients_.Remove(aura::client::GetActivationClient( | 141 observed_activation_clients_.Remove(aura::client::GetActivationClient( |
86 static_cast<aura::RootWindow*>(window))); | 142 static_cast<aura::RootWindow*>(window))); |
87 } | 143 } |
88 | 144 |
89 void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) { | 145 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 | 194 // Instead, we can remove RootWindow and its ActivationClient in the |
139 // OnWindowRemoved(). | 195 // OnWindowRemoved(). |
140 // Do nothing here. | 196 // Do nothing here. |
141 } | 197 } |
142 | 198 |
143 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, | 199 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, |
144 content::WebContents* new_contents, | 200 content::WebContents* new_contents, |
145 int index, | 201 int index, |
146 int reason) { | 202 int reason) { |
147 Browser* browser = NULL; | 203 Browser* browser = NULL; |
148 if (old_contents) | 204 // Use |new_contents|. |old_contents| could be NULL. |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
Just checking: Is there a guarantee that new_conte
simonhong_
2013/09/18 13:38:38
I checked that |new_contents| is set to WebContent
| |
149 browser = chrome::FindBrowserWithWebContents(old_contents); | 205 if (new_contents) |
206 browser = chrome::FindBrowserWithWebContents(new_contents); | |
150 | 207 |
151 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | 208 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) |
152 return; | 209 return; |
153 | 210 |
211 ChromeLauncherController::AppState state = | |
212 ChromeLauncherController::APP_STATE_INACTIVE; | |
213 | |
154 // Update immediately on a tab change. | 214 // Update immediately on a tab change. |
155 if (browser && | 215 if (browser && |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
maybe you want to test also for old_contents == NU
simonhong_
2013/09/18 13:38:38
Done.
| |
156 (TabStripModel::kNoTab != | 216 (TabStripModel::kNoTab != |
157 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { | 217 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) |
158 launcher_controller_->UpdateAppState( | 218 launcher_controller_->UpdateAppState(old_contents, state); |
159 old_contents, | 219 |
160 ChromeLauncherController::APP_STATE_INACTIVE); | 220 if (new_contents) { |
221 state = browser->window()->IsActive() ? | |
222 ChromeLauncherController::APP_STATE_WINDOW_ACTIVE : | |
223 ChromeLauncherController::APP_STATE_ACTIVE; | |
224 UpdateAppItemState(new_contents, state); | |
225 UpdateBrowserItemState(); | |
161 } | 226 } |
162 | |
163 UpdateAppAndBrowserState(new_contents); | |
164 } | 227 } |
165 | 228 |
166 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, | 229 void BrowserStatusMonitor::TabInsertedAt(content::WebContents* contents, |
167 int index, | 230 int index, |
168 bool foreground) { | 231 bool foreground) { |
169 UpdateAppAndBrowserState(contents); | 232 if (webcontents_to_observer_map_.find(contents) == |
233 webcontents_to_observer_map_.end()) { | |
234 webcontents_to_observer_map_[contents] = | |
235 new LocalWebContentsObserver(contents, this); | |
236 } | |
237 | |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
same what I commented on in 264 is valid here. You
simonhong_
2013/09/18 13:38:38
When an app tab is running, browser item state is
| |
238 // There are three cases for tab inserting. | |
239 // 1) New tab is inserted as an active tab. | |
240 // 2) Tab is dragged and dropped. | |
241 // 3) New tab is inserted not as an active tab. | |
242 // For 1) and 2), This app tab's item status will be updated active state by | |
243 // ActiveTabChanged(). | |
244 // For 3), This app tab's item status is running state. | |
245 // Because of this reason, |APP_STATE_INACTIVE| is set to all tab inserting. | |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
Instead of the comment above: what about
// An in
simonhong_
2013/09/18 13:38:38
Done.
| |
246 launcher_controller_->UpdateAppState( | |
247 contents, | |
248 ChromeLauncherController::APP_STATE_INACTIVE); | |
249 // We don't need to call UpdateBrowserItemState(). For 1) and 2), browser item | |
250 // will be updated by following ActiveTabChanged(). For 3), browser item will | |
251 // be by other tab's state change. | |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
And why should any tab's state change if it only g
simonhong_
2013/09/18 13:38:38
If we don't update the state of inserted tab, all
| |
170 } | 252 } |
171 | 253 |
172 void BrowserStatusMonitor::TabDetachedAt(content::WebContents* contents, | 254 void BrowserStatusMonitor::TabClosingAt(TabStripModel* tab_strip_mode, |
173 int index) { | 255 content::WebContents* contents, |
256 int index) { | |
257 DCHECK(webcontents_to_observer_map_.find(contents) != | |
258 webcontents_to_observer_map_.end()); | |
259 delete webcontents_to_observer_map_[contents]; | |
260 webcontents_to_observer_map_.erase(contents); | |
261 | |
174 launcher_controller_->UpdateAppState( | 262 launcher_controller_->UpdateAppState( |
175 contents, ChromeLauncherController::APP_STATE_REMOVED); | 263 contents, |
176 UpdateBrowserItemState(); | 264 ChromeLauncherController::APP_STATE_REMOVED); |
Mr4D (OOO till 08-26)
2013/09/18 01:43:49
You can remove a browser tab which is not an app a
simonhong_
2013/09/18 13:38:38
When a tab is closing, ActiveTabChanged() is also
| |
177 } | 265 } |
178 | |
179 void BrowserStatusMonitor::TabChangedAt( | |
180 content::WebContents* contents, | |
181 int index, | |
182 TabStripModelObserver::TabChangeType change_type) { | |
183 UpdateAppAndBrowserState(contents); | |
184 } | |
185 | |
186 void BrowserStatusMonitor::TabReplacedAt(TabStripModel* tab_strip_model, | |
187 content::WebContents* old_contents, | |
188 content::WebContents* new_contents, | |
189 int index) { | |
190 launcher_controller_->UpdateAppState( | |
191 old_contents, | |
192 ChromeLauncherController::APP_STATE_REMOVED); | |
193 UpdateAppAndBrowserState(new_contents); | |
194 } | |
195 | |
196 void BrowserStatusMonitor::UpdateAppAndBrowserState( | |
197 content::WebContents* contents) { | |
198 if (contents) { | |
199 ChromeLauncherController::AppState app_state = | |
200 ChromeLauncherController::APP_STATE_INACTIVE; | |
201 | |
202 Browser* browser = chrome::FindBrowserWithWebContents(contents); | |
203 DCHECK(browser); | |
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 } | |
217 | |
218 void BrowserStatusMonitor::UpdateBrowserItemState() { | |
219 launcher_controller_->GetBrowserShortcutLauncherItemController()-> | |
220 UpdateBrowserItemState(); | |
221 } | |
OLD | NEW |