This source file includes following definitions.
- SortSessionsByRecency
- SortTabsByRecency
- IsTabModelCommandId
- IsWindowModelCommandId
- IsDeviceNameCommandId
- TabVectorIndexToCommandId
- WindowVectorIndexToCommandId
- CommandIdToWindowVectorIndex
- weak_ptr_factory_
- IsCommandIdChecked
- IsCommandIdEnabled
- GetAcceleratorForCommandId
- ExecuteCommand
- GetLabelFontListAt
- GetMaxWidthForItemAtIndex
- GetURLAndTitleForItemAtIndex
- Build
- BuildLocalEntries
- BuildTabsFromOtherDevices
- BuildLocalTabItem
- BuildLocalWindowItem
- BuildOtherDevicesTabItem
- AddDeviceFavicon
- AddTabFavicon
- OnFaviconDataAvailable
- CommandIdToTabVectorIndex
- ClearLocalEntries
- GetOpenTabsUIDelegate
- TabRestoreServiceChanged
- TabRestoreServiceDestroyed
#include "chrome/browser/ui/toolbar/recent_tabs_sub_menu_model.h"
#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/sessions/session_restore.h"
#include "chrome/browser/sessions/tab_restore_service.h"
#include "chrome/browser/sessions/tab_restore_service_delegate.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/browser/sync/glue/synced_session.h"
#include "chrome/browser/sync/open_tabs_ui_delegate.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/wrench_menu_model.h"
#include "chrome/common/favicon/favicon_types.h"
#include "chrome/common/pref_names.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "grit/ui_resources.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/favicon_size.h"
#if defined(USE_ASH)
#include "ash/accelerators/accelerator_table.h"
#endif
namespace {
const int kFirstLocalTabCommandId = WrenchMenuModel::kMinRecentTabsCommandId;
const int kFirstLocalWindowCommandId = 1031;
const int kFirstOtherDevicesTabCommandId = 1051;
const int kMinDeviceNameCommandId = 1100;
const int kMaxDeviceNameCommandId = 1110;
const int kMaxLocalEntries = 8;
bool SortSessionsByRecency(const browser_sync::SyncedSession* s1,
const browser_sync::SyncedSession* s2) {
return s1->modified_time > s2->modified_time;
}
bool SortTabsByRecency(const SessionTab* t1, const SessionTab* t2) {
return t1->timestamp > t2->timestamp;
}
bool IsTabModelCommandId(int command_id) {
return ((command_id >= kFirstLocalTabCommandId &&
command_id < kFirstLocalWindowCommandId) ||
(command_id >= kFirstOtherDevicesTabCommandId &&
command_id < kMinDeviceNameCommandId));
}
bool IsWindowModelCommandId(int command_id) {
return command_id >= kFirstLocalWindowCommandId &&
command_id < kFirstOtherDevicesTabCommandId;
}
bool IsDeviceNameCommandId(int command_id) {
return command_id >= kMinDeviceNameCommandId &&
command_id <= kMaxDeviceNameCommandId;
}
int TabVectorIndexToCommandId(int tab_vector_index, int first_command_id) {
int command_id = tab_vector_index + first_command_id;
DCHECK(IsTabModelCommandId(command_id));
return command_id;
}
int WindowVectorIndexToCommandId(int window_vector_index) {
int command_id = window_vector_index + kFirstLocalWindowCommandId;
DCHECK(IsWindowModelCommandId(command_id));
return command_id;
}
int CommandIdToWindowVectorIndex(int command_id) {
DCHECK(IsWindowModelCommandId(command_id));
return command_id - kFirstLocalWindowCommandId;
}
}
enum RecentTabAction {
LOCAL_SESSION_TAB = 0,
OTHER_DEVICE_TAB,
RESTORE_WINDOW,
SHOW_MORE,
LIMIT_RECENT_TAB_ACTION
};
struct RecentTabsSubMenuModel::TabNavigationItem {
TabNavigationItem() : tab_id(-1) {}
TabNavigationItem(const std::string& session_tag,
const SessionID::id_type& tab_id,
const base::string16& title,
const GURL& url)
: session_tag(session_tag),
tab_id(tab_id),
title(title),
url(url) {}
bool operator<(const TabNavigationItem& other) const {
return url < other.url;
}
std::string session_tag;
SessionID::id_type tab_id;
base::string16 title;
GURL url;
};
const int RecentTabsSubMenuModel::kRecentlyClosedHeaderCommandId = 1120;
const int RecentTabsSubMenuModel::kDisabledRecentlyClosedHeaderCommandId = 1121;
RecentTabsSubMenuModel::RecentTabsSubMenuModel(
ui::AcceleratorProvider* accelerator_provider,
Browser* browser,
browser_sync::OpenTabsUIDelegate* open_tabs_delegate)
: ui::SimpleMenuModel(this),
browser_(browser),
open_tabs_delegate_(open_tabs_delegate),
last_local_model_index_(-1),
default_favicon_(ui::ResourceBundle::GetSharedInstance().
GetNativeImageNamed(IDR_DEFAULT_FAVICON)),
weak_ptr_factory_(this) {
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile());
if (service) {
service->LoadTabsFromLastSession();
#if !defined(OS_MACOSX)
service->AddObserver(this);
#endif
}
Build();
#if defined(USE_ASH)
for (size_t i = 0; i < ash::kAcceleratorDataLength; ++i) {
const ash::AcceleratorData& accel_data = ash::kAcceleratorData[i];
if (accel_data.action == ash::RESTORE_TAB) {
reopen_closed_tab_accelerator_ = ui::Accelerator(accel_data.keycode,
accel_data.modifiers);
break;
}
}
#else
if (accelerator_provider) {
accelerator_provider->GetAcceleratorForCommandId(
IDC_RESTORE_TAB, &reopen_closed_tab_accelerator_);
}
#endif
}
RecentTabsSubMenuModel::~RecentTabsSubMenuModel() {
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile());
if (service)
service->RemoveObserver(this);
}
bool RecentTabsSubMenuModel::IsCommandIdChecked(int command_id) const {
return false;
}
bool RecentTabsSubMenuModel::IsCommandIdEnabled(int command_id) const {
if (command_id == kRecentlyClosedHeaderCommandId ||
command_id == kDisabledRecentlyClosedHeaderCommandId ||
command_id == IDC_RECENT_TABS_NO_DEVICE_TABS ||
IsDeviceNameCommandId(command_id)) {
return false;
}
return true;
}
bool RecentTabsSubMenuModel::GetAcceleratorForCommandId(
int command_id, ui::Accelerator* accelerator) {
int index_in_menu = GetIndexOfCommandId(command_id);
int header_index = GetIndexOfCommandId(kRecentlyClosedHeaderCommandId);
if ((command_id == kDisabledRecentlyClosedHeaderCommandId ||
(header_index != -1 && index_in_menu == header_index + 1)) &&
reopen_closed_tab_accelerator_.key_code() != ui::VKEY_UNKNOWN) {
*accelerator = reopen_closed_tab_accelerator_;
return true;
}
return false;
}
void RecentTabsSubMenuModel::ExecuteCommand(int command_id, int event_flags) {
if (command_id == IDC_SHOW_HISTORY) {
UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu", SHOW_MORE,
LIMIT_RECENT_TAB_ACTION);
chrome::ExecuteCommandWithDisposition(browser_, IDC_SHOW_HISTORY,
ui::DispositionFromEventFlags(event_flags));
return;
}
DCHECK_NE(IDC_RECENT_TABS_NO_DEVICE_TABS, command_id);
DCHECK(!IsDeviceNameCommandId(command_id));
WindowOpenDisposition disposition =
ui::DispositionFromEventFlags(event_flags);
if (disposition == CURRENT_TAB)
disposition = NEW_FOREGROUND_TAB;
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile());
TabRestoreServiceDelegate* delegate =
TabRestoreServiceDelegate::FindDelegateForWebContents(
browser_->tab_strip_model()->GetActiveWebContents());
if (IsTabModelCommandId(command_id)) {
TabNavigationItems* tab_items = NULL;
int tab_items_idx = CommandIdToTabVectorIndex(command_id, &tab_items);
const TabNavigationItem& item = (*tab_items)[tab_items_idx];
DCHECK(item.tab_id > -1 && item.url.is_valid());
if (item.session_tag.empty()) {
if (service && delegate) {
UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu",
LOCAL_SESSION_TAB, LIMIT_RECENT_TAB_ACTION);
service->RestoreEntryById(delegate, item.tab_id,
browser_->host_desktop_type(), disposition);
}
} else {
browser_sync::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate();
if (!open_tabs)
return;
const SessionTab* tab;
if (!open_tabs->GetForeignTab(item.session_tag, item.tab_id, &tab))
return;
if (tab->navigations.empty())
return;
UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu",
OTHER_DEVICE_TAB, LIMIT_RECENT_TAB_ACTION);
SessionRestore::RestoreForeignSessionTab(
browser_->tab_strip_model()->GetActiveWebContents(),
*tab, disposition);
}
} else {
DCHECK(IsWindowModelCommandId(command_id));
if (service && delegate) {
int window_items_idx = CommandIdToWindowVectorIndex(command_id);
DCHECK(window_items_idx >= 0 &&
window_items_idx < static_cast<int>(local_window_items_.size()));
UMA_HISTOGRAM_ENUMERATION("WrenchMenu.RecentTabsSubMenu", RESTORE_WINDOW,
LIMIT_RECENT_TAB_ACTION);
service->RestoreEntryById(delegate, local_window_items_[window_items_idx],
browser_->host_desktop_type(), disposition);
}
}
}
const gfx::FontList* RecentTabsSubMenuModel::GetLabelFontListAt(
int index) const {
int command_id = GetCommandIdAt(index);
if (command_id == kRecentlyClosedHeaderCommandId ||
IsDeviceNameCommandId(command_id)) {
return &ui::ResourceBundle::GetSharedInstance().GetFontList(
ui::ResourceBundle::BoldFont);
}
return NULL;
}
int RecentTabsSubMenuModel::GetMaxWidthForItemAtIndex(int item_index) const {
int command_id = GetCommandIdAt(item_index);
if (command_id == IDC_RECENT_TABS_NO_DEVICE_TABS ||
command_id == kRecentlyClosedHeaderCommandId ||
command_id == kDisabledRecentlyClosedHeaderCommandId) {
return -1;
}
return 320;
}
bool RecentTabsSubMenuModel::GetURLAndTitleForItemAtIndex(
int index,
std::string* url,
base::string16* title) {
int command_id = GetCommandIdAt(index);
if (IsTabModelCommandId(command_id)) {
TabNavigationItems* tab_items = NULL;
int tab_items_idx = CommandIdToTabVectorIndex(command_id, &tab_items);
const TabNavigationItem& item = (*tab_items)[tab_items_idx];
*url = item.url.possibly_invalid_spec();
*title = item.title;
return true;
}
return false;
}
void RecentTabsSubMenuModel::Build() {
BuildLocalEntries();
BuildTabsFromOtherDevices();
}
void RecentTabsSubMenuModel::BuildLocalEntries() {
DCHECK_EQ(last_local_model_index_, -1);
TabRestoreService* service =
TabRestoreServiceFactory::GetForProfile(browser_->profile());
if (!service || service->entries().size() == 0) {
InsertItemWithStringIdAt(++last_local_model_index_,
kDisabledRecentlyClosedHeaderCommandId,
IDS_NEW_TAB_RECENTLY_CLOSED);
} else {
InsertItemWithStringIdAt(++last_local_model_index_,
kRecentlyClosedHeaderCommandId,
IDS_NEW_TAB_RECENTLY_CLOSED);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
SetIcon(last_local_model_index_,
rb.GetNativeImageNamed(IDR_RECENTLY_CLOSED_WINDOW));
int added_count = 0;
TabRestoreService::Entries entries = service->entries();
for (TabRestoreService::Entries::const_iterator it = entries.begin();
it != entries.end() && added_count < kMaxLocalEntries; ++it) {
TabRestoreService::Entry* entry = *it;
if (entry->type == TabRestoreService::TAB) {
TabRestoreService::Tab* tab =
static_cast<TabRestoreService::Tab*>(entry);
const sessions::SerializedNavigationEntry& current_navigation =
tab->navigations.at(tab->current_navigation_index);
BuildLocalTabItem(
entry->id,
current_navigation.title(),
current_navigation.virtual_url(),
++last_local_model_index_);
} else {
DCHECK_EQ(entry->type, TabRestoreService::WINDOW);
BuildLocalWindowItem(
entry->id,
static_cast<TabRestoreService::Window*>(entry)->tabs.size(),
++last_local_model_index_);
}
++added_count;
}
}
DCHECK_GE(last_local_model_index_, 0);
}
void RecentTabsSubMenuModel::BuildTabsFromOtherDevices() {
browser_sync::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate();
std::vector<const browser_sync::SyncedSession*> sessions;
if (!open_tabs || !open_tabs->GetAllForeignSessions(&sessions)) {
AddSeparator(ui::NORMAL_SEPARATOR);
AddItemWithStringId(IDC_RECENT_TABS_NO_DEVICE_TABS,
IDS_RECENT_TABS_NO_DEVICE_TABS);
return;
}
std::sort(sessions.begin(), sessions.end(), SortSessionsByRecency);
const size_t kMaxSessionsToShow = 3;
size_t num_sessions_added = 0;
for (size_t i = 0;
i < sessions.size() && num_sessions_added < kMaxSessionsToShow; ++i) {
const browser_sync::SyncedSession* session = sessions[i];
const std::string& session_tag = session->session_tag;
std::vector<const SessionWindow*> windows;
if (!open_tabs->GetForeignSession(session_tag, &windows) ||
windows.empty()) {
continue;
}
std::vector<const SessionTab*> tabs_in_session;
for (size_t j = 0; j < windows.size(); ++j) {
const SessionWindow* window = windows[j];
for (size_t t = 0; t < window->tabs.size(); ++t) {
const SessionTab* tab = window->tabs[t];
if (tab->navigations.empty())
continue;
const sessions::SerializedNavigationEntry& current_navigation =
tab->navigations.at(tab->normalized_navigation_index());
if (chrome::IsNTPURL(current_navigation.virtual_url(),
browser_->profile())) {
continue;
}
tabs_in_session.push_back(tab);
}
}
if (tabs_in_session.empty())
continue;
std::sort(tabs_in_session.begin(), tabs_in_session.end(),
SortTabsByRecency);
DCHECK(!session->session_name.empty());
AddSeparator(ui::NORMAL_SEPARATOR);
int command_id = kMinDeviceNameCommandId + i;
DCHECK_LE(command_id, kMaxDeviceNameCommandId);
AddItem(command_id, base::UTF8ToUTF16(session->session_name));
AddDeviceFavicon(GetItemCount() - 1, session->device_type);
const size_t kMaxTabsPerSessionToShow = 4;
for (size_t k = 0;
k < std::min(tabs_in_session.size(), kMaxTabsPerSessionToShow);
++k) {
BuildOtherDevicesTabItem(session_tag, *tabs_in_session[k]);
}
++num_sessions_added;
}
DCHECK_GT(GetItemCount(), 0);
AddSeparator(ui::NORMAL_SEPARATOR);
AddItemWithStringId(IDC_SHOW_HISTORY, IDS_RECENT_TABS_MORE);
}
void RecentTabsSubMenuModel::BuildLocalTabItem(int session_id,
const base::string16& title,
const GURL& url,
int curr_model_index) {
TabNavigationItem item(std::string(), session_id, title, url);
int command_id = TabVectorIndexToCommandId(
local_tab_navigation_items_.size(), kFirstLocalTabCommandId);
InsertItemAt(curr_model_index, command_id,
title.empty() ? base::UTF8ToUTF16(item.url.spec()) : title);
AddTabFavicon(command_id, item.url);
local_tab_navigation_items_.push_back(item);
}
void RecentTabsSubMenuModel::BuildLocalWindowItem(
const SessionID::id_type& window_id,
int num_tabs,
int curr_model_index) {
int command_id = WindowVectorIndexToCommandId(local_window_items_.size());
if (num_tabs == 1) {
InsertItemWithStringIdAt(curr_model_index, command_id,
IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE);
} else {
InsertItemAt(curr_model_index, command_id, l10n_util::GetStringFUTF16(
IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE,
base::IntToString16(num_tabs)));
}
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
SetIcon(curr_model_index, rb.GetNativeImageNamed(IDR_RECENTLY_CLOSED_WINDOW));
local_window_items_.push_back(window_id);
}
void RecentTabsSubMenuModel::BuildOtherDevicesTabItem(
const std::string& session_tag,
const SessionTab& tab) {
const sessions::SerializedNavigationEntry& current_navigation =
tab.navigations.at(tab.normalized_navigation_index());
TabNavigationItem item(session_tag, tab.tab_id.id(),
current_navigation.title(),
current_navigation.virtual_url());
int command_id = TabVectorIndexToCommandId(
other_devices_tab_navigation_items_.size(),
kFirstOtherDevicesTabCommandId);
AddItem(command_id,
current_navigation.title().empty() ?
base::UTF8ToUTF16(item.url.spec()) : current_navigation.title());
AddTabFavicon(command_id, item.url);
other_devices_tab_navigation_items_.push_back(item);
}
void RecentTabsSubMenuModel::AddDeviceFavicon(
int index_in_menu,
browser_sync::SyncedSession::DeviceType device_type) {
int favicon_id = -1;
switch (device_type) {
case browser_sync::SyncedSession::TYPE_PHONE:
favicon_id = IDR_PHONE_FAVICON;
break;
case browser_sync::SyncedSession::TYPE_TABLET:
favicon_id = IDR_TABLET_FAVICON;
break;
case browser_sync::SyncedSession::TYPE_CHROMEOS:
case browser_sync::SyncedSession::TYPE_WIN:
case browser_sync::SyncedSession::TYPE_MACOSX:
case browser_sync::SyncedSession::TYPE_LINUX:
case browser_sync::SyncedSession::TYPE_OTHER:
case browser_sync::SyncedSession::TYPE_UNSET:
favicon_id = IDR_LAPTOP_FAVICON;
break;
};
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
SetIcon(index_in_menu, rb.GetNativeImageNamed(favicon_id));
}
void RecentTabsSubMenuModel::AddTabFavicon(int command_id, const GURL& url) {
bool is_local_tab = command_id < kFirstOtherDevicesTabCommandId;
int index_in_menu = GetIndexOfCommandId(command_id);
if (!is_local_tab) {
browser_sync::OpenTabsUIDelegate* open_tabs = GetOpenTabsUIDelegate();
scoped_refptr<base::RefCountedMemory> favicon_png;
if (open_tabs &&
open_tabs->GetSyncedFaviconForPageURL(url.spec(), &favicon_png)) {
gfx::Image image = gfx::Image::CreateFrom1xPNGBytes(favicon_png);
SetIcon(index_in_menu, image);
return;
}
}
SetIcon(index_in_menu, default_favicon_);
FaviconService* favicon_service = FaviconServiceFactory::GetForProfile(
browser_->profile(), Profile::EXPLICIT_ACCESS);
if (!favicon_service)
return;
favicon_service->GetFaviconImageForURL(
FaviconService::FaviconForURLParams(url,
chrome::FAVICON,
gfx::kFaviconSize),
base::Bind(&RecentTabsSubMenuModel::OnFaviconDataAvailable,
weak_ptr_factory_.GetWeakPtr(),
command_id),
is_local_tab ? &local_tab_cancelable_task_tracker_ :
&other_devices_tab_cancelable_task_tracker_);
}
void RecentTabsSubMenuModel::OnFaviconDataAvailable(
int command_id,
const chrome::FaviconImageResult& image_result) {
if (image_result.image.IsEmpty())
return;
int index_in_menu = GetIndexOfCommandId(command_id);
DCHECK_GT(index_in_menu, -1);
SetIcon(index_in_menu, image_result.image);
ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate();
if (menu_model_delegate)
menu_model_delegate->OnIconChanged(index_in_menu);
}
int RecentTabsSubMenuModel::CommandIdToTabVectorIndex(
int command_id,
TabNavigationItems** tab_items) {
DCHECK(IsTabModelCommandId(command_id));
if (command_id >= kFirstOtherDevicesTabCommandId) {
*tab_items = &other_devices_tab_navigation_items_;
return command_id - kFirstOtherDevicesTabCommandId;
}
*tab_items = &local_tab_navigation_items_;
return command_id - kFirstLocalTabCommandId;
}
void RecentTabsSubMenuModel::ClearLocalEntries() {
while (last_local_model_index_ >= 0)
RemoveItemAt(last_local_model_index_--);
local_tab_cancelable_task_tracker_.TryCancelAll();
local_tab_navigation_items_.clear();
local_window_items_.clear();
}
browser_sync::OpenTabsUIDelegate*
RecentTabsSubMenuModel::GetOpenTabsUIDelegate() {
if (!open_tabs_delegate_) {
ProfileSyncService* service = ProfileSyncServiceFactory::GetInstance()->
GetForProfile(browser_->profile());
if (service && service->ShouldPushChanges())
open_tabs_delegate_ = service->GetOpenTabsUIDelegate();
}
return open_tabs_delegate_;
}
void RecentTabsSubMenuModel::TabRestoreServiceChanged(
TabRestoreService* service) {
ClearLocalEntries();
BuildLocalEntries();
ui::MenuModelDelegate* menu_model_delegate = GetMenuModelDelegate();
if (menu_model_delegate)
menu_model_delegate->OnMenuStructureChanged();
}
void RecentTabsSubMenuModel::TabRestoreServiceDestroyed(
TabRestoreService* service) {
TabRestoreServiceChanged(service);
}