| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/app_shortcut_launcher_item_controller.h
" | 5 #include "chrome/browser/ui/ash/launcher/app_shortcut_launcher_item_controller.h
" |
| 6 | 6 |
| 7 #include "apps/native_app_window.h" | 7 #include "apps/native_app_window.h" |
| 8 #include "ash/wm/window_util.h" | 8 #include "ash/wm/window_util.h" |
| 9 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/favicon/favicon_tab_helper.h" | 11 #include "chrome/browser/favicon/favicon_tab_helper.h" |
| 12 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" | 12 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h" |
| 13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h" | 13 #include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h" |
| 14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" | |
| 15 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_app.h" | 14 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller_per_app.h" |
| 16 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h" | 15 #include "chrome/browser/ui/ash/launcher/launcher_item_controller.h" |
| 17 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 19 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/host_desktop.h" | 20 #include "chrome/browser/ui/host_desktop.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 22 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 24 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 25 #include "ui/aura/window.h" | 24 #include "ui/aura/window.h" |
| 26 #include "ui/base/events/event.h" | 25 #include "ui/base/events/event.h" |
| 27 #include "ui/views/corewm/window_animations.h" | 26 #include "ui/views/corewm/window_animations.h" |
| 28 | 27 |
| 29 using extensions::Extension; | 28 using extensions::Extension; |
| 30 | 29 |
| 31 namespace { | 30 namespace { |
| 32 | 31 |
| 33 // The time delta between clicks in which clicks to launch V2 apps are ignored. | 32 // The time delta between clicks in which clicks to launch V2 apps are ignored. |
| 34 const int kClickSuppressionInMS = 1000; | 33 const int kClickSuppressionInMS = 1000; |
| 35 | 34 |
| 36 } // namespace | 35 } // namespace |
| 37 | 36 |
| 38 // Item controller for an app shortcut. Shortcuts track app and launcher ids, | 37 // Item controller for an app shortcut. Shortcuts track app and launcher ids, |
| 39 // but do not have any associated windows (opening a shortcut will replace the | 38 // but do not have any associated windows (opening a shortcut will replace the |
| 40 // item with the appropriate LauncherItemController type). | 39 // item with the appropriate LauncherItemController type). |
| 41 AppShortcutLauncherItemController::AppShortcutLauncherItemController( | 40 AppShortcutLauncherItemController::AppShortcutLauncherItemController( |
| 42 const std::string& app_id, | 41 const std::string& app_id, |
| 43 ChromeLauncherControllerPerApp* controller) | 42 ChromeLauncherController* controller) |
| 44 : LauncherItemController(TYPE_SHORTCUT, app_id, controller), | 43 : LauncherItemController(TYPE_SHORTCUT, app_id, controller), |
| 45 app_controller_(controller) { | 44 chrome_launcher_controller_(controller) { |
| 46 // To detect V1 applications we use their domain and match them against the | 45 // To detect V1 applications we use their domain and match them against the |
| 47 // used URL. This will also work with applications like Google Drive. | 46 // used URL. This will also work with applications like Google Drive. |
| 48 const Extension* extension = | 47 const Extension* extension = |
| 49 launcher_controller()->GetExtensionForAppID(app_id); | 48 launcher_controller()->GetExtensionForAppID(app_id); |
| 50 // Some unit tests have no real extension. | 49 // Some unit tests have no real extension. |
| 51 if (extension) { | 50 if (extension) { |
| 52 set_refocus_url(GURL( | 51 set_refocus_url(GURL( |
| 53 extensions::AppLaunchInfo::GetLaunchWebURL(extension).spec() + "*")); | 52 extensions::AppLaunchInfo::GetLaunchWebURL(extension).spec() + "*")); |
| 54 } | 53 } |
| 55 } | 54 } |
| 56 | 55 |
| 57 AppShortcutLauncherItemController::~AppShortcutLauncherItemController() { | 56 AppShortcutLauncherItemController::~AppShortcutLauncherItemController() { |
| 58 } | 57 } |
| 59 | 58 |
| 60 string16 AppShortcutLauncherItemController::GetTitle() { | 59 string16 AppShortcutLauncherItemController::GetTitle() { |
| 61 return GetAppTitle(); | 60 return GetAppTitle(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 bool AppShortcutLauncherItemController::IsCurrentlyShownInWindow( | 63 bool AppShortcutLauncherItemController::IsCurrentlyShownInWindow( |
| 65 aura::Window* window) const { | 64 aura::Window* window) const { |
| 66 Browser* browser = chrome::FindBrowserWithWindow(window); | 65 Browser* browser = chrome::FindBrowserWithWindow(window); |
| 67 content::WebContents* active_content_of_window = | 66 content::WebContents* active_content_of_window = |
| 68 browser ? browser->tab_strip_model()->GetActiveWebContents() : NULL; | 67 browser ? browser->tab_strip_model()->GetActiveWebContents() : NULL; |
| 69 | 68 |
| 70 std::vector<content::WebContents*> content = | 69 std::vector<content::WebContents*> content = |
| 71 app_controller_->GetV1ApplicationsFromAppId(app_id()); | 70 chrome_launcher_controller_->GetV1ApplicationsFromAppId(app_id()); |
| 72 | 71 |
| 73 std::vector<content::WebContents*>::const_iterator iter = | 72 std::vector<content::WebContents*>::const_iterator iter = |
| 74 std::find(content.begin(), content.end(), active_content_of_window); | 73 std::find(content.begin(), content.end(), active_content_of_window); |
| 75 | 74 |
| 76 return iter != content.end() ? true : false; | 75 return iter != content.end() ? true : false; |
| 77 } | 76 } |
| 78 | 77 |
| 79 bool AppShortcutLauncherItemController::IsOpen() const { | 78 bool AppShortcutLauncherItemController::IsOpen() const { |
| 80 return !app_controller_->GetV1ApplicationsFromAppId(app_id()).empty(); | 79 return !chrome_launcher_controller_-> |
| 80 GetV1ApplicationsFromAppId(app_id()).empty(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool AppShortcutLauncherItemController::IsVisible() const { | 83 bool AppShortcutLauncherItemController::IsVisible() const { |
| 84 // Return true if any browser window associated with the app is visible. | 84 // Return true if any browser window associated with the app is visible. |
| 85 std::vector<content::WebContents*> content = | 85 std::vector<content::WebContents*> content = |
| 86 app_controller_->GetV1ApplicationsFromAppId(app_id()); | 86 chrome_launcher_controller_->GetV1ApplicationsFromAppId(app_id()); |
| 87 for (size_t i = 0; i < content.size(); i++) { | 87 for (size_t i = 0; i < content.size(); i++) { |
| 88 Browser* browser = chrome::FindBrowserWithWebContents(content[i]); | 88 Browser* browser = chrome::FindBrowserWithWebContents(content[i]); |
| 89 if (browser && browser->window()->GetNativeWindow()->IsVisible()) | 89 if (browser && browser->window()->GetNativeWindow()->IsVisible()) |
| 90 return true; | 90 return true; |
| 91 } | 91 } |
| 92 return false; | 92 return false; |
| 93 } | 93 } |
| 94 | 94 |
| 95 void AppShortcutLauncherItemController::Launch(int event_flags) { | 95 void AppShortcutLauncherItemController::Launch(int event_flags) { |
| 96 app_controller_->LaunchApp(app_id(), event_flags); | 96 launcher_controller()->LaunchApp(app_id(), event_flags); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void AppShortcutLauncherItemController::Activate() { | 99 void AppShortcutLauncherItemController::Activate() { |
| 100 content::WebContents* content = GetLRUApplication(); | 100 content::WebContents* content = GetLRUApplication(); |
| 101 if (!content) { | 101 if (!content) { |
| 102 if (IsV2App()) { | 102 if (IsV2App()) { |
| 103 // Ideally we come here only once. After that ShellLauncherItemController | 103 // Ideally we come here only once. After that ShellLauncherItemController |
| 104 // will take over when the shell window gets opened. However there are | 104 // will take over when the shell window gets opened. However there are |
| 105 // apps which take a lot of time for pre-processing (like the files app) | 105 // apps which take a lot of time for pre-processing (like the files app) |
| 106 // before they open a window. Since there is currently no other way to | 106 // before they open a window. Since there is currently no other way to |
| 107 // detect if an app was started we suppress any further clicks within a | 107 // detect if an app was started we suppress any further clicks within a |
| 108 // special time out. | 108 // special time out. |
| 109 if (!AllowNextLaunchAttempt()) | 109 if (!AllowNextLaunchAttempt()) |
| 110 return; | 110 return; |
| 111 } | 111 } |
| 112 Launch(ui::EF_NONE); | 112 Launch(ui::EF_NONE); |
| 113 return; | 113 return; |
| 114 } | 114 } |
| 115 ActivateContent(content); | 115 ActivateContent(content); |
| 116 } | 116 } |
| 117 | 117 |
| 118 void AppShortcutLauncherItemController::Close() { | 118 void AppShortcutLauncherItemController::Close() { |
| 119 // Close all running 'programs' of this type. | 119 // Close all running 'programs' of this type. |
| 120 std::vector<content::WebContents*> content = | 120 std::vector<content::WebContents*> content = |
| 121 app_controller_->GetV1ApplicationsFromAppId(app_id()); | 121 launcher_controller()->GetV1ApplicationsFromAppId(app_id()); |
| 122 for (size_t i = 0; i < content.size(); i++) { | 122 for (size_t i = 0; i < content.size(); i++) { |
| 123 Browser* browser = chrome::FindBrowserWithWebContents(content[i]); | 123 Browser* browser = chrome::FindBrowserWithWebContents(content[i]); |
| 124 if (!browser) | 124 if (!browser) |
| 125 continue; | 125 continue; |
| 126 TabStripModel* tab_strip = browser->tab_strip_model(); | 126 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 127 int index = tab_strip->GetIndexOfWebContents(content[i]); | 127 int index = tab_strip->GetIndexOfWebContents(content[i]); |
| 128 DCHECK(index != TabStripModel::kNoTab); | 128 DCHECK(index != TabStripModel::kNoTab); |
| 129 tab_strip->CloseWebContentsAt(index, TabStripModel::CLOSE_NONE); | 129 tab_strip->CloseWebContentsAt(index, TabStripModel::CLOSE_NONE); |
| 130 } | 130 } |
| 131 } | 131 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 154 AppShortcutLauncherItemController::GetApplicationList(int event_flags) { | 154 AppShortcutLauncherItemController::GetApplicationList(int event_flags) { |
| 155 ChromeLauncherAppMenuItems items; | 155 ChromeLauncherAppMenuItems items; |
| 156 // Add the application name to the menu. | 156 // Add the application name to the menu. |
| 157 items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false)); | 157 items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false)); |
| 158 | 158 |
| 159 std::vector<content::WebContents*> content_list = GetRunningApplications(); | 159 std::vector<content::WebContents*> content_list = GetRunningApplications(); |
| 160 | 160 |
| 161 for (size_t i = 0; i < content_list.size(); i++) { | 161 for (size_t i = 0; i < content_list.size(); i++) { |
| 162 content::WebContents* web_contents = content_list[i]; | 162 content::WebContents* web_contents = content_list[i]; |
| 163 // Get the icon. | 163 // Get the icon. |
| 164 gfx::Image app_icon = app_controller_->GetAppListIcon(web_contents); | 164 gfx::Image app_icon = launcher_controller()->GetAppListIcon(web_contents); |
| 165 string16 title = app_controller_->GetAppListTitle(web_contents); | 165 string16 title = launcher_controller()->GetAppListTitle(web_contents); |
| 166 items.push_back(new ChromeLauncherAppMenuItemTab( | 166 items.push_back(new ChromeLauncherAppMenuItemTab( |
| 167 title, &app_icon, web_contents, i == 0)); | 167 title, &app_icon, web_contents, i == 0)); |
| 168 } | 168 } |
| 169 return items.Pass(); | 169 return items.Pass(); |
| 170 } | 170 } |
| 171 | 171 |
| 172 std::vector<content::WebContents*> | 172 std::vector<content::WebContents*> |
| 173 AppShortcutLauncherItemController::GetRunningApplications() { | 173 AppShortcutLauncherItemController::GetRunningApplications() { |
| 174 std::vector<content::WebContents*> items; | 174 std::vector<content::WebContents*> items; |
| 175 | 175 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 const GURL tab_url = web_contents->GetURL(); | 258 const GURL tab_url = web_contents->GetURL(); |
| 259 // There are three ways to identify the association of a URL with this | 259 // There are three ways to identify the association of a URL with this |
| 260 // extension: | 260 // extension: |
| 261 // - The refocus pattern is matched (needed for apps like drive). | 261 // - The refocus pattern is matched (needed for apps like drive). |
| 262 // - The extension's origin + extent gets matched. | 262 // - The extension's origin + extent gets matched. |
| 263 // - The launcher controller knows that the tab got created for this app. | 263 // - The launcher controller knows that the tab got created for this app. |
| 264 return ((!refocus_pattern.match_all_urls() && | 264 return ((!refocus_pattern.match_all_urls() && |
| 265 refocus_pattern.MatchesURL(tab_url)) || | 265 refocus_pattern.MatchesURL(tab_url)) || |
| 266 (extension->OverlapsWithOrigin(tab_url) && | 266 (extension->OverlapsWithOrigin(tab_url) && |
| 267 extension->web_extent().MatchesURL(tab_url)) || | 267 extension->web_extent().MatchesURL(tab_url)) || |
| 268 launcher_controller()->GetPerAppInterface()-> | 268 launcher_controller()->IsWebContentHandledByApplication(web_contents, |
| 269 IsWebContentHandledByApplication(web_contents, app_id())); | 269 app_id())); |
| 270 } | 270 } |
| 271 | 271 |
| 272 void AppShortcutLauncherItemController::ActivateContent( | 272 void AppShortcutLauncherItemController::ActivateContent( |
| 273 content::WebContents* content) { | 273 content::WebContents* content) { |
| 274 Browser* browser = chrome::FindBrowserWithWebContents(content); | 274 Browser* browser = chrome::FindBrowserWithWebContents(content); |
| 275 TabStripModel* tab_strip = browser->tab_strip_model(); | 275 TabStripModel* tab_strip = browser->tab_strip_model(); |
| 276 int index = tab_strip->GetIndexOfWebContents(content); | 276 int index = tab_strip->GetIndexOfWebContents(content); |
| 277 DCHECK_NE(TabStripModel::kNoTab, index); | 277 DCHECK_NE(TabStripModel::kNoTab, index); |
| 278 | 278 |
| 279 int old_index = tab_strip->active_index(); | 279 int old_index = tab_strip->active_index(); |
| 280 if (index != old_index) | 280 if (index != old_index) |
| 281 tab_strip->ActivateTabAt(index, false); | 281 tab_strip->ActivateTabAt(index, false); |
| 282 app_controller_->ActivateWindowOrMinimizeIfActive(browser->window(), | 282 launcher_controller()->ActivateWindowOrMinimizeIfActive( |
| 283 browser->window(), |
| 283 index == old_index && GetRunningApplications().size() == 1); | 284 index == old_index && GetRunningApplications().size() == 1); |
| 284 } | 285 } |
| 285 | 286 |
| 286 bool AppShortcutLauncherItemController::AdvanceToNextApp() { | 287 bool AppShortcutLauncherItemController::AdvanceToNextApp() { |
| 287 std::vector<content::WebContents*> items = GetRunningApplications(); | 288 std::vector<content::WebContents*> items = GetRunningApplications(); |
| 288 if (items.size() >= 1) { | 289 if (items.size() >= 1) { |
| 289 Browser* browser = chrome::FindBrowserWithWindow( | 290 Browser* browser = chrome::FindBrowserWithWindow( |
| 290 ash::wm::GetActiveWindow()); | 291 ash::wm::GetActiveWindow()); |
| 291 if (browser) { | 292 if (browser) { |
| 292 TabStripModel* tab_strip = browser->tab_strip_model(); | 293 TabStripModel* tab_strip = browser->tab_strip_model(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 305 ActivateContent(items[index]); | 306 ActivateContent(items[index]); |
| 306 } | 307 } |
| 307 return true; | 308 return true; |
| 308 } | 309 } |
| 309 } | 310 } |
| 310 } | 311 } |
| 311 return false; | 312 return false; |
| 312 } | 313 } |
| 313 | 314 |
| 314 bool AppShortcutLauncherItemController::IsV2App() { | 315 bool AppShortcutLauncherItemController::IsV2App() { |
| 315 const Extension* extension = app_controller_->GetExtensionForAppID(app_id()); | 316 const Extension* extension = |
| 317 launcher_controller()->GetExtensionForAppID(app_id()); |
| 316 return extension && extension->is_platform_app(); | 318 return extension && extension->is_platform_app(); |
| 317 } | 319 } |
| 318 | 320 |
| 319 bool AppShortcutLauncherItemController::AllowNextLaunchAttempt() { | 321 bool AppShortcutLauncherItemController::AllowNextLaunchAttempt() { |
| 320 if (last_launch_attempt_.is_null() || | 322 if (last_launch_attempt_.is_null() || |
| 321 last_launch_attempt_ + base::TimeDelta::FromMilliseconds( | 323 last_launch_attempt_ + base::TimeDelta::FromMilliseconds( |
| 322 kClickSuppressionInMS) < base::Time::Now()) { | 324 kClickSuppressionInMS) < base::Time::Now()) { |
| 323 last_launch_attempt_ = base::Time::Now(); | 325 last_launch_attempt_ = base::Time::Now(); |
| 324 return true; | 326 return true; |
| 325 } | 327 } |
| 326 return false; | 328 return false; |
| 327 } | 329 } |
| OLD | NEW |