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 "build/build_config.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_factory.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/browser_window.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "components/sessions/core/tab_restore_service.h"
16 #include "ui/base/l10n/l10n_util.h"
17 
18 #if (defined(OS_LINUX) || defined(OS_BSD)) && !defined(OS_CHROMEOS)
19 #include "chrome/common/pref_names.h"
20 #include "components/prefs/pref_service.h"
21 #endif
22 
SystemMenuModelDelegate(ui::AcceleratorProvider * provider,Browser * browser)23 SystemMenuModelDelegate::SystemMenuModelDelegate(
24     ui::AcceleratorProvider* provider,
25     Browser* browser)
26     : provider_(provider),
27       browser_(browser) {
28 }
29 
~SystemMenuModelDelegate()30 SystemMenuModelDelegate::~SystemMenuModelDelegate() {}
31 
IsCommandIdChecked(int command_id) const32 bool SystemMenuModelDelegate::IsCommandIdChecked(int command_id) const {
33 #if (defined(OS_LINUX) || defined(OS_BSD)) && !defined(OS_CHROMEOS)
34   if (command_id == IDC_USE_SYSTEM_TITLE_BAR) {
35     PrefService* prefs = browser_->profile()->GetPrefs();
36     return !prefs->GetBoolean(prefs::kUseCustomChromeFrame);
37   }
38 #endif
39   return false;
40 }
41 
IsCommandIdEnabled(int command_id) const42 bool SystemMenuModelDelegate::IsCommandIdEnabled(int command_id) const {
43   return chrome::IsCommandEnabled(browser_, command_id);
44 }
45 
IsCommandIdVisible(int command_id) const46 bool SystemMenuModelDelegate::IsCommandIdVisible(int command_id) const {
47 #if (defined(OS_LINUX) && !defined(OS_CHROMEOS)) || defined(OS_BSD)
48   bool is_maximized = browser_->window()->IsMaximized();
49   switch (command_id) {
50     case IDC_MAXIMIZE_WINDOW:
51       return !is_maximized;
52     case IDC_RESTORE_WINDOW:
53       return is_maximized;
54   }
55 #endif
56   return true;
57 }
58 
GetAcceleratorForCommandId(int command_id,ui::Accelerator * accelerator) const59 bool SystemMenuModelDelegate::GetAcceleratorForCommandId(
60     int command_id,
61     ui::Accelerator* accelerator) const {
62   return provider_->GetAcceleratorForCommandId(command_id, accelerator);
63 }
64 
IsItemForCommandIdDynamic(int command_id) const65 bool SystemMenuModelDelegate::IsItemForCommandIdDynamic(int command_id) const {
66   return command_id == IDC_RESTORE_TAB;
67 }
68 
GetLabelForCommandId(int command_id) const69 base::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     sessions::TabRestoreService* trs =
76         TabRestoreServiceFactory::GetForProfile(browser_->profile());
77     DCHECK(trs);
78     trs->LoadTabsFromLastSession();
79     if (!trs->entries().empty() &&
80         trs->entries().front()->type == sessions::TabRestoreService::WINDOW)
81       string_id = IDS_RESTORE_WINDOW;
82   }
83   return l10n_util::GetStringUTF16(string_id);
84 }
85 
ExecuteCommand(int command_id,int event_flags)86 void SystemMenuModelDelegate::ExecuteCommand(int command_id, int event_flags) {
87   chrome::ExecuteCommand(browser_, command_id);
88 }
89