Chromium Code Reviews| 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/chrome_launcher_controller_per_app.h" | 5 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_app.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "ash/launcher/launcher_model.h" | 9 #include "ash/launcher/launcher_model.h" |
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 // Since ordinary browser windows are not registered, we might get a negative | 275 // Since ordinary browser windows are not registered, we might get a negative |
| 276 // index here. | 276 // index here. |
| 277 if (index >= 0) { | 277 if (index >= 0) { |
| 278 ash::LauncherItem item = model_->items()[index]; | 278 ash::LauncherItem item = model_->items()[index]; |
| 279 item.status = status; | 279 item.status = status; |
| 280 model_->Set(index, item); | 280 model_->Set(index, item); |
| 281 | 281 |
| 282 if (model_->items()[index].type == ash::TYPE_BROWSER_SHORTCUT) | 282 if (model_->items()[index].type == ash::TYPE_BROWSER_SHORTCUT) |
| 283 return; | 283 return; |
| 284 } | 284 } |
| 285 // Determine the new browser's active state and change if necessary. | 285 UpdateBrowserItemStatus(); |
| 286 int browser_index = -1; | |
| 287 for (size_t index = 0; index < model_->items().size() && browser_index == -1; | |
| 288 index++) { | |
| 289 if (model_->items()[index].type == ash::TYPE_BROWSER_SHORTCUT) | |
| 290 browser_index = index; | |
| 291 } | |
| 292 DCHECK(browser_index >= 0); | |
| 293 ash::LauncherItem browser_item = model_->items()[browser_index]; | |
| 294 ash::LauncherItemStatus browser_status = browser_item.status; | |
| 295 // See if the active window is a browser. | |
| 296 if (chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow())) { | |
| 297 browser_status = ash::STATUS_ACTIVE; | |
| 298 } else if (!BrowserList::empty()) { | |
| 299 browser_status = ash::STATUS_RUNNING; | |
| 300 } else { | |
| 301 browser_status = ash::STATUS_CLOSED; | |
| 302 } | |
| 303 if (browser_status != browser_item.status) { | |
| 304 browser_item.status = browser_status; | |
| 305 model_->Set(browser_index, browser_item); | |
| 306 } | |
| 307 } | 286 } |
| 308 | 287 |
| 309 void ChromeLauncherControllerPerApp::SetItemController( | 288 void ChromeLauncherControllerPerApp::SetItemController( |
| 310 ash::LauncherID id, | 289 ash::LauncherID id, |
| 311 LauncherItemController* controller) { | 290 LauncherItemController* controller) { |
| 312 IDToItemControllerMap::iterator iter = id_to_item_controller_map_.find(id); | 291 IDToItemControllerMap::iterator iter = id_to_item_controller_map_.find(id); |
| 313 DCHECK(iter != id_to_item_controller_map_.end()); | 292 DCHECK(iter != id_to_item_controller_map_.end()); |
| 314 iter->second->OnRemoved(); | 293 iter->second->OnRemoved(); |
| 315 iter->second = controller; | 294 iter->second = controller; |
| 316 controller->set_launcher_id(id); | 295 controller->set_launcher_id(id); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 434 } | 413 } |
| 435 | 414 |
| 436 // If there is an existing non-shortcut controller for this app, open it. | 415 // If there is an existing non-shortcut controller for this app, open it. |
| 437 ash::LauncherID id = GetLauncherIDForAppID(app_id); | 416 ash::LauncherID id = GetLauncherIDForAppID(app_id); |
| 438 if (id) { | 417 if (id) { |
| 439 LauncherItemController* controller = id_to_item_controller_map_[id]; | 418 LauncherItemController* controller = id_to_item_controller_map_[id]; |
| 440 controller->Activate(); | 419 controller->Activate(); |
| 441 return; | 420 return; |
| 442 } | 421 } |
| 443 | 422 |
| 444 // Otherwise launch the app. | 423 // Create a temporary application launcher item and use it to see if there are |
| 445 LaunchApp(app_id, event_flags); | 424 // running instances. |
| 425 scoped_ptr<AppShortcutLauncherItemController> app_controller( | |
| 426 new AppShortcutLauncherItemController(app_id, this)); | |
| 427 if (!app_controller->GetRunningApplications().empty()) | |
| 428 app_controller->Activate(); | |
| 429 else | |
| 430 LaunchApp(app_id, event_flags); | |
| 446 } | 431 } |
| 447 | 432 |
| 448 extensions::ExtensionPrefs::LaunchType | 433 extensions::ExtensionPrefs::LaunchType |
| 449 ChromeLauncherControllerPerApp::GetLaunchType(ash::LauncherID id) { | 434 ChromeLauncherControllerPerApp::GetLaunchType(ash::LauncherID id) { |
| 450 DCHECK(HasItemController(id)); | 435 DCHECK(HasItemController(id)); |
| 451 | 436 |
| 452 const Extension* extension = GetExtensionForAppID( | 437 const Extension* extension = GetExtensionForAppID( |
| 453 id_to_item_controller_map_[id]->app_id()); | 438 id_to_item_controller_map_[id]->app_id()); |
| 454 return profile_->GetExtensionService()->extension_prefs()->GetLaunchType( | 439 return profile_->GetExtensionService()->extension_prefs()->GetLaunchType( |
| 455 extension, | 440 extension, |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 671 std::string app_id = GetAppID(contents); | 656 std::string app_id = GetAppID(contents); |
| 672 | 657 |
| 673 // Check the old |app_id| for a tab. If the contents has changed we need to | 658 // Check the old |app_id| for a tab. If the contents has changed we need to |
| 674 // remove it from the previous app. | 659 // remove it from the previous app. |
| 675 if (web_contents_to_app_id_.find(contents) != web_contents_to_app_id_.end()) { | 660 if (web_contents_to_app_id_.find(contents) != web_contents_to_app_id_.end()) { |
| 676 std::string last_app_id = web_contents_to_app_id_[contents]; | 661 std::string last_app_id = web_contents_to_app_id_[contents]; |
| 677 if (last_app_id != app_id) | 662 if (last_app_id != app_id) |
| 678 RemoveTabFromRunningApp(contents, last_app_id); | 663 RemoveTabFromRunningApp(contents, last_app_id); |
| 679 } | 664 } |
| 680 | 665 |
| 681 if (app_id.empty()) | 666 if (app_id.empty()) { |
| 667 // Even if there is no application running, we should update the activation | |
| 668 // state of the associated browser. | |
| 669 UpdateBrowserItemStatus(); | |
| 682 return; | 670 return; |
| 671 } | |
| 683 | 672 |
| 684 web_contents_to_app_id_[contents] = app_id; | 673 web_contents_to_app_id_[contents] = app_id; |
| 685 | 674 |
| 686 if (app_state == APP_STATE_REMOVED) { | 675 if (app_state == APP_STATE_REMOVED) { |
| 687 // The tab has gone away. | 676 // The tab has gone away. |
| 688 RemoveTabFromRunningApp(contents, app_id); | 677 RemoveTabFromRunningApp(contents, app_id); |
| 689 } else { | 678 } else { |
| 690 WebContentsList& tab_list(app_id_to_web_contents_list_[app_id]); | 679 WebContentsList& tab_list(app_id_to_web_contents_list_[app_id]); |
| 691 | 680 |
| 692 if (app_state == APP_STATE_INACTIVE) { | 681 if (app_state == APP_STATE_INACTIVE) { |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 988 AppIconLoader* loader) { | 977 AppIconLoader* loader) { |
| 989 app_icon_loader_.reset(loader); | 978 app_icon_loader_.reset(loader); |
| 990 } | 979 } |
| 991 | 980 |
| 992 const std::string& | 981 const std::string& |
| 993 ChromeLauncherControllerPerApp::GetAppIdFromLauncherIdForTest( | 982 ChromeLauncherControllerPerApp::GetAppIdFromLauncherIdForTest( |
| 994 ash::LauncherID id) { | 983 ash::LauncherID id) { |
| 995 return id_to_item_controller_map_[id]->app_id(); | 984 return id_to_item_controller_map_[id]->app_id(); |
| 996 } | 985 } |
| 997 | 986 |
| 987 void ChromeLauncherControllerPerApp::UpdateBrowserItemStatus() { | |
| 988 // Determine the new browser's active state and change if necessary. | |
| 989 int browser_index = -1; | |
| 990 for (size_t index = 0; index < model_->items().size() && browser_index == -1; | |
| 991 index++) { | |
| 992 if (model_->items()[index].type == ash::TYPE_BROWSER_SHORTCUT) | |
| 993 browser_index = index; | |
| 994 } | |
| 995 DCHECK(browser_index >= 0); | |
| 996 ash::LauncherItem browser_item = model_->items()[browser_index]; | |
| 997 ash::LauncherItemStatus browser_status = browser_item.status; | |
| 998 // See if the active window is a browser. | |
| 999 aura::Window* window = ash::wm::GetActiveWindow(); | |
| 1000 if (window && chrome::FindBrowserWithWindow(window)) { | |
| 1001 browser_status = ash::STATUS_ACTIVE; | |
| 1002 } else if (BrowserIsRunning()) { | |
| 1003 browser_status = ash::STATUS_RUNNING; | |
| 1004 } else { | |
| 1005 browser_status = ash::STATUS_CLOSED; | |
| 1006 } | |
| 1007 if (browser_status != browser_item.status) { | |
| 1008 browser_item.status = browser_status; | |
| 1009 model_->Set(browser_index, browser_item); | |
| 1010 } | |
| 1011 } | |
| 1012 | |
| 1013 bool ChromeLauncherControllerPerApp::BrowserIsRunning() { | |
| 1014 if (BrowserList::empty()) | |
| 1015 return false; | |
| 1016 | |
| 1017 // If there is only one browser left, we might have been called while the | |
| 1018 // last browser is shutting down. In that case we need to do more checks. | |
| 1019 // If on the other hand there are more browsers still running, we will get | |
| 1020 // called again when the others are going down and are safe to say that | |
| 1021 // there are still browsers running. | |
| 1022 if (BrowserList::size() > 1) | |
| 1023 return true; | |
| 1024 | |
| 1025 Browser* browser = *BrowserList::begin(); | |
| 1026 TabStripModel* tab_strip = browser->tab_strip_model(); | |
| 1027 DCHECK(tab_strip); | |
| 1028 | |
| 1029 // The browser active if it is not trying to close all tabs. | |
|
sky
2013/01/17 23:59:09
is active.
Also, is closing_all true if an unload
Mr4D (OOO till 08-26)
2013/01/18 17:46:08
Added now global observer instead. Also changed co
| |
| 1030 return !tab_strip->closing_all(); | |
| 1031 } | |
| 1032 | |
| 998 Profile* ChromeLauncherControllerPerApp::GetProfileForNewWindows() { | 1033 Profile* ChromeLauncherControllerPerApp::GetProfileForNewWindows() { |
| 999 return ProfileManager::GetDefaultProfileOrOffTheRecord(); | 1034 return ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 1000 } | 1035 } |
| 1001 | 1036 |
| 1002 void ChromeLauncherControllerPerApp::LauncherItemClosed(ash::LauncherID id) { | 1037 void ChromeLauncherControllerPerApp::LauncherItemClosed(ash::LauncherID id) { |
| 1003 IDToItemControllerMap::iterator iter = id_to_item_controller_map_.find(id); | 1038 IDToItemControllerMap::iterator iter = id_to_item_controller_map_.find(id); |
| 1004 DCHECK(iter != id_to_item_controller_map_.end()); | 1039 DCHECK(iter != id_to_item_controller_map_.end()); |
| 1005 app_icon_loader_->ClearImage(iter->second->app_id()); | 1040 app_icon_loader_->ClearImage(iter->second->app_id()); |
| 1006 iter->second->OnRemoved(); | 1041 iter->second->OnRemoved(); |
| 1007 id_to_item_controller_map_.erase(iter); | 1042 id_to_item_controller_map_.erase(iter); |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1252 WebContents* web_contents = | 1287 WebContents* web_contents = |
| 1253 tab_strip->GetWebContentsAt(tab_strip->active_index()); | 1288 tab_strip->GetWebContentsAt(tab_strip->active_index()); |
| 1254 gfx::Image app_icon = GetAppListIcon(web_contents); | 1289 gfx::Image app_icon = GetAppListIcon(web_contents); |
| 1255 items->push_back(new ChromeLauncherAppMenuItemBrowser( | 1290 items->push_back(new ChromeLauncherAppMenuItemBrowser( |
| 1256 web_contents->GetTitle(), | 1291 web_contents->GetTitle(), |
| 1257 app_icon.IsEmpty() ? NULL : &app_icon, | 1292 app_icon.IsEmpty() ? NULL : &app_icon, |
| 1258 browser)); | 1293 browser)); |
| 1259 } | 1294 } |
| 1260 return items; | 1295 return items; |
| 1261 } | 1296 } |
| OLD | NEW |