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.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string16.h"
15 #include "extensions/common/stack_frame.h"
19 class DictionaryValue
;
22 namespace extensions
{
24 class ExtensionError
{
29 NUM_ERROR_TYPES
// Put new values above this.
32 virtual ~ExtensionError();
34 // Serializes the ExtensionError into JSON format.
35 virtual scoped_ptr
<base::DictionaryValue
> ToValue() const;
37 virtual std::string
PrintForTest() const;
39 // Return true if this error and |rhs| are considered equal, and should be
41 bool IsEqual(const ExtensionError
* rhs
) const;
43 Type
type() const { return type_
; }
44 const std::string
& extension_id() const { return extension_id_
; }
45 bool from_incognito() const { return from_incognito_
; }
46 logging::LogSeverity
level() const { return level_
; }
47 const base::string16
& source() const { return source_
; }
48 const base::string16
& message() const { return message_
; }
49 size_t occurrences() const { return occurrences_
; }
50 void set_occurrences(size_t occurrences
) { occurrences_
= occurrences
; }
52 // Keys used for retrieving JSON values.
53 static const char kExtensionIdKey
[];
54 static const char kFromIncognitoKey
[];
55 static const char kLevelKey
[];
56 static const char kMessageKey
[];
57 static const char kSourceKey
[];
58 static const char kTypeKey
[];
61 ExtensionError(Type type
,
62 const std::string
& extension_id
,
64 logging::LogSeverity level
,
65 const base::string16
& source
,
66 const base::string16
& message
);
68 virtual bool IsEqualImpl(const ExtensionError
* rhs
) const = 0;
70 // Which type of error this is.
72 // The ID of the extension which caused the error.
73 std::string extension_id_
;
74 // Whether or not the error was caused while incognito.
76 // The severity level of the error.
77 logging::LogSeverity level_
;
78 // The source for the error; this can be a script, web page, or manifest file.
79 // This is stored as a string (rather than a url) since it can be a Chrome
80 // script file (e.g., event_bindings.js).
81 base::string16 source_
;
82 // The error message itself.
83 base::string16 message_
;
84 // The number of times this error has occurred.
88 DISALLOW_COPY_AND_ASSIGN(ExtensionError
);
91 class ManifestError
: public ExtensionError
{
93 ManifestError(const std::string
& extension_id
,
94 const base::string16
& message
,
95 const base::string16
& manifest_key
,
96 const base::string16
& manifest_specific
);
97 ~ManifestError() override
;
99 scoped_ptr
<base::DictionaryValue
> ToValue() const override
;
101 std::string
PrintForTest() const override
;
103 const base::string16
& manifest_key() const { return manifest_key_
; }
104 const base::string16
& manifest_specific() const { return manifest_specific_
; }
106 // Keys used for retrieving JSON values.
107 static const char kManifestKeyKey
[];
108 static const char kManifestSpecificKey
[];
111 bool IsEqualImpl(const ExtensionError
* rhs
) const override
;
113 // If present, this indicates the feature in the manifest which caused the
115 base::string16 manifest_key_
;
116 // If present, this is a more-specific location of the error - for instance,
117 // a specific permission which is incorrect, rather than simply "permissions".
118 base::string16 manifest_specific_
;
120 DISALLOW_COPY_AND_ASSIGN(ManifestError
);
123 class RuntimeError
: public ExtensionError
{
125 RuntimeError(const std::string
& extension_id
, // optional, sometimes unknown.
127 const base::string16
& source
,
128 const base::string16
& message
,
129 const StackTrace
& stack_trace
,
130 const GURL
& context_url
,
131 logging::LogSeverity level
,
133 int render_process_id
);
134 ~RuntimeError() override
;
136 scoped_ptr
<base::DictionaryValue
> ToValue() const override
;
138 std::string
PrintForTest() const override
;
140 const GURL
& context_url() const { return context_url_
; }
141 const StackTrace
& stack_trace() const { return stack_trace_
; }
142 int render_view_id() const { return render_view_id_
; }
143 int render_process_id() const { return render_process_id_
; }
145 // Keys used for retrieving JSON values.
146 static const char kColumnNumberKey
[];
147 static const char kContextUrlKey
[];
148 static const char kFunctionNameKey
[];
149 static const char kLineNumberKey
[];
150 static const char kStackTraceKey
[];
151 static const char kUrlKey
[];
152 static const char kRenderProcessIdKey
[];
153 static const char kRenderViewIdKey
[];
156 bool IsEqualImpl(const ExtensionError
* rhs
) const override
;
158 // Since we piggy-back onto other error reporting systems (like V8 and
159 // WebKit), the reported information may need to be cleaned up in order to be
160 // in a consistent format.
164 StackTrace stack_trace_
;
166 // Keep track of the render process which caused the error in order to
167 // inspect the view later, if possible.
169 int render_process_id_
;
171 DISALLOW_COPY_AND_ASSIGN(RuntimeError
);
174 } // namespace extensions
176 #endif // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_