system_menu_model_delegate.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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/views/frame/system_menu_model_delegate.h"
6
7#include "base/prefs/pref_service.h"
8#include "chrome/app/chrome_command_ids.h"
9#include "chrome/browser/command_updater.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/sessions/tab_restore_service.h"
12#include "chrome/browser/sessions/tab_restore_service_factory.h"
13#include "chrome/browser/ui/browser_commands.h"
14#include "chrome/common/pref_names.h"
15#include "chrome/grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17
18SystemMenuModelDelegate::SystemMenuModelDelegate(
19    ui::AcceleratorProvider* provider,
20    Browser* browser)
21    : provider_(provider),
22      browser_(browser) {
23}
24
25SystemMenuModelDelegate::~SystemMenuModelDelegate() {
26}
27
28bool SystemMenuModelDelegate::IsCommandIdChecked(int command_id) const {
29  switch (command_id) {
30    case IDC_USE_SYSTEM_TITLE_BAR: {
31      PrefService* prefs = browser_->profile()->GetPrefs();
32      return !prefs->GetBoolean(prefs::kUseCustomChromeFrame);
33    }
34    default:
35      return false;
36  }
37}
38
39bool SystemMenuModelDelegate::IsCommandIdEnabled(int command_id) const {
40  if (!chrome::IsCommandEnabled(browser_, command_id))
41    return false;
42
43  if (command_id != IDC_RESTORE_TAB)
44    return true;
45
46  // chrome::IsCommandEnabled(IDC_RESTORE_TAB) returns true if TabRestoreService
47  // hasn't been loaded yet. Return false if this is the case as we don't have
48  // a good way to dynamically update the menu when TabRestoreService finishes
49  // loading.
50  // TODO(sky): add a way to update menu.
51  TabRestoreService* trs =
52      TabRestoreServiceFactory::GetForProfile(browser_->profile());
53  if (!trs->IsLoaded()) {
54    trs->LoadTabsFromLastSession();
55    return false;
56  }
57  return true;
58}
59
60bool SystemMenuModelDelegate::GetAcceleratorForCommandId(int command_id,
61                                             ui::Accelerator* accelerator) {
62  return provider_->GetAcceleratorForCommandId(command_id, accelerator);
63}
64
65bool SystemMenuModelDelegate::IsItemForCommandIdDynamic(int command_id) const {
66  return command_id == IDC_RESTORE_TAB;
67}
68
69base::string16 SystemMenuModelDelegate::GetLabelForCommandId(
70    int command_id) const {
71  DCHECK_EQ(command_id, IDC_RESTORE_TAB);
72
73  int string_id = IDS_RESTORE_TAB;
74  if (IsCommandIdEnabled(command_id)) {
75    TabRestoreService* trs =
76        TabRestoreServiceFactory::GetForProfile(browser_->profile());
77    trs->LoadTabsFromLastSession();
78    if (trs && !trs->entries().empty() &&
79        trs->entries().front()->type == TabRestoreService::WINDOW)
80      string_id = IDS_RESTORE_WINDOW;
81  }
82  return l10n_util::GetStringUTF16(string_id);
83}
84
85void SystemMenuModelDelegate::ExecuteCommand(int command_id, int event_flags) {
86  chrome::ExecuteCommand(browser_, command_id);
87}
88