blob: 6c97e89e52175bfddaae105cda496649e45d0147 [file] [log] [blame]
Avi Drissman60039d42022-09-13 21:49:051// Copyright 2013 The Chromium Authors
[email protected]1b66fdb2013-07-26 09:57:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]921237d062013-08-10 15:30:495#include "extensions/browser/extension_error.h"
[email protected]1b66fdb2013-07-26 09:57:286
[email protected]1b66fdb2013-07-26 09:57:287#include "base/strings/string_number_conversions.h"
8#include "base/strings/utf_string_conversions.h"
[email protected]1b66fdb2013-07-26 09:57:289#include "extensions/common/constants.h"
[email protected]921237d062013-08-10 15:30:4910#include "url/gurl.h"
[email protected]1b66fdb2013-07-26 09:57:2811
[email protected]1b66fdb2013-07-26 09:57:2812namespace extensions {
13
[email protected]fa5fed32013-09-05 21:56:2214////////////////////////////////////////////////////////////////////////////////
15// ExtensionError
[email protected]1b66fdb2013-07-26 09:57:2816
[email protected]1b66fdb2013-07-26 09:57:2817ExtensionError::ExtensionError(Type type,
[email protected]921237d062013-08-10 15:30:4918 const std::string& extension_id,
[email protected]1b66fdb2013-07-26 09:57:2819 bool from_incognito,
[email protected]d466f782013-08-28 21:59:2320 logging::LogSeverity level,
Jan Wilken Dörrie85285b02021-03-11 23:38:4721 const std::u16string& source,
22 const std::u16string& message)
[email protected]1b66fdb2013-07-26 09:57:2823 : type_(type),
[email protected]921237d062013-08-10 15:30:4924 extension_id_(extension_id),
rdevlin.croninc799f9f2015-03-21 00:56:3025 id_(0),
[email protected]1b66fdb2013-07-26 09:57:2826 from_incognito_(from_incognito),
[email protected]d466f782013-08-28 21:59:2327 level_(level),
[email protected]1b66fdb2013-07-26 09:57:2828 source_(source),
[email protected]d466f782013-08-28 21:59:2329 message_(message),
Jan Wilken Dörrie85285b02021-03-11 23:38:4730 occurrences_(1u) {}
[email protected]1b66fdb2013-07-26 09:57:2831
32ExtensionError::~ExtensionError() {
33}
34
wittmanb3ee0482015-06-24 17:47:4035std::string ExtensionError::GetDebugString() const {
[email protected]1b66fdb2013-07-26 09:57:2836 return std::string("Extension Error:") +
37 "\n OTR: " + std::string(from_incognito_ ? "true" : "false") +
Raul Tambre8d922582019-02-05 20:58:5538 "\n Level: " + base::NumberToString(level_) +
[email protected]1b66fdb2013-07-26 09:57:2839 "\n Source: " + base::UTF16ToUTF8(source_) +
40 "\n Message: " + base::UTF16ToUTF8(message_) +
41 "\n ID: " + extension_id_;
42}
43
[email protected]d466f782013-08-28 21:59:2344bool ExtensionError::IsEqual(const ExtensionError* rhs) const {
45 // We don't check |source_| or |level_| here, since they are constant for
46 // manifest errors. Check them in RuntimeError::IsEqualImpl() instead.
47 return type_ == rhs->type_ &&
48 extension_id_ == rhs->extension_id_ &&
49 message_ == rhs->message_ &&
50 IsEqualImpl(rhs);
51}
52
[email protected]fa5fed32013-09-05 21:56:2253////////////////////////////////////////////////////////////////////////////////
54// ManifestError
55
[email protected]d466f782013-08-28 21:59:2356ManifestError::ManifestError(const std::string& extension_id,
Jan Wilken Dörrie85285b02021-03-11 23:38:4757 const std::u16string& message,
58 const std::u16string& manifest_key,
59 const std::u16string& manifest_specific)
[email protected]d466f782013-08-28 21:59:2360 : ExtensionError(ExtensionError::MANIFEST_ERROR,
[email protected]921237d062013-08-10 15:30:4961 extension_id,
62 false, // extensions can't be installed while incognito.
[email protected]d466f782013-08-28 21:59:2363 logging::LOG_WARNING, // All manifest errors are warnings.
[email protected]921237d062013-08-10 15:30:4964 base::FilePath(kManifestFilename).AsUTF16Unsafe(),
[email protected]b191e2d32013-09-03 21:08:3065 message),
66 manifest_key_(manifest_key),
Jan Wilken Dörrie85285b02021-03-11 23:38:4767 manifest_specific_(manifest_specific) {}
[email protected]1b66fdb2013-07-26 09:57:2868
[email protected]d466f782013-08-28 21:59:2369ManifestError::~ManifestError() {
[email protected]1b66fdb2013-07-26 09:57:2870}
71
wittmanb3ee0482015-06-24 17:47:4072std::string ManifestError::GetDebugString() const {
73 return ExtensionError::GetDebugString() +
[email protected]d466f782013-08-28 21:59:2374 "\n Type: ManifestError";
[email protected]1b66fdb2013-07-26 09:57:2875}
76
[email protected]d466f782013-08-28 21:59:2377bool ManifestError::IsEqualImpl(const ExtensionError* rhs) const {
78 // If two manifest errors have the same extension id and message (which are
79 // both checked in ExtensionError::IsEqual), then they are equal.
80 return true;
[email protected]1b66fdb2013-07-26 09:57:2881}
82
[email protected]2fb9bd22013-09-07 00:08:0883////////////////////////////////////////////////////////////////////////////////
84// RuntimeError
85
[email protected]a0ed2682013-09-06 08:41:0786RuntimeError::RuntimeError(const std::string& extension_id,
87 bool from_incognito,
Jan Wilken Dörrie85285b02021-03-11 23:38:4788 const std::u16string& source,
89 const std::u16string& message,
[email protected]88b50b62013-09-01 23:05:0690 const StackTrace& stack_trace,
91 const GURL& context_url,
[email protected]c934c382013-11-01 00:36:0192 logging::LogSeverity level,
rdevlin.cronin86f5b702015-06-24 18:49:1793 int render_frame_id,
[email protected]c934c382013-11-01 00:36:0194 int render_process_id)
[email protected]d466f782013-08-28 21:59:2395 : ExtensionError(ExtensionError::RUNTIME_ERROR,
[email protected]a0ed2682013-09-06 08:41:0796 !extension_id.empty() ? extension_id : GURL(source).host(),
[email protected]1b66fdb2013-07-26 09:57:2897 from_incognito,
[email protected]d466f782013-08-28 21:59:2398 level,
[email protected]1b66fdb2013-07-26 09:57:2899 source,
[email protected]88b50b62013-09-01 23:05:06100 message),
101 context_url_(context_url),
[email protected]c934c382013-11-01 00:36:01102 stack_trace_(stack_trace),
rdevlin.cronin86f5b702015-06-24 18:49:17103 render_frame_id_(render_frame_id),
[email protected]c934c382013-11-01 00:36:01104 render_process_id_(render_process_id) {
[email protected]88b50b62013-09-01 23:05:06105 CleanUpInit();
[email protected]1b66fdb2013-07-26 09:57:28106}
107
[email protected]d466f782013-08-28 21:59:23108RuntimeError::~RuntimeError() {
[email protected]1b66fdb2013-07-26 09:57:28109}
110
wittmanb3ee0482015-06-24 17:47:40111std::string RuntimeError::GetDebugString() const {
112 std::string result = ExtensionError::GetDebugString() +
[email protected]d466f782013-08-28 21:59:23113 "\n Type: RuntimeError"
[email protected]88b50b62013-09-01 23:05:06114 "\n Context: " + context_url_.spec() +
[email protected]1b66fdb2013-07-26 09:57:28115 "\n Stack Trace: ";
jdoerriea1e1598b2018-10-10 09:10:37116 for (auto iter = stack_trace_.cbegin(); iter != stack_trace_.cend(); ++iter) {
riceac7009272015-09-25 18:45:13117 result += "\n {";
Brett Wilson5accd242017-11-30 22:07:32118 result += "\n Line: " + base::NumberToString(iter->line_number) +
119 "\n Column: " + base::NumberToString(iter->column_number) +
120 "\n URL: " + base::UTF16ToUTF8(iter->source) +
121 "\n Function: " + base::UTF16ToUTF8(iter->function) +
122 "\n }";
[email protected]1b66fdb2013-07-26 09:57:28123 }
124 return result;
125}
126
[email protected]d466f782013-08-28 21:59:23127bool RuntimeError::IsEqualImpl(const ExtensionError* rhs) const {
128 const RuntimeError* error = static_cast<const RuntimeError*>(rhs);
129
130 // Only look at the first frame of a stack trace to save time and group
131 // nearly-identical errors. The most recent error is kept, so there's no risk
132 // of displaying an old and inaccurate stack trace.
[email protected]010ff5072013-09-10 08:36:50133 return level_ == error->level_ &&
134 source_ == error->source_ &&
[email protected]88b50b62013-09-01 23:05:06135 context_url_ == error->context_url_ &&
[email protected]d466f782013-08-28 21:59:23136 stack_trace_.size() == error->stack_trace_.size() &&
137 (stack_trace_.empty() || stack_trace_[0] == error->stack_trace_[0]);
138}
139
[email protected]88b50b62013-09-01 23:05:06140void RuntimeError::CleanUpInit() {
141 // If the error came from a generated background page, the "context" is empty
142 // because there's no visible URL. We should set context to be the generated
143 // background page in this case.
144 GURL source_url = GURL(source_);
145 if (context_url_.is_empty() &&
csharrison88b3b712016-11-14 23:12:35146 source_url.path_piece() ==
[email protected]88b50b62013-09-01 23:05:06147 std::string("/") + kGeneratedBackgroundPageFilename) {
148 context_url_ = source_url;
[email protected]1b66fdb2013-07-26 09:57:28149 }
150
[email protected]88b50b62013-09-01 23:05:06151 // In some instances (due to the fact that we're reusing error reporting from
152 // other systems), the source won't match up with the final entry in the stack
153 // trace. (For instance, in a browser action error, the source is the page -
154 // sometimes the background page - but the error is thrown from the script.)
155 // Make the source match the stack trace, since that is more likely the cause
156 // of the error.
157 if (!stack_trace_.empty() && source_ != stack_trace_[0].source)
158 source_ = stack_trace_[0].source;
[email protected]1b66fdb2013-07-26 09:57:28159}
160
wittmanb3ee0482015-06-24 17:47:40161////////////////////////////////////////////////////////////////////////////////
162// InternalError
163
164InternalError::InternalError(const std::string& extension_id,
Jan Wilken Dörrie85285b02021-03-11 23:38:47165 const std::u16string& message,
wittmanb3ee0482015-06-24 17:47:40166 logging::LogSeverity level)
167 : ExtensionError(ExtensionError::INTERNAL_ERROR,
168 extension_id,
169 false, // not incognito.
170 level,
Jan Wilken Dörrie85285b02021-03-11 23:38:47171 std::u16string(),
172 message) {}
wittmanb3ee0482015-06-24 17:47:40173
174InternalError::~InternalError() {
175}
176
177std::string InternalError::GetDebugString() const {
178 return ExtensionError::GetDebugString() +
179 "\n Type: InternalError";
180}
181
182bool InternalError::IsEqualImpl(const ExtensionError* rhs) const {
183 // ExtensionError logic is sufficient for comparison.
184 return true;
185}
186
[email protected]1b66fdb2013-07-26 09:57:28187} // namespace extensions