Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / warning_service.h
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.
4
5 #ifndef EXTENSIONS_BROWSER_WARNING_SERVICE_H_
6 #define EXTENSIONS_BROWSER_WARNING_SERVICE_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
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"
18
19 // TODO(battre) Remove the Extension prefix.
20
21 namespace content {
22 class BrowserContext;
23 class NotificationDetails;
24 class NotificationSource;
25 }
26
27 namespace extensions {
28
29 class ExtensionRegistry;
30
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;
42   };
43
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;
48
49   // Get the instance of the WarningService for |browser_context|.
50   // Redirected in incognito.
51   static WarningService* Get(content::BrowserContext* browser_context);
52
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);
56
57   // Returns all types of warnings effecting extension |extension_id|.
58   std::set<Warning::WarningType> GetWarningTypesAffectingExtension(
59       const std::string& extension_id) const;
60
61   // Returns all localized warnings for extension |extension_id| in |result|.
62   std::vector<std::string> GetWarningMessagesForExtension(
63       const std::string& extension_id) const;
64
65   const WarningSet& warnings() const { return warnings_; }
66
67   // Adds a set of warnings and notifies observers if any warning is new.
68   void AddWarnings(const WarningSet& warnings);
69
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);
73
74   void AddObserver(Observer* observer);
75   void RemoveObserver(Observer* observer);
76
77  private:
78   void NotifyWarningsChanged();
79
80   // ExtensionRegistryObserver implementation.
81   void OnExtensionUnloaded(content::BrowserContext* browser_context,
82                            const Extension* extension,
83                            UnloadedExtensionInfo::Reason reason) override;
84
85   // Currently existing warnings.
86   WarningSet warnings_;
87
88   content::BrowserContext* const browser_context_;
89
90   // Listen to extension unloaded notifications.
91   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
92       extension_registry_observer_;
93
94   ObserverList<Observer> observer_list_;
95 };
96
97 }  // namespace extensions
98
99 #endif  // EXTENSIONS_BROWSER_WARNING_SERVICE_H_