Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / extensions / browser / warning_service.h
blob5aa9a50a885898da5650caa9bafb849e9f42ba5d
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 #ifndef EXTENSIONS_BROWSER_WARNING_SERVICE_H_
6 #define EXTENSIONS_BROWSER_WARNING_SERVICE_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/observer_list.h"
13 #include "base/scoped_observer.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "extensions/browser/extension_registry_observer.h"
17 #include "extensions/browser/warning_set.h"
19 // TODO(battre) Remove the Extension prefix.
21 namespace content {
22 class BrowserContext;
23 class NotificationDetails;
24 class NotificationSource;
27 namespace extensions {
29 class ExtensionRegistry;
31 // Manages a set of warnings caused by extensions. These warnings (e.g.
32 // conflicting modifications of network requests by extensions, slow extensions,
33 // etc.) trigger a warning badge in the UI and and provide means to resolve
34 // them. This class must be used on the UI thread only.
35 class WarningService : public KeyedService,
36 public ExtensionRegistryObserver,
37 public base::NonThreadSafe {
38 public:
39 class Observer {
40 public:
41 virtual void ExtensionWarningsChanged() = 0;
44 // |browser_context| may be NULL for testing. In this case, be sure to not
45 // insert any warnings.
46 explicit WarningService(content::BrowserContext* browser_context);
47 ~WarningService() override;
49 // Get the instance of the WarningService for |browser_context|.
50 // Redirected in incognito.
51 static WarningService* Get(content::BrowserContext* browser_context);
53 // Clears all warnings of types contained in |types| and notifies observers
54 // of the changed warnings.
55 void ClearWarnings(const std::set<Warning::WarningType>& types);
57 // Returns all types of warnings effecting extension |extension_id|.
58 std::set<Warning::WarningType> GetWarningTypesAffectingExtension(
59 const std::string& extension_id) const;
61 // Returns all localized warnings for extension |extension_id| in |result|.
62 std::vector<std::string> GetWarningMessagesForExtension(
63 const std::string& extension_id) const;
65 const WarningSet& warnings() const { return warnings_; }
67 // Adds a set of warnings and notifies observers if any warning is new.
68 void AddWarnings(const WarningSet& warnings);
70 // Notifies the WarningService of browser_context |browser_context_id| that
71 // new |warnings| occurred and triggers a warning badge.
72 static void NotifyWarningsOnUI(void* profile_id, const WarningSet& warnings);
74 void AddObserver(Observer* observer);
75 void RemoveObserver(Observer* observer);
77 private:
78 void NotifyWarningsChanged();
80 // ExtensionRegistryObserver implementation.
81 void OnExtensionUnloaded(content::BrowserContext* browser_context,
82 const Extension* extension,
83 UnloadedExtensionInfo::Reason reason) override;
85 // Currently existing warnings.
86 WarningSet warnings_;
88 content::BrowserContext* const browser_context_;
90 // Listen to extension unloaded notifications.
91 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
92 extension_registry_observer_;
94 ObserverList<Observer> observer_list_;
97 } // namespace extensions
99 #endif // EXTENSIONS_BROWSER_WARNING_SERVICE_H_