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 CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SERVICE_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_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 "chrome/browser/extensions/extension_warning_set.h"
16#include "extensions/browser/extension_registry_observer.h"
17
18// TODO(battre) Remove the Extension prefix.
19
20class Profile;
21
22namespace content {
23class NotificationDetails;
24class NotificationSource;
25}
26
27namespace extensions {
28
29class 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.
35class ExtensionWarningService : public ExtensionRegistryObserver,
36                                public base::NonThreadSafe {
37 public:
38  class Observer {
39   public:
40    virtual void ExtensionWarningsChanged() = 0;
41  };
42
43  // |profile| may be NULL for testing. In this case, be sure to not insert
44  // any warnings.
45  explicit ExtensionWarningService(Profile* profile);
46  virtual ~ExtensionWarningService();
47
48  // Clears all warnings of types contained in |types| and notifies observers
49  // of the changed warnings.
50  void ClearWarnings(const std::set<ExtensionWarning::WarningType>& types);
51
52  // Returns all types of warnings effecting extension |extension_id|.
53  std::set<ExtensionWarning::WarningType> GetWarningTypesAffectingExtension(
54      const std::string& extension_id) const;
55
56  // Returns all localized warnings for extension |extension_id| in |result|.
57  std::vector<std::string> GetWarningMessagesForExtension(
58      const std::string& extension_id) const;
59
60  const ExtensionWarningSet& warnings() const { return warnings_; }
61
62  // Adds a set of warnings and notifies observers if any warning is new.
63  void AddWarnings(const ExtensionWarningSet& warnings);
64
65  // Notifies the ExtensionWarningService of profile |profile_id| that new
66  // |warnings| occurred and triggers a warning badge.
67  static void NotifyWarningsOnUI(void* profile_id,
68                                 const ExtensionWarningSet& warnings);
69
70  void AddObserver(Observer* observer);
71  void RemoveObserver(Observer* observer);
72
73 private:
74  void NotifyWarningsChanged();
75
76  // ExtensionRegistryObserver implementation.
77  virtual void OnExtensionUnloaded(content::BrowserContext* browser_context,
78                                   const Extension* extension,
79                                   UnloadedExtensionInfo::Reason reason)
80      OVERRIDE;
81
82  // Currently existing warnings.
83  ExtensionWarningSet warnings_;
84
85  Profile* profile_;
86
87  // Listen to extension unloaded notifications.
88  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
89      extension_registry_observer_;
90
91  ObserverList<Observer> observer_list_;
92};
93
94}  // namespace extensions
95
96#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SERVICE_H_
97