| 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/shell_window_launcher_controller.h" | 5 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_controller.h" |
| 6 | 6 |
| 7 #include "apps/shell_window.h" | 7 #include "apps/app_window.h" |
| 8 #include "ash/shelf/shelf_util.h" | 8 #include "ash/shelf/shelf_util.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 "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| 14 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_item_controller.h
" | 14 #include "chrome/browser/ui/ash/launcher/shell_window_launcher_item_controller.h
" |
| 15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 15 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 16 #include "chrome/browser/ui/host_desktop.h" | 16 #include "chrome/browser/ui/host_desktop.h" |
| 17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
| 18 #include "ui/aura/client/activation_client.h" | 18 #include "ui/aura/client/activation_client.h" |
| 19 #include "ui/aura/root_window.h" | 19 #include "ui/aura/root_window.h" |
| 20 | 20 |
| 21 using apps::ShellWindow; | 21 using apps::AppWindow; |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 std::string GetAppShelfId(ShellWindow* shell_window) { | 25 std::string GetAppShelfId(AppWindow* app_window) { |
| 26 if (shell_window->window_type_is_panel()) | 26 if (app_window->window_type_is_panel()) |
| 27 return base::StringPrintf("panel:%d", shell_window->session_id().id()); | 27 return base::StringPrintf("panel:%d", app_window->session_id().id()); |
| 28 return shell_window->extension()->id(); | 28 return app_window->extension()->id(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 bool ControlsWindow(aura::Window* window) { | 31 bool ControlsWindow(aura::Window* window) { |
| 32 return chrome::GetHostDesktopTypeForNativeWindow(window) == | 32 return chrome::GetHostDesktopTypeForNativeWindow(window) == |
| 33 chrome::HOST_DESKTOP_TYPE_ASH; | 33 chrome::HOST_DESKTOP_TYPE_ASH; |
| 34 } | 34 } |
| 35 | 35 |
| 36 } // namespace | 36 } // namespace |
| 37 | 37 |
| 38 ShellWindowLauncherController::ShellWindowLauncherController( | 38 ShellWindowLauncherController::ShellWindowLauncherController( |
| 39 ChromeLauncherController* owner) | 39 ChromeLauncherController* owner) |
| 40 : owner_(owner), | 40 : owner_(owner), |
| 41 activation_client_(NULL) { | 41 activation_client_(NULL) { |
| 42 apps::ShellWindowRegistry* registry = | 42 apps::AppWindowRegistry* registry = |
| 43 apps::ShellWindowRegistry::Get(owner->profile()); | 43 apps::AppWindowRegistry::Get(owner->profile()); |
| 44 registry_.insert(registry); | 44 registry_.insert(registry); |
| 45 registry->AddObserver(this); | 45 registry->AddObserver(this); |
| 46 if (ash::Shell::HasInstance()) { | 46 if (ash::Shell::HasInstance()) { |
| 47 if (ash::Shell::GetInstance()->GetPrimaryRootWindow()) { | 47 if (ash::Shell::GetInstance()->GetPrimaryRootWindow()) { |
| 48 activation_client_ = aura::client::GetActivationClient( | 48 activation_client_ = aura::client::GetActivationClient( |
| 49 ash::Shell::GetInstance()->GetPrimaryRootWindow()); | 49 ash::Shell::GetInstance()->GetPrimaryRootWindow()); |
| 50 if (activation_client_) | 50 if (activation_client_) |
| 51 activation_client_->AddObserver(this); | 51 activation_client_->AddObserver(this); |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 } | 54 } |
| 55 | 55 |
| 56 ShellWindowLauncherController::~ShellWindowLauncherController() { | 56 ShellWindowLauncherController::~ShellWindowLauncherController() { |
| 57 for (std::set<apps::ShellWindowRegistry*>::iterator it = registry_.begin(); | 57 for (std::set<apps::AppWindowRegistry*>::iterator it = registry_.begin(); |
| 58 it != registry_.end(); ++it) | 58 it != registry_.end(); |
| 59 ++it) |
| 59 (*it)->RemoveObserver(this); | 60 (*it)->RemoveObserver(this); |
| 60 | 61 |
| 61 if (activation_client_) | 62 if (activation_client_) |
| 62 activation_client_->RemoveObserver(this); | 63 activation_client_->RemoveObserver(this); |
| 63 for (WindowToAppShelfIdMap::iterator iter = | 64 for (WindowToAppShelfIdMap::iterator iter = |
| 64 window_to_app_shelf_id_map_.begin(); | 65 window_to_app_shelf_id_map_.begin(); |
| 65 iter != window_to_app_shelf_id_map_.end(); ++iter) { | 66 iter != window_to_app_shelf_id_map_.end(); ++iter) { |
| 66 iter->first->RemoveObserver(this); | 67 iter->first->RemoveObserver(this); |
| 67 } | 68 } |
| 68 } | 69 } |
| 69 | 70 |
| 70 void ShellWindowLauncherController::AdditionalUserAddedToSession( | 71 void ShellWindowLauncherController::AdditionalUserAddedToSession( |
| 71 Profile* profile) { | 72 Profile* profile) { |
| 72 // TODO(skuhne): This was added for the legacy side by side mode in M32. If | 73 // TODO(skuhne): This was added for the legacy side by side mode in M32. If |
| 73 // this mode gets no longer pursued this special case can be removed. | 74 // this mode gets no longer pursued this special case can be removed. |
| 74 if (chrome::MultiUserWindowManager::GetMultiProfileMode() != | 75 if (chrome::MultiUserWindowManager::GetMultiProfileMode() != |
| 75 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED) | 76 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_MIXED) |
| 76 return; | 77 return; |
| 77 | 78 |
| 78 apps::ShellWindowRegistry* registry = apps::ShellWindowRegistry::Get(profile); | 79 apps::AppWindowRegistry* registry = apps::AppWindowRegistry::Get(profile); |
| 79 if (registry_.find(registry) != registry_.end()) | 80 if (registry_.find(registry) != registry_.end()) |
| 80 return; | 81 return; |
| 81 | 82 |
| 82 registry->AddObserver(this); | 83 registry->AddObserver(this); |
| 83 registry_.insert(registry); | 84 registry_.insert(registry); |
| 84 } | 85 } |
| 85 | 86 |
| 86 void ShellWindowLauncherController::OnShellWindowAdded( | 87 void ShellWindowLauncherController::OnAppWindowAdded(AppWindow* app_window) { |
| 87 ShellWindow* shell_window) { | 88 if (!ControlsWindow(app_window->GetNativeWindow())) |
| 88 if (!ControlsWindow(shell_window->GetNativeWindow())) | |
| 89 return; | 89 return; |
| 90 RegisterApp(shell_window); | 90 RegisterApp(app_window); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void ShellWindowLauncherController::OnShellWindowIconChanged( | 93 void ShellWindowLauncherController::OnAppWindowIconChanged( |
| 94 ShellWindow* shell_window) { | 94 AppWindow* app_window) { |
| 95 if (!ControlsWindow(shell_window->GetNativeWindow())) | 95 if (!ControlsWindow(app_window->GetNativeWindow())) |
| 96 return; | 96 return; |
| 97 | 97 |
| 98 const std::string app_shelf_id = GetAppShelfId(shell_window); | 98 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 99 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); | 99 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); |
| 100 if (iter == app_controller_map_.end()) | 100 if (iter == app_controller_map_.end()) |
| 101 return; | 101 return; |
| 102 ShellWindowLauncherItemController* controller = iter->second; | 102 ShellWindowLauncherItemController* controller = iter->second; |
| 103 controller->set_image_set_by_controller(true); | 103 controller->set_image_set_by_controller(true); |
| 104 owner_->SetLauncherItemImage(controller->shelf_id(), | 104 owner_->SetLauncherItemImage(controller->shelf_id(), |
| 105 shell_window->app_icon().AsImageSkia()); | 105 app_window->app_icon().AsImageSkia()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void ShellWindowLauncherController::OnShellWindowRemoved( | 108 void ShellWindowLauncherController::OnAppWindowRemoved(AppWindow* app_window) { |
| 109 ShellWindow* shell_window) { | 109 // Do nothing here; app_window->window() has already been deleted and |
| 110 // Do nothing here; shell_window->window() has already been deleted and | |
| 111 // OnWindowDestroying() has been called, doing the removal. | 110 // OnWindowDestroying() has been called, doing the removal. |
| 112 } | 111 } |
| 113 | 112 |
| 114 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed() | 113 // Called from aura::Window::~Window(), before delegate_->OnWindowDestroyed() |
| 115 // which destroys ShellWindow, so both |window| and the associated ShellWindow | 114 // which destroys AppWindow, so both |window| and the associated AppWindow |
| 116 // are valid here. | 115 // are valid here. |
| 117 void ShellWindowLauncherController::OnWindowDestroying(aura::Window* window) { | 116 void ShellWindowLauncherController::OnWindowDestroying(aura::Window* window) { |
| 118 if (!ControlsWindow(window)) | 117 if (!ControlsWindow(window)) |
| 119 return; | 118 return; |
| 120 UnregisterApp(window); | 119 UnregisterApp(window); |
| 121 } | 120 } |
| 122 | 121 |
| 123 void ShellWindowLauncherController::OnWindowActivated( | 122 void ShellWindowLauncherController::OnWindowActivated( |
| 124 aura::Window* new_active, | 123 aura::Window* new_active, |
| 125 aura::Window* old_active) { | 124 aura::Window* old_active) { |
| 126 // Make the newly active window the active (first) entry in the controller. | 125 // Make the newly active window the active (first) entry in the controller. |
| 127 ShellWindowLauncherItemController* new_controller = | 126 ShellWindowLauncherItemController* new_controller = |
| 128 ControllerForWindow(new_active); | 127 ControllerForWindow(new_active); |
| 129 if (new_controller) { | 128 if (new_controller) { |
| 130 new_controller->SetActiveWindow(new_active); | 129 new_controller->SetActiveWindow(new_active); |
| 131 owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE); | 130 owner_->SetItemStatus(new_controller->shelf_id(), ash::STATUS_ACTIVE); |
| 132 } | 131 } |
| 133 | 132 |
| 134 // Mark the old active window's launcher item as running (if different). | 133 // Mark the old active window's launcher item as running (if different). |
| 135 ShellWindowLauncherItemController* old_controller = | 134 ShellWindowLauncherItemController* old_controller = |
| 136 ControllerForWindow(old_active); | 135 ControllerForWindow(old_active); |
| 137 if (old_controller && old_controller != new_controller) | 136 if (old_controller && old_controller != new_controller) |
| 138 owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING); | 137 owner_->SetItemStatus(old_controller->shelf_id(), ash::STATUS_RUNNING); |
| 139 } | 138 } |
| 140 | 139 |
| 141 void ShellWindowLauncherController::RegisterApp(ShellWindow* shell_window) { | 140 void ShellWindowLauncherController::RegisterApp(AppWindow* app_window) { |
| 142 aura::Window* window = shell_window->GetNativeWindow(); | 141 aura::Window* window = app_window->GetNativeWindow(); |
| 143 // Get the app's shelf identifier and add an entry to the map. | 142 // Get the app's shelf identifier and add an entry to the map. |
| 144 DCHECK(window_to_app_shelf_id_map_.find(window) == | 143 DCHECK(window_to_app_shelf_id_map_.find(window) == |
| 145 window_to_app_shelf_id_map_.end()); | 144 window_to_app_shelf_id_map_.end()); |
| 146 const std::string app_shelf_id = GetAppShelfId(shell_window); | 145 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 147 window_to_app_shelf_id_map_[window] = app_shelf_id; | 146 window_to_app_shelf_id_map_[window] = app_shelf_id; |
| 148 window->AddObserver(this); | 147 window->AddObserver(this); |
| 149 | 148 |
| 150 // Find or create an item controller and launcher item. | 149 // Find or create an item controller and launcher item. |
| 151 std::string app_id = shell_window->extension()->id(); | 150 std::string app_id = app_window->extension()->id(); |
| 152 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) ? | 151 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) ? |
| 153 ash::STATUS_ACTIVE : ash::STATUS_RUNNING; | 152 ash::STATUS_ACTIVE : ash::STATUS_RUNNING; |
| 154 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); | 153 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); |
| 155 ash::ShelfID shelf_id = 0; | 154 ash::ShelfID shelf_id = 0; |
| 156 if (iter != app_controller_map_.end()) { | 155 if (iter != app_controller_map_.end()) { |
| 157 ShellWindowLauncherItemController* controller = iter->second; | 156 ShellWindowLauncherItemController* controller = iter->second; |
| 158 DCHECK(controller->app_id() == app_id); | 157 DCHECK(controller->app_id() == app_id); |
| 159 shelf_id = controller->shelf_id(); | 158 shelf_id = controller->shelf_id(); |
| 160 controller->AddShellWindow(shell_window, status); | 159 controller->AddAppWindow(app_window, status); |
| 161 } else { | 160 } else { |
| 162 LauncherItemController::Type type = shell_window->window_type_is_panel() | 161 LauncherItemController::Type type = |
| 163 ? LauncherItemController::TYPE_APP_PANEL | 162 app_window->window_type_is_panel() |
| 164 : LauncherItemController::TYPE_APP; | 163 ? LauncherItemController::TYPE_APP_PANEL |
| 164 : LauncherItemController::TYPE_APP; |
| 165 ShellWindowLauncherItemController* controller = | 165 ShellWindowLauncherItemController* controller = |
| 166 new ShellWindowLauncherItemController( | 166 new ShellWindowLauncherItemController( |
| 167 type, app_shelf_id, app_id, owner_); | 167 type, app_shelf_id, app_id, owner_); |
| 168 controller->AddShellWindow(shell_window, status); | 168 controller->AddAppWindow(app_window, status); |
| 169 // If the app shelf id is not unique, and there is already a shelf | 169 // If the app shelf id is not unique, and there is already a shelf |
| 170 // item for this app id (e.g. pinned), use that shelf item. | 170 // item for this app id (e.g. pinned), use that shelf item. |
| 171 if (app_shelf_id == app_id) | 171 if (app_shelf_id == app_id) |
| 172 shelf_id = owner_->GetShelfIDForAppID(app_id); | 172 shelf_id = owner_->GetShelfIDForAppID(app_id); |
| 173 if (shelf_id == 0) { | 173 if (shelf_id == 0) { |
| 174 shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status); | 174 shelf_id = owner_->CreateAppLauncherItem(controller, app_id, status); |
| 175 // Restore any existing app icon and flag as set. | 175 // Restore any existing app icon and flag as set. |
| 176 const gfx::Image& app_icon = shell_window->app_icon(); | 176 const gfx::Image& app_icon = app_window->app_icon(); |
| 177 if (!app_icon.IsEmpty()) { | 177 if (!app_icon.IsEmpty()) { |
| 178 owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 178 owner_->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
| 179 controller->set_image_set_by_controller(true); | 179 controller->set_image_set_by_controller(true); |
| 180 } | 180 } |
| 181 } else { | 181 } else { |
| 182 owner_->SetItemController(shelf_id, controller); | 182 owner_->SetItemController(shelf_id, controller); |
| 183 } | 183 } |
| 184 const std::string app_shelf_id = GetAppShelfId(shell_window); | 184 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 185 app_controller_map_[app_shelf_id] = controller; | 185 app_controller_map_[app_shelf_id] = controller; |
| 186 } | 186 } |
| 187 owner_->SetItemStatus(shelf_id, status); | 187 owner_->SetItemStatus(shelf_id, status); |
| 188 ash::SetShelfIDForWindow(shelf_id, window); | 188 ash::SetShelfIDForWindow(shelf_id, window); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void ShellWindowLauncherController::UnregisterApp(aura::Window* window) { | 191 void ShellWindowLauncherController::UnregisterApp(aura::Window* window) { |
| 192 WindowToAppShelfIdMap::iterator iter1 = | 192 WindowToAppShelfIdMap::iterator iter1 = |
| 193 window_to_app_shelf_id_map_.find(window); | 193 window_to_app_shelf_id_map_.find(window); |
| 194 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); | 194 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); |
| 195 std::string app_shelf_id = iter1->second; | 195 std::string app_shelf_id = iter1->second; |
| 196 window_to_app_shelf_id_map_.erase(iter1); | 196 window_to_app_shelf_id_map_.erase(iter1); |
| 197 window->RemoveObserver(this); | 197 window->RemoveObserver(this); |
| 198 | 198 |
| 199 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 199 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); |
| 200 DCHECK(iter2 != app_controller_map_.end()); | 200 DCHECK(iter2 != app_controller_map_.end()); |
| 201 ShellWindowLauncherItemController* controller = iter2->second; | 201 ShellWindowLauncherItemController* controller = iter2->second; |
| 202 controller->RemoveShellWindowForWindow(window); | 202 controller->RemoveShellWindowForWindow(window); |
| 203 if (controller->shell_window_count() == 0) { | 203 if (controller->app_window_count() == 0) { |
| 204 // If this is the last window associated with the app shelf id, close the | 204 // If this is the last window associated with the app shelf id, close the |
| 205 // shelf item. | 205 // shelf item. |
| 206 ash::ShelfID shelf_id = controller->shelf_id(); | 206 ash::ShelfID shelf_id = controller->shelf_id(); |
| 207 owner_->CloseLauncherItem(shelf_id); | 207 owner_->CloseLauncherItem(shelf_id); |
| 208 app_controller_map_.erase(iter2); | 208 app_controller_map_.erase(iter2); |
| 209 } | 209 } |
| 210 } | 210 } |
| 211 | 211 |
| 212 bool ShellWindowLauncherController::IsRegisteredApp(aura::Window* window) { | 212 bool ShellWindowLauncherController::IsRegisteredApp(aura::Window* window) { |
| 213 return window_to_app_shelf_id_map_.find(window) != | 213 return window_to_app_shelf_id_map_.find(window) != |
| 214 window_to_app_shelf_id_map_.end(); | 214 window_to_app_shelf_id_map_.end(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 // Private Methods | 217 // Private Methods |
| 218 | 218 |
| 219 ShellWindowLauncherItemController* | 219 ShellWindowLauncherItemController* |
| 220 ShellWindowLauncherController::ControllerForWindow( | 220 ShellWindowLauncherController::ControllerForWindow( |
| 221 aura::Window* window) { | 221 aura::Window* window) { |
| 222 WindowToAppShelfIdMap::iterator iter1 = | 222 WindowToAppShelfIdMap::iterator iter1 = |
| 223 window_to_app_shelf_id_map_.find(window); | 223 window_to_app_shelf_id_map_.find(window); |
| 224 if (iter1 == window_to_app_shelf_id_map_.end()) | 224 if (iter1 == window_to_app_shelf_id_map_.end()) |
| 225 return NULL; | 225 return NULL; |
| 226 std::string app_shelf_id = iter1->second; | 226 std::string app_shelf_id = iter1->second; |
| 227 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 227 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); |
| 228 if (iter2 == app_controller_map_.end()) | 228 if (iter2 == app_controller_map_.end()) |
| 229 return NULL; | 229 return NULL; |
| 230 return iter2->second; | 230 return iter2->second; |
| 231 } | 231 } |
| OLD | NEW |