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.
5 #include "extensions/browser/warning_service.h"
7 #include "content/public/browser/browser_thread.h"
8 #include "extensions/browser/extension_registry.h"
9 #include "extensions/browser/extensions_browser_client.h"
10 #include "extensions/browser/warning_service_factory.h"
11 #include "extensions/common/extension_set.h"
13 using content::BrowserThread
;
15 namespace extensions
{
17 WarningService::WarningService(content::BrowserContext
* browser_context
)
18 : browser_context_(browser_context
), extension_registry_observer_(this) {
19 DCHECK(CalledOnValidThread());
20 if (browser_context_
) {
21 extension_registry_observer_
.Add(ExtensionRegistry::Get(
22 ExtensionsBrowserClient::Get()->GetOriginalContext(browser_context_
)));
26 WarningService::~WarningService() {}
29 WarningService
* WarningService::Get(content::BrowserContext
* browser_context
) {
30 return WarningServiceFactory::GetForBrowserContext(browser_context
);
33 void WarningService::ClearWarnings(
34 const std::set
<Warning::WarningType
>& types
) {
35 DCHECK(CalledOnValidThread());
36 ExtensionIdSet affected_extensions
;
37 for (WarningSet::iterator i
= warnings_
.begin();
38 i
!= warnings_
.end();) {
39 if (types
.find(i
->warning_type()) != types
.end()) {
40 affected_extensions
.insert(i
->extension_id());
47 if (!affected_extensions
.empty())
48 NotifyWarningsChanged(affected_extensions
);
51 std::set
<Warning::WarningType
> WarningService::
52 GetWarningTypesAffectingExtension(const std::string
& extension_id
) const {
53 DCHECK(CalledOnValidThread());
54 std::set
<Warning::WarningType
> result
;
55 for (WarningSet::const_iterator i
= warnings_
.begin();
56 i
!= warnings_
.end(); ++i
) {
57 if (i
->extension_id() == extension_id
)
58 result
.insert(i
->warning_type());
63 std::vector
<std::string
> WarningService::GetWarningMessagesForExtension(
64 const std::string
& extension_id
) const {
65 DCHECK(CalledOnValidThread());
66 std::vector
<std::string
> result
;
68 const ExtensionSet
& extension_set
=
69 ExtensionRegistry::Get(browser_context_
)->enabled_extensions();
71 for (WarningSet::const_iterator i
= warnings_
.begin();
72 i
!= warnings_
.end(); ++i
) {
73 if (i
->extension_id() == extension_id
)
74 result
.push_back(i
->GetLocalizedMessage(&extension_set
));
79 void WarningService::AddWarnings(const WarningSet
& warnings
) {
80 DCHECK(CalledOnValidThread());
82 ExtensionIdSet affected_extensions
;
83 for (const Warning
& warning
: warnings
) {
84 if (warnings_
.insert(warning
).second
)
85 affected_extensions
.insert(warning
.extension_id());
87 if (!affected_extensions
.empty())
88 NotifyWarningsChanged(affected_extensions
);
92 void WarningService::NotifyWarningsOnUI(
94 const WarningSet
& warnings
) {
95 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
96 content::BrowserContext
* browser_context
=
97 reinterpret_cast<content::BrowserContext
*>(profile_id
);
99 if (!browser_context
||
100 !ExtensionsBrowserClient::Get() ||
101 !ExtensionsBrowserClient::Get()->IsValidContext(browser_context
)) {
105 WarningService
* warning_service
= WarningService::Get(browser_context
);
107 warning_service
->AddWarnings(warnings
);
110 void WarningService::AddObserver(Observer
* observer
) {
111 observer_list_
.AddObserver(observer
);
114 void WarningService::RemoveObserver(Observer
* observer
) {
115 observer_list_
.RemoveObserver(observer
);
118 void WarningService::NotifyWarningsChanged(
119 const ExtensionIdSet
& affected_extensions
) {
120 FOR_EACH_OBSERVER(Observer
, observer_list_
,
121 ExtensionWarningsChanged(affected_extensions
));
124 void WarningService::OnExtensionUnloaded(
125 content::BrowserContext
* browser_context
,
126 const Extension
* extension
,
127 UnloadedExtensionInfo::Reason reason
) {
128 // Unloading one extension might have solved the problems of others.
129 // Therefore, we clear warnings of this type for all extensions.
130 std::set
<Warning::WarningType
> warning_types
=
131 GetWarningTypesAffectingExtension(extension
->id());
132 ClearWarnings(warning_types
);
135 } // namespace extensions