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/shelf/shelf_delegate.h" | 7 #include "ash/shelf/shelf_delegate.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/stl_util.h" | |
| 11 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.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/extension_app_window_launcher_item_cont roller.h" | 15 #include "chrome/browser/ui/ash/launcher/extension_app_window_launcher_item_cont roller.h" |
| 15 #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" |
| 16 #include "extensions/browser/app_window/app_window.h" | 17 #include "extensions/browser/app_window/app_window.h" |
| 17 #include "extensions/browser/app_window/native_app_window.h" | 18 #include "extensions/browser/app_window/native_app_window.h" |
| 18 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 19 #include "ui/aura/window_event_dispatcher.h" | 20 #include "ui/aura/window_event_dispatcher.h" |
| 20 | 21 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 window_to_app_shelf_id_map_[window] = app_shelf_id; | 117 window_to_app_shelf_id_map_[window] = app_shelf_id; |
| 117 window->AddObserver(this); | 118 window->AddObserver(this); |
| 118 | 119 |
| 119 // Find or create an item controller and launcher item. | 120 // Find or create an item controller and launcher item. |
| 120 std::string app_id = app_window->extension_id(); | 121 std::string app_id = app_window->extension_id(); |
| 121 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) | 122 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) |
| 122 ? ash::STATUS_ACTIVE | 123 ? ash::STATUS_ACTIVE |
| 123 : ash::STATUS_RUNNING; | 124 : ash::STATUS_RUNNING; |
| 124 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); | 125 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); |
| 125 ash::ShelfID shelf_id = 0; | 126 ash::ShelfID shelf_id = 0; |
| 126 if (iter != app_controller_map_.end()) { | 127 std::string window_key = app_window->window_key(); |
| 128 | |
| 129 if (!app_window->show_in_shelf() && iter != app_controller_map_.end()) { | |
| 127 ExtensionAppWindowLauncherItemController* controller = iter->second; | 130 ExtensionAppWindowLauncherItemController* controller = iter->second; |
| 128 DCHECK(controller->app_id() == app_id); | 131 DCHECK(controller->app_id() == app_id); |
| 129 shelf_id = controller->shelf_id(); | 132 shelf_id = controller->shelf_id(); |
| 130 controller->AddAppWindow(app_window); | 133 controller->AddAppWindow(app_window); |
| 131 } else { | 134 } else { |
| 132 LauncherItemController::Type type = | 135 LauncherItemController::Type type = |
| 133 app_window->window_type_is_panel() | 136 app_window->window_type_is_panel() |
| 134 ? LauncherItemController::TYPE_APP_PANEL | 137 ? LauncherItemController::TYPE_APP_PANEL |
| 135 : LauncherItemController::TYPE_APP; | 138 : LauncherItemController::TYPE_APP; |
| 136 ExtensionAppWindowLauncherItemController* controller = | 139 ExtensionAppWindowLauncherItemController* controller = |
| 137 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 140 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, |
| 138 owner()); | 141 owner()); |
| 139 controller->AddAppWindow(app_window); | 142 controller->AddAppWindow(app_window); |
| 140 // If the app shelf id is not unique, and there is already a shelf | 143 // If the app shelf id is not unique, and there is already a shelf |
| 141 // item for this app id (e.g. pinned), use that shelf item. | 144 // item for this app id (e.g. pinned), use that shelf item, except for the |
| 142 if (app_shelf_id == app_id) { | 145 // case when the showInShelf parameter is true. |
|
stevenjb
2016/06/14 18:33:21
This is confusing. We should have a comment in the
Andra Paraschiv
2016/06/28 07:04:46
Done.
| |
| 146 if (!window_key.empty() && | |
| 147 ContainsKey(window_id_to_shelf_id_map_, window_key)) { | |
|
stevenjb
2016/06/14 18:33:21
This should be window_key_to_shelf_id_map_.
Andra Paraschiv
2016/06/28 07:04:46
Done.
| |
| 148 shelf_id = window_id_to_shelf_id_map_[window_key]; | |
|
stevenjb
2016/06/14 18:33:21
We are doing two lookups. We should get an iterato
Andra Paraschiv
2016/06/28 07:04:46
Done.
| |
| 149 if (!owner()->IsPinned(shelf_id)) { | |
|
stevenjb
2016/06/14 18:33:21
I'm not following what this is supposed to do. Why
Andra Paraschiv
2016/06/28 07:04:46
If a pinned window is closed, then unpinned and op
| |
| 150 window_id_to_shelf_id_map_.erase(window_key); | |
| 151 shelf_id = 0; | |
| 152 } | |
| 153 } | |
| 154 else if (app_shelf_id == app_id && !app_window->show_in_shelf() && | |
|
stevenjb
2016/06/14 18:33:21
else on previous line
Andra Paraschiv
2016/06/28 07:04:46
Done.
| |
| 155 secondary_window_controller_map_.empty() && | |
| 156 window_id_to_shelf_id_map_.empty()) { | |
|
stevenjb
2016/06/14 18:33:21
Why test for an empty map? Couldn't a different ap
Andra Paraschiv
2016/06/28 07:04:46
Yes, you are right, thank you for this notice. The
| |
| 143 shelf_id = | 157 shelf_id = |
| 144 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID( | 158 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID( |
| 145 app_id); | 159 app_id); |
| 146 } | 160 } |
| 161 | |
| 147 if (shelf_id == 0) { | 162 if (shelf_id == 0) { |
| 148 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 163 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
| 149 // Restore any existing app icon and flag as set. | 164 // Restore any existing app icon and flag as set. |
| 150 const gfx::Image& app_icon = app_window->app_icon(); | 165 const gfx::Image& app_icon = app_window->app_icon(); |
| 151 if (!app_icon.IsEmpty()) { | 166 if (!app_icon.IsEmpty()) { |
| 152 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 167 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
|
stevenjb
2016/06/14 18:33:21
Currently the extra window icons will still have t
Andra Paraschiv
2016/06/21 09:04:13
Yes, extra window icons still have the app icon im
| |
| 153 controller->set_image_set_by_controller(true); | 168 controller->set_image_set_by_controller(true); |
| 154 } | 169 } |
| 155 } else { | 170 } else { |
| 156 owner()->SetItemController(shelf_id, controller); | 171 owner()->SetItemController(shelf_id, controller); |
| 157 } | 172 } |
| 173 | |
| 158 const std::string app_shelf_id = GetAppShelfId(app_window); | 174 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 159 app_controller_map_[app_shelf_id] = controller; | 175 // We need to change the controller associated with the app if there is no |
| 176 // controller currently tied to the app shelf id (first window). | |
| 177 if (!ContainsKey(app_controller_map_, app_shelf_id) && | |
| 178 !app_window->show_in_shelf()) { | |
| 179 app_controller_map_[app_shelf_id] = controller; | |
| 180 } else if (app_window->show_in_shelf()) { | |
| 181 secondary_window_controller_map_[window] = controller; | |
| 182 } | |
| 183 if (!window_key.empty()) { | |
| 184 window_id_to_shelf_id_map_[window_key] = shelf_id; | |
| 185 } | |
| 160 } | 186 } |
|
stevenjb
2016/06/14 18:33:21
Having 3 different maps in this code now is super
Andra Paraschiv
2016/06/21 09:04:13
We could use the window key to map to the shelf it
| |
| 161 owner()->SetItemStatus(shelf_id, status); | 187 owner()->SetItemStatus(shelf_id, status); |
| 162 ash::SetShelfIDForWindow(shelf_id, window); | 188 ash::SetShelfIDForWindow(shelf_id, window); |
| 163 } | 189 } |
| 164 | 190 |
| 165 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 191 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
| 166 WindowToAppShelfIdMap::iterator iter1 = | 192 WindowToAppShelfIdMap::iterator shelf_id_iter = |
| 167 window_to_app_shelf_id_map_.find(window); | 193 window_to_app_shelf_id_map_.find(window); |
| 168 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); | 194 DCHECK(shelf_id_iter != window_to_app_shelf_id_map_.end()); |
| 169 std::string app_shelf_id = iter1->second; | 195 std::string app_shelf_id = shelf_id_iter->second; |
| 170 window_to_app_shelf_id_map_.erase(iter1); | 196 window_to_app_shelf_id_map_.erase(shelf_id_iter); |
| 171 window->RemoveObserver(this); | 197 window->RemoveObserver(this); |
| 172 | 198 |
| 173 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 199 AppControllerMap::iterator app_controller_iter = |
| 174 DCHECK(iter2 != app_controller_map_.end()); | 200 app_controller_map_.find(app_shelf_id); |
| 175 ExtensionAppWindowLauncherItemController* controller = iter2->second; | 201 WindowControllerMap::iterator secondary_window_iter = |
| 202 secondary_window_controller_map_.find(window); | |
| 203 DCHECK(app_controller_iter != app_controller_map_.end() || | |
| 204 secondary_window_iter != secondary_window_controller_map_.end()); | |
|
stevenjb
2016/06/14 18:33:21
I think we should be able to combine these maps. w
Andra Paraschiv
2016/06/28 07:04:46
We combined the app_controller_map and the seconda
| |
| 205 ExtensionAppWindowLauncherItemController* controller; | |
| 206 if (secondary_window_iter != secondary_window_controller_map_.end()) | |
| 207 controller = secondary_window_iter->second; | |
| 208 else | |
| 209 controller = app_controller_iter->second; | |
| 210 | |
| 176 controller->RemoveWindow(controller->GetAppWindow(window)); | 211 controller->RemoveWindow(controller->GetAppWindow(window)); |
| 177 if (controller->window_count() == 0) { | 212 if (controller->window_count() == 0) { |
| 178 // If this is the last window associated with the app shelf id, close the | 213 // If this is the last window associated with the app shelf id, close the |
| 179 // shelf item. | 214 // shelf item. |
| 180 ash::ShelfID shelf_id = controller->shelf_id(); | 215 ash::ShelfID shelf_id = controller->shelf_id(); |
| 216 if (!owner()->IsPinned(shelf_id)) { | |
| 217 for (WindowIdToShelfIdMap::const_iterator i = | |
| 218 window_id_to_shelf_id_map_.begin(); | |
| 219 i != window_id_to_shelf_id_map_.end(); ++i) { | |
| 220 if (shelf_id == i->second) { | |
| 221 window_id_to_shelf_id_map_.erase(i); | |
| 222 break; | |
| 223 } | |
| 224 } | |
|
stevenjb
2016/06/14 18:33:21
I think we ma have an stl util helper to do this i
Andra Paraschiv
2016/06/28 07:04:46
We should delete the shelf_id from the map for unp
| |
| 225 } | |
| 181 owner()->CloseLauncherItem(shelf_id); | 226 owner()->CloseLauncherItem(shelf_id); |
| 182 app_controller_map_.erase(iter2); | 227 if (secondary_window_iter != secondary_window_controller_map_.end()) |
| 228 secondary_window_controller_map_.erase(secondary_window_iter); | |
| 229 else | |
| 230 app_controller_map_.erase(app_controller_iter); | |
| 183 } | 231 } |
| 184 } | 232 } |
| 185 | 233 |
| 186 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 234 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
| 187 aura::Window* window) { | 235 aura::Window* window) { |
| 188 return window_to_app_shelf_id_map_.find(window) != | 236 return window_to_app_shelf_id_map_.find(window) != |
| 189 window_to_app_shelf_id_map_.end(); | 237 window_to_app_shelf_id_map_.end(); |
| 190 } | 238 } |
| 191 | 239 |
| 192 // Private Methods | 240 // Private Methods |
| 193 | 241 |
| 194 AppWindowLauncherItemController* | 242 AppWindowLauncherItemController* |
| 195 ExtensionAppWindowLauncherController::ControllerForWindow( | 243 ExtensionAppWindowLauncherController::ControllerForWindow( |
| 196 aura::Window* window) { | 244 aura::Window* window) { |
| 197 WindowToAppShelfIdMap::iterator iter1 = | 245 WindowToAppShelfIdMap::iterator shelf_id_iter = |
| 198 window_to_app_shelf_id_map_.find(window); | 246 window_to_app_shelf_id_map_.find(window); |
| 199 if (iter1 == window_to_app_shelf_id_map_.end()) | 247 if (shelf_id_iter == window_to_app_shelf_id_map_.end()) |
| 200 return nullptr; | 248 return nullptr; |
| 201 std::string app_shelf_id = iter1->second; | 249 WindowControllerMap::iterator secondary_window_iter = |
| 202 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 250 secondary_window_controller_map_.find(window); |
| 203 if (iter2 == app_controller_map_.end()) | 251 if (secondary_window_iter != secondary_window_controller_map_.end()) |
| 204 return nullptr; | 252 return secondary_window_iter->second; |
| 205 return iter2->second; | 253 AppControllerMap::iterator app_controller_iter = |
| 254 app_controller_map_.find(shelf_id_iter->second); | |
| 255 if (app_controller_iter != app_controller_map_.end()) | |
| 256 return app_controller_iter->second; | |
| 257 return nullptr; | |
| 206 } | 258 } |
| OLD | NEW |