extension_error.h revision 424c4d7b64af9d0d8fd9624f381f469654d5e3d2
1// Copyright 2013 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_EXTENSION_ERROR_H_
6#define EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
7
8#include <string>
9#include <vector>
10
11#include "base/compiler_specific.h"
12#include "base/logging.h"
13#include "base/strings/string16.h"
14
15namespace extensions {
16
17class ExtensionError {
18 public:
19  enum Type {
20    MANIFEST_ERROR,
21    RUNTIME_ERROR
22  };
23
24  virtual ~ExtensionError();
25
26  virtual std::string PrintForTest() const;
27
28  // Return true if this error and |rhs| are considered equal, and should be
29  // grouped together.
30  bool IsEqual(const ExtensionError* rhs) const;
31
32  Type type() const { return type_; }
33  const std::string& extension_id() const { return extension_id_; }
34  bool from_incognito() const { return from_incognito_; }
35  logging::LogSeverity level() const { return level_; }
36  const base::string16& source() const { return source_; }
37  const base::string16& message() const { return message_; }
38  size_t occurrences() const { return occurrences_; }
39  void set_occurrences(size_t occurrences) { occurrences_ = occurrences; }
40
41 protected:
42  ExtensionError(Type type,
43                 const std::string& extension_id,
44                 bool from_incognito,
45                 logging::LogSeverity level,
46                 const base::string16& source,
47                 const base::string16& message);
48
49  virtual bool IsEqualImpl(const ExtensionError* rhs) const = 0;
50
51  // Which type of error this is.
52  Type type_;
53  // The ID of the extension which caused the error.
54  std::string extension_id_;
55  // Whether or not the error was caused while incognito.
56  bool from_incognito_;
57  // The severity level of the error.
58  logging::LogSeverity level_;
59  // The source for the error; this can be a script, web page, or manifest file.
60  // This is stored as a string (rather than a url) since it can be a Chrome
61  // script file (e.g., event_bindings.js).
62  base::string16 source_;
63  // The error message itself.
64  base::string16 message_;
65  // The number of times this error has occurred.
66  size_t occurrences_;
67
68  DISALLOW_COPY_AND_ASSIGN(ExtensionError);
69};
70
71class ManifestError : public ExtensionError {
72 public:
73  ManifestError(const std::string& extension_id,
74                const base::string16& message);
75  virtual ~ManifestError();
76
77  virtual std::string PrintForTest() const OVERRIDE;
78 private:
79  virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE;
80
81  DISALLOW_COPY_AND_ASSIGN(ManifestError);
82};
83
84class RuntimeError : public ExtensionError {
85 public:
86  struct StackFrame {
87    size_t line_number;
88    size_t column_number;
89    // This is stored as a string (rather than a url) since it can be a
90    // Chrome script file (e.g., event_bindings.js).
91    base::string16 url;
92    base::string16 function;  // optional
93
94    // STL-Required constructor
95    StackFrame();
96
97    StackFrame(size_t frame_line,
98               size_t frame_column,
99               const base::string16& frame_url,
100               const base::string16& frame_function  /* can be empty */);
101
102    ~StackFrame();
103
104    bool operator==(const StackFrame& rhs) const;
105  };
106  typedef std::vector<StackFrame> StackTrace;
107
108  RuntimeError(bool from_incognito,
109               const base::string16& source,
110               const base::string16& message,
111               logging::LogSeverity level,
112               const base::string16& details);
113  virtual ~RuntimeError();
114
115  virtual std::string PrintForTest() const OVERRIDE;
116
117  const base::string16& execution_context_url() const {
118      return execution_context_url_;
119  }
120  const StackTrace& stack_trace() const { return stack_trace_; }
121 private:
122  virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE;
123
124  // Parse the JSON |details| passed to the error. This includes a stack trace
125  // and an execution context url.
126  void ParseDetails(const base::string16& details);
127  // Try to determine the ID of the extension. This may be obtained through the
128  // reported source, or through the execution context url.
129  void DetermineExtensionID();
130
131  base::string16 execution_context_url_;
132  StackTrace stack_trace_;
133
134  DISALLOW_COPY_AND_ASSIGN(RuntimeError);
135};
136
137}  // namespace extensions
138
139#endif  // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
140