| Index: chrome/browser/ui/tabs/recently_closed_tabs_menu_model.cc
|
| ===================================================================
|
| --- chrome/browser/ui/tabs/recently_closed_tabs_menu_model.cc (revision 0)
|
| +++ chrome/browser/ui/tabs/recently_closed_tabs_menu_model.cc (revision 0)
|
| @@ -0,0 +1,197 @@
|
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "build/build_config.h"
|
| +
|
| +#include "chrome/browser/ui/tabs/recently_closed_tabs_menu_model.h"
|
| +#include "chrome/browser/ui/tabs/recently_closed_tabs_sub_menu_model.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/bind_helpers.h"
|
| +#include "base/string_number_conversions.h"
|
| +#include "base/utf_string_conversions.h"
|
| +#include "chrome/browser/event_disposition.h"
|
| +#include "chrome/browser/sessions/tab_restore_service_delegate.h"
|
| +#include "chrome/browser/sessions/tab_restore_service_factory.h"
|
| +#include "chrome/browser/prefs/pref_service.h"
|
| +#include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/ui/browser.h"
|
| +#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
|
| +#include "chrome/common/pref_names.h"
|
| +#include "chrome/common/url_constants.h"
|
| +#include "content/public/browser/favicon_status.h"
|
| +#include "content/public/browser/navigation_entry.h"
|
| +#include "content/public/browser/web_contents.h"
|
| +#include "grit/generated_resources.h"
|
| +#include "grit/theme_resources.h"
|
| +#include "grit/theme_resources_standard.h"
|
| +#include "grit/ui_resources.h"
|
| +#include "net/base/registry_controlled_domain.h"
|
| +#include "ui/base/l10n/l10n_util.h"
|
| +#include "ui/base/resource/resource_bundle.h"
|
| +#include "ui/base/text/text_elider.h"
|
| +#include "ui/gfx/codec/png_codec.h"
|
| +
|
| +
|
| +using content::NavigationEntry;
|
| +
|
| +static const int kMaxWidth = 700;
|
| +
|
| +RecentlyClosedTabsMenuModel::RecentlyClosedTabsMenuModel(Browser* browser)
|
| + : browser_(browser),
|
| + menu_model_delegate_(NULL) {
|
| + tab_restore_service_ =
|
| + TabRestoreServiceFactory::GetForProfile(browser_->profile());
|
| +}
|
| +
|
| +RecentlyClosedTabsMenuModel::~RecentlyClosedTabsMenuModel() {
|
| +}
|
| +
|
| +bool RecentlyClosedTabsMenuModel::HasIcons() const {
|
| + return true;
|
| +}
|
| +
|
| +int RecentlyClosedTabsMenuModel::GetItemCount() const {
|
| + int items = GetRecentlyClosedItemCount();
|
| + return items;
|
| +}
|
| +
|
| +ui::MenuModel::ItemType
|
| + RecentlyClosedTabsMenuModel::GetTypeAt(int index) const {
|
| + return TYPE_COMMAND;
|
| +}
|
| +
|
| +int RecentlyClosedTabsMenuModel::GetCommandIdAt(int index) const {
|
| + return index;
|
| +}
|
| +
|
| +TabRestoreService::Entry*
|
| + RecentlyClosedTabsMenuModel::GetNavigationEntryAt(int index) const {
|
| + TabRestoreService::Entries entries = tab_restore_service_->entries();
|
| + int count = 0;
|
| + TabRestoreService::Entry* entry;
|
| + for (TabRestoreService::Entries::const_iterator it = entries.begin();
|
| + it != entries.end(); ++it) {
|
| + if (count == index) {
|
| + entry = *it;
|
| + return entry;
|
| + }
|
| + count++;
|
| + }
|
| + return NULL;
|
| +}
|
| +
|
| +string16 RecentlyClosedTabsMenuModel::GetLabelAt(int index) const {
|
| + int i = index;
|
| + TabRestoreService::Entry* entry = GetNavigationEntryAt(i);
|
| + // Return the entry title, escaping any '&' characters and eliding it if it's
|
| + // super long.
|
| + string16 menu_text;
|
| + if (entry->type == TabRestoreService::TAB) {
|
| + const TabRestoreService::Tab tab
|
| + = *static_cast<TabRestoreService::Tab*>(entry);
|
| + const TabNavigation& current_navigation =
|
| + tab.navigations.at(tab.current_navigation_index);
|
| + menu_text = current_navigation.title();
|
| + } else {
|
| + const TabRestoreService::Window window
|
| + = *static_cast<TabRestoreService::Window*>(entry);
|
| + std::string text = base::IntToString(window.tabs.size());
|
| + text = text + " Tabs";
|
| + menu_text = UTF8ToUTF16(text);
|
| + }
|
| + menu_text =
|
| + ui::ElideText(menu_text, gfx::Font(), kMaxWidth, ui::ELIDE_AT_END);
|
| +
|
| +#if !defined(OS_MACOSX)
|
| + for (size_t i = menu_text.find('&'); i != string16::npos;
|
| + i = menu_text.find('&', i + 2)) {
|
| + menu_text.insert(i, 1, '&');
|
| + }
|
| +#endif
|
| +
|
| + return menu_text;
|
| +}
|
| +
|
| +bool RecentlyClosedTabsMenuModel::IsItemDynamicAt(int index) const {
|
| + return false;
|
| +}
|
| +
|
| +bool RecentlyClosedTabsMenuModel::GetAcceleratorAt(
|
| + int index,
|
| + ui::Accelerator* accelerator) const {
|
| + return false;
|
| +}
|
| +
|
| +bool RecentlyClosedTabsMenuModel::IsItemCheckedAt(int index) const {
|
| + return false;
|
| +}
|
| +
|
| +int RecentlyClosedTabsMenuModel::GetGroupIdAt(int index) const {
|
| + return false;
|
| +}
|
| +
|
| +NavigationEntry* nav_entry = NULL;
|
| +bool RecentlyClosedTabsMenuModel::GetIconAt(int index, gfx::ImageSkia* icon) {
|
| + TabRestoreService::Entry* entry = GetNavigationEntryAt(index);
|
| + if (entry->type == TabRestoreService::WINDOW) {
|
| + *icon = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
|
| + IDR_HISTORY_FAVICON);
|
| + } else {
|
| + const TabRestoreService::Tab tab
|
| + = *static_cast<TabRestoreService::Tab*>(entry);
|
| + const TabNavigation& current_navigation =
|
| + tab.navigations.at(tab.current_navigation_index);
|
| + nav_entry = current_navigation.ToNavigationEntry(index,
|
| + browser_->profile());
|
| + *icon = nav_entry->GetFavicon().bitmap;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +ui::ButtonMenuItemModel* RecentlyClosedTabsMenuModel::GetButtonMenuItemAt(
|
| + int index) const {
|
| + return NULL;
|
| +}
|
| +
|
| +bool RecentlyClosedTabsMenuModel::IsEnabledAt(int index) const {
|
| + return true;
|
| +}
|
| +
|
| +ui::MenuModel* RecentlyClosedTabsMenuModel::GetSubmenuModelAt(int index) const {
|
| + return NULL;
|
| +}
|
| +
|
| +void RecentlyClosedTabsMenuModel::HighlightChangedTo(int index) {
|
| +}
|
| +
|
| +void RecentlyClosedTabsMenuModel::ActivatedAt(int index) {
|
| + ActivatedAt(index, 0);
|
| +}
|
| +
|
| +void RecentlyClosedTabsMenuModel::ActivatedAt(int index, int event_flags) {
|
| + TabRestoreService::Entry* entry = GetNavigationEntryAt(index);
|
| + TabRestoreServiceDelegate* delegate =
|
| + TabRestoreServiceDelegate::FindDelegateForController(
|
| + &browser_->GetSelectedTabContentsWrapper()->
|
| + web_contents()->GetController(), NULL);
|
| + if (!delegate)
|
| + return;
|
| + tab_restore_service_->RestoreEntryById(delegate,
|
| + entry->id,
|
| + NEW_FOREGROUND_TAB);
|
| +}
|
| +
|
| +void RecentlyClosedTabsMenuModel::MenuWillShow() {
|
| +}
|
| +
|
| +int RecentlyClosedTabsMenuModel::GetRecentlyClosedItemCount() const {
|
| + TabRestoreService::Entries entries = tab_restore_service_->entries();
|
| + return entries.size();
|
| +}
|
| +
|
| +void RecentlyClosedTabsMenuModel::SetMenuModelDelegate(
|
| + ui::MenuModelDelegate* menu_model_delegate) {
|
| + menu_model_delegate_ = menu_model_delegate;
|
| +}
|
|
|