| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/browser_shortcut_launcher_item_controll
er.h" | 5 #include "chrome/browser/ui/ash/launcher/browser_shortcut_launcher_item_controll
er.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "ash/common/shelf/shelf_delegate.h" | 10 #include "ash/common/shelf/shelf_delegate.h" |
| 11 #include "ash/common/shelf/shelf_model.h" | 11 #include "ash/common/shelf/shelf_model.h" |
| 12 #include "ash/common/wm_window.h" | 12 #include "ash/common/wm_window.h" |
| 13 #include "ash/resources/grit/ash_resources.h" | 13 #include "ash/resources/grit/ash_resources.h" |
| 14 #include "ash/shell.h" | 14 #include "ash/shell.h" |
| 15 #include "ash/wm/window_properties.h" | 15 #include "ash/wm/window_properties.h" |
| 16 #include "ash/wm/window_util.h" | 16 #include "ash/wm/window_util.h" |
| 17 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 19 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| 21 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_util.h" | 20 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_util.h" |
| 22 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" | 21 #include "chrome/browser/ui/ash/launcher/launcher_context_menu.h" |
| 23 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 22 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
| 24 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 25 #include "chrome/browser/ui/browser_commands.h" | 24 #include "chrome/browser/ui/browser_commands.h" |
| 26 #include "chrome/browser/ui/browser_finder.h" | 25 #include "chrome/browser/ui/browser_finder.h" |
| 27 #include "chrome/browser/ui/browser_window.h" | 26 #include "chrome/browser/ui/browser_window.h" |
| 28 #include "chrome/browser/ui/chrome_pages.h" | 27 #include "chrome/browser/ui/chrome_pages.h" |
| 29 #include "chrome/browser/ui/settings_window_manager.h" | 28 #include "chrome/browser/ui/settings_window_manager.h" |
| 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 31 #include "chrome/browser/web_applications/web_app.h" | 30 #include "chrome/browser/web_applications/web_app.h" |
| 32 #include "chrome/common/extensions/extension_constants.h" | 31 #include "chrome/common/extensions/extension_constants.h" |
| 33 #include "chrome/grit/chromium_strings.h" | 32 #include "chrome/grit/chromium_strings.h" |
| 34 #include "chrome/grit/generated_resources.h" | 33 #include "chrome/grit/generated_resources.h" |
| 35 #include "components/strings/grit/components_strings.h" | 34 #include "components/strings/grit/components_strings.h" |
| 36 #include "content/public/browser/notification_service.h" | |
| 37 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 38 #include "content/public/common/url_constants.h" | 36 #include "content/public/common/url_constants.h" |
| 39 #include "ui/aura/window.h" | 37 #include "ui/aura/window.h" |
| 40 #include "ui/base/l10n/l10n_util.h" | 38 #include "ui/base/l10n/l10n_util.h" |
| 41 #include "ui/base/resource/resource_bundle.h" | 39 #include "ui/base/resource/resource_bundle.h" |
| 42 #include "ui/events/event.h" | 40 #include "ui/events/event.h" |
| 43 #include "ui/events/event_constants.h" | 41 #include "ui/events/event_constants.h" |
| 44 #include "ui/gfx/image/image.h" | 42 #include "ui/gfx/image/image.h" |
| 45 #include "ui/wm/core/window_animations.h" | 43 #include "ui/wm/core/window_animations.h" |
| 46 | 44 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 return title.empty() ? l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE) : title; | 101 return title.empty() ? l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE) : title; |
| 104 } | 102 } |
| 105 | 103 |
| 106 } // namespace | 104 } // namespace |
| 107 | 105 |
| 108 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( | 106 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( |
| 109 ChromeLauncherController* launcher_controller, | 107 ChromeLauncherController* launcher_controller, |
| 110 ash::ShelfModel* shelf_model) | 108 ash::ShelfModel* shelf_model) |
| 111 : LauncherItemController(ash::AppLaunchId(extension_misc::kChromeAppId), | 109 : LauncherItemController(ash::AppLaunchId(extension_misc::kChromeAppId), |
| 112 launcher_controller), | 110 launcher_controller), |
| 113 shelf_model_(shelf_model) { | 111 shelf_model_(shelf_model), |
| 112 browser_list_observer_(this) { |
| 114 // Tag all open browser windows with the appropriate shelf id property. This | 113 // Tag all open browser windows with the appropriate shelf id property. This |
| 115 // associates each window with the shelf item for the active web contents. | 114 // associates each window with the shelf item for the active web contents. |
| 116 for (auto* browser : *BrowserList::GetInstance()) { | 115 for (auto* browser : *BrowserList::GetInstance()) { |
| 117 if (IsBrowserRepresentedInBrowserList(browser) && | 116 if (IsBrowserRepresentedInBrowserList(browser) && |
| 118 browser->tab_strip_model()->GetActiveWebContents()) { | 117 browser->tab_strip_model()->GetActiveWebContents()) { |
| 119 SetShelfIDForBrowserWindowContents( | 118 SetShelfIDForBrowserWindowContents( |
| 120 browser, browser->tab_strip_model()->GetActiveWebContents()); | 119 browser, browser->tab_strip_model()->GetActiveWebContents()); |
| 121 } | 120 } |
| 122 } | 121 } |
| 123 } | 122 } |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 | 211 |
| 213 ash::ShelfAction action = | 212 ash::ShelfAction action = |
| 214 launcher_controller()->ActivateWindowOrMinimizeIfActive( | 213 launcher_controller()->ActivateWindowOrMinimizeIfActive( |
| 215 last_browser->window(), items.size() == 1); | 214 last_browser->window(), items.size() == 1); |
| 216 callback.Run(action, std::move(items)); | 215 callback.Run(action, std::move(items)); |
| 217 } | 216 } |
| 218 | 217 |
| 219 MenuItemList BrowserShortcutLauncherItemController::GetAppMenuItems( | 218 MenuItemList BrowserShortcutLauncherItemController::GetAppMenuItems( |
| 220 int event_flags) { | 219 int event_flags) { |
| 221 browser_menu_items_.clear(); | 220 browser_menu_items_.clear(); |
| 222 registrar_.RemoveAll(); | 221 browser_list_observer_.RemoveAll(); |
| 223 | 222 |
| 224 MenuItemList items; | 223 MenuItemList items; |
| 225 bool found_tabbed_browser = false; | 224 bool found_tabbed_browser = false; |
| 226 for (auto* browser : GetListOfActiveBrowsers()) { | 225 for (auto* browser : GetListOfActiveBrowsers()) { |
| 227 if (browser_menu_items_.size() >= kMaxItems) | 226 if (browser_menu_items_.size() >= kMaxItems) |
| 228 break; | 227 break; |
| 229 TabStripModel* tab_strip = browser->tab_strip_model(); | 228 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 230 const int tab_index = tab_strip->active_index(); | 229 const int tab_index = tab_strip->active_index(); |
| 231 if (tab_index < 0 || tab_index >= kMaxItems) | 230 if (tab_index < 0 || tab_index >= kMaxItems) |
| 232 continue; | 231 continue; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 243 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { | 242 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { |
| 244 content::WebContents* tab = tab_strip->GetWebContentsAt(i); | 243 content::WebContents* tab = tab_strip->GetWebContentsAt(i); |
| 245 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); | 244 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 246 item->command_id = GetCommandId(browser_menu_items_.size(), i); | 245 item->command_id = GetCommandId(browser_menu_items_.size(), i); |
| 247 item->label = launcher_controller()->GetAppListTitle(tab); | 246 item->label = launcher_controller()->GetAppListTitle(tab); |
| 248 item->image = *launcher_controller()->GetAppListIcon(tab).ToSkBitmap(); | 247 item->image = *launcher_controller()->GetAppListIcon(tab).ToSkBitmap(); |
| 249 items.push_back(std::move(item)); | 248 items.push_back(std::move(item)); |
| 250 } | 249 } |
| 251 } | 250 } |
| 252 browser_menu_items_.push_back(browser); | 251 browser_menu_items_.push_back(browser); |
| 253 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 252 if (!browser_list_observer_.IsObservingSources()) |
| 254 content::Source<Browser>(browser)); | 253 browser_list_observer_.Add(BrowserList::GetInstance()); |
| 255 } | 254 } |
| 256 // If only windowed applications are open, we return an empty list to | 255 // If only windowed applications are open, we return an empty list to |
| 257 // enforce the creation of a new browser. | 256 // enforce the creation of a new browser. |
| 258 if (!found_tabbed_browser) { | 257 if (!found_tabbed_browser) { |
| 259 items.clear(); | 258 items.clear(); |
| 260 browser_menu_items_.clear(); | 259 browser_menu_items_.clear(); |
| 261 registrar_.RemoveAll(); | 260 browser_list_observer_.RemoveAll(); |
| 262 } | 261 } |
| 263 return items; | 262 return items; |
| 264 } | 263 } |
| 265 | 264 |
| 266 void BrowserShortcutLauncherItemController::ExecuteCommand( | 265 void BrowserShortcutLauncherItemController::ExecuteCommand( |
| 267 uint32_t command_id, | 266 uint32_t command_id, |
| 268 int32_t event_flags) { | 267 int32_t event_flags) { |
| 269 const uint16_t browser_index = GetBrowserIndex(command_id); | 268 const uint16_t browser_index = GetBrowserIndex(command_id); |
| 270 // Check that the index is valid and the browser has not been closed. | 269 // Check that the index is valid and the browser has not been closed. |
| 271 if (browser_index < browser_menu_items_.size() && | 270 if (browser_index < browser_menu_items_.size() && |
| (...skipping 12 matching lines...) Expand all Loading... |
| 284 multi_user_util::MoveWindowToCurrentDesktop( | 283 multi_user_util::MoveWindowToCurrentDesktop( |
| 285 browser->window()->GetNativeWindow()); | 284 browser->window()->GetNativeWindow()); |
| 286 if (tab_index != kNoTab && tab_strip->ContainsIndex(tab_index)) | 285 if (tab_index != kNoTab && tab_strip->ContainsIndex(tab_index)) |
| 287 tab_strip->ActivateTabAt(tab_index, false); | 286 tab_strip->ActivateTabAt(tab_index, false); |
| 288 browser->window()->Show(); | 287 browser->window()->Show(); |
| 289 browser->window()->Activate(); | 288 browser->window()->Activate(); |
| 290 } | 289 } |
| 291 } | 290 } |
| 292 | 291 |
| 293 browser_menu_items_.clear(); | 292 browser_menu_items_.clear(); |
| 294 registrar_.RemoveAll(); | 293 browser_list_observer_.RemoveAll(); |
| 295 } | 294 } |
| 296 | 295 |
| 297 void BrowserShortcutLauncherItemController::Close() { | 296 void BrowserShortcutLauncherItemController::Close() { |
| 298 for (auto* browser : GetListOfActiveBrowsers()) | 297 for (auto* browser : GetListOfActiveBrowsers()) |
| 299 browser->window()->Close(); | 298 browser->window()->Close(); |
| 300 } | 299 } |
| 301 | 300 |
| 302 bool BrowserShortcutLauncherItemController::IsListOfActiveBrowserEmpty() { | 301 bool BrowserShortcutLauncherItemController::IsListOfActiveBrowserEmpty() { |
| 303 return GetListOfActiveBrowsers().empty(); | 302 return GetListOfActiveBrowsers().empty(); |
| 304 } | 303 } |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 386 continue; | 385 continue; |
| 387 } | 386 } |
| 388 if (!IsBrowserRepresentedInBrowserList(browser) && | 387 if (!IsBrowserRepresentedInBrowserList(browser) && |
| 389 !browser->is_type_tabbed()) | 388 !browser->is_type_tabbed()) |
| 390 continue; | 389 continue; |
| 391 active_browsers.push_back(browser); | 390 active_browsers.push_back(browser); |
| 392 } | 391 } |
| 393 return active_browsers; | 392 return active_browsers; |
| 394 } | 393 } |
| 395 | 394 |
| 396 void BrowserShortcutLauncherItemController::Observe( | 395 void BrowserShortcutLauncherItemController::OnBrowserCloseStarted( |
| 397 int type, | 396 Browser* browser) { |
| 398 const content::NotificationSource& source, | |
| 399 const content::NotificationDetails& details) { | |
| 400 DCHECK_EQ(chrome::NOTIFICATION_BROWSER_CLOSING, type); | |
| 401 Browser* browser = content::Source<Browser>(source).ptr(); | |
| 402 DCHECK(browser); | 397 DCHECK(browser); |
| 403 BrowserList::BrowserVector::iterator item = std::find( | 398 BrowserList::BrowserVector::iterator item = std::find( |
| 404 browser_menu_items_.begin(), browser_menu_items_.end(), browser); | 399 browser_menu_items_.begin(), browser_menu_items_.end(), browser); |
| 405 DCHECK(item != browser_menu_items_.end()); | |
| 406 // Clear the entry for the closed browser and leave other indices intact. | 400 // Clear the entry for the closed browser and leave other indices intact. |
| 407 *item = nullptr; | 401 if (item != browser_menu_items_.end()) |
| 408 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 402 *item = nullptr; |
| 409 content::Source<Browser>(browser)); | |
| 410 } | 403 } |
| OLD | NEW |