This source file includes following definitions.
- CanBrowserBeUsedForDirectActivation
- chrome_launcher_controller_
- IsOpen
- IsVisible
- Activate
- Close
- GetApplicationList
- GetRunningApplications
- ItemSelected
- GetTitle
- CreateContextMenu
- CreateApplicationMenu
- IsDraggable
- ShouldShowTooltip
- GetLRUApplication
- WebContentMatchesApp
- ActivateContent
- AdvanceToNextApp
- IsV2App
- AllowNextLaunchAttempt
#include "chrome/browser/ui/ash/launcher/app_shortcut_launcher_item_controller.h"
#include "apps/ui/native_app_window.h"
#include "ash/shelf/shelf_model.h"
#include "ash/shell.h"
#include "ash/wm/window_util.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h"
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
#include "chrome/browser/ui/ash/launcher/launcher_context_menu.h"
#include "chrome/browser/ui/ash/launcher/launcher_item_controller.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/process_manager.h"
#include "ui/aura/window.h"
#include "ui/events/event.h"
#include "ui/wm/core/window_animations.h"
using extensions::Extension;
namespace {
const int kClickSuppressionInMS = 1000;
bool CanBrowserBeUsedForDirectActivation(Browser* browser,
ChromeLauncherController* launcher) {
if (chrome::MultiUserWindowManager::GetMultiProfileMode() ==
chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_OFF)
return true;
return multi_user_util::IsProfileFromActiveUser(browser->profile());
}
}
AppShortcutLauncherItemController::AppShortcutLauncherItemController(
const std::string& app_id,
ChromeLauncherController* controller)
: LauncherItemController(TYPE_SHORTCUT, app_id, controller),
chrome_launcher_controller_(controller) {
const Extension* extension =
launcher_controller()->GetExtensionForAppID(app_id);
if (extension) {
set_refocus_url(GURL(
extensions::AppLaunchInfo::GetLaunchWebURL(extension).spec() + "*"));
}
}
AppShortcutLauncherItemController::~AppShortcutLauncherItemController() {
}
bool AppShortcutLauncherItemController::IsOpen() const {
return !chrome_launcher_controller_->
GetV1ApplicationsFromAppId(app_id()).empty();
}
bool AppShortcutLauncherItemController::IsVisible() const {
std::vector<content::WebContents*> content =
chrome_launcher_controller_->GetV1ApplicationsFromAppId(app_id());
for (size_t i = 0; i < content.size(); i++) {
Browser* browser = chrome::FindBrowserWithWebContents(content[i]);
if (browser && browser->window()->GetNativeWindow()->IsVisible())
return true;
}
return false;
}
void AppShortcutLauncherItemController::Launch(ash::LaunchSource source,
int event_flags) {
launcher_controller()->LaunchApp(app_id(), source, event_flags);
}
bool AppShortcutLauncherItemController::Activate(ash::LaunchSource source) {
content::WebContents* content = GetLRUApplication();
if (!content) {
if (IsV2App()) {
if (!AllowNextLaunchAttempt())
return false;
}
Launch(source, ui::EF_NONE);
return true;
}
ActivateContent(content);
return false;
}
void AppShortcutLauncherItemController::Close() {
std::vector<content::WebContents*> content =
launcher_controller()->GetV1ApplicationsFromAppId(app_id());
for (size_t i = 0; i < content.size(); i++) {
Browser* browser = chrome::FindBrowserWithWebContents(content[i]);
if (!browser || !launcher_controller()->IsBrowserFromActiveUser(browser))
continue;
TabStripModel* tab_strip = browser->tab_strip_model();
int index = tab_strip->GetIndexOfWebContents(content[i]);
DCHECK(index != TabStripModel::kNoTab);
tab_strip->CloseWebContentsAt(index, TabStripModel::CLOSE_NONE);
}
}
ChromeLauncherAppMenuItems
AppShortcutLauncherItemController::GetApplicationList(int event_flags) {
ChromeLauncherAppMenuItems items;
items.push_back(new ChromeLauncherAppMenuItem(GetTitle(), NULL, false));
std::vector<content::WebContents*> content_list = GetRunningApplications();
for (size_t i = 0; i < content_list.size(); i++) {
content::WebContents* web_contents = content_list[i];
gfx::Image app_icon = launcher_controller()->GetAppListIcon(web_contents);
base::string16 title = launcher_controller()->GetAppListTitle(web_contents);
items.push_back(new ChromeLauncherAppMenuItemTab(
title, &app_icon, web_contents, i == 0));
}
return items.Pass();
}
std::vector<content::WebContents*>
AppShortcutLauncherItemController::GetRunningApplications() {
std::vector<content::WebContents*> items;
URLPattern refocus_pattern(URLPattern::SCHEME_ALL);
refocus_pattern.SetMatchAllURLs(true);
if (!refocus_url_.is_empty()) {
refocus_pattern.SetMatchAllURLs(false);
refocus_pattern.Parse(refocus_url_.spec());
}
const Extension* extension =
launcher_controller()->GetExtensionForAppID(app_id());
if (!extension)
return items;
const BrowserList* ash_browser_list =
BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
for (BrowserList::const_iterator it = ash_browser_list->begin();
it != ash_browser_list->end(); ++it) {
Browser* browser = *it;
if (!launcher_controller()->IsBrowserFromActiveUser(browser))
continue;
TabStripModel* tab_strip = browser->tab_strip_model();
for (int index = 0; index < tab_strip->count(); index++) {
content::WebContents* web_contents = tab_strip->GetWebContentsAt(index);
if (WebContentMatchesApp(
extension, refocus_pattern, web_contents, browser))
items.push_back(web_contents);
}
}
return items;
}
bool AppShortcutLauncherItemController::ItemSelected(const ui::Event& event) {
if (event.type() == ui::ET_KEY_RELEASED) {
if (AdvanceToNextApp())
return false;
}
return Activate(ash::LAUNCH_FROM_UNKNOWN);
}
base::string16 AppShortcutLauncherItemController::GetTitle() {
return GetAppTitle();
}
ui::MenuModel* AppShortcutLauncherItemController::CreateContextMenu(
aura::Window* root_window) {
ash::ShelfItem item =
*(launcher_controller()->model()->ItemByID(shelf_id()));
return new LauncherContextMenu(launcher_controller(), &item, root_window);
}
ash::ShelfMenuModel* AppShortcutLauncherItemController::CreateApplicationMenu(
int event_flags) {
return new LauncherApplicationMenuItemModel(GetApplicationList(event_flags));
}
bool AppShortcutLauncherItemController::IsDraggable() {
return true;
}
bool AppShortcutLauncherItemController::ShouldShowTooltip() {
return true;
}
content::WebContents* AppShortcutLauncherItemController::GetLRUApplication() {
URLPattern refocus_pattern(URLPattern::SCHEME_ALL);
refocus_pattern.SetMatchAllURLs(true);
if (!refocus_url_.is_empty()) {
refocus_pattern.SetMatchAllURLs(false);
refocus_pattern.Parse(refocus_url_.spec());
}
const Extension* extension =
launcher_controller()->GetExtensionForAppID(app_id());
if (!extension)
return NULL;
const BrowserList* ash_browser_list =
BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_ASH);
for (BrowserList::const_reverse_iterator
it = ash_browser_list->begin_last_active();
it != ash_browser_list->end_last_active(); ++it) {
Browser* browser = *it;
if (!CanBrowserBeUsedForDirectActivation(browser, launcher_controller()))
continue;
TabStripModel* tab_strip = browser->tab_strip_model();
int active_index = tab_strip->active_index();
for (int index = 0; index < tab_strip->count(); index++) {
content::WebContents* web_contents = tab_strip->GetWebContentsAt(
(index + active_index) % tab_strip->count());
if (WebContentMatchesApp(
extension, refocus_pattern, web_contents, browser))
return web_contents;
}
}
for (BrowserList::const_iterator it = ash_browser_list->begin();
it != ash_browser_list->end(); ++it) {
Browser* browser = *it;
if (!CanBrowserBeUsedForDirectActivation(browser, launcher_controller()))
continue;
TabStripModel* tab_strip = browser->tab_strip_model();
for (int index = 0; index < tab_strip->count(); index++) {
content::WebContents* web_contents = tab_strip->GetWebContentsAt(index);
if (WebContentMatchesApp(
extension, refocus_pattern, web_contents, browser))
return web_contents;
}
}
return NULL;
}
bool AppShortcutLauncherItemController::WebContentMatchesApp(
const extensions::Extension* extension,
const URLPattern& refocus_pattern,
content::WebContents* web_contents,
Browser* browser) {
if (browser->is_app()) {
const extensions::Extension* browser_extension = NULL;
const ExtensionService* extension_service =
browser->profile()->GetExtensionService();
if (extension_service) {
browser_extension = extension_service->GetInstalledExtension(
web_app::GetExtensionIdFromApplicationName(browser->app_name()));
}
return browser_extension == extension;
}
const GURL tab_url = web_contents->GetURL();
return ((!refocus_pattern.match_all_urls() &&
refocus_pattern.MatchesURL(tab_url)) ||
(extension->OverlapsWithOrigin(tab_url) &&
extension->web_extent().MatchesURL(tab_url)) ||
launcher_controller()->IsWebContentHandledByApplication(web_contents,
app_id()));
}
void AppShortcutLauncherItemController::ActivateContent(
content::WebContents* content) {
Browser* browser = chrome::FindBrowserWithWebContents(content);
TabStripModel* tab_strip = browser->tab_strip_model();
int index = tab_strip->GetIndexOfWebContents(content);
DCHECK_NE(TabStripModel::kNoTab, index);
int old_index = tab_strip->active_index();
if (index != old_index)
tab_strip->ActivateTabAt(index, false);
launcher_controller()->ActivateWindowOrMinimizeIfActive(
browser->window(),
index == old_index && GetRunningApplications().size() == 1);
}
bool AppShortcutLauncherItemController::AdvanceToNextApp() {
std::vector<content::WebContents*> items = GetRunningApplications();
if (items.size() >= 1) {
Browser* browser = chrome::FindBrowserWithWindow(
ash::wm::GetActiveWindow());
if (browser) {
TabStripModel* tab_strip = browser->tab_strip_model();
content::WebContents* active = tab_strip->GetWebContentsAt(
tab_strip->active_index());
std::vector<content::WebContents*>::const_iterator i(
std::find(items.begin(), items.end(), active));
if (i != items.end()) {
if (items.size() == 1) {
AnimateWindow(browser->window()->GetNativeWindow(),
wm::WINDOW_ANIMATION_TYPE_BOUNCE);
} else {
int index = (static_cast<int>(i - items.begin()) + 1) % items.size();
ActivateContent(items[index]);
}
return true;
}
}
}
return false;
}
bool AppShortcutLauncherItemController::IsV2App() {
const Extension* extension =
launcher_controller()->GetExtensionForAppID(app_id());
return extension && extension->is_platform_app();
}
bool AppShortcutLauncherItemController::AllowNextLaunchAttempt() {
if (last_launch_attempt_.is_null() ||
last_launch_attempt_ + base::TimeDelta::FromMilliseconds(
kClickSuppressionInMS) < base::Time::Now()) {
last_launch_attempt_ = base::Time::Now();
return true;
}
return false;
}