Chromium Code Reviews| 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 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 std::string app_shelf_id = app_id + launch_id; | |
|
stevenjb
2016/08/30 16:12:52
Nice this is much more clear now, thanks!
nit: ret
Andra Paraschiv
2016/08/31 10:57:19
Done.
| |
| 39 return app_shelf_id; | 47 return app_shelf_id; |
| 40 } | 48 } |
| 41 | 49 |
| 42 } // namespace | 50 } // namespace |
| 43 | 51 |
| 44 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( | 52 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( |
| 45 ChromeLauncherController* owner) | 53 ChromeLauncherController* owner) |
| 46 : AppWindowLauncherController(owner) { | 54 : AppWindowLauncherController(owner) { |
| 47 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); | 55 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); |
| 48 registry_.insert(registry); | 56 registry_.insert(registry); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 aura::Window* window = app_window->GetNativeWindow(); | 129 aura::Window* window = app_window->GetNativeWindow(); |
| 122 // Get the app's shelf identifier and add an entry to the map. | 130 // Get the app's shelf identifier and add an entry to the map. |
| 123 DCHECK(window_to_app_shelf_id_map_.find(window) == | 131 DCHECK(window_to_app_shelf_id_map_.find(window) == |
| 124 window_to_app_shelf_id_map_.end()); | 132 window_to_app_shelf_id_map_.end()); |
| 125 const std::string app_shelf_id = GetAppShelfId(app_window); | 133 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 126 window_to_app_shelf_id_map_[window] = app_shelf_id; | 134 window_to_app_shelf_id_map_[window] = app_shelf_id; |
| 127 window->AddObserver(this); | 135 window->AddObserver(this); |
| 128 | 136 |
| 129 // Find or create an item controller and launcher item. | 137 // Find or create an item controller and launcher item. |
| 130 std::string app_id = app_window->extension_id(); | 138 std::string app_id = app_window->extension_id(); |
| 139 std::string launch_id = GetLaunchId(app_window); | |
|
stevenjb
2016/08/30 16:12:52
Move this to where it is used.
Andra Paraschiv
2016/08/31 10:57:19
Done.
| |
| 131 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) | 140 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) |
| 132 ? ash::STATUS_ACTIVE | 141 ? ash::STATUS_ACTIVE |
| 133 : ash::STATUS_RUNNING; | 142 : ash::STATUS_RUNNING; |
| 134 AppControllerMap::iterator app_controller_iter = | 143 AppControllerMap::iterator app_controller_iter = |
| 135 app_controller_map_.find(app_shelf_id); | 144 app_controller_map_.find(app_shelf_id); |
| 136 ash::ShelfID shelf_id = 0; | 145 ash::ShelfID shelf_id = 0; |
| 137 | 146 |
| 138 if (app_controller_iter != app_controller_map_.end()) { | 147 if (app_controller_iter != app_controller_map_.end()) { |
| 139 ExtensionAppWindowLauncherItemController* controller = | 148 ExtensionAppWindowLauncherItemController* controller = |
| 140 app_controller_iter->second; | 149 app_controller_iter->second; |
| 141 DCHECK(controller->app_id() == app_id); | 150 DCHECK(controller->app_id() == app_id); |
| 142 shelf_id = controller->shelf_id(); | 151 shelf_id = controller->shelf_id(); |
| 143 controller->AddAppWindow(app_window); | 152 controller->AddAppWindow(app_window); |
| 144 } else { | 153 } else { |
| 145 LauncherItemController::Type type = | 154 LauncherItemController::Type type = |
| 146 app_window->window_type_is_panel() | 155 app_window->window_type_is_panel() |
| 147 ? LauncherItemController::TYPE_APP_PANEL | 156 ? LauncherItemController::TYPE_APP_PANEL |
| 148 : LauncherItemController::TYPE_APP; | 157 : LauncherItemController::TYPE_APP; |
| 149 ExtensionAppWindowLauncherItemController* controller = | 158 ExtensionAppWindowLauncherItemController* controller = |
| 150 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 159 new ExtensionAppWindowLauncherItemController(type, launch_id, app_id, |
| 151 owner()); | 160 owner()); |
| 152 controller->AddAppWindow(app_window); | 161 controller->AddAppWindow(app_window); |
| 153 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), | 162 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), |
| 154 // use that shelf item. | 163 // use that shelf item. |
| 155 AppShelfIdToShelfIdMap::iterator app_shelf_id_iter = | 164 if (app_shelf_id != app_id) { |
| 156 app_shelf_id_to_shelf_id_map_.find(app_shelf_id); | 165 shelf_id = |
| 157 if (app_shelf_id_iter != app_shelf_id_to_shelf_id_map_.end()) { | 166 ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppIDAndLaunchID( |
| 158 if (owner()->IsPinned(app_shelf_id_iter->second)) { | 167 app_id, launch_id); |
| 159 shelf_id = app_shelf_id_iter->second; | 168 // show_in_shelf is false and not a panel |
|
stevenjb
2016/08/30 16:12:52
This comment looks like it is associated with the
Andra Paraschiv
2016/08/31 10:57:19
Done.
| |
| 160 } else { | 169 } 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 = | 170 shelf_id = |
| 166 ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppID(app_id); | 171 ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppID(app_id); |
|
stevenjb
2016/08/30 16:12:52
These can be combined now, right? launch_id should
Andra Paraschiv
2016/08/31 10:57:18
Yes, we can combine these now.
| |
| 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 } | 172 } |
| 180 | 173 |
| 181 if (shelf_id == 0) { | 174 if (shelf_id == 0) { |
| 182 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 175 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
| 183 // Restore any existing app icon and flag as set. | 176 // Restore any existing app icon and flag as set. |
| 184 const gfx::Image& app_icon = app_window->app_icon(); | 177 const gfx::Image& app_icon = app_window->app_icon(); |
| 185 if (!app_icon.IsEmpty()) { | 178 if (!app_icon.IsEmpty()) { |
| 186 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 179 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
| 187 controller->set_image_set_by_controller(true); | 180 controller->set_image_set_by_controller(true); |
| 188 } | 181 } |
| 189 } else { | 182 } else { |
| 190 owner()->SetItemController(shelf_id, controller); | 183 owner()->SetItemController(shelf_id, controller); |
| 191 } | 184 } |
| 192 | 185 |
| 193 // We need to change the controller associated with app_shelf_id. | 186 // We need to change the controller associated with app_shelf_id. |
| 194 app_controller_map_[app_shelf_id] = controller; | 187 app_controller_map_[app_shelf_id] = controller; |
| 195 app_shelf_id_to_shelf_id_map_[app_shelf_id] = shelf_id; | |
| 196 } | 188 } |
| 197 owner()->SetItemStatus(shelf_id, status); | 189 owner()->SetItemStatus(shelf_id, status); |
| 198 ash::SetShelfIDForWindow(shelf_id, window); | 190 ash::SetShelfIDForWindow(shelf_id, window); |
| 199 } | 191 } |
| 200 | 192 |
| 201 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 193 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
| 202 WindowToAppShelfIdMap::iterator window_iter = | 194 WindowToAppShelfIdMap::iterator window_iter = |
| 203 window_to_app_shelf_id_map_.find(window); | 195 window_to_app_shelf_id_map_.find(window); |
| 204 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); | 196 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); |
| 205 std::string app_shelf_id = window_iter->second; | 197 std::string app_shelf_id = window_iter->second; |
| 206 window_to_app_shelf_id_map_.erase(window_iter); | 198 window_to_app_shelf_id_map_.erase(window_iter); |
| 207 window->RemoveObserver(this); | 199 window->RemoveObserver(this); |
| 208 | 200 |
| 209 AppControllerMap::iterator app_controller_iter = | 201 AppControllerMap::iterator app_controller_iter = |
| 210 app_controller_map_.find(app_shelf_id); | 202 app_controller_map_.find(app_shelf_id); |
| 211 DCHECK(app_controller_iter != app_controller_map_.end()); | 203 DCHECK(app_controller_iter != app_controller_map_.end()); |
| 212 ExtensionAppWindowLauncherItemController* controller; | 204 ExtensionAppWindowLauncherItemController* controller; |
| 213 controller = app_controller_iter->second; | 205 controller = app_controller_iter->second; |
| 214 | 206 |
| 215 controller->RemoveWindow(controller->GetAppWindow(window)); | 207 controller->RemoveWindow(controller->GetAppWindow(window)); |
| 216 if (controller->window_count() == 0) { | 208 if (controller->window_count() == 0) { |
| 217 // If this is the last window associated with the app window shelf id, | 209 // If this is the last window associated with the app window shelf id, |
| 218 // close the shelf item. | 210 // close the shelf item. |
| 219 ash::ShelfID shelf_id = controller->shelf_id(); | 211 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); | 212 owner()->CloseLauncherItem(shelf_id); |
| 224 app_controller_map_.erase(app_controller_iter); | 213 app_controller_map_.erase(app_controller_iter); |
| 225 } | 214 } |
| 226 } | 215 } |
| 227 | 216 |
| 228 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 217 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
| 229 aura::Window* window) { | 218 aura::Window* window) { |
| 230 return window_to_app_shelf_id_map_.find(window) != | 219 return window_to_app_shelf_id_map_.find(window) != |
| 231 window_to_app_shelf_id_map_.end(); | 220 window_to_app_shelf_id_map_.end(); |
| 232 } | 221 } |
| 233 | 222 |
| 234 // Private Methods | 223 // Private Methods |
| 235 | 224 |
| 236 AppWindowLauncherItemController* | 225 AppWindowLauncherItemController* |
| 237 ExtensionAppWindowLauncherController::ControllerForWindow( | 226 ExtensionAppWindowLauncherController::ControllerForWindow( |
| 238 aura::Window* window) { | 227 aura::Window* window) { |
| 239 WindowToAppShelfIdMap::iterator window_iter = | 228 WindowToAppShelfIdMap::iterator window_iter = |
| 240 window_to_app_shelf_id_map_.find(window); | 229 window_to_app_shelf_id_map_.find(window); |
| 241 if (window_iter == window_to_app_shelf_id_map_.end()) | 230 if (window_iter == window_to_app_shelf_id_map_.end()) |
| 242 return nullptr; | 231 return nullptr; |
| 243 AppControllerMap::iterator app_controller_iter = | 232 AppControllerMap::iterator app_controller_iter = |
| 244 app_controller_map_.find(window_iter->second); | 233 app_controller_map_.find(window_iter->second); |
| 245 if (app_controller_iter == app_controller_map_.end()) | 234 if (app_controller_iter == app_controller_map_.end()) |
| 246 return nullptr; | 235 return nullptr; |
| 247 return app_controller_iter->second; | 236 return app_controller_iter->second; |
| 248 } | 237 } |
| OLD | NEW |