blob: 97d269ccb2ef88685a77f7b38a0ebcc9019da1c3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
// Copyright (c) 2010 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 "chrome/browser/notifications/notification_options_menu_model.h"
#include "app/l10n_util.h"
#include "base/compiler_specific.h"
#include "base/logging.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/extensions/extensions_service.h"
#include "chrome/browser/notifications/desktop_notification_service.h"
#include "chrome/browser/notifications/notifications_prefs_cache.h"
#include "chrome/browser/profile.h"
#include "chrome/common/content_settings_types.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/url_constants.h"
#include "grit/generated_resources.h"
// Menu commands
const int kTogglePermissionCommand = 0;
const int kToggleExtensionCommand = 1;
const int kOpenContentSettingsCommand = 2;
NotificationOptionsMenuModel::NotificationOptionsMenuModel(Balloon* balloon)
: ALLOW_THIS_IN_INITIALIZER_LIST(menus::SimpleMenuModel(this)),
balloon_(balloon) {
const Notification& notification = balloon->notification();
const GURL& origin = notification.origin_url();
if (origin.SchemeIs(chrome::kExtensionScheme)) {
const string16 disable_label = l10n_util::GetStringUTF16(
IDS_EXTENSIONS_DISABLE);
AddItem(kToggleExtensionCommand, disable_label);
} else {
const string16 disable_label = l10n_util::GetStringFUTF16(
IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
notification.display_source());
AddItem(kTogglePermissionCommand, disable_label);
}
const string16 settings_label = l10n_util::GetStringUTF16(
IDS_NOTIFICATIONS_SETTINGS_BUTTON);
AddItem(kOpenContentSettingsCommand, settings_label);
}
NotificationOptionsMenuModel::~NotificationOptionsMenuModel() {
}
bool NotificationOptionsMenuModel::IsLabelForCommandIdDynamic(int command_id)
const {
return command_id == kTogglePermissionCommand ||
command_id == kToggleExtensionCommand;
}
string16 NotificationOptionsMenuModel::GetLabelForCommandId(int command_id)
const {
// TODO(tfarina,johnnyg): Removed this code if we decide to close
// notifications after permissions are revoked.
if (command_id == kTogglePermissionCommand ||
command_id == kToggleExtensionCommand) {
const Notification& notification = balloon_->notification();
const GURL& origin = notification.origin_url();
DesktopNotificationService* service =
balloon_->profile()->GetDesktopNotificationService();
if (origin.SchemeIs(chrome::kExtensionScheme)) {
ExtensionsService* ext_service =
balloon_->profile()->GetExtensionsService();
const Extension* extension = ext_service->GetExtensionByURL(origin);
if (extension) {
ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
const std::string& id = extension->id();
if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE);
else
return l10n_util::GetStringUTF16(IDS_EXTENSIONS_ENABLE);
}
} else {
if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW) {
return l10n_util::GetStringFUTF16(
IDS_NOTIFICATION_BALLOON_REVOKE_MESSAGE,
notification.display_source());
} else {
return l10n_util::GetStringFUTF16(
IDS_NOTIFICATION_BALLOON_ENABLE_MESSAGE,
notification.display_source());
}
}
} else if (command_id == kOpenContentSettingsCommand) {
return l10n_util::GetStringUTF16(IDS_NOTIFICATIONS_SETTINGS_BUTTON);
}
return string16();
}
bool NotificationOptionsMenuModel::IsCommandIdChecked(int /* command_id */)
const {
// Nothing in the menu is checked.
return false;
}
bool NotificationOptionsMenuModel::IsCommandIdEnabled(int /* command_id */)
const {
// All the menu options are always enabled.
return true;
}
bool NotificationOptionsMenuModel::GetAcceleratorForCommandId(
int /* command_id */, menus::Accelerator* /* accelerator */) {
// Currently no accelerators.
return false;
}
void NotificationOptionsMenuModel::ExecuteCommand(int command_id) {
DesktopNotificationService* service =
balloon_->profile()->GetDesktopNotificationService();
ExtensionsService* ext_service =
balloon_->profile()->GetExtensionsService();
const GURL& origin = balloon_->notification().origin_url();
switch (command_id) {
case kTogglePermissionCommand:
if (service->GetContentSetting(origin) == CONTENT_SETTING_ALLOW)
service->DenyPermission(origin);
else
service->GrantPermission(origin);
break;
case kToggleExtensionCommand: {
const Extension* extension = ext_service->GetExtensionByURL(origin);
if (extension) {
ExtensionPrefs* extension_prefs = ext_service->extension_prefs();
const std::string& id = extension->id();
if (extension_prefs->GetExtensionState(id) == Extension::ENABLED)
ext_service->DisableExtension(id);
else
ext_service->EnableExtension(id);
}
break;
}
case kOpenContentSettingsCommand: {
Browser* browser = BrowserList::GetLastActive();
if (browser)
static_cast<TabContentsDelegate*>(browser)->ShowContentSettingsWindow(
CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
break;
}
default:
NOTREACHED();
break;
}
}
|