Roll src/third_party/skia 054eef2:51985e3
[chromium-blink-merge.git] / extensions / browser / warning_service.cc
blob11dc84c95d223ff1c0382577da14acd249b25330
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() {}
28 // static
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 bool deleted_anything = false;
37 for (WarningSet::iterator i = warnings_.begin();
38 i != warnings_.end();) {
39 if (types.find(i->warning_type()) != types.end()) {
40 deleted_anything = true;
41 warnings_.erase(i++);
42 } else {
43 ++i;
47 if (deleted_anything)
48 NotifyWarningsChanged();
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());
60 return result;
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));
76 return result;
79 void WarningService::AddWarnings(const WarningSet& warnings) {
80 DCHECK(CalledOnValidThread());
81 size_t old_size = warnings_.size();
83 warnings_.insert(warnings.begin(), warnings.end());
85 if (old_size != warnings_.size())
86 NotifyWarningsChanged();
89 // static
90 void WarningService::NotifyWarningsOnUI(
91 void* profile_id,
92 const WarningSet& warnings) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI);
94 content::BrowserContext* browser_context =
95 reinterpret_cast<content::BrowserContext*>(profile_id);
97 if (!browser_context ||
98 !ExtensionsBrowserClient::Get() ||
99 !ExtensionsBrowserClient::Get()->IsValidContext(browser_context)) {
100 return;
103 WarningService* warning_service = WarningService::Get(browser_context);
105 warning_service->AddWarnings(warnings);
108 void WarningService::AddObserver(Observer* observer) {
109 observer_list_.AddObserver(observer);
112 void WarningService::RemoveObserver(Observer* observer) {
113 observer_list_.RemoveObserver(observer);
116 void WarningService::NotifyWarningsChanged() {
117 FOR_EACH_OBSERVER(Observer, observer_list_, ExtensionWarningsChanged());
120 void WarningService::OnExtensionUnloaded(
121 content::BrowserContext* browser_context,
122 const Extension* extension,
123 UnloadedExtensionInfo::Reason reason) {
124 // Unloading one extension might have solved the problems of others.
125 // Therefore, we clear warnings of this type for all extensions.
126 std::set<Warning::WarningType> warning_types =
127 GetWarningTypesAffectingExtension(extension->id());
128 ClearWarnings(warning_types);
131 } // namespace extensions