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
3// found in the LICENSE file.
4
5#include "chrome/browser/ui/ash/launcher/chrome_launcher_app_menu_item_tab.h"
6
7#include "ash/wm/window_util.h"
8#include "chrome/browser/ui/browser.h"
9#include "chrome/browser/ui/browser_finder.h"
10#include "chrome/browser/ui/browser_window.h"
11#include "chrome/browser/ui/tabs/tab_strip_model.h"
12#include "ui/events/event_constants.h"
13
14ChromeLauncherAppMenuItemTab::ChromeLauncherAppMenuItemTab(
15    const base::string16 title,
16    const gfx::Image* icon,
17    content::WebContents* content,
18    bool has_leading_separator)
19    : ChromeLauncherAppMenuItem(title, icon, has_leading_separator),
20      content::WebContentsObserver(content) {
21}
22
23bool ChromeLauncherAppMenuItemTab::IsActive() const {
24  Browser* browser = chrome::FindBrowserWithWindow(ash::wm::GetActiveWindow());
25  if (!browser)
26    return false;
27  return web_contents() == browser->tab_strip_model()->GetActiveWebContents();
28}
29
30bool ChromeLauncherAppMenuItemTab::IsEnabled() const {
31  return true;
32}
33
34void ChromeLauncherAppMenuItemTab::Execute(int event_flags) {
35  if (!web_contents())
36    return;
37  Browser* browser = chrome::FindBrowserWithWebContents(web_contents());
38  if (!browser)
39    return;
40  TabStripModel* tab_strip = browser->tab_strip_model();
41  int index = tab_strip->GetIndexOfWebContents(web_contents());
42  DCHECK_NE(index, TabStripModel::kNoTab);
43  if (event_flags & (ui::EF_SHIFT_DOWN | ui::EF_MIDDLE_MOUSE_BUTTON)) {
44    tab_strip->CloseWebContentsAt(index, TabStripModel::CLOSE_USER_GESTURE);
45  } else {
46    tab_strip->ActivateTabAt(index, false);
47    browser->window()->Show();
48    // Need this check to prevent unit tests from crashing.
49    if (browser->window()->GetNativeWindow())
50      ash::wm::ActivateWindow(browser->window()->GetNativeWindow());
51  }
52}
53