| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/extension_app_window_launcher_controlle
r.h" | 5 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_controlle
r.h" |
| 6 | 6 |
| 7 #include "ash/common/shelf/shelf_delegate.h" | 7 #include "ash/common/shelf/shelf_delegate.h" |
| 8 #include "ash/common/wm_shell.h" | 8 #include "ash/common/wm_shell.h" |
| 9 #include "ash/shelf/shelf_util.h" | 9 #include "ash/shelf/shelf_util.h" |
| 10 #include "ash/wm/window_util.h" | 10 #include "ash/wm/window_util.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| 15 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_item_cont
roller.h" | 15 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_item_cont
roller.h" |
| 16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 16 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 17 #include "extensions/browser/app_window/app_window.h" | 17 #include "extensions/browser/app_window/app_window.h" |
| 18 #include "extensions/browser/app_window/native_app_window.h" | 18 #include "extensions/browser/app_window/native_app_window.h" |
| 19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "ui/aura/window_event_dispatcher.h" | 20 #include "ui/aura/window_event_dispatcher.h" |
| 21 | 21 |
| 22 using extensions::AppWindow; | 22 using extensions::AppWindow; |
| 23 using extensions::AppWindowRegistry; | 23 using extensions::AppWindowRegistry; |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 std::string GetAppShelfId(AppWindow* app_window) { | 27 std::string GetLaunchId(AppWindow* app_window) { |
| 28 // Set app_shelf_id default value to extension_id. If showInShelf parameter | 28 // Set launch_id default value to an empty string. If showInShelf parameter |
| 29 // is true or the window type is panel and the window key is not empty, its | 29 // is true or the window type is panel and the window key is not empty, its |
| 30 // value is appended to the app_shelf_id. Otherwise, if the window key is | 30 // value is appended to the launch_id. Otherwise, if the window key is |
| 31 // empty, the session_id is used. | 31 // empty, the session_id is used. |
| 32 std::string app_shelf_id = app_window->extension_id(); | 32 std::string launch_id; |
| 33 if (app_window->show_in_shelf() || app_window->window_type_is_panel()) { | 33 if (app_window->show_in_shelf() || app_window->window_type_is_panel()) { |
| 34 if (!app_window->window_key().empty()) | 34 if (!app_window->window_key().empty()) |
| 35 app_shelf_id += app_window->window_key(); | 35 launch_id = app_window->window_key(); |
| 36 else | 36 else |
| 37 app_shelf_id += base::StringPrintf("%d", app_window->session_id().id()); | 37 launch_id = base::StringPrintf("%d", app_window->session_id().id()); |
| 38 } | 38 } |
| 39 return app_shelf_id; | 39 return launch_id; |
| 40 } |
| 41 |
| 42 std::string GetAppShelfId(AppWindow* app_window) { |
| 43 // Set app_shelf_id value to app_id and then append launch_id. |
| 44 std::string app_id = app_window->extension_id(); |
| 45 std::string launch_id = GetLaunchId(app_window); |
| 46 return app_id + launch_id; |
| 40 } | 47 } |
| 41 | 48 |
| 42 } // namespace | 49 } // namespace |
| 43 | 50 |
| 44 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( | 51 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( |
| 45 ChromeLauncherController* owner) | 52 ChromeLauncherController* owner) |
| 46 : AppWindowLauncherController(owner) { | 53 : AppWindowLauncherController(owner) { |
| 47 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); | 54 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); |
| 48 registry_.insert(registry); | 55 registry_.insert(registry); |
| 49 registry->AddObserver(this); | 56 registry->AddObserver(this); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 ExtensionAppWindowLauncherItemController* controller = | 146 ExtensionAppWindowLauncherItemController* controller = |
| 140 app_controller_iter->second; | 147 app_controller_iter->second; |
| 141 DCHECK(controller->app_id() == app_id); | 148 DCHECK(controller->app_id() == app_id); |
| 142 shelf_id = controller->shelf_id(); | 149 shelf_id = controller->shelf_id(); |
| 143 controller->AddAppWindow(app_window); | 150 controller->AddAppWindow(app_window); |
| 144 } else { | 151 } else { |
| 145 LauncherItemController::Type type = | 152 LauncherItemController::Type type = |
| 146 app_window->window_type_is_panel() | 153 app_window->window_type_is_panel() |
| 147 ? LauncherItemController::TYPE_APP_PANEL | 154 ? LauncherItemController::TYPE_APP_PANEL |
| 148 : LauncherItemController::TYPE_APP; | 155 : LauncherItemController::TYPE_APP; |
| 156 std::string launch_id = GetLaunchId(app_window); |
| 149 ExtensionAppWindowLauncherItemController* controller = | 157 ExtensionAppWindowLauncherItemController* controller = |
| 150 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 158 new ExtensionAppWindowLauncherItemController(type, app_id, launch_id, |
| 151 owner()); | 159 owner()); |
| 152 controller->AddAppWindow(app_window); | 160 controller->AddAppWindow(app_window); |
| 153 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), | 161 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), |
| 154 // use that shelf item. | 162 // use that shelf item. |
| 155 AppShelfIdToShelfIdMap::iterator app_shelf_id_iter = | 163 shelf_id = |
| 156 app_shelf_id_to_shelf_id_map_.find(app_shelf_id); | 164 ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppIDAndLaunchID( |
| 157 if (app_shelf_id_iter != app_shelf_id_to_shelf_id_map_.end()) { | 165 app_id, launch_id); |
| 158 if (owner()->IsPinned(app_shelf_id_iter->second)) { | |
| 159 shelf_id = app_shelf_id_iter->second; | |
| 160 } else { | |
| 161 app_shelf_id_to_shelf_id_map_.erase(app_shelf_id); | |
| 162 } | |
| 163 } else if (app_shelf_id == app_id) { | |
| 164 // show_in_shelf in false and not a panel | |
| 165 shelf_id = | |
| 166 ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppID(app_id); | |
| 167 // Check if the shelf_id corresponds to an already opened | |
| 168 // showInShelf=true window that has the same app_id. The current | |
| 169 // showInShelf=false window should not fold under this shelf item, | |
| 170 // so the shelf_id is set to 0 to get a new shelf_id. | |
| 171 auto&& id_map = app_shelf_id_to_shelf_id_map_; | |
| 172 if (std::find_if( | |
| 173 id_map.begin(), id_map.end(), | |
| 174 [shelf_id](const AppShelfIdToShelfIdMap::value_type& pair) { | |
| 175 return pair.second == shelf_id; | |
| 176 }) != id_map.end()) { | |
| 177 shelf_id = 0; | |
| 178 } | |
| 179 } | |
| 180 | 166 |
| 181 if (shelf_id == 0) { | 167 if (shelf_id == 0) { |
| 182 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 168 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
| 183 // Restore any existing app icon and flag as set. | 169 // Restore any existing app icon and flag as set. |
| 184 const gfx::Image& app_icon = app_window->app_icon(); | 170 const gfx::Image& app_icon = app_window->app_icon(); |
| 185 if (!app_icon.IsEmpty()) { | 171 if (!app_icon.IsEmpty()) { |
| 186 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 172 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
| 187 controller->set_image_set_by_controller(true); | 173 controller->set_image_set_by_controller(true); |
| 188 } | 174 } |
| 189 } else { | 175 } else { |
| 190 owner()->SetItemController(shelf_id, controller); | 176 owner()->SetItemController(shelf_id, controller); |
| 191 } | 177 } |
| 192 | 178 |
| 193 // We need to change the controller associated with app_shelf_id. | 179 // We need to change the controller associated with app_shelf_id. |
| 194 app_controller_map_[app_shelf_id] = controller; | 180 app_controller_map_[app_shelf_id] = controller; |
| 195 app_shelf_id_to_shelf_id_map_[app_shelf_id] = shelf_id; | |
| 196 } | 181 } |
| 197 owner()->SetItemStatus(shelf_id, status); | 182 owner()->SetItemStatus(shelf_id, status); |
| 198 ash::SetShelfIDForWindow(shelf_id, window); | 183 ash::SetShelfIDForWindow(shelf_id, window); |
| 199 } | 184 } |
| 200 | 185 |
| 201 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 186 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
| 202 WindowToAppShelfIdMap::iterator window_iter = | 187 WindowToAppShelfIdMap::iterator window_iter = |
| 203 window_to_app_shelf_id_map_.find(window); | 188 window_to_app_shelf_id_map_.find(window); |
| 204 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); | 189 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); |
| 205 std::string app_shelf_id = window_iter->second; | 190 std::string app_shelf_id = window_iter->second; |
| 206 window_to_app_shelf_id_map_.erase(window_iter); | 191 window_to_app_shelf_id_map_.erase(window_iter); |
| 207 window->RemoveObserver(this); | 192 window->RemoveObserver(this); |
| 208 | 193 |
| 209 AppControllerMap::iterator app_controller_iter = | 194 AppControllerMap::iterator app_controller_iter = |
| 210 app_controller_map_.find(app_shelf_id); | 195 app_controller_map_.find(app_shelf_id); |
| 211 DCHECK(app_controller_iter != app_controller_map_.end()); | 196 DCHECK(app_controller_iter != app_controller_map_.end()); |
| 212 ExtensionAppWindowLauncherItemController* controller; | 197 ExtensionAppWindowLauncherItemController* controller; |
| 213 controller = app_controller_iter->second; | 198 controller = app_controller_iter->second; |
| 214 | 199 |
| 215 controller->RemoveWindow(controller->GetAppWindow(window)); | 200 controller->RemoveWindow(controller->GetAppWindow(window)); |
| 216 if (controller->window_count() == 0) { | 201 if (controller->window_count() == 0) { |
| 217 // If this is the last window associated with the app window shelf id, | 202 // If this is the last window associated with the app window shelf id, |
| 218 // close the shelf item. | 203 // close the shelf item. |
| 219 ash::ShelfID shelf_id = controller->shelf_id(); | 204 ash::ShelfID shelf_id = controller->shelf_id(); |
| 220 if (!owner()->IsPinned(shelf_id)) { | |
| 221 app_shelf_id_to_shelf_id_map_.erase(app_shelf_id); | |
| 222 } | |
| 223 owner()->CloseLauncherItem(shelf_id); | 205 owner()->CloseLauncherItem(shelf_id); |
| 224 app_controller_map_.erase(app_controller_iter); | 206 app_controller_map_.erase(app_controller_iter); |
| 225 } | 207 } |
| 226 } | 208 } |
| 227 | 209 |
| 228 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 210 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
| 229 aura::Window* window) { | 211 aura::Window* window) { |
| 230 return window_to_app_shelf_id_map_.find(window) != | 212 return window_to_app_shelf_id_map_.find(window) != |
| 231 window_to_app_shelf_id_map_.end(); | 213 window_to_app_shelf_id_map_.end(); |
| 232 } | 214 } |
| 233 | 215 |
| 234 // Private Methods | 216 // Private Methods |
| 235 | 217 |
| 236 AppWindowLauncherItemController* | 218 AppWindowLauncherItemController* |
| 237 ExtensionAppWindowLauncherController::ControllerForWindow( | 219 ExtensionAppWindowLauncherController::ControllerForWindow( |
| 238 aura::Window* window) { | 220 aura::Window* window) { |
| 239 WindowToAppShelfIdMap::iterator window_iter = | 221 WindowToAppShelfIdMap::iterator window_iter = |
| 240 window_to_app_shelf_id_map_.find(window); | 222 window_to_app_shelf_id_map_.find(window); |
| 241 if (window_iter == window_to_app_shelf_id_map_.end()) | 223 if (window_iter == window_to_app_shelf_id_map_.end()) |
| 242 return nullptr; | 224 return nullptr; |
| 243 AppControllerMap::iterator app_controller_iter = | 225 AppControllerMap::iterator app_controller_iter = |
| 244 app_controller_map_.find(window_iter->second); | 226 app_controller_map_.find(window_iter->second); |
| 245 if (app_controller_iter == app_controller_map_.end()) | 227 if (app_controller_iter == app_controller_map_.end()) |
| 246 return nullptr; | 228 return nullptr; |
| 247 return app_controller_iter->second; | 229 return app_controller_iter->second; |
| 248 } | 230 } |
| OLD | NEW |