Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: chrome/browser/ui/ash/launcher/extension_app_window_launcher_controller.cc

Issue 1914993002: Enhance chrome.app.window API with better shelf integration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix LaunchPinned/UnpinRunning v9 Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 default value to extension_id. If showInShelf parameter
28 return base::StringPrintf("panel:%d", app_window->session_id().id()); 29 // is true or the window type is panel and the window key is not empty, its
29 return app_window->extension_id(); 30 // value is appended to the app_shelf_id. Otherwise, if the window key is
31 // empty, the session_id is used.
32 std::string app_shelf_id = app_window->extension_id();
33 if (app_window->show_in_shelf() || app_window->window_type_is_panel()) {
34 if (!app_window->window_key().empty())
35 app_shelf_id += app_window->window_key();
36 else
37 app_shelf_id += base::StringPrintf("%d", app_window->session_id().id());
38 }
39 return app_shelf_id;
30 } 40 }
31 41
32 } // namespace 42 } // namespace
33 43
34 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController( 44 ExtensionAppWindowLauncherController::ExtensionAppWindowLauncherController(
35 ChromeLauncherController* owner) 45 ChromeLauncherController* owner)
36 : AppWindowLauncherController(owner) { 46 : AppWindowLauncherController(owner) {
37 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile()); 47 AppWindowRegistry* registry = AppWindowRegistry::Get(owner->GetProfile());
38 registry_.insert(registry); 48 registry_.insert(registry);
39 registry->AddObserver(this); 49 registry->AddObserver(this);
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 window_to_app_shelf_id_map_.end()); 124 window_to_app_shelf_id_map_.end());
115 const std::string app_shelf_id = GetAppShelfId(app_window); 125 const std::string app_shelf_id = GetAppShelfId(app_window);
116 window_to_app_shelf_id_map_[window] = app_shelf_id; 126 window_to_app_shelf_id_map_[window] = app_shelf_id;
117 window->AddObserver(this); 127 window->AddObserver(this);
118 128
119 // Find or create an item controller and launcher item. 129 // Find or create an item controller and launcher item.
120 std::string app_id = app_window->extension_id(); 130 std::string app_id = app_window->extension_id();
121 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window) 131 ash::ShelfItemStatus status = ash::wm::IsActiveWindow(window)
122 ? ash::STATUS_ACTIVE 132 ? ash::STATUS_ACTIVE
123 : ash::STATUS_RUNNING; 133 : ash::STATUS_RUNNING;
124 AppControllerMap::iterator iter = app_controller_map_.find(app_shelf_id); 134 AppControllerMap::iterator app_controller_iter =
135 app_controller_map_.find(app_shelf_id);
125 ash::ShelfID shelf_id = 0; 136 ash::ShelfID shelf_id = 0;
126 if (iter != app_controller_map_.end()) { 137
127 ExtensionAppWindowLauncherItemController* controller = iter->second; 138 if (app_controller_iter != app_controller_map_.end()) {
139 ExtensionAppWindowLauncherItemController* controller =
140 app_controller_iter->second;
128 DCHECK(controller->app_id() == app_id); 141 DCHECK(controller->app_id() == app_id);
129 shelf_id = controller->shelf_id(); 142 shelf_id = controller->shelf_id();
130 controller->AddAppWindow(app_window); 143 controller->AddAppWindow(app_window);
131 } else { 144 } else {
132 LauncherItemController::Type type = 145 LauncherItemController::Type type =
133 app_window->window_type_is_panel() 146 app_window->window_type_is_panel()
134 ? LauncherItemController::TYPE_APP_PANEL 147 ? LauncherItemController::TYPE_APP_PANEL
135 : LauncherItemController::TYPE_APP; 148 : LauncherItemController::TYPE_APP;
136 ExtensionAppWindowLauncherItemController* controller = 149 ExtensionAppWindowLauncherItemController* controller =
137 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id, 150 new ExtensionAppWindowLauncherItemController(type, app_shelf_id, app_id,
138 owner()); 151 owner());
139 controller->AddAppWindow(app_window); 152 controller->AddAppWindow(app_window);
140 // If the app shelf id is not unique, and there is already a shelf 153 // 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. 154 // use that shelf item.
142 if (app_shelf_id == app_id) { 155 AppShelfIdToShelfIdMap::iterator app_shelf_id_iter =
156 app_shelf_id_to_shelf_id_map_.find(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) {
stevenjb 2016/07/15 16:58:31 Add a comment: // show_in_shelf is false and not a
Valentin Ilie 2016/07/18 11:05:20 Done.
143 shelf_id = 164 shelf_id =
144 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID( 165 ash::Shell::GetInstance()->GetShelfDelegate()->GetShelfIDForAppID(
145 app_id); 166 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 for (AppShelfIdToShelfIdMap::const_iterator i =
172 app_shelf_id_to_shelf_id_map_.begin();
173 i != app_shelf_id_to_shelf_id_map_.end(); ++i) {
174 if (shelf_id == i->second) {
175 shelf_id = 0;
176 break;
177 }
178 }
stevenjb 2016/07/15 16:58:31 We should be able to do this with find + lambda, e
Valentin Ilie 2016/07/18 11:05:20 Done.
146 } 179 }
180
147 if (shelf_id == 0) { 181 if (shelf_id == 0) {
148 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status); 182 shelf_id = owner()->CreateAppLauncherItem(controller, app_id, status);
149 // Restore any existing app icon and flag as set. 183 // Restore any existing app icon and flag as set.
150 const gfx::Image& app_icon = app_window->app_icon(); 184 const gfx::Image& app_icon = app_window->app_icon();
151 if (!app_icon.IsEmpty()) { 185 if (!app_icon.IsEmpty()) {
152 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia()); 186 owner()->SetLauncherItemImage(shelf_id, app_icon.AsImageSkia());
153 controller->set_image_set_by_controller(true); 187 controller->set_image_set_by_controller(true);
154 } 188 }
155 } else { 189 } else {
156 owner()->SetItemController(shelf_id, controller); 190 owner()->SetItemController(shelf_id, controller);
157 } 191 }
158 const std::string app_shelf_id = GetAppShelfId(app_window); 192
193 // We need to change the controller associated with app_shelf_id.
159 app_controller_map_[app_shelf_id] = controller; 194 app_controller_map_[app_shelf_id] = controller;
195 app_shelf_id_to_shelf_id_map_[app_shelf_id] = shelf_id;
160 } 196 }
161 owner()->SetItemStatus(shelf_id, status); 197 owner()->SetItemStatus(shelf_id, status);
162 ash::SetShelfIDForWindow(shelf_id, window); 198 ash::SetShelfIDForWindow(shelf_id, window);
163 } 199 }
164 200
165 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) { 201 void ExtensionAppWindowLauncherController::UnregisterApp(aura::Window* window) {
166 WindowToAppShelfIdMap::iterator iter1 = 202 WindowToAppShelfIdMap::iterator window_iter =
167 window_to_app_shelf_id_map_.find(window); 203 window_to_app_shelf_id_map_.find(window);
168 DCHECK(iter1 != window_to_app_shelf_id_map_.end()); 204 DCHECK(window_iter != window_to_app_shelf_id_map_.end());
169 std::string app_shelf_id = iter1->second; 205 std::string app_shelf_id = window_iter->second;
170 window_to_app_shelf_id_map_.erase(iter1); 206 window_to_app_shelf_id_map_.erase(window_iter);
171 window->RemoveObserver(this); 207 window->RemoveObserver(this);
172 208
173 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); 209 AppControllerMap::iterator app_controller_iter =
174 DCHECK(iter2 != app_controller_map_.end()); 210 app_controller_map_.find(app_shelf_id);
175 ExtensionAppWindowLauncherItemController* controller = iter2->second; 211 DCHECK(app_controller_iter != app_controller_map_.end());
212 ExtensionAppWindowLauncherItemController* controller;
213 controller = app_controller_iter->second;
214
176 controller->RemoveWindow(controller->GetAppWindow(window)); 215 controller->RemoveWindow(controller->GetAppWindow(window));
177 if (controller->window_count() == 0) { 216 if (controller->window_count() == 0) {
178 // If this is the last window associated with the app shelf id, close the 217 // If this is the last window associated with the app window shelf id,
179 // shelf item. 218 // close the shelf item.
180 ash::ShelfID shelf_id = controller->shelf_id(); 219 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 }
181 owner()->CloseLauncherItem(shelf_id); 223 owner()->CloseLauncherItem(shelf_id);
182 app_controller_map_.erase(iter2); 224 app_controller_map_.erase(app_controller_iter);
183 } 225 }
184 } 226 }
185 227
186 bool ExtensionAppWindowLauncherController::IsRegisteredApp( 228 bool ExtensionAppWindowLauncherController::IsRegisteredApp(
187 aura::Window* window) { 229 aura::Window* window) {
188 return window_to_app_shelf_id_map_.find(window) != 230 return window_to_app_shelf_id_map_.find(window) !=
189 window_to_app_shelf_id_map_.end(); 231 window_to_app_shelf_id_map_.end();
190 } 232 }
191 233
192 // Private Methods 234 // Private Methods
193 235
194 AppWindowLauncherItemController* 236 AppWindowLauncherItemController*
195 ExtensionAppWindowLauncherController::ControllerForWindow( 237 ExtensionAppWindowLauncherController::ControllerForWindow(
196 aura::Window* window) { 238 aura::Window* window) {
197 WindowToAppShelfIdMap::iterator iter1 = 239 WindowToAppShelfIdMap::iterator window_iter =
198 window_to_app_shelf_id_map_.find(window); 240 window_to_app_shelf_id_map_.find(window);
199 if (iter1 == window_to_app_shelf_id_map_.end()) 241 if (window_iter == window_to_app_shelf_id_map_.end())
200 return nullptr; 242 return nullptr;
201 std::string app_shelf_id = iter1->second; 243 AppControllerMap::iterator app_controller_iter =
202 AppControllerMap::iterator iter2 = app_controller_map_.find(app_shelf_id); 244 app_controller_map_.find(window_iter->second);
203 if (iter2 == app_controller_map_.end()) 245 if (app_controller_iter == app_controller_map_.end())
204 return nullptr; 246 return nullptr;
205 return iter2->second; 247 return app_controller_iter->second;
206 } 248 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698