| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/extension_error.h" | 5 #include "extensions/browser/extension_error.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 return true; | 33 return true; |
| 34 } | 34 } |
| 35 return false; | 35 return false; |
| 36 } | 36 } |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 ExtensionError::ExtensionError(Type type, | 40 ExtensionError::ExtensionError(Type type, |
| 41 const std::string& extension_id, | 41 const std::string& extension_id, |
| 42 bool from_incognito, | 42 bool from_incognito, |
| 43 logging::LogSeverity level, |
| 43 const string16& source, | 44 const string16& source, |
| 44 const string16& message) | 45 const string16& message) |
| 45 : type_(type), | 46 : type_(type), |
| 46 extension_id_(extension_id), | 47 extension_id_(extension_id), |
| 47 from_incognito_(from_incognito), | 48 from_incognito_(from_incognito), |
| 49 level_(level), |
| 48 source_(source), | 50 source_(source), |
| 49 message_(message) { | 51 message_(message) { |
| 50 } | 52 } |
| 51 | 53 |
| 52 ExtensionError::~ExtensionError() { | 54 ExtensionError::~ExtensionError() { |
| 53 } | 55 } |
| 54 | 56 |
| 55 std::string ExtensionError::PrintForTest() const { | 57 std::string ExtensionError::PrintForTest() const { |
| 56 return std::string("Extension Error:") + | 58 return std::string("Extension Error:") + |
| 57 "\n OTR: " + std::string(from_incognito_ ? "true" : "false") + | 59 "\n OTR: " + std::string(from_incognito_ ? "true" : "false") + |
| 60 "\n Level: " + base::IntToString(static_cast<int>(level_)); |
| 58 "\n Source: " + base::UTF16ToUTF8(source_) + | 61 "\n Source: " + base::UTF16ToUTF8(source_) + |
| 59 "\n Message: " + base::UTF16ToUTF8(message_) + | 62 "\n Message: " + base::UTF16ToUTF8(message_) + |
| 60 "\n ID: " + extension_id_; | 63 "\n ID: " + extension_id_; |
| 61 } | 64 } |
| 62 | 65 |
| 63 ManifestParsingError::ManifestParsingError(const std::string& extension_id, | 66 bool ExtensionError::IsEqual(const ExtensionError* rhs) const { |
| 64 const string16& message) | 67 // We don't check |source_| or |level_| here, since they are constant for |
| 65 : ExtensionError(ExtensionError::MANIFEST_PARSING_ERROR, | 68 // manifest errors. Check them in RuntimeError::IsEqualImpl() instead. |
| 69 return type_ == rhs->type_ && |
| 70 extension_id_ == rhs->extension_id_ && |
| 71 message_ == rhs->message_ && |
| 72 IsEqualImpl(rhs); |
| 73 } |
| 74 |
| 75 ManifestError::ManifestError(const std::string& extension_id, |
| 76 const string16& message) |
| 77 : ExtensionError(ExtensionError::MANIFEST_ERROR, |
| 66 extension_id, | 78 extension_id, |
| 67 false, // extensions can't be installed while incognito. | 79 false, // extensions can't be installed while incognito. |
| 80 logging::LOG_WARNING, // All manifest errors are warnings. |
| 68 base::FilePath(kManifestFilename).AsUTF16Unsafe(), | 81 base::FilePath(kManifestFilename).AsUTF16Unsafe(), |
| 69 message) { | 82 message) { |
| 70 } | 83 } |
| 71 | 84 |
| 72 ManifestParsingError::~ManifestParsingError() { | 85 ManifestError::~ManifestError() { |
| 73 } | 86 } |
| 74 | 87 |
| 75 std::string ManifestParsingError::PrintForTest() const { | 88 std::string ManifestError::PrintForTest() const { |
| 76 return ExtensionError::PrintForTest() + | 89 return ExtensionError::PrintForTest() + |
| 77 "\n Type: ManifestParsingError"; | 90 "\n Type: ManifestError"; |
| 78 } | 91 } |
| 79 | 92 |
| 80 JavascriptRuntimeError::StackFrame::StackFrame() : line_number(-1), | 93 bool ManifestError::IsEqualImpl(const ExtensionError* rhs) const { |
| 81 column_number(-1) { | 94 // If two manifest errors have the same extension id and message (which are |
| 95 // both checked in ExtensionError::IsEqual), then they are equal. |
| 96 return true; |
| 82 } | 97 } |
| 83 | 98 |
| 84 JavascriptRuntimeError::StackFrame::StackFrame(size_t frame_line, | 99 RuntimeError::StackFrame::StackFrame() : line_number(-1), column_number(-1) { |
| 85 size_t frame_column, | 100 } |
| 86 const string16& frame_url, | 101 |
| 87 const string16& frame_function) | 102 RuntimeError::StackFrame::StackFrame(size_t frame_line, |
| 103 size_t frame_column, |
| 104 const string16& frame_url, |
| 105 const string16& frame_function) |
| 88 : line_number(frame_line), | 106 : line_number(frame_line), |
| 89 column_number(frame_column), | 107 column_number(frame_column), |
| 90 url(frame_url), | 108 url(frame_url), |
| 91 function(frame_function) { | 109 function(frame_function) { |
| 92 } | 110 } |
| 93 | 111 |
| 94 JavascriptRuntimeError::StackFrame::~StackFrame() { | 112 RuntimeError::StackFrame::~StackFrame() { |
| 95 } | 113 } |
| 96 | 114 |
| 97 JavascriptRuntimeError::JavascriptRuntimeError(bool from_incognito, | 115 bool RuntimeError::StackFrame::operator==( |
| 98 const string16& source, | 116 const RuntimeError::StackFrame& rhs) const { |
| 99 const string16& message, | 117 return line_number == rhs.line_number && |
| 100 logging::LogSeverity level, | 118 column_number == rhs.column_number && |
| 101 const string16& details) | 119 url == rhs.url && |
| 102 : ExtensionError(ExtensionError::JAVASCRIPT_RUNTIME_ERROR, | 120 function == rhs.function; |
| 121 } |
| 122 RuntimeError::RuntimeError(bool from_incognito, |
| 123 const string16& source, |
| 124 const string16& message, |
| 125 logging::LogSeverity level, |
| 126 const string16& details) |
| 127 : ExtensionError(ExtensionError::RUNTIME_ERROR, |
| 103 std::string(), // We don't know the id yet. | 128 std::string(), // We don't know the id yet. |
| 104 from_incognito, | 129 from_incognito, |
| 130 level, |
| 105 source, | 131 source, |
| 106 message), | 132 message) { |
| 107 level_(level) { | |
| 108 ParseDetails(details); | 133 ParseDetails(details); |
| 109 DetermineExtensionID(); | 134 DetermineExtensionID(); |
| 110 } | 135 } |
| 111 | 136 |
| 112 JavascriptRuntimeError::~JavascriptRuntimeError() { | 137 RuntimeError::~RuntimeError() { |
| 113 } | 138 } |
| 114 | 139 |
| 115 std::string JavascriptRuntimeError::PrintForTest() const { | 140 std::string RuntimeError::PrintForTest() const { |
| 116 std::string result = ExtensionError::PrintForTest() + | 141 std::string result = ExtensionError::PrintForTest() + |
| 117 "\n Type: JavascriptRuntimeError" | 142 "\n Type: RuntimeError" |
| 118 "\n Context: " + base::UTF16ToUTF8(execution_context_url_) + | 143 "\n Context: " + base::UTF16ToUTF8(execution_context_url_) + |
| 119 "\n Stack Trace: "; | 144 "\n Stack Trace: "; |
| 120 for (StackTrace::const_iterator iter = stack_trace_.begin(); | 145 for (StackTrace::const_iterator iter = stack_trace_.begin(); |
| 121 iter != stack_trace_.end(); ++iter) { | 146 iter != stack_trace_.end(); ++iter) { |
| 122 result += "\n {" | 147 result += "\n {" |
| 123 "\n Line: " + base::IntToString(iter->line_number) + | 148 "\n Line: " + base::IntToString(iter->line_number) + |
| 124 "\n Column: " + base::IntToString(iter->column_number) + | 149 "\n Column: " + base::IntToString(iter->column_number) + |
| 125 "\n URL: " + base::UTF16ToUTF8(iter->url) + | 150 "\n URL: " + base::UTF16ToUTF8(iter->url) + |
| 126 "\n Function: " + base::UTF16ToUTF8(iter->function) + | 151 "\n Function: " + base::UTF16ToUTF8(iter->function) + |
| 127 "\n }"; | 152 "\n }"; |
| 128 } | 153 } |
| 129 return result; | 154 return result; |
| 130 } | 155 } |
| 131 | 156 |
| 132 void JavascriptRuntimeError::ParseDetails(const string16& details) { | 157 bool RuntimeError::IsEqualImpl(const ExtensionError* rhs) const { |
| 158 const RuntimeError* error = static_cast<const RuntimeError*>(rhs); |
| 159 |
| 160 // Only look at the first frame of a stack trace to save time and group |
| 161 // nearly-identical errors. The most recent error is kept, so there's no risk |
| 162 // of displaying an old and inaccurate stack trace. |
| 163 return level_ == level_ && |
| 164 source_ == source_ && |
| 165 execution_context_url_ == error->execution_context_url_ && |
| 166 stack_trace_.size() == error->stack_trace_.size() && |
| 167 (stack_trace_.empty() || stack_trace_[0] == error->stack_trace_[0]); |
| 168 } |
| 169 |
| 170 void RuntimeError::ParseDetails(const string16& details) { |
| 133 scoped_ptr<base::Value> value( | 171 scoped_ptr<base::Value> value( |
| 134 base::JSONReader::Read(base::UTF16ToUTF8(details))); | 172 base::JSONReader::Read(base::UTF16ToUTF8(details))); |
| 135 const base::DictionaryValue* details_value; | 173 const base::DictionaryValue* details_value; |
| 136 const base::ListValue* trace_value = NULL; | 174 const base::ListValue* trace_value = NULL; |
| 137 | 175 |
| 138 // The |details| value should contain an execution context url and a stack | 176 // The |details| value should contain an execution context url and a stack |
| 139 // trace. | 177 // trace. |
| 140 if (!value.get() || | 178 if (!value.get() || |
| 141 !value->GetAsDictionary(&details_value) || | 179 !value->GetAsDictionary(&details_value) || |
| 142 !details_value->GetString(kExecutionContextURLKey, | 180 !details_value->GetString(kExecutionContextURLKey, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 157 frame_value->GetInteger(kLineNumberKey, &line); | 195 frame_value->GetInteger(kLineNumberKey, &line); |
| 158 frame_value->GetInteger(kColumnNumberKey, &column); | 196 frame_value->GetInteger(kColumnNumberKey, &column); |
| 159 frame_value->GetString(kURLKey, &url); | 197 frame_value->GetString(kURLKey, &url); |
| 160 | 198 |
| 161 string16 function; | 199 string16 function; |
| 162 frame_value->GetString(kFunctionNameKey, &function); // This can be empty. | 200 frame_value->GetString(kFunctionNameKey, &function); // This can be empty. |
| 163 stack_trace_.push_back(StackFrame(line, column, url, function)); | 201 stack_trace_.push_back(StackFrame(line, column, url, function)); |
| 164 } | 202 } |
| 165 } | 203 } |
| 166 | 204 |
| 167 void JavascriptRuntimeError::DetermineExtensionID() { | 205 void RuntimeError::DetermineExtensionID() { |
| 168 if (!GetExtensionIDFromGURL(GURL(source_), &extension_id_)) | 206 if (!GetExtensionIDFromGURL(GURL(source_), &extension_id_)) |
| 169 GetExtensionIDFromGURL(GURL(execution_context_url_), &extension_id_); | 207 GetExtensionIDFromGURL(GURL(execution_context_url_), &extension_id_); |
| 170 } | 208 } |
| 171 | 209 |
| 172 } // namespace extensions | 210 } // namespace extensions |
| OLD | NEW |