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 "chrome/browser/extensions/extension_warning_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/extensions/extension.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/notification_service.h"
19 using content::BrowserThread
;
21 namespace extensions
{
23 ExtensionWarningService::ExtensionWarningService(Profile
* profile
)
25 DCHECK(CalledOnValidThread());
27 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
28 content::Source
<Profile
>(profile_
->GetOriginalProfile()));
32 ExtensionWarningService::~ExtensionWarningService() {}
34 void ExtensionWarningService::ClearWarnings(
35 const std::set
<ExtensionWarning::WarningType
>& types
) {
36 DCHECK(CalledOnValidThread());
37 bool deleted_anything
= false;
38 for (ExtensionWarningSet::iterator i
= warnings_
.begin();
39 i
!= warnings_
.end();) {
40 if (types
.find(i
->warning_type()) != types
.end()) {
41 deleted_anything
= true;
49 NotifyWarningsChanged();
52 std::set
<ExtensionWarning::WarningType
>
53 ExtensionWarningService::GetWarningTypesAffectingExtension(
54 const std::string
& extension_id
) const {
55 DCHECK(CalledOnValidThread());
56 std::set
<ExtensionWarning::WarningType
> result
;
57 for (ExtensionWarningSet::const_iterator i
= warnings_
.begin();
58 i
!= warnings_
.end(); ++i
) {
59 if (i
->extension_id() == extension_id
)
60 result
.insert(i
->warning_type());
65 std::vector
<std::string
>
66 ExtensionWarningService::GetWarningMessagesForExtension(
67 const std::string
& extension_id
) const {
68 DCHECK(CalledOnValidThread());
69 std::vector
<std::string
> result
;
71 const ExtensionService
* extension_service
=
72 ExtensionSystem::Get(profile_
)->extension_service();
74 for (ExtensionWarningSet::const_iterator i
= warnings_
.begin();
75 i
!= warnings_
.end(); ++i
) {
76 if (i
->extension_id() == extension_id
)
77 result
.push_back(i
->GetLocalizedMessage(extension_service
->extensions()));
82 void ExtensionWarningService::AddWarnings(
83 const ExtensionWarningSet
& warnings
) {
84 DCHECK(CalledOnValidThread());
85 size_t old_size
= warnings_
.size();
87 warnings_
.insert(warnings
.begin(), warnings
.end());
89 if (old_size
!= warnings_
.size())
90 NotifyWarningsChanged();
94 void ExtensionWarningService::NotifyWarningsOnUI(
96 const ExtensionWarningSet
& warnings
) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
98 Profile
* profile
= reinterpret_cast<Profile
*>(profile_id
);
100 !g_browser_process
->profile_manager() ||
101 !g_browser_process
->profile_manager()->IsValidProfile(profile
)) {
105 extensions::ExtensionWarningService
* warning_service
=
106 extensions::ExtensionSystem::Get(profile
)->warning_service();
108 warning_service
->AddWarnings(warnings
);
111 void ExtensionWarningService::AddObserver(Observer
* observer
) {
112 observer_list_
.AddObserver(observer
);
115 void ExtensionWarningService::RemoveObserver(Observer
* observer
) {
116 observer_list_
.RemoveObserver(observer
);
119 void ExtensionWarningService::NotifyWarningsChanged() {
120 FOR_EACH_OBSERVER(Observer
, observer_list_
, ExtensionWarningsChanged());
123 void ExtensionWarningService::Observe(
125 const content::NotificationSource
& source
,
126 const content::NotificationDetails
& details
) {
128 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
129 const Extension
* extension
=
130 content::Details
<extensions::UnloadedExtensionInfo
>(details
)->
132 // Unloading one extension might have solved the problems of others.
133 // Therefore, we clear warnings of this type for all extensions.
134 std::set
<ExtensionWarning::WarningType
> warning_types
=
135 GetWarningTypesAffectingExtension(extension
->id());
136 ClearWarnings(warning_types
);
145 } // namespace extensions