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/memory/scoped_ptr.h" 14 #include "base/strings/string16.h" 15 #include "extensions/common/stack_frame.h" 16 #include "url/gurl.h" 17 18 namespace base { 19 class DictionaryValue; 20 } 21 22 namespace extensions { 23 24 class ExtensionError { 25 public: 26 enum Type { 27 MANIFEST_ERROR, 28 RUNTIME_ERROR, 29 NUM_ERROR_TYPES // Put new values above this. 30 }; 31 32 virtual ~ExtensionError(); 33 34 // Serializes the ExtensionError into JSON format. 35 virtual scoped_ptr<base::DictionaryValue> ToValue() const; 36 37 virtual std::string PrintForTest() const; 38 39 // Return true if this error and |rhs| are considered equal, and should be 40 // grouped together. 41 bool IsEqual(const ExtensionError* rhs) const; 42 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; } 51 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[]; 59 60 protected: 61 ExtensionError(Type type, 62 const std::string& extension_id, 63 bool from_incognito, 64 logging::LogSeverity level, 65 const base::string16& source, 66 const base::string16& message); 67 68 virtual bool IsEqualImpl(const ExtensionError* rhs) const = 0; 69 70 // Which type of error this is. 71 Type type_; 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. 75 bool from_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. 85 size_t occurrences_; 86 87 private: 88 DISALLOW_COPY_AND_ASSIGN(ExtensionError); 89 }; 90 91 class ManifestError : public ExtensionError { 92 public: 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 virtual ~ManifestError(); 98 99 virtual scoped_ptr<base::DictionaryValue> ToValue() const OVERRIDE; 100 101 virtual std::string PrintForTest() const OVERRIDE; 102 103 const base::string16& manifest_key() const { return manifest_key_; } 104 const base::string16& manifest_specific() const { return manifest_specific_; } 105 106 // Keys used for retrieving JSON values. 107 static const char kManifestKeyKey[]; 108 static const char kManifestSpecificKey[]; 109 110 private: 111 virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE; 112 113 // If present, this indicates the feature in the manifest which caused the 114 // error. 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_; 119 120 DISALLOW_COPY_AND_ASSIGN(ManifestError); 121 }; 122 123 class RuntimeError : public ExtensionError { 124 public: 125 RuntimeError(const std::string& extension_id, // optional, sometimes unknown. 126 bool from_incognito, 127 const base::string16& source, 128 const base::string16& message, 129 const StackTrace& stack_trace, 130 const GURL& context_url, 131 logging::LogSeverity level, 132 int render_view_id, 133 int render_process_id); 134 virtual ~RuntimeError(); 135 136 virtual scoped_ptr<base::DictionaryValue> ToValue() const OVERRIDE; 137 138 virtual std::string PrintForTest() const OVERRIDE; 139 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_; } 144 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[]; 154 155 private: 156 virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE; 157 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. 161 void CleanUpInit(); 162 163 GURL context_url_; 164 StackTrace stack_trace_; 165 166 // Keep track of the render process which caused the error in order to 167 // inspect the view later, if possible. 168 int render_view_id_; 169 int render_process_id_; 170 171 DISALLOW_COPY_AND_ASSIGN(RuntimeError); 172 }; 173 174 } // namespace extensions 175 176 #endif // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_ 177