1// Copyright (c) 2011 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_ERROR_REPORTER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_ERROR_REPORTER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/observer_list.h"
12#include "base/strings/string16.h"
13
14namespace base {
15class MessageLoop;
16class FilePath;
17}
18
19namespace content {
20class BrowserContext;
21}
22
23// Exposes an easy way for the various components of the extension system to
24// report errors. This is a singleton that lives on the UI thread, with the
25// exception of ReportError() which may be called from any thread.
26// TODO(aa): Hook this up to about:extensions, when we have about:extensions.
27// TODO(aa): Consider exposing directly, or via a helper, to the renderer
28// process and plumbing the errors out to the browser.
29// TODO(aa): Add ReportError(extension_id, message, be_noisy), so that we can
30// report errors that are specific to a particular extension.
31class ExtensionErrorReporter {
32 public:
33  class Observer {
34   public:
35    virtual ~Observer() {}
36
37    // Called when an unpacked extension fails to load.
38    virtual void OnLoadFailure(content::BrowserContext* browser_context,
39                               const base::FilePath& extension_path,
40                               const std::string& error) = 0;
41  };
42
43  // Initializes the error reporter. Must be called before any other methods
44  // and on the UI thread.
45  static void Init(bool enable_noisy_errors);
46
47  // Get the singleton instance.
48  static ExtensionErrorReporter* GetInstance();
49
50  // Report an extension load error. This forwards to ReportError() after
51  // sending an EXTENSION_LOAD_ERROR notification.
52  // TODO(rdevlin.cronin): There's a lot wrong with this. But some of our
53  // systems rely on the notification. Investigate what it will take to remove
54  // the notification and this method.
55  void ReportLoadError(const base::FilePath& extension_path,
56                       const std::string& error,
57                       content::BrowserContext* browser_context,
58                       bool be_noisy);
59
60  // Report an error. Errors always go to VLOG(1). Optionally, they can also
61  // cause a noisy alert box.
62  void ReportError(const base::string16& message, bool be_noisy);
63
64  // Get the errors that have been reported so far.
65  const std::vector<base::string16>* GetErrors();
66
67  // Clear the list of errors reported so far.
68  void ClearErrors();
69
70  void AddObserver(Observer* observer);
71
72  void RemoveObserver(Observer* observer);
73
74 private:
75  static ExtensionErrorReporter* instance_;
76
77  explicit ExtensionErrorReporter(bool enable_noisy_errors);
78  ~ExtensionErrorReporter();
79
80  base::MessageLoop* ui_loop_;
81  std::vector<base::string16> errors_;
82  bool enable_noisy_errors_;
83
84  ObserverList<Observer> observers_;
85};
86
87#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_ERROR_REPORTER_H_
88