OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_launcher_item_controller.h" | 5 #include "chrome/browser/ui/ash/launcher/browser_launcher_item_controller.h" |
6 | 6 |
7 #include "ash/launcher/launcher.h" | 7 #include "ash/launcher/launcher.h" |
8 #include "ash/launcher/launcher_model.h" | 8 #include "ash/launcher/launcher_model.h" |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/wm/window_util.h" | 10 #include "ash/wm/window_util.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/tab_helper.h" | 12 #include "chrome/browser/extensions/tab_helper.h" |
13 #include "chrome/browser/favicon/favicon_tab_helper.h" | 13 #include "chrome/browser/favicon/favicon_tab_helper.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" | 15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" |
16 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 16 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_app.h" |
17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
19 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
20 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 20 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
21 #include "chrome/browser/web_applications/web_app.h" | 21 #include "chrome/browser/web_applications/web_app.h" |
22 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" | 22 #include "chrome/common/extensions/manifest_handlers/icons_handler.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "grit/ui_resources.h" | 24 #include "grit/ui_resources.h" |
25 #include "ui/aura/client/aura_constants.h" | 25 #include "ui/aura/client/aura_constants.h" |
26 #include "ui/aura/window.h" | 26 #include "ui/aura/window.h" |
(...skipping 29 matching lines...) Expand all Loading... |
56 if (type() == TYPE_WINDOWED_APP) | 56 if (type() == TYPE_WINDOWED_APP) |
57 return empty_app_id_; | 57 return empty_app_id_; |
58 return LauncherItemController::app_id(); | 58 return LauncherItemController::app_id(); |
59 } | 59 } |
60 | 60 |
61 void BrowserLauncherItemController::Init() { | 61 void BrowserLauncherItemController::Init() { |
62 tab_model_->AddObserver(this); | 62 tab_model_->AddObserver(this); |
63 ash::LauncherItemStatus app_status = | 63 ash::LauncherItemStatus app_status = |
64 ash::wm::IsActiveWindow(window_) ? | 64 ash::wm::IsActiveWindow(window_) ? |
65 ash::STATUS_ACTIVE : ash::STATUS_RUNNING; | 65 ash::STATUS_ACTIVE : ash::STATUS_RUNNING; |
66 if (type() != TYPE_TABBED && type() != TYPE_WINDOWED_APP) { | 66 if (type() != TYPE_TABBED && type() != TYPE_WINDOWED_APP) |
67 launcher_controller()->CreateAppLauncherItem(this, app_id(), app_status); | 67 launcher_controller()->CreateAppLauncherItem(this, app_id(), app_status); |
68 } else { | 68 else if (type() == TYPE_WINDOWED_APP) |
69 launcher_controller()->CreateTabbedLauncherItem( | 69 launcher_controller()->LockV1AppWithID(LauncherItemController::app_id()); |
70 this, | 70 |
71 is_incognito_ ? ChromeLauncherController::STATE_INCOGNITO : | |
72 ChromeLauncherController::STATE_NOT_INCOGNITO, | |
73 app_status); | |
74 if (type() == TYPE_WINDOWED_APP) | |
75 launcher_controller()->LockV1AppWithID(LauncherItemController::app_id()); | |
76 } | |
77 // In testing scenarios we can get tab strips with no active contents. | 71 // In testing scenarios we can get tab strips with no active contents. |
78 if (tab_model_->active_index() != TabStripModel::kNoTab) | 72 if (tab_model_->active_index() != TabStripModel::kNoTab) |
79 UpdateLauncher(tab_model_->GetActiveWebContents()); | 73 UpdateLauncher(tab_model_->GetActiveWebContents()); |
80 } | 74 } |
81 | 75 |
82 // static | 76 // static |
83 BrowserLauncherItemController* BrowserLauncherItemController::Create( | 77 BrowserLauncherItemController* BrowserLauncherItemController::Create( |
84 Browser* browser) { | 78 Browser* browser) { |
85 // Under testing this can be called before the controller is created. | 79 // Under testing this can be called before the controller is created. |
86 if (!ChromeLauncherController::instance()) | 80 if (!ChromeLauncherController::instance()) |
87 return NULL; | 81 return NULL; |
88 | 82 |
89 Type type; | 83 Type type; |
90 std::string app_id; | 84 std::string app_id; |
91 if (browser->is_type_tabbed() || browser->is_type_popup()) { | 85 if (browser->is_type_tabbed() || browser->is_type_popup()) { |
92 type = TYPE_TABBED; | 86 type = TYPE_TABBED; |
93 if (!browser->is_type_tabbed() && | 87 if (!browser->is_type_tabbed() && |
94 browser->is_type_popup() && | 88 browser->is_type_popup() && |
95 browser->is_app() && | 89 browser->is_app()) { |
96 ChromeLauncherController::instance()->GetPerAppInterface()) { | |
97 app_id = web_app::GetExtensionIdFromApplicationName( | 90 app_id = web_app::GetExtensionIdFromApplicationName( |
98 browser->app_name()); | 91 browser->app_name()); |
99 // Only allow this for known applications. Some unit tests for example | 92 // Only allow this for known applications. Some unit tests for example |
100 // do not have one. | 93 // do not have one. |
101 if (!app_id.empty()) | 94 if (!app_id.empty()) |
102 type = TYPE_WINDOWED_APP; | 95 type = TYPE_WINDOWED_APP; |
103 } | 96 } |
104 } else if (browser->is_app()) { | 97 } else if (browser->is_app()) { |
105 type = TYPE_TABBED; | 98 type = TYPE_TABBED; |
106 app_id = web_app::GetExtensionIdFromApplicationName(browser->app_name()); | 99 app_id = web_app::GetExtensionIdFromApplicationName(browser->app_name()); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 Activate(); | 166 Activate(); |
174 } | 167 } |
175 } | 168 } |
176 | 169 |
177 void BrowserLauncherItemController::OnRemoved() { | 170 void BrowserLauncherItemController::OnRemoved() { |
178 } | 171 } |
179 | 172 |
180 void BrowserLauncherItemController::LauncherItemChanged( | 173 void BrowserLauncherItemController::LauncherItemChanged( |
181 int index, | 174 int index, |
182 const ash::LauncherItem& old_item) { | 175 const ash::LauncherItem& old_item) { |
183 if (!launcher_controller()->GetPerAppInterface() && | |
184 launcher_model()->items()[index].status == ash::STATUS_ACTIVE && | |
185 old_item.status == ash::STATUS_RUNNING) { | |
186 Activate(); | |
187 } | |
188 } | 176 } |
189 | 177 |
190 ChromeLauncherAppMenuItems | 178 ChromeLauncherAppMenuItems |
191 BrowserLauncherItemController::GetApplicationList(int event_flags) { | 179 BrowserLauncherItemController::GetApplicationList(int event_flags) { |
192 // This will never be called and the entire class will go away. | 180 // This will never be called and the entire class will go away. |
193 ChromeLauncherAppMenuItems items; | 181 ChromeLauncherAppMenuItems items; |
194 return items.Pass(); | 182 return items.Pass(); |
195 } | 183 } |
196 | 184 |
197 void BrowserLauncherItemController::ActiveTabChanged( | 185 void BrowserLauncherItemController::ActiveTabChanged( |
198 content::WebContents* old_contents, | 186 content::WebContents* old_contents, |
199 content::WebContents* new_contents, | 187 content::WebContents* new_contents, |
200 int index, | 188 int index, |
201 int reason) { | 189 int reason) { |
202 // Update immediately on a tab change. | 190 // Update immediately on a tab change. |
203 if (old_contents && | 191 if (old_contents && |
204 (!launcher_controller()->GetPerAppInterface() || | 192 TabStripModel::kNoTab != |
205 TabStripModel::kNoTab != | 193 tab_model_->GetIndexOfWebContents(old_contents)) |
206 tab_model_->GetIndexOfWebContents(old_contents))) | |
207 UpdateAppState(old_contents); | 194 UpdateAppState(old_contents); |
208 UpdateAppState(new_contents); | 195 UpdateAppState(new_contents); |
209 UpdateLauncher(new_contents); | 196 UpdateLauncher(new_contents); |
210 } | 197 } |
211 | 198 |
212 void BrowserLauncherItemController::TabInsertedAt( | 199 void BrowserLauncherItemController::TabInsertedAt( |
213 content::WebContents* contents, | 200 content::WebContents* contents, |
214 int index, | 201 int index, |
215 bool foreground) { | 202 bool foreground) { |
216 UpdateAppState(contents); | 203 UpdateAppState(contents); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 237 |
251 void BrowserLauncherItemController::OnWindowPropertyChanged( | 238 void BrowserLauncherItemController::OnWindowPropertyChanged( |
252 aura::Window* window, | 239 aura::Window* window, |
253 const void* key, | 240 const void* key, |
254 intptr_t old) { | 241 intptr_t old) { |
255 if (key == aura::client::kDrawAttentionKey) | 242 if (key == aura::client::kDrawAttentionKey) |
256 UpdateItemStatus(); | 243 UpdateItemStatus(); |
257 } | 244 } |
258 | 245 |
259 void BrowserLauncherItemController::UpdateItemStatus() { | 246 void BrowserLauncherItemController::UpdateItemStatus() { |
260 if (launcher_controller()->GetPerAppInterface()) | |
261 return; | |
262 | |
263 ash::LauncherItemStatus status; | |
264 if (ash::wm::IsActiveWindow(window_)) { | |
265 // Clear attention state if active. | |
266 if (window_->GetProperty(aura::client::kDrawAttentionKey)) | |
267 window_->SetProperty(aura::client::kDrawAttentionKey, false); | |
268 status = ash::STATUS_ACTIVE; | |
269 } else if (window_->GetProperty(aura::client::kDrawAttentionKey)) { | |
270 status = ash::STATUS_ATTENTION; | |
271 } else { | |
272 status = ash::STATUS_RUNNING; | |
273 } | |
274 launcher_controller()->SetItemStatus(launcher_id(), status); | |
275 } | 247 } |
276 | 248 |
277 void BrowserLauncherItemController::UpdateLauncher(content::WebContents* tab) { | 249 void BrowserLauncherItemController::UpdateLauncher(content::WebContents* tab) { |
278 if (launcher_controller()->GetPerAppInterface()) | |
279 return; | |
280 | |
281 if (type() == TYPE_APP_PANEL) | |
282 return; // Maintained entirely by ChromeLauncherController. | |
283 | |
284 if (!tab) | |
285 return; // Assume the window is going to be closed if there are no tabs. | |
286 | |
287 int item_index = launcher_model()->ItemIndexByID(launcher_id()); | |
288 if (item_index == -1) | |
289 return; | |
290 | |
291 ash::LauncherItem item = launcher_model()->items()[item_index]; | |
292 DCHECK_EQ(TYPE_TABBED, type()); | |
293 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
294 FaviconTabHelper* favicon_tab_helper = | |
295 FaviconTabHelper::FromWebContents(tab); | |
296 if (favicon_tab_helper->ShouldDisplayFavicon()) { | |
297 item.image = favicon_tab_helper->GetFavicon().AsImageSkia(); | |
298 if (item.image.isNull()) { | |
299 item.image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); | |
300 } | |
301 } else { | |
302 item.image = *rb.GetImageSkiaNamed(IDR_DEFAULT_FAVICON); | |
303 } | |
304 launcher_model()->Set(item_index, item); | |
305 } | 250 } |
306 | 251 |
307 void BrowserLauncherItemController::UpdateAppState(content::WebContents* tab) { | 252 void BrowserLauncherItemController::UpdateAppState(content::WebContents* tab) { |
308 ChromeLauncherController::AppState app_state; | 253 ChromeLauncherController::AppState app_state; |
309 | 254 |
310 if (!launcher_controller()->GetPerAppInterface() && | 255 if (tab_model_->GetActiveWebContents() == tab) { |
311 tab_model_->GetIndexOfWebContents(tab) == TabStripModel::kNoTab) { | |
312 app_state = ChromeLauncherController::APP_STATE_REMOVED; | |
313 } else if (tab_model_->GetActiveWebContents() == tab) { | |
314 if (ash::wm::IsActiveWindow(window_)) | 256 if (ash::wm::IsActiveWindow(window_)) |
315 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; | 257 app_state = ChromeLauncherController::APP_STATE_WINDOW_ACTIVE; |
316 else | 258 else |
317 app_state = ChromeLauncherController::APP_STATE_ACTIVE; | 259 app_state = ChromeLauncherController::APP_STATE_ACTIVE; |
318 } else { | 260 } else { |
319 app_state = ChromeLauncherController::APP_STATE_INACTIVE; | 261 app_state = ChromeLauncherController::APP_STATE_INACTIVE; |
320 } | 262 } |
321 launcher_controller()->UpdateAppState(tab, app_state); | 263 launcher_controller()->UpdateAppState(tab, app_state); |
322 } | 264 } |
323 | 265 |
324 ash::LauncherModel* BrowserLauncherItemController::launcher_model() { | 266 ash::LauncherModel* BrowserLauncherItemController::launcher_model() { |
325 return launcher_controller()->model(); | 267 return launcher_controller()->model(); |
326 } | 268 } |
OLD | NEW |