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 |
21 using extensions::AppWindow; | 22 using extensions::AppWindow; |
22 using extensions::AppWindowRegistry; | 23 using extensions::AppWindowRegistry; |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 std::string GetAppShelfId(AppWindow* app_window) { | 27 std::string GetAppShelfId(AppWindow* app_window) { |
27 if (app_window->window_type_is_panel()) | 28 // Set app_shelf_id if showInShelf parameter is false. If showInShelf |
28 return base::StringPrintf("panel:%d", app_window->session_id().id()); | 29 // parameter is true the window key value is appended to the app_shelf_id. |
stevenjb
2016/07/11 19:45:13
Update comment.
Valentin Ilie
2016/07/12 08:01:53
Done.
| |
29 return app_window->extension_id(); | 30 std::string app_shelf_id = app_window->extension_id(); |
31 if (app_window->show_in_shelf() || app_window->window_type_is_panel()) { | |
32 if (!app_window->window_key().empty()) | |
33 app_shelf_id += app_window->window_key(); | |
34 else | |
35 app_shelf_id += base::StringPrintf("%d", app_window->session_id().id()); | |
36 } | |
37 return app_shelf_id; | |
30 } | 38 } |
31 | 39 |
32 } // namespace | 40 } // namespace |
33 | 41 |
34 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( | 42 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( |
35 ChromeLauncherController* owner) | 43 ChromeLauncherController* owner) |
36 : AppWindowLauncherController(owner) { | 44 : AppWindowLauncherController(owner) { |
37 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); | 45 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); |
38 registry_.insert(registry); | 46 registry_.insert(registry); |
39 registry->AddObserver(this); | 47 registry->AddObserver(this); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 window_to_app_shelf_id_map_.end()); | 122 window_to_app_shelf_id_map_.end()); |
115 const std::string app_shelf_id = GetAppShelfId(app_window); | 123 const std::string app_shelf_id = GetAppShelfId(app_window); |
116 window_to_app_shelf_id_map_[window] = app_shelf_id; | 124 window_to_app_shelf_id_map_[window] = app_shelf_id; |
117 window->AddObserver(this); | 125 window->AddObserver(this); |
118 | 126 |
119 // Find or create an item controller and launcher item. | 127 // Find or create an item controller and launcher item. |
120 std::string app_id = app_window->extension_id(); | 128 std::string app_id = app_window->extension_id(); |
121 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) | 129 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) |
122 ? ash::STATUS_ACTIVE | 130 ? ash::STATUS_ACTIVE |
123 : ash::STATUS_RUNNING; | 131 : ash::STATUS_RUNNING; |
124 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); | 132 AppControllerMap::iterator app_controller_iter = |
133 app_controller_map_.find(app_shelf_id); | |
125 ash::ShelfID shelf_id = 0; | 134 ash::ShelfID shelf_id = 0; |
126 if (iter != app_controller_map_.end()) { | 135 |
127 ExtensionAppWindowLauncherItemController* controller = iter->second; | 136 if (app_controller_iter != app_controller_map_.end()) { |
137 ExtensionAppWindowLauncherItemController* controller = | |
138 app_controller_iter->second; | |
128 DCHECK(controller->app_id() == app_id); | 139 DCHECK(controller->app_id() == app_id); |
129 shelf_id = controller->shelf_id(); | 140 shelf_id = controller->shelf_id(); |
130 controller->AddAppWindow(app_window); | 141 controller->AddAppWindow(app_window); |
131 } else { | 142 } else { |
132 LauncherItemController::Type type = | 143 LauncherItemController::Type type = |
133 app_window->window_type_is_panel() | 144 app_window->window_type_is_panel() |
134 ? LauncherItemController::TYPE_APP_PANEL | 145 ? LauncherItemController::TYPE_APP_PANEL |
135 : LauncherItemController::TYPE_APP; | 146 : LauncherItemController::TYPE_APP; |
136 ExtensionAppWindowLauncherItemController* controller = | 147 ExtensionAppWindowLauncherItemController* controller = |
137 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, | 148 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, |
138 owner()); | 149 owner()); |
139 controller->AddAppWindow(app_window); | 150 controller->AddAppWindow(app_window); |
140 // If the app shelf id is not unique, and there is already a shelf | 151 // If there is already a shelf id mapped to this app_shelf_id (e.g. pinned), |
141 // item for this app id (e.g. pinned), use that shelf item. | 152 // use that shelf item. |
142 if (app_shelf_id == app_id) { | 153 AppShelfIdToShelfIdMap::iterator app_shelf_id_iter = |
143 shelf_id = | 154 app_shelf_id_to_shelf_id_map_.find(app_shelf_id); |
144 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID( | 155 if (app_shelf_id_iter != app_shelf_id_to_shelf_id_map_.end()) { |
145 app_id); | 156 if (owner()->IsPinned(app_shelf_id_iter->second)) { |
157 shelf_id = app_shelf_id_iter->second; | |
158 } else { | |
159 app_shelf_id_to_shelf_id_map_.erase(app_shelf_id); | |
160 } | |
146 } | 161 } |
162 | |
147 if (shelf_id == 0) { | 163 if (shelf_id == 0) { |
148 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); | 164 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); |
149 // Restore any existing app icon and flag as set. | 165 // Restore any existing app icon and flag as set. |
150 const gfx::Image& app_icon = app_window->app_icon(); | 166 const gfx::Image& app_icon = app_window->app_icon(); |
151 if (!app_icon.IsEmpty()) { | 167 if (!app_icon.IsEmpty()) { |
152 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); | 168 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); |
153 controller->set_image_set_by_controller(true); | 169 controller->set_image_set_by_controller(true); |
154 } | 170 } |
155 } else { | 171 } else { |
156 owner()->SetItemController(shelf_id, controller); | 172 owner()->SetItemController(shelf_id, controller); |
157 } | 173 } |
158 const std::string app_shelf_id = GetAppShelfId(app_window); | 174 |
175 // We need to change the controller associated with app_shelf_id. | |
159 app_controller_map_[app_shelf_id] = controller; | 176 app_controller_map_[app_shelf_id] = controller; |
177 app_shelf_id_to_shelf_id_map_[app_shelf_id] = shelf_id; | |
160 } | 178 } |
161 owner()->SetItemStatus(shelf_id, status); | 179 owner()->SetItemStatus(shelf_id, status); |
162 ash::SetShelfIDForWindow(shelf_id, window); | 180 ash::SetShelfIDForWindow(shelf_id, window); |
163 } | 181 } |
164 | 182 |
165 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { | 183 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { |
166 WindowToAppShelfIdMap::iterator iter1 = | 184 WindowToAppShelfIdMap::iterator window_iter = |
167 window_to_app_shelf_id_map_.find(window); | 185 window_to_app_shelf_id_map_.find(window); |
168 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); | 186 DCHECK(window_iter != window_to_app_shelf_id_map_.end()); |
169 std::string app_shelf_id = iter1->second; | 187 std::string app_shelf_id = window_iter->second; |
170 window_to_app_shelf_id_map_.erase(iter1); | 188 window_to_app_shelf_id_map_.erase(window_iter); |
171 window->RemoveObserver(this); | 189 window->RemoveObserver(this); |
172 | 190 |
173 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 191 AppControllerMap::iterator app_controller_iter = |
174 DCHECK(iter2 != app_controller_map_.end()); | 192 app_controller_map_.find(app_shelf_id); |
175 ExtensionAppWindowLauncherItemController* controller = iter2->second; | 193 DCHECK(app_controller_iter != app_controller_map_.end()); |
194 ExtensionAppWindowLauncherItemController* controller; | |
195 controller = app_controller_iter->second; | |
196 | |
176 controller->RemoveWindow(controller->GetAppWindow(window)); | 197 controller->RemoveWindow(controller->GetAppWindow(window)); |
177 if (controller->window_count() == 0) { | 198 if (controller->window_count() == 0) { |
178 // If this is the last window associated with the app shelf id, close the | 199 // If this is the last window associated with the app window shelf id, |
179 // shelf item. | 200 // close the shelf item. |
180 ash::ShelfID shelf_id = controller->shelf_id(); | 201 ash::ShelfID shelf_id = controller->shelf_id(); |
202 if (!owner()->IsPinned(shelf_id)) { | |
203 app_shelf_id_to_shelf_id_map_.erase(app_shelf_id); | |
204 } | |
181 owner()->CloseLauncherItem(shelf_id); | 205 owner()->CloseLauncherItem(shelf_id); |
182 app_controller_map_.erase(iter2); | 206 app_controller_map_.erase(app_controller_iter); |
183 } | 207 } |
184 } | 208 } |
185 | 209 |
186 bool ExtensionAppWindowLauncherController::IsRegisteredApp( | 210 bool ExtensionAppWindowLauncherController::IsRegisteredApp( |
187 aura::Window* window) { | 211 aura::Window* window) { |
188 return window_to_app_shelf_id_map_.find(window) != | 212 return window_to_app_shelf_id_map_.find(window) != |
189 window_to_app_shelf_id_map_.end(); | 213 window_to_app_shelf_id_map_.end(); |
190 } | 214 } |
191 | 215 |
192 // Private Methods | 216 // Private Methods |
193 | 217 |
194 AppWindowLauncherItemController* | 218 AppWindowLauncherItemController* |
195 ExtensionAppWindowLauncherController::ControllerForWindow( | 219 ExtensionAppWindowLauncherController::ControllerForWindow( |
196 aura::Window* window) { | 220 aura::Window* window) { |
197 WindowToAppShelfIdMap::iterator iter1 = | 221 WindowToAppShelfIdMap::iterator window_iter = |
198 window_to_app_shelf_id_map_.find(window); | 222 window_to_app_shelf_id_map_.find(window); |
199 if (iter1 == window_to_app_shelf_id_map_.end()) | 223 if (window_iter == window_to_app_shelf_id_map_.end()) |
200 return nullptr; | 224 return nullptr; |
201 std::string app_shelf_id = iter1->second; | 225 AppControllerMap::iterator app_controller_iter = |
202 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); | 226 app_controller_map_.find(window_iter->second); |
203 if (iter2 == app_controller_map_.end()) | 227 if (app_controller_iter == app_controller_map_.end()) |
204 return nullptr; | 228 return nullptr; |
205 return iter2->second; | 229 return app_controller_iter->second; |
206 } | 230 } |
OLD | NEW |