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 "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll
er.h" | 9 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll
er.h" |
10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 10 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_finder.h" | 12 #include "chrome/browser/ui/browser_finder.h" |
13 #include "chrome/browser/ui/browser_list.h" | 13 #include "chrome/browser/ui/browser_list.h" |
14 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
15 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
16 #include "chrome/browser/web_applications/web_app.h" | 16 #include "chrome/browser/web_applications/web_app.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 #include "content/public/browser/web_contents_view.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::BrowserStatusMonitor( | 24 BrowserStatusMonitor::BrowserStatusMonitor( |
25 ChromeLauncherController* launcher_controller) | 25 ChromeLauncherController* launcher_controller) |
26 : launcher_controller_(launcher_controller), | 26 : launcher_controller_(launcher_controller), |
27 observed_activation_clients_(this), | 27 observed_activation_clients_(this), |
28 observed_root_windows_(this) { | 28 observed_root_windows_(this) { |
29 DCHECK(launcher_controller_); | 29 DCHECK(launcher_controller_); |
30 BrowserList::AddObserver(this); | 30 BrowserList* browser_list = |
| 31 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
| 32 |
| 33 browser_list->AddObserver(this); |
31 | 34 |
32 // This check needs for win7_aura. Without this, all tests in | 35 // This check needs for win7_aura. Without this, all tests in |
33 // ChromeLauncherController will fail in win7_aura. | 36 // ChromeLauncherController will fail in win7_aura. |
34 if (ash::Shell::HasInstance()) { | 37 if (ash::Shell::HasInstance()) { |
35 // We can't assume all RootWindows have the same ActivationClient. | 38 // We can't assume all RootWindows have the same ActivationClient. |
36 // Add a RootWindow and its ActivationClient to the observed list. | 39 // Add a RootWindow and its ActivationClient to the observed list. |
37 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); | 40 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); |
38 ash::Shell::RootWindowList::const_iterator iter = root_windows.begin(); | 41 ash::Shell::RootWindowList::const_iterator iter = root_windows.begin(); |
39 for (; iter != root_windows.end(); ++iter) { | 42 for (; iter != root_windows.end(); ++iter) { |
40 // |observed_activation_clients_| can have the same activation client | 43 // |observed_activation_clients_| can have the same activation client |
41 // multiple times - which would be handled by the used | 44 // multiple times - which would be handled by the used |
42 // |ScopedObserverWithDuplicatedSources|. | 45 // |ScopedObserverWithDuplicatedSources|. |
43 observed_activation_clients_.Add( | 46 observed_activation_clients_.Add( |
44 aura::client::GetActivationClient(*iter)); | 47 aura::client::GetActivationClient(*iter)); |
45 observed_root_windows_.Add(static_cast<aura::Window*>(*iter)); | 48 observed_root_windows_.Add(static_cast<aura::Window*>(*iter)); |
46 } | 49 } |
47 ash::Shell::GetInstance()->GetScreen()->AddObserver(this); | 50 ash::Shell::GetInstance()->GetScreen()->AddObserver(this); |
48 } | 51 } |
49 } | 52 } |
50 | 53 |
51 BrowserStatusMonitor::~BrowserStatusMonitor() { | 54 BrowserStatusMonitor::~BrowserStatusMonitor() { |
52 // This check needs for win7_aura. Without this, all tests in | 55 // This check needs for win7_aura. Without this, all tests in |
53 // ChromeLauncherController will fail in win7_aura. | 56 // ChromeLauncherController will fail in win7_aura. |
54 if (ash::Shell::HasInstance()) | 57 if (ash::Shell::HasInstance()) |
55 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this); | 58 ash::Shell::GetInstance()->GetScreen()->RemoveObserver(this); |
56 | 59 |
57 BrowserList::RemoveObserver(this); | |
58 | |
59 BrowserList* browser_list = | 60 BrowserList* browser_list = |
60 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); | 61 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
61 for (BrowserList::const_iterator i = browser_list->begin(); | 62 |
62 i != browser_list->end(); ++i) { | 63 browser_list->RemoveObserver(this); |
63 OnBrowserRemoved(*i); | |
64 } | |
65 } | 64 } |
66 | 65 |
67 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, | 66 void BrowserStatusMonitor::OnWindowActivated(aura::Window* gained_active, |
68 aura::Window* lost_active) { | 67 aura::Window* lost_active) { |
69 Browser* browser = chrome::FindBrowserWithWindow(lost_active); | 68 Browser* browser = chrome::FindBrowserWithWindow(lost_active); |
70 if (browser) { | 69 if (browser) { |
71 UpdateAppAndBrowserState( | 70 UpdateAppAndBrowserState( |
72 browser->tab_strip_model()->GetActiveWebContents()); | 71 browser->tab_strip_model()->GetActiveWebContents()); |
73 } | 72 } |
74 | 73 |
75 browser = chrome::FindBrowserWithWindow(gained_active); | 74 browser = chrome::FindBrowserWithWindow(gained_active); |
76 if (browser) { | 75 if (browser) { |
77 UpdateAppAndBrowserState( | 76 UpdateAppAndBrowserState( |
78 browser->tab_strip_model()->GetActiveWebContents()); | 77 browser->tab_strip_model()->GetActiveWebContents()); |
79 } | 78 } |
80 } | 79 } |
81 | 80 |
82 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { | 81 void BrowserStatusMonitor::OnWindowDestroyed(aura::Window* window) { |
83 // Remove RootWindow and its ActivationClient from observed list. | 82 // Remove RootWindow and its ActivationClient from observed list. |
84 observed_root_windows_.Remove(window); | 83 observed_root_windows_.Remove(window); |
85 observed_activation_clients_.Remove(aura::client::GetActivationClient( | 84 observed_activation_clients_.Remove(aura::client::GetActivationClient( |
86 static_cast<aura::RootWindow*>(window))); | 85 static_cast<aura::RootWindow*>(window))); |
87 } | 86 } |
88 | 87 |
89 void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) { | 88 void BrowserStatusMonitor::OnBrowserAdded(Browser* browser) { |
90 if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | |
91 return; | |
92 | |
93 browser->tab_strip_model()->AddObserver(this); | 89 browser->tab_strip_model()->AddObserver(this); |
94 | 90 |
95 if (browser->is_type_popup() && browser->is_app()) { | 91 if (browser->is_type_popup() && browser->is_app()) { |
96 std::string app_id = | 92 std::string app_id = |
97 web_app::GetExtensionIdFromApplicationName(browser->app_name()); | 93 web_app::GetExtensionIdFromApplicationName(browser->app_name()); |
98 if (!app_id.empty()) { | 94 if (!app_id.empty()) { |
99 browser_to_app_id_map_[browser] = app_id; | 95 browser_to_app_id_map_[browser] = app_id; |
100 launcher_controller_->LockV1AppWithID(app_id); | 96 launcher_controller_->LockV1AppWithID(app_id); |
101 } | 97 } |
102 } | 98 } |
103 } | 99 } |
104 | 100 |
105 void BrowserStatusMonitor::OnBrowserRemoved(Browser* browser) { | 101 void BrowserStatusMonitor::OnBrowserRemoved(Browser* browser) { |
106 if (browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | |
107 return; | |
108 | |
109 browser->tab_strip_model()->RemoveObserver(this); | 102 browser->tab_strip_model()->RemoveObserver(this); |
110 | 103 |
111 if (browser_to_app_id_map_.find(browser) != browser_to_app_id_map_.end()) { | 104 if (browser_to_app_id_map_.find(browser) != browser_to_app_id_map_.end()) { |
112 launcher_controller_->UnlockV1AppWithID(browser_to_app_id_map_[browser]); | 105 launcher_controller_->UnlockV1AppWithID(browser_to_app_id_map_[browser]); |
113 browser_to_app_id_map_.erase(browser); | 106 browser_to_app_id_map_.erase(browser); |
114 } | 107 } |
115 UpdateBrowserItemState(); | 108 UpdateBrowserItemState(); |
116 } | 109 } |
117 | 110 |
118 void BrowserStatusMonitor::OnDisplayBoundsChanged( | 111 void BrowserStatusMonitor::OnDisplayBoundsChanged( |
(...skipping 22 matching lines...) Expand all Loading... |
141 } | 134 } |
142 | 135 |
143 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, | 136 void BrowserStatusMonitor::ActiveTabChanged(content::WebContents* old_contents, |
144 content::WebContents* new_contents, | 137 content::WebContents* new_contents, |
145 int index, | 138 int index, |
146 int reason) { | 139 int reason) { |
147 Browser* browser = NULL; | 140 Browser* browser = NULL; |
148 if (old_contents) | 141 if (old_contents) |
149 browser = chrome::FindBrowserWithWebContents(old_contents); | 142 browser = chrome::FindBrowserWithWebContents(old_contents); |
150 | 143 |
151 if (browser && browser->host_desktop_type() != chrome::HOST_DESKTOP_TYPE_ASH) | |
152 return; | |
153 | |
154 // Update immediately on a tab change. | 144 // Update immediately on a tab change. |
155 if (browser && | 145 if (browser && |
156 (TabStripModel::kNoTab != | 146 (TabStripModel::kNoTab != |
157 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { | 147 browser->tab_strip_model()->GetIndexOfWebContents(old_contents))) { |
158 launcher_controller_->UpdateAppState( | 148 launcher_controller_->UpdateAppState( |
159 old_contents, | 149 old_contents, |
160 ChromeLauncherController::APP_STATE_INACTIVE); | 150 ChromeLauncherController::APP_STATE_INACTIVE); |
161 } | 151 } |
162 | 152 |
163 UpdateAppAndBrowserState(new_contents); | 153 UpdateAppAndBrowserState(new_contents); |
(...skipping 29 matching lines...) Expand all Loading... |
193 UpdateAppAndBrowserState(new_contents); | 183 UpdateAppAndBrowserState(new_contents); |
194 } | 184 } |
195 | 185 |
196 void BrowserStatusMonitor::UpdateAppAndBrowserState( | 186 void BrowserStatusMonitor::UpdateAppAndBrowserState( |
197 content::WebContents* contents) { | 187 content::WebContents* contents) { |
198 if (contents) { | 188 if (contents) { |
199 ChromeLauncherController::AppState app_state = | 189 ChromeLauncherController::AppState app_state = |
200 ChromeLauncherController::APP_STATE_INACTIVE; | 190 ChromeLauncherController::APP_STATE_INACTIVE; |
201 | 191 |
202 Browser* browser = chrome::FindBrowserWithWebContents(contents); | 192 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) { | 193 if (browser->tab_strip_model()->GetActiveWebContents() == contents) { |
207 if (browser->window()->IsActive()) | 194 if (browser->window()->IsActive()) |
208 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; | 195 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; |
209 else | 196 else |
210 app_state = ChromeLauncherController::APP_STATE_ACTIVE; | 197 app_state = ChromeLauncherController::APP_STATE_ACTIVE; |
211 } | 198 } |
212 | 199 |
213 launcher_controller_->UpdateAppState(contents, app_state); | 200 launcher_controller_->UpdateAppState(contents, app_state); |
214 } | 201 } |
215 UpdateBrowserItemState(); | 202 UpdateBrowserItemState(); |
216 } | 203 } |
217 | 204 |
218 void BrowserStatusMonitor::UpdateBrowserItemState() { | 205 void BrowserStatusMonitor::UpdateBrowserItemState() { |
219 launcher_controller_->GetBrowserShortcutLauncherItemController()-> | 206 launcher_controller_->GetBrowserShortcutLauncherItemController()-> |
220 UpdateBrowserItemState(); | 207 UpdateBrowserItemState(); |
221 } | 208 } |
OLD | NEW |