blob: 3ef80c4ebac44001b74213e9b381f4fc47d4af3b (
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
|
// 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 "chrome/browser/extensions/extension_warning_badge_service.h"
#include "base/stl_util.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/ui/global_error/global_error.h"
#include "chrome/browser/ui/global_error/global_error_service.h"
#include "chrome/browser/ui/global_error/global_error_service_factory.h"
#include "chrome/browser/ui/browser_commands.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace extensions {
namespace {
// Non-modal GlobalError implementation that warns the user if extensions
// created warnings or errors. If the user clicks on the wrench menu, the user
// is redirected to chrome://extensions to inspect the errors.
class ErrorBadge : public GlobalError {
public:
explicit ErrorBadge(ExtensionWarningBadgeService* badge_service);
virtual ~ErrorBadge();
// Implementation for GlobalError:
virtual bool HasMenuItem() OVERRIDE;
virtual int MenuItemCommandID() OVERRIDE;
virtual string16 MenuItemLabel() OVERRIDE;
virtual void ExecuteMenuItem(Browser* browser) OVERRIDE;
virtual bool HasBubbleView() OVERRIDE;
virtual bool HasShownBubbleView() OVERRIDE;
virtual void ShowBubbleView(Browser* browser) OVERRIDE;
virtual GlobalErrorBubbleViewBase* GetBubbleView() OVERRIDE;
static int GetMenuItemCommandID();
private:
ExtensionWarningBadgeService* badge_service_;
DISALLOW_COPY_AND_ASSIGN(ErrorBadge);
};
ErrorBadge::ErrorBadge(ExtensionWarningBadgeService* badge_service)
: badge_service_(badge_service) {}
ErrorBadge::~ErrorBadge() {}
bool ErrorBadge::HasMenuItem() {
return true;
}
int ErrorBadge::MenuItemCommandID() {
return GetMenuItemCommandID();
}
string16 ErrorBadge::MenuItemLabel() {
return l10n_util::GetStringUTF16(IDS_EXTENSION_WARNINGS_WRENCH_MENU_ITEM);
}
void ErrorBadge::ExecuteMenuItem(Browser* browser) {
// Suppress all current warnings in the extension service from triggering
// a badge on the wrench menu in the future of this session.
badge_service_->SuppressCurrentWarnings();
chrome::ExecuteCommand(browser, IDC_MANAGE_EXTENSIONS);
}
bool ErrorBadge::HasBubbleView() { return false; }
bool ErrorBadge::HasShownBubbleView() { return false; }
void ErrorBadge::ShowBubbleView(Browser* browser) { NOTREACHED(); }
GlobalErrorBubbleViewBase* ErrorBadge::GetBubbleView() {
return NULL;
}
// static
int ErrorBadge::GetMenuItemCommandID() {
return IDC_EXTENSION_ERRORS;
}
} // namespace
ExtensionWarningBadgeService::ExtensionWarningBadgeService(Profile* profile)
: profile_(profile) {
DCHECK(CalledOnValidThread());
}
ExtensionWarningBadgeService::~ExtensionWarningBadgeService() {}
void ExtensionWarningBadgeService::SuppressCurrentWarnings() {
DCHECK(CalledOnValidThread());
size_t old_size = suppressed_warnings_.size();
const ExtensionWarningSet& warnings = GetCurrentWarnings();
suppressed_warnings_.insert(warnings.begin(), warnings.end());
if (old_size != suppressed_warnings_.size())
UpdateBadgeStatus();
}
const ExtensionWarningSet&
ExtensionWarningBadgeService::GetCurrentWarnings() const {
return ExtensionSystem::Get(profile_)->warning_service()->warnings();
}
void ExtensionWarningBadgeService::ExtensionWarningsChanged() {
DCHECK(CalledOnValidThread());
UpdateBadgeStatus();
}
void ExtensionWarningBadgeService::UpdateBadgeStatus() {
const std::set<ExtensionWarning>& warnings = GetCurrentWarnings();
bool non_suppressed_warnings_exist = false;
for (std::set<ExtensionWarning>::const_iterator i = warnings.begin();
i != warnings.end(); ++i) {
if (!ContainsKey(suppressed_warnings_, *i)) {
non_suppressed_warnings_exist = true;
break;
}
}
ShowBadge(non_suppressed_warnings_exist);
}
void ExtensionWarningBadgeService::ShowBadge(bool show) {
GlobalErrorService* service =
GlobalErrorServiceFactory::GetForProfile(profile_);
GlobalError* error = service->GetGlobalErrorByMenuItemCommandID(
ErrorBadge::GetMenuItemCommandID());
// Activate or hide the warning badge in case the current state is incorrect.
if (error && !show) {
service->RemoveGlobalError(error);
delete error;
} else if (!error && show) {
service->AddGlobalError(new ErrorBadge(this));
}
}
} // namespace extensions
|