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 <vector> | 7 #include <vector> |
8 | 8 |
9 #include "ash/launcher/launcher.h" | 9 #include "ash/launcher/launcher.h" |
| 10 #include "ash/launcher/launcher_model.h" |
| 11 #include "ash/launcher/launcher_util.h" |
10 #include "ash/shell.h" | 12 #include "ash/shell.h" |
11 #include "ash/wm/window_util.h" | 13 #include "ash/wm/window_util.h" |
12 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" | 15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" |
14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_browser.h
" | 16 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_browser.h
" |
15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h" | 17 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h" |
16 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | 18 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
17 #include "chrome/browser/ui/browser.h" | 19 #include "chrome/browser/ui/browser.h" |
18 #include "chrome/browser/ui/browser_finder.h" | 20 #include "chrome/browser/ui/browser_finder.h" |
19 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
20 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 24 #include "chrome/browser/web_applications/web_app.h" |
22 #include "chrome/common/extensions/extension_constants.h" | 25 #include "chrome/common/extensions/extension_constants.h" |
23 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
24 #include "grit/ash_resources.h" | 27 #include "grit/ash_resources.h" |
25 #include "grit/chromium_strings.h" | 28 #include "grit/chromium_strings.h" |
26 #include "grit/generated_resources.h" | 29 #include "grit/generated_resources.h" |
27 #include "ui/aura/window.h" | 30 #include "ui/aura/window.h" |
28 #include "ui/base/events/event.h" | 31 #include "ui/base/events/event.h" |
29 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
30 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
31 #include "ui/gfx/image/image.h" | 34 #include "ui/gfx/image/image.h" |
32 #include "ui/views/corewm/window_animations.h" | 35 #include "ui/views/corewm/window_animations.h" |
33 | 36 |
34 #if defined(OS_CHROMEOS) | 37 #if defined(OS_CHROMEOS) |
35 #include "chrome/browser/chromeos/login/default_pinned_apps_field_trial.h" | 38 #include "chrome/browser/chromeos/login/default_pinned_apps_field_trial.h" |
36 #endif | 39 #endif |
37 | 40 |
38 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( | 41 BrowserShortcutLauncherItemController::BrowserShortcutLauncherItemController( |
39 ChromeLauncherController* launcher_controller, | 42 ChromeLauncherController* launcher_controller, |
40 Profile* profile) | 43 Profile* profile) |
41 : LauncherItemController(TYPE_SHORTCUT, | 44 : LauncherItemController(TYPE_SHORTCUT, |
42 extension_misc::kChromeAppId, | 45 extension_misc::kChromeAppId, |
43 launcher_controller), | 46 launcher_controller), |
44 profile_(profile) { | 47 profile_(profile) { |
45 } | 48 } |
46 | 49 |
47 BrowserShortcutLauncherItemController:: | 50 BrowserShortcutLauncherItemController:: |
48 ~BrowserShortcutLauncherItemController() { | 51 ~BrowserShortcutLauncherItemController() { |
49 } | 52 } |
50 | 53 |
| 54 void BrowserShortcutLauncherItemController::UpdateBrowserItemStatus() { |
| 55 // The shell will not be available for win7_aura unittests like |
| 56 // ChromeLauncherControllerTest.BrowserMenuGeneration. |
| 57 if (!ash::Shell::HasInstance()) |
| 58 return; |
| 59 |
| 60 ash::LauncherModel* model = launcher_controller()->model(); |
| 61 |
| 62 // Determine the new browser's active state and change if necessary. |
| 63 size_t browser_index = ash::launcher::GetBrowserItemIndex(*model); |
| 64 DCHECK_GE(browser_index, 0u); |
| 65 ash::LauncherItem browser_item = model->items()[browser_index]; |
| 66 ash::LauncherItemStatus browser_status = ash::STATUS_CLOSED; |
| 67 |
| 68 aura::Window* window = ash::wm::GetActiveWindow(); |
| 69 if (window) { |
| 70 // Check if the active browser / tab is a browser which is not an app, |
| 71 // a windowed app, a popup or any other item which is not a browser of |
| 72 // interest. |
| 73 Browser* browser = chrome::FindBrowserWithWindow(window); |
| 74 if (IsBrowserRepresentedInBrowserList(browser)) { |
| 75 browser_status = ash::STATUS_ACTIVE; |
| 76 // If an app is running in active window, browser item status cannot be |
| 77 // active. |
| 78 if (launcher_controller()->GetIDByWindow(window) != browser_item.id) |
| 79 browser_status = ash::STATUS_RUNNING; |
| 80 } |
| 81 } |
| 82 |
| 83 if (browser_status == ash::STATUS_CLOSED) { |
| 84 const BrowserList* ash_browser_list = |
| 85 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
| 86 for (BrowserList::const_reverse_iterator it = |
| 87 ash_browser_list->begin_last_active(); |
| 88 it != ash_browser_list->end_last_active() && |
| 89 browser_status == ash::STATUS_CLOSED; ++it) { |
| 90 if (IsBrowserRepresentedInBrowserList(*it)) |
| 91 browser_status = ash::STATUS_RUNNING; |
| 92 } |
| 93 } |
| 94 |
| 95 if (browser_status != browser_item.status) { |
| 96 browser_item.status = browser_status; |
| 97 model->Set(browser_index, browser_item); |
| 98 } |
| 99 } |
| 100 |
51 string16 BrowserShortcutLauncherItemController::GetTitle() { | 101 string16 BrowserShortcutLauncherItemController::GetTitle() { |
52 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); | 102 return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
53 } | 103 } |
54 | 104 |
55 bool BrowserShortcutLauncherItemController::IsCurrentlyShownInWindow( | 105 bool BrowserShortcutLauncherItemController::IsCurrentlyShownInWindow( |
56 aura::Window* window) const { | 106 aura::Window* window) const { |
57 const BrowserList* ash_browser_list = | 107 const BrowserList* ash_browser_list = |
58 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); | 108 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
59 for (BrowserList::const_iterator it = ash_browser_list->begin(); | 109 for (BrowserList::const_iterator it = ash_browser_list->begin(); |
60 it != ash_browser_list->end(); ++it) { | 110 it != ash_browser_list->end(); ++it) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 it != ash_browser_list->end(); ++it) { | 200 it != ash_browser_list->end(); ++it) { |
151 Browser* browser = *it; | 201 Browser* browser = *it; |
152 // Make sure that the browser was already shown and it has a proper window. | 202 // Make sure that the browser was already shown and it has a proper window. |
153 if (std::find(ash_browser_list->begin_last_active(), | 203 if (std::find(ash_browser_list->begin_last_active(), |
154 ash_browser_list->end_last_active(), | 204 ash_browser_list->end_last_active(), |
155 browser) == ash_browser_list->end_last_active() || | 205 browser) == ash_browser_list->end_last_active() || |
156 !browser->window()) | 206 !browser->window()) |
157 continue; | 207 continue; |
158 if (browser->is_type_tabbed()) | 208 if (browser->is_type_tabbed()) |
159 found_tabbed_browser = true; | 209 found_tabbed_browser = true; |
160 else if (!launcher_controller()->IsBrowserRepresentedInBrowserList(browser)) | 210 else if (!IsBrowserRepresentedInBrowserList(browser)) |
161 continue; | 211 continue; |
162 TabStripModel* tab_strip = browser->tab_strip_model(); | 212 TabStripModel* tab_strip = browser->tab_strip_model(); |
163 if (tab_strip->active_index() == -1) | 213 if (tab_strip->active_index() == -1) |
164 continue; | 214 continue; |
165 if (!(event_flags & ui::EF_SHIFT_DOWN)) { | 215 if (!(event_flags & ui::EF_SHIFT_DOWN)) { |
166 content::WebContents* web_contents = | 216 content::WebContents* web_contents = |
167 tab_strip->GetWebContentsAt(tab_strip->active_index()); | 217 tab_strip->GetWebContentsAt(tab_strip->active_index()); |
168 gfx::Image app_icon = GetBrowserListIcon(web_contents); | 218 gfx::Image app_icon = GetBrowserListIcon(web_contents); |
169 string16 title = GetBrowserListTitle(web_contents); | 219 string16 title = GetBrowserListTitle(web_contents); |
170 items.push_back(new ChromeLauncherAppMenuItemBrowser( | 220 items.push_back(new ChromeLauncherAppMenuItemBrowser( |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 void BrowserShortcutLauncherItemController::ActivateOrAdvanceToNextBrowser() { | 266 void BrowserShortcutLauncherItemController::ActivateOrAdvanceToNextBrowser() { |
217 // Create a list of all suitable running browsers. | 267 // Create a list of all suitable running browsers. |
218 std::vector<Browser*> items; | 268 std::vector<Browser*> items; |
219 // We use the list in the order of how the browsers got created - not the LRU | 269 // We use the list in the order of how the browsers got created - not the LRU |
220 // order. | 270 // order. |
221 const BrowserList* ash_browser_list = | 271 const BrowserList* ash_browser_list = |
222 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); | 272 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH); |
223 for (BrowserList::const_iterator it = | 273 for (BrowserList::const_iterator it = |
224 ash_browser_list->begin(); | 274 ash_browser_list->begin(); |
225 it != ash_browser_list->end(); ++it) { | 275 it != ash_browser_list->end(); ++it) { |
226 if (launcher_controller()->IsBrowserRepresentedInBrowserList(*it)) | 276 if (IsBrowserRepresentedInBrowserList(*it)) |
227 items.push_back(*it); | 277 items.push_back(*it); |
228 } | 278 } |
229 // If there are no suitable browsers we create a new one. | 279 // If there are no suitable browsers we create a new one. |
230 if (!items.size()) { | 280 if (!items.size()) { |
231 launcher_controller()->CreateNewWindow(); | 281 launcher_controller()->CreateNewWindow(); |
232 return; | 282 return; |
233 } | 283 } |
234 Browser* browser = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()); | 284 Browser* browser = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow()); |
235 if (items.size() == 1) { | 285 if (items.size() == 1) { |
236 // If there is only one suitable browser, we can either activate it, or | 286 // If there is only one suitable browser, we can either activate it, or |
(...skipping 10 matching lines...) Expand all Loading... |
247 // if it can be used. | 297 // if it can be used. |
248 std::vector<Browser*>::iterator i = | 298 std::vector<Browser*>::iterator i = |
249 std::find(items.begin(), items.end(), browser); | 299 std::find(items.begin(), items.end(), browser); |
250 if (i != items.end()) { | 300 if (i != items.end()) { |
251 browser = (++i == items.end()) ? items[0] : *i; | 301 browser = (++i == items.end()) ? items[0] : *i; |
252 } else { | 302 } else { |
253 browser = chrome::FindTabbedBrowser(profile_, | 303 browser = chrome::FindTabbedBrowser(profile_, |
254 true, | 304 true, |
255 chrome::HOST_DESKTOP_TYPE_ASH); | 305 chrome::HOST_DESKTOP_TYPE_ASH); |
256 if (!browser || | 306 if (!browser || |
257 !launcher_controller()->IsBrowserRepresentedInBrowserList(browser)) | 307 !IsBrowserRepresentedInBrowserList(browser)) |
258 browser = items[0]; | 308 browser = items[0]; |
259 } | 309 } |
260 } | 310 } |
261 DCHECK(browser); | 311 DCHECK(browser); |
262 browser->window()->Show(); | 312 browser->window()->Show(); |
263 browser->window()->Activate(); | 313 browser->window()->Activate(); |
264 } | 314 } |
| 315 |
| 316 bool BrowserShortcutLauncherItemController::IsBrowserRepresentedInBrowserList( |
| 317 Browser* browser) { |
| 318 return (browser && |
| 319 (browser->is_type_tabbed() || |
| 320 !browser->is_app() || |
| 321 !browser->is_type_popup() || |
| 322 launcher_controller()-> |
| 323 GetLauncherIDForAppID(web_app::GetExtensionIdFromApplicationName( |
| 324 browser->app_name())) <= 0)); |
| 325 } |
OLD | NEW |