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" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 LauncherItemController::Type type = | 145 LauncherItemController::Type type = |
146 app_window->window_type_is_panel() | 146 app_window->window_type_is_panel() |
147 ? LauncherItemController::TYPE_APP_PANEL | 147 ? LauncherItemController::TYPE_APP_PANEL |
148 : LauncherItemController::TYPE_APP; | 148 : LauncherItemController::TYPE_APP; |
149 ExtensionAppWindowLauncherItemController* controller = | 149 ExtensionAppWindowLauncherItemController* controller = |
150 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 150 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, |
151 owner()); | 151 owner()); |
152 controller->AddAppWindow(app_window); | 152 controller->AddAppWindow(app_window); |
153 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), | 153 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), |
154 // use that shelf item. | 154 // use that shelf item. |
155 AppShelfIdToShelfIdMap::iterator app_shelf_id_iter = | 155 shelf_id = ash::WmShell::Get()->shelf_delegate()->GetShelfIDForAppID( |
156 app_shelf_id_to_shelf_id_map_.find(app_shelf_id); | 156 app_id, app_shelf_id); |
157 if (app_shelf_id_iter != app_shelf_id_to_shelf_id_map_.end()) { | |
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 | 157 |
181 if (shelf_id == 0) { | 158 if (shelf_id == 0) { |
182 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 159 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
183 // Restore any existing app icon and flag as set. | 160 // Restore any existing app icon and flag as set. |
184 const gfx::Image& app_icon = app_window->app_icon(); | 161 const gfx::Image& app_icon = app_window->app_icon(); |
185 if (!app_icon.IsEmpty()) { | 162 if (!app_icon.IsEmpty()) { |
186 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 163 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
187 controller->set_image_set_by_controller(true); | 164 controller->set_image_set_by_controller(true); |
188 } | 165 } |
189 } else { | 166 } else { |
190 owner()->SetItemController(shelf_id, controller); | 167 owner()->SetItemController(shelf_id, controller); |
191 } | 168 } |
192 | 169 |
193 // We need to change the controller associated with app_shelf_id. | 170 // We need to change the controller associated with app_shelf_id. |
194 app_controller_map_[app_shelf_id] = controller; | 171 app_controller_map_[app_shelf_id] = controller; |
195 app_shelf_id_to_shelf_id_map_[app_shelf_id] = shelf_id; | |
196 } | 172 } |
197 owner()->SetItemStatus(shelf_id, status); | 173 owner()->SetItemStatus(shelf_id, status); |
198 ash::SetShelfIDForWindow(shelf_id, window); | 174 ash::SetShelfIDForWindow(shelf_id, window); |
199 } | 175 } |
200 | 176 |
201 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 177 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
202 WindowToAppShelfIdMap::iterator window_iter = | 178 WindowToAppShelfIdMap::iterator window_iter = |
203 window_to_app_shelf_id_map_.find(window); | 179 window_to_app_shelf_id_map_.find(window); |
204 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); | 180 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); |
205 std::string app_shelf_id = window_iter->second; | 181 std::string app_shelf_id = window_iter->second; |
206 window_to_app_shelf_id_map_.erase(window_iter); | 182 window_to_app_shelf_id_map_.erase(window_iter); |
207 window->RemoveObserver(this); | 183 window->RemoveObserver(this); |
208 | 184 |
209 AppControllerMap::iterator app_controller_iter = | 185 AppControllerMap::iterator app_controller_iter = |
210 app_controller_map_.find(app_shelf_id); | 186 app_controller_map_.find(app_shelf_id); |
211 DCHECK(app_controller_iter != app_controller_map_.end()); | 187 DCHECK(app_controller_iter != app_controller_map_.end()); |
212 ExtensionAppWindowLauncherItemController* controller; | 188 ExtensionAppWindowLauncherItemController* controller; |
213 controller = app_controller_iter->second; | 189 controller = app_controller_iter->second; |
214 | 190 |
215 controller->RemoveWindow(controller->GetAppWindow(window)); | 191 controller->RemoveWindow(controller->GetAppWindow(window)); |
216 if (controller->window_count() == 0) { | 192 if (controller->window_count() == 0) { |
217 // If this is the last window associated with the app window shelf id, | 193 // If this is the last window associated with the app window shelf id, |
218 // close the shelf item. | 194 // close the shelf item. |
219 ash::ShelfID shelf_id = controller->shelf_id(); | 195 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); | 196 owner()->CloseLauncherItem(shelf_id); |
224 app_controller_map_.erase(app_controller_iter); | 197 app_controller_map_.erase(app_controller_iter); |
225 } | 198 } |
226 } | 199 } |
227 | 200 |
228 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 201 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
229 aura::Window* window) { | 202 aura::Window* window) { |
230 return window_to_app_shelf_id_map_.find(window) != | 203 return window_to_app_shelf_id_map_.find(window) != |
231 window_to_app_shelf_id_map_.end(); | 204 window_to_app_shelf_id_map_.end(); |
232 } | 205 } |
233 | 206 |
234 // Private Methods | 207 // Private Methods |
235 | 208 |
236 AppWindowLauncherItemController* | 209 AppWindowLauncherItemController* |
237 ExtensionAppWindowLauncherController::ControllerForWindow( | 210 ExtensionAppWindowLauncherController::ControllerForWindow( |
238 aura::Window* window) { | 211 aura::Window* window) { |
239 WindowToAppShelfIdMap::iterator window_iter = | 212 WindowToAppShelfIdMap::iterator window_iter = |
240 window_to_app_shelf_id_map_.find(window); | 213 window_to_app_shelf_id_map_.find(window); |
241 if (window_iter == window_to_app_shelf_id_map_.end()) | 214 if (window_iter == window_to_app_shelf_id_map_.end()) |
242 return nullptr; | 215 return nullptr; |
243 AppControllerMap::iterator app_controller_iter = | 216 AppControllerMap::iterator app_controller_iter = |
244 app_controller_map_.find(window_iter->second); | 217 app_controller_map_.find(window_iter->second); |
245 if (app_controller_iter == app_controller_map_.end()) | 218 if (app_controller_iter == app_controller_map_.end()) |
246 return nullptr; | 219 return nullptr; |
247 return app_controller_iter->second; | 220 return app_controller_iter->second; |
248 } | 221 } |
OLD | NEW |