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