summaryrefslogtreecommitdiffstats
path: root/extensions/browser/extension_error.h
blob: c5be1696e71ab1d9ba99bee385b31621cbe7b1da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
#define EXTENSIONS_BROWSER_EXTENSION_ERROR_H_

#include <string>
#include <vector>

#include "base/compiler_specific.h"
#include "base/logging.h"
#include "base/strings/string16.h"

namespace extensions {

class ExtensionError {
 public:
  enum Type {
    MANIFEST_ERROR,
    RUNTIME_ERROR
  };

  virtual ~ExtensionError();

  virtual std::string PrintForTest() const;

  // Return true if this error and |rhs| are considered equal, and should be
  // grouped together.
  bool IsEqual(const ExtensionError* rhs) const;

  Type type() const { return type_; }
  const std::string& extension_id() const { return extension_id_; }
  bool from_incognito() const { return from_incognito_; }
  logging::LogSeverity level() const { return level_; }
  const base::string16& source() const { return source_; }
  const base::string16& message() const { return message_; }
  size_t occurrences() const { return occurrences_; }
  void set_occurrences(size_t occurrences) { occurrences_ = occurrences; }

 protected:
  ExtensionError(Type type,
                 const std::string& extension_id,
                 bool from_incognito,
                 logging::LogSeverity level,
                 const base::string16& source,
                 const base::string16& message);

  virtual bool IsEqualImpl(const ExtensionError* rhs) const = 0;

  // Which type of error this is.
  Type type_;
  // The ID of the extension which caused the error.
  std::string extension_id_;
  // Whether or not the error was caused while incognito.
  bool from_incognito_;
  // The severity level of the error.
  logging::LogSeverity level_;
  // The source for the error; this can be a script, web page, or manifest file.
  // This is stored as a string (rather than a url) since it can be a Chrome
  // script file (e.g., event_bindings.js).
  base::string16 source_;
  // The error message itself.
  base::string16 message_;
  // The number of times this error has occurred.
  size_t occurrences_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionError);
};

class ManifestError : public ExtensionError {
 public:
  ManifestError(const std::string& extension_id,
                const base::string16& message);
  virtual ~ManifestError();

  virtual std::string PrintForTest() const OVERRIDE;
 private:
  virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE;

  DISALLOW_COPY_AND_ASSIGN(ManifestError);
};

class RuntimeError : public ExtensionError {
 public:
  struct StackFrame {
    size_t line_number;
    size_t column_number;
    // This is stored as a string (rather than a url) since it can be a
    // Chrome script file (e.g., event_bindings.js).
    base::string16 url;
    base::string16 function;  // optional

    // STL-Required constructor
    StackFrame();

    StackFrame(size_t frame_line,
               size_t frame_column,
               const base::string16& frame_url,
               const base::string16& frame_function  /* can be empty */);

    ~StackFrame();

    bool operator==(const StackFrame& rhs) const;
  };
  typedef std::vector<StackFrame> StackTrace;

  RuntimeError(bool from_incognito,
               const base::string16& source,
               const base::string16& message,
               logging::LogSeverity level,
               const base::string16& details);
  virtual ~RuntimeError();

  virtual std::string PrintForTest() const OVERRIDE;

  const base::string16& execution_context_url() const {
      return execution_context_url_;
  }
  const StackTrace& stack_trace() const { return stack_trace_; }
 private:
  virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE;

  // Parse the JSON |details| passed to the error. This includes a stack trace
  // and an execution context url.
  void ParseDetails(const base::string16& details);
  // Try to determine the ID of the extension. This may be obtained through the
  // reported source, or through the execution context url.
  void DetermineExtensionID();

  base::string16 execution_context_url_;
  StackTrace stack_trace_;

  DISALLOW_COPY_AND_ASSIGN(RuntimeError);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_