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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 106 // Windows created by IME extension should be treated the same way as the | 107 // Windows created by IME extension should be treated the same way as the |
| 107 // virtual keyboard window, which does not register itself in launcher. | 108 // virtual keyboard window, which does not register itself in launcher. |
| 108 if (app_window->is_ime_window()) | 109 if (app_window->is_ime_window()) |
| 109 return; | 110 return; |
| 110 | 111 |
| 111 aura::Window* window = app_window->GetNativeWindow(); | 112 aura::Window* window = app_window->GetNativeWindow(); |
| 112 // Get the app's shelf identifier and add an entry to the map. | 113 // Get the app's shelf identifier and add an entry to the map. |
| 113 DCHECK(window_to_app_shelf_id_map_.find(window) == | 114 DCHECK(window_to_app_shelf_id_map_.find(window) == |
| 114 window_to_app_shelf_id_map_.end()); | 115 window_to_app_shelf_id_map_.end()); |
| 115 const std::string app_shelf_id = GetAppShelfId(app_window); | 116 const std::string app_shelf_id = GetAppShelfId(app_window); |
| 116 window_to_app_shelf_id_map_[window] = app_shelf_id; | 117 // Set window key to app shelf id if showInShelf parameter is false. |
| 118 // If showInShelf parameter is true the window key value is used. | |
| 119 std::string window_key = app_window->show_in_shelf() ? | |
| 120 app_window->window_key() : app_shelf_id; | |
|
Reilly Grant (use Gerrit)
2016/07/06 19:53:00
app_window->window_key() is controlled by the appl
stevenjb
2016/07/07 22:55:54
Good catch, thanks!
Andra Paraschiv
2016/07/08 07:20:26
Thank you Reilly for the review and for noticing t
| |
| 121 window_to_app_shelf_id_map_[window] = window_key; | |
| 117 window->AddObserver(this); | 122 window->AddObserver(this); |
| 118 | 123 |
| 119 // Find or create an item controller and launcher item. | 124 // Find or create an item controller and launcher item. |
| 120 std::string app_id = app_window->extension_id(); | 125 std::string app_id = app_window->extension_id(); |
| 121 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) | 126 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) |
| 122 ? ash::STATUS_ACTIVE | 127 ? ash::STATUS_ACTIVE |
| 123 : ash::STATUS_RUNNING; | 128 : ash::STATUS_RUNNING; |
| 124 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); | 129 AppControllerMap::iterator app_controller_iter = |
| 130 app_controller_map_.find(window_key); | |
| 125 ash::ShelfID shelf_id = 0; | 131 ash::ShelfID shelf_id = 0; |
| 126 if (iter != app_controller_map_.end()) { | 132 |
| 127 ExtensionAppWindowLauncherItemController* controller = iter->second; | 133 if (app_controller_iter != app_controller_map_.end()) { |
| 134 ExtensionAppWindowLauncherItemController* controller = | |
| 135 app_controller_iter->second; | |
| 128 DCHECK(controller->app_id() == app_id); | 136 DCHECK(controller->app_id() == app_id); |
| 129 shelf_id = controller->shelf_id(); | 137 shelf_id = controller->shelf_id(); |
| 130 controller->AddAppWindow(app_window); | 138 controller->AddAppWindow(app_window); |
| 131 } else { | 139 } else { |
| 132 LauncherItemController::Type type = | 140 LauncherItemController::Type type = |
| 133 app_window->window_type_is_panel() | 141 app_window->window_type_is_panel() |
| 134 ? LauncherItemController::TYPE_APP_PANEL | 142 ? LauncherItemController::TYPE_APP_PANEL |
| 135 : LauncherItemController::TYPE_APP; | 143 : LauncherItemController::TYPE_APP; |
| 136 ExtensionAppWindowLauncherItemController* controller = | 144 ExtensionAppWindowLauncherItemController* controller = |
| 137 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 145 new ExtensionAppWindowLauncherItemController(type, window_key, app_id, |
| 138 owner()); | 146 owner()); |
| 139 controller->AddAppWindow(app_window); | 147 controller->AddAppWindow(app_window); |
| 140 // If the app shelf id is not unique, and there is already a shelf | 148 // If there is already a shelf id mapped to this window key (e.g. pinned), |
| 141 // item for this app id (e.g. pinned), use that shelf item. | 149 // use that shelf item. |
| 142 if (app_shelf_id == app_id) { | 150 WindowKeyToShelfIdMap::iterator window_key_iter = |
| 143 shelf_id = | 151 window_key_to_shelf_id_map_.find(window_key); |
| 144 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID( | 152 if (window_key_iter != window_key_to_shelf_id_map_.end()) { |
| 145 app_id); | 153 if (!owner()->IsPinned(window_key_iter->second)) { |
|
Reilly Grant (use Gerrit)
2016/07/06 19:53:00
Reverse this if statement for better readability.
Andra Paraschiv
2016/07/08 07:20:26
Done.
| |
| 154 window_key_to_shelf_id_map_.erase(window_key); | |
| 155 } else { | |
| 156 shelf_id = window_key_iter->second; | |
| 157 } | |
| 146 } | 158 } |
| 159 | |
| 147 if (shelf_id == 0) { | 160 if (shelf_id == 0) { |
| 148 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 161 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
| 149 // Restore any existing app icon and flag as set. | 162 // Restore any existing app icon and flag as set. |
| 150 const gfx::Image& app_icon = app_window->app_icon(); | 163 const gfx::Image& app_icon = app_window->app_icon(); |
| 151 if (!app_icon.IsEmpty()) { | 164 if (!app_icon.IsEmpty()) { |
| 152 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 165 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
| 153 controller->set_image_set_by_controller(true); | 166 controller->set_image_set_by_controller(true); |
| 154 } | 167 } |
| 155 } else { | 168 } else { |
| 156 owner()->SetItemController(shelf_id, controller); | 169 owner()->SetItemController(shelf_id, controller); |
| 157 } | 170 } |
| 158 const std::string app_shelf_id = GetAppShelfId(app_window); | 171 |
| 159 app_controller_map_[app_shelf_id] = controller; | 172 // We need to change the controller associated with app window shelf id. |
| 173 app_controller_map_[window_key] = controller; | |
| 174 window_key_to_shelf_id_map_[window_key] = shelf_id; | |
| 160 } | 175 } |
| 161 owner()->SetItemStatus(shelf_id, status); | 176 owner()->SetItemStatus(shelf_id, status); |
| 162 ash::SetShelfIDForWindow(shelf_id, window); | 177 ash::SetShelfIDForWindow(shelf_id, window); |
| 163 } | 178 } |
| 164 | 179 |
| 165 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 180 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
| 166 WindowToAppShelfIdMap::iterator iter1 = | 181 WindowToAppShelfIdMap::iterator app_shelf_id_iter = |
| 167 window_to_app_shelf_id_map_.find(window); | 182 window_to_app_shelf_id_map_.find(window); |
| 168 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); | 183 DCHECK(app_shelf_id_iter != window_to_app_shelf_id_map_.end()); |
| 169 std::string app_shelf_id = iter1->second; | 184 std::string window_key = app_shelf_id_iter->second; |
|
Reilly Grant (use Gerrit)
2016/07/06 19:53:00
This isn't window_key, it's app_shelf_id.
stevenjb
2016/07/07 22:55:54
ditto
Andra Paraschiv
2016/07/08 07:20:26
Done.
| |
| 170 window_to_app_shelf_id_map_.erase(iter1); | 185 window_to_app_shelf_id_map_.erase(app_shelf_id_iter); |
| 171 window->RemoveObserver(this); | 186 window->RemoveObserver(this); |
| 172 | 187 |
| 173 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 188 AppControllerMap::iterator app_controller_iter = |
| 174 DCHECK(iter2 != app_controller_map_.end()); | 189 app_controller_map_.find(window_key); |
| 175 ExtensionAppWindowLauncherItemController* controller = iter2->second; | 190 DCHECK(app_controller_iter != app_controller_map_.end()); |
| 191 ExtensionAppWindowLauncherItemController* controller; | |
| 192 controller = app_controller_iter->second; | |
| 193 | |
| 176 controller->RemoveWindow(controller->GetAppWindow(window)); | 194 controller->RemoveWindow(controller->GetAppWindow(window)); |
| 177 if (controller->window_count() == 0) { | 195 if (controller->window_count() == 0) { |
| 178 // If this is the last window associated with the app shelf id, close the | 196 // If this is the last window associated with the app window shelf id, |
| 179 // shelf item. | 197 // close the shelf item. |
| 180 ash::ShelfID shelf_id = controller->shelf_id(); | 198 ash::ShelfID shelf_id = controller->shelf_id(); |
| 199 if (!owner()->IsPinned(shelf_id)) { | |
| 200 window_key_to_shelf_id_map_.erase(window_key); | |
| 201 } | |
| 181 owner()->CloseLauncherItem(shelf_id); | 202 owner()->CloseLauncherItem(shelf_id); |
| 182 app_controller_map_.erase(iter2); | 203 app_controller_map_.erase(app_controller_iter); |
| 183 } | 204 } |
| 184 } | 205 } |
| 185 | 206 |
| 186 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 207 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
| 187 aura::Window* window) { | 208 aura::Window* window) { |
| 188 return window_to_app_shelf_id_map_.find(window) != | 209 return window_to_app_shelf_id_map_.find(window) != |
| 189 window_to_app_shelf_id_map_.end(); | 210 window_to_app_shelf_id_map_.end(); |
| 190 } | 211 } |
| 191 | 212 |
| 192 // Private Methods | 213 // Private Methods |
| 193 | 214 |
| 194 AppWindowLauncherItemController* | 215 AppWindowLauncherItemController* |
| 195 ExtensionAppWindowLauncherController::ControllerForWindow( | 216 ExtensionAppWindowLauncherController::ControllerForWindow( |
| 196 aura::Window* window) { | 217 aura::Window* window) { |
| 197 WindowToAppShelfIdMap::iterator iter1 = | 218 WindowToAppShelfIdMap::iterator app_shelf_id_iter = |
| 198 window_to_app_shelf_id_map_.find(window); | 219 window_to_app_shelf_id_map_.find(window); |
| 199 if (iter1 == window_to_app_shelf_id_map_.end()) | 220 if (app_shelf_id_iter == window_to_app_shelf_id_map_.end()) |
| 200 return nullptr; | 221 return nullptr; |
| 201 std::string app_shelf_id = iter1->second; | 222 AppControllerMap::iterator app_controller_iter = |
| 202 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 223 app_controller_map_.find(app_shelf_id_iter->second); |
| 203 if (iter2 == app_controller_map_.end()) | 224 if (app_controller_iter != app_controller_map_.end()) |
| 204 return nullptr; | 225 return app_controller_iter->second; |
| 205 return iter2->second; | 226 return nullptr; |
|
Reilly Grant (use Gerrit)
2016/07/06 19:53:00
For symmetry with the check above I would prefer:
Andra Paraschiv
2016/07/08 07:20:26
Done.
| |
| 206 } | 227 } |
| OLD | NEW |