| 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" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 // Get the title for the browser list entry for |web_contents|. | 99 // Get the title for the browser list entry for |web_contents|. |
| 100 // If |web_contents| has not loaded, returns "New Tab". | 100 // If |web_contents| has not loaded, returns "New Tab". |
| 101 base::string16 GetBrowserListTitle(content::WebContents* web_contents) { | 101 base::string16 GetBrowserListTitle(content::WebContents* web_contents) { |
| 102 const base::string16& title = web_contents->GetTitle(); | 102 const base::string16& title = web_contents->GetTitle(); |
| 103 return title.empty() ? l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE) : title; | 103 return title.empty() ? l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE) : title; |
| 104 } | 104 } |
| 105 | 105 |
| 106 } // namespace | 106 } // namespace |
| 107 | 107 |
| 108 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( | 108 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( |
| 109 ChromeLauncherController* launcher_controller, | |
| 110 ash::ShelfModel* shelf_model) | 109 ash::ShelfModel* shelf_model) |
| 111 : LauncherItemController(ash::AppLaunchId(extension_misc::kChromeAppId), | 110 : ash::ShelfItemDelegate(ash::AppLaunchId(extension_misc::kChromeAppId)), |
| 112 launcher_controller), | |
| 113 shelf_model_(shelf_model) { | 111 shelf_model_(shelf_model) { |
| 114 // Tag all open browser windows with the appropriate shelf id property. This | 112 // 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. | 113 // associates each window with the shelf item for the active web contents. |
| 116 for (auto* browser : *BrowserList::GetInstance()) { | 114 for (auto* browser : *BrowserList::GetInstance()) { |
| 117 if (IsBrowserRepresentedInBrowserList(browser) && | 115 if (IsBrowserRepresentedInBrowserList(browser) && |
| 118 browser->tab_strip_model()->GetActiveWebContents()) { | 116 browser->tab_strip_model()->GetActiveWebContents()) { |
| 119 SetShelfIDForBrowserWindowContents( | 117 SetShelfIDForBrowserWindowContents( |
| 120 browser, browser->tab_strip_model()->GetActiveWebContents()); | 118 browser, browser->tab_strip_model()->GetActiveWebContents()); |
| 121 } | 119 } |
| 122 } | 120 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 139 // a windowed app, a popup or any other item which is not a browser of | 137 // a windowed app, a popup or any other item which is not a browser of |
| 140 // interest. | 138 // interest. |
| 141 Browser* browser = chrome::FindBrowserWithWindow(window); | 139 Browser* browser = chrome::FindBrowserWithWindow(window); |
| 142 if (IsBrowserRepresentedInBrowserList(browser)) { | 140 if (IsBrowserRepresentedInBrowserList(browser)) { |
| 143 browser_status = ash::STATUS_ACTIVE; | 141 browser_status = ash::STATUS_ACTIVE; |
| 144 // If an app that has item is running in active WebContents, browser item | 142 // If an app that has item is running in active WebContents, browser item |
| 145 // status cannot be active. | 143 // status cannot be active. |
| 146 content::WebContents* contents = | 144 content::WebContents* contents = |
| 147 browser->tab_strip_model()->GetActiveWebContents(); | 145 browser->tab_strip_model()->GetActiveWebContents(); |
| 148 if (contents && | 146 if (contents && |
| 149 (launcher_controller()->GetShelfIDForWebContents(contents) != | 147 (ChromeLauncherController::instance()->GetShelfIDForWebContents( |
| 150 browser_item.id)) | 148 contents) != browser_item.id)) |
| 151 browser_status = ash::STATUS_RUNNING; | 149 browser_status = ash::STATUS_RUNNING; |
| 152 } | 150 } |
| 153 } | 151 } |
| 154 | 152 |
| 155 if (browser_status == ash::STATUS_CLOSED) { | 153 if (browser_status == ash::STATUS_CLOSED) { |
| 156 for (auto* browser : *BrowserList::GetInstance()) { | 154 for (auto* browser : *BrowserList::GetInstance()) { |
| 157 if (IsBrowserRepresentedInBrowserList(browser)) { | 155 if (IsBrowserRepresentedInBrowserList(browser)) { |
| 158 browser_status = ash::STATUS_RUNNING; | 156 browser_status = ash::STATUS_RUNNING; |
| 159 break; | 157 break; |
| 160 } | 158 } |
| 161 } | 159 } |
| 162 } | 160 } |
| 163 | 161 |
| 164 if (browser_status != browser_item.status) { | 162 if (browser_status != browser_item.status) { |
| 165 browser_item.status = browser_status; | 163 browser_item.status = browser_status; |
| 166 shelf_model_->Set(browser_index, browser_item); | 164 shelf_model_->Set(browser_index, browser_item); |
| 167 } | 165 } |
| 168 } | 166 } |
| 169 | 167 |
| 170 void BrowserShortcutLauncherItemController::SetShelfIDForBrowserWindowContents( | 168 void BrowserShortcutLauncherItemController::SetShelfIDForBrowserWindowContents( |
| 171 Browser* browser, | 169 Browser* browser, |
| 172 content::WebContents* web_contents) { | 170 content::WebContents* web_contents) { |
| 173 // We need to set the window ShelfID for V1 applications since they are | 171 // We need to set the window ShelfID for V1 applications since they are |
| 174 // content which might change and as such change the application type. | 172 // content which might change and as such change the application type. |
| 175 if (!browser || !IsBrowserFromActiveUser(browser) || | 173 // The browser window may not exist in unit tests. |
| 176 IsSettingsBrowser(browser)) | 174 if (!browser || !browser->window() || !browser->window()->GetNativeWindow() || |
| 175 !IsBrowserFromActiveUser(browser) || IsSettingsBrowser(browser)) { |
| 177 return; | 176 return; |
| 177 } |
| 178 | 178 |
| 179 browser->window()->GetNativeWindow()->SetProperty( | 179 browser->window()->GetNativeWindow()->SetProperty( |
| 180 ash::kShelfIDKey, | 180 ash::kShelfIDKey, |
| 181 launcher_controller()->GetShelfIDForWebContents(web_contents)); | 181 ChromeLauncherController::instance()->GetShelfIDForWebContents( |
| 182 web_contents)); |
| 182 } | 183 } |
| 183 | 184 |
| 184 void BrowserShortcutLauncherItemController::ItemSelected( | 185 void BrowserShortcutLauncherItemController::ItemSelected( |
| 185 std::unique_ptr<ui::Event> event, | 186 std::unique_ptr<ui::Event> event, |
| 186 int64_t display_id, | 187 int64_t display_id, |
| 187 ash::ShelfLaunchSource source, | 188 ash::ShelfLaunchSource source, |
| 188 const ItemSelectedCallback& callback) { | 189 const ItemSelectedCallback& callback) { |
| 189 if (event && (event->flags() & ui::EF_CONTROL_DOWN)) { | 190 if (event && (event->flags() & ui::EF_CONTROL_DOWN)) { |
| 190 chrome::NewEmptyWindow(launcher_controller()->profile()); | 191 chrome::NewEmptyWindow(ChromeLauncherController::instance()->profile()); |
| 191 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); | 192 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); |
| 192 return; | 193 return; |
| 193 } | 194 } |
| 194 | 195 |
| 195 MenuItemList items = GetAppMenuItems(event ? event->flags() : ui::EF_NONE); | 196 ash::MenuItemList items = |
| 197 GetAppMenuItems(event ? event->flags() : ui::EF_NONE); |
| 196 | 198 |
| 197 // In case of a keyboard event, we were called by a hotkey. In that case we | 199 // In case of a keyboard event, we were called by a hotkey. In that case we |
| 198 // activate the next item in line if an item of our list is already active. | 200 // activate the next item in line if an item of our list is already active. |
| 199 if (event && event->type() == ui::ET_KEY_RELEASED) { | 201 if (event && event->type() == ui::ET_KEY_RELEASED) { |
| 200 callback.Run(ActivateOrAdvanceToNextBrowser(), std::move(items)); | 202 callback.Run(ActivateOrAdvanceToNextBrowser(), std::move(items)); |
| 201 return; | 203 return; |
| 202 } | 204 } |
| 203 | 205 |
| 204 Browser* last_browser = | 206 Profile* profile = ChromeLauncherController::instance()->profile(); |
| 205 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); | 207 Browser* last_browser = chrome::FindTabbedBrowser(profile, true); |
| 206 | 208 |
| 207 if (!last_browser) { | 209 if (!last_browser) { |
| 208 chrome::NewEmptyWindow(launcher_controller()->profile()); | 210 chrome::NewEmptyWindow(profile); |
| 209 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); | 211 callback.Run(ash::SHELF_ACTION_NEW_WINDOW_CREATED, base::nullopt); |
| 210 return; | 212 return; |
| 211 } | 213 } |
| 212 | 214 |
| 213 ash::ShelfAction action = | 215 ash::ShelfAction action = |
| 214 launcher_controller()->ActivateWindowOrMinimizeIfActive( | 216 ChromeLauncherController::instance()->ActivateWindowOrMinimizeIfActive( |
| 215 last_browser->window(), items.size() == 1); | 217 last_browser->window(), items.size() == 1); |
| 216 callback.Run(action, std::move(items)); | 218 callback.Run(action, std::move(items)); |
| 217 } | 219 } |
| 218 | 220 |
| 219 MenuItemList BrowserShortcutLauncherItemController::GetAppMenuItems( | 221 ash::MenuItemList BrowserShortcutLauncherItemController::GetAppMenuItems( |
| 220 int event_flags) { | 222 int event_flags) { |
| 221 browser_menu_items_.clear(); | 223 browser_menu_items_.clear(); |
| 222 registrar_.RemoveAll(); | 224 registrar_.RemoveAll(); |
| 223 | 225 |
| 224 MenuItemList items; | 226 ash::MenuItemList items; |
| 225 bool found_tabbed_browser = false; | 227 bool found_tabbed_browser = false; |
| 228 ChromeLauncherController* controller = ChromeLauncherController::instance(); |
| 226 for (auto* browser : GetListOfActiveBrowsers()) { | 229 for (auto* browser : GetListOfActiveBrowsers()) { |
| 227 if (browser_menu_items_.size() >= kMaxItems) | 230 if (browser_menu_items_.size() >= kMaxItems) |
| 228 break; | 231 break; |
| 229 TabStripModel* tab_strip = browser->tab_strip_model(); | 232 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 230 const int tab_index = tab_strip->active_index(); | 233 const int tab_index = tab_strip->active_index(); |
| 231 if (tab_index < 0 || tab_index >= kMaxItems) | 234 if (tab_index < 0 || tab_index >= kMaxItems) |
| 232 continue; | 235 continue; |
| 233 if (browser->is_type_tabbed()) | 236 if (browser->is_type_tabbed()) |
| 234 found_tabbed_browser = true; | 237 found_tabbed_browser = true; |
| 235 if (!(event_flags & ui::EF_SHIFT_DOWN)) { | 238 if (!(event_flags & ui::EF_SHIFT_DOWN)) { |
| 236 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); | 239 content::WebContents* tab = tab_strip->GetWebContentsAt(tab_index); |
| 237 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); | 240 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 238 item->command_id = GetCommandId(browser_menu_items_.size(), kNoTab); | 241 item->command_id = GetCommandId(browser_menu_items_.size(), kNoTab); |
| 239 item->label = GetBrowserListTitle(tab); | 242 item->label = GetBrowserListTitle(tab); |
| 240 item->image = *GetBrowserListIcon(tab).ToSkBitmap(); | 243 item->image = *GetBrowserListIcon(tab).ToSkBitmap(); |
| 241 items.push_back(std::move(item)); | 244 items.push_back(std::move(item)); |
| 242 } else { | 245 } else { |
| 243 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { | 246 for (uint16_t i = 0; i < tab_strip->count() && i < kMaxItems; ++i) { |
| 244 content::WebContents* tab = tab_strip->GetWebContentsAt(i); | 247 content::WebContents* tab = tab_strip->GetWebContentsAt(i); |
| 245 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); | 248 ash::mojom::MenuItemPtr item(ash::mojom::MenuItem::New()); |
| 246 item->command_id = GetCommandId(browser_menu_items_.size(), i); | 249 item->command_id = GetCommandId(browser_menu_items_.size(), i); |
| 247 item->label = launcher_controller()->GetAppListTitle(tab); | 250 item->label = controller->GetAppListTitle(tab); |
| 248 item->image = *launcher_controller()->GetAppListIcon(tab).ToSkBitmap(); | 251 item->image = *controller->GetAppListIcon(tab).ToSkBitmap(); |
| 249 items.push_back(std::move(item)); | 252 items.push_back(std::move(item)); |
| 250 } | 253 } |
| 251 } | 254 } |
| 252 browser_menu_items_.push_back(browser); | 255 browser_menu_items_.push_back(browser); |
| 253 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 256 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 254 content::Source<Browser>(browser)); | 257 content::Source<Browser>(browser)); |
| 255 } | 258 } |
| 256 // If only windowed applications are open, we return an empty list to | 259 // If only windowed applications are open, we return an empty list to |
| 257 // enforce the creation of a new browser. | 260 // enforce the creation of a new browser. |
| 258 if (!found_tabbed_browser) { | 261 if (!found_tabbed_browser) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 // We use the list in the order of how the browsers got created - not the LRU | 313 // We use the list in the order of how the browsers got created - not the LRU |
| 311 // order. | 314 // order. |
| 312 const BrowserList* browser_list = BrowserList::GetInstance(); | 315 const BrowserList* browser_list = BrowserList::GetInstance(); |
| 313 for (BrowserList::const_iterator it = browser_list->begin(); | 316 for (BrowserList::const_iterator it = browser_list->begin(); |
| 314 it != browser_list->end(); ++it) { | 317 it != browser_list->end(); ++it) { |
| 315 if (IsBrowserRepresentedInBrowserList(*it)) | 318 if (IsBrowserRepresentedInBrowserList(*it)) |
| 316 items.push_back(*it); | 319 items.push_back(*it); |
| 317 } | 320 } |
| 318 // If there are no suitable browsers we create a new one. | 321 // If there are no suitable browsers we create a new one. |
| 319 if (items.empty()) { | 322 if (items.empty()) { |
| 320 chrome::NewEmptyWindow(launcher_controller()->profile()); | 323 chrome::NewEmptyWindow(ChromeLauncherController::instance()->profile()); |
| 321 return ash::SHELF_ACTION_NEW_WINDOW_CREATED; | 324 return ash::SHELF_ACTION_NEW_WINDOW_CREATED; |
| 322 } | 325 } |
| 323 Browser* browser = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()); | 326 Browser* browser = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()); |
| 324 if (items.size() == 1) { | 327 if (items.size() == 1) { |
| 325 // If there is only one suitable browser, we can either activate it, or | 328 // If there is only one suitable browser, we can either activate it, or |
| 326 // bounce it (if it is already active). | 329 // bounce it (if it is already active). |
| 327 if (browser == items[0]) { | 330 if (browser == items[0]) { |
| 328 AnimateWindow(browser->window()->GetNativeWindow(), | 331 AnimateWindow(browser->window()->GetNativeWindow(), |
| 329 wm::WINDOW_ANIMATION_TYPE_BOUNCE); | 332 wm::WINDOW_ANIMATION_TYPE_BOUNCE); |
| 330 return ash::SHELF_ACTION_NONE; | 333 return ash::SHELF_ACTION_NONE; |
| 331 } | 334 } |
| 332 browser = items[0]; | 335 browser = items[0]; |
| 333 } else { | 336 } else { |
| 334 // If there is more than one suitable browser, we advance to the next if | 337 // If there is more than one suitable browser, we advance to the next if |
| 335 // |browser| is already active - or - check the last used browser if it can | 338 // |browser| is already active - or - check the last used browser if it can |
| 336 // be used. | 339 // be used. |
| 337 std::vector<Browser*>::iterator i = | 340 std::vector<Browser*>::iterator i = |
| 338 std::find(items.begin(), items.end(), browser); | 341 std::find(items.begin(), items.end(), browser); |
| 339 if (i != items.end()) { | 342 if (i != items.end()) { |
| 340 browser = (++i == items.end()) ? items[0] : *i; | 343 browser = (++i == items.end()) ? items[0] : *i; |
| 341 } else { | 344 } else { |
| 342 browser = | 345 browser = chrome::FindTabbedBrowser( |
| 343 chrome::FindTabbedBrowser(launcher_controller()->profile(), true); | 346 ChromeLauncherController::instance()->profile(), true); |
| 344 if (!browser || !IsBrowserRepresentedInBrowserList(browser)) | 347 if (!browser || !IsBrowserRepresentedInBrowserList(browser)) |
| 345 browser = items[0]; | 348 browser = items[0]; |
| 346 } | 349 } |
| 347 } | 350 } |
| 348 DCHECK(browser); | 351 DCHECK(browser); |
| 349 browser->window()->Show(); | 352 browser->window()->Show(); |
| 350 browser->window()->Activate(); | 353 browser->window()->Activate(); |
| 351 return ash::SHELF_ACTION_WINDOW_ACTIVATED; | 354 return ash::SHELF_ACTION_WINDOW_ACTIVATED; |
| 352 } | 355 } |
| 353 | 356 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 Browser* browser = content::Source<Browser>(source).ptr(); | 404 Browser* browser = content::Source<Browser>(source).ptr(); |
| 402 DCHECK(browser); | 405 DCHECK(browser); |
| 403 BrowserList::BrowserVector::iterator item = std::find( | 406 BrowserList::BrowserVector::iterator item = std::find( |
| 404 browser_menu_items_.begin(), browser_menu_items_.end(), browser); | 407 browser_menu_items_.begin(), browser_menu_items_.end(), browser); |
| 405 DCHECK(item != browser_menu_items_.end()); | 408 DCHECK(item != browser_menu_items_.end()); |
| 406 // Clear the entry for the closed browser and leave other indices intact. | 409 // Clear the entry for the closed browser and leave other indices intact. |
| 407 *item = nullptr; | 410 *item = nullptr; |
| 408 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, | 411 registrar_.Remove(this, chrome::NOTIFICATION_BROWSER_CLOSING, |
| 409 content::Source<Browser>(browser)); | 412 content::Source<Browser>(browser)); |
| 410 } | 413 } |
| OLD | NEW |