1558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// Copyright 2013 The Chromium Authors. All rights reserved. 2558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// Use of this source code is governed by a BSD-style license that can be 3558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch// found in the LICENSE file. 4558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 5ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#ifndef EXTENSIONS_BROWSER_EXTENSION_ERROR_H_ 6ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#define EXTENSIONS_BROWSER_EXTENSION_ERROR_H_ 7558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 8558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include <string> 9558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include <vector> 10558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 11558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "base/compiler_specific.h" 12558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "base/logging.h" 1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 14558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch#include "base/strings/string16.h" 1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "extensions/common/stack_frame.h" 1658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "url/gurl.h" 1758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 1858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)namespace base { 1958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)class DictionaryValue; 2058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)} 21558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 22558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochnamespace extensions { 23558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 24558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdochclass ExtensionError { 25558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch public: 26558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch enum Type { 27424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) MANIFEST_ERROR, 28424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) RUNTIME_ERROR 29558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch }; 30558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 31558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch virtual ~ExtensionError(); 32558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Serializes the ExtensionError into JSON format. 3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) virtual scoped_ptr<base::DictionaryValue> ToValue() const; 3558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 36558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch virtual std::string PrintForTest() const; 37558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 38424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // Return true if this error and |rhs| are considered equal, and should be 39424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // grouped together. 40424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) bool IsEqual(const ExtensionError* rhs) const; 41424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 42558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch Type type() const { return type_; } 43558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const std::string& extension_id() const { return extension_id_; } 44558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch bool from_incognito() const { return from_incognito_; } 45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) logging::LogSeverity level() const { return level_; } 46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::string16& source() const { return source_; } 47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::string16& message() const { return message_; } 48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size_t occurrences() const { return occurrences_; } 49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) void set_occurrences(size_t occurrences) { occurrences_ = occurrences; } 50558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 5158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Keys used for retrieving JSON values. 5258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kExtensionIdKey[]; 5358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kFromIncognitoKey[]; 5458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kLevelKey[]; 5558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kMessageKey[]; 5658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kSourceKey[]; 5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kTypeKey[]; 5858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 59558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch protected: 60558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch ExtensionError(Type type, 61ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch const std::string& extension_id, 62558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch bool from_incognito, 63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) logging::LogSeverity level, 64558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const base::string16& source, 65558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const base::string16& message); 66558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual bool IsEqualImpl(const ExtensionError* rhs) const = 0; 68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 69558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // Which type of error this is. 70558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch Type type_; 71ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch // The ID of the extension which caused the error. 72ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch std::string extension_id_; 73558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // Whether or not the error was caused while incognito. 74558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch bool from_incognito_; 75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // The severity level of the error. 76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) logging::LogSeverity level_; 77558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // The source for the error; this can be a script, web page, or manifest file. 78558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // This is stored as a string (rather than a url) since it can be a Chrome 79558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // script file (e.g., event_bindings.js). 80558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch base::string16 source_; 81558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch // The error message itself. 82558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch base::string16 message_; 83424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) // The number of times this error has occurred. 84424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) size_t occurrences_; 85558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 86558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch DISALLOW_COPY_AND_ASSIGN(ExtensionError); 87558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}; 88558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 89424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class ManifestError : public ExtensionError { 90558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch public: 91424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) ManifestError(const std::string& extension_id, 9258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::string16& message, 9358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::string16& manifest_key, 9458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::string16& manifest_specific); 95424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual ~ManifestError(); 96558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 9758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) virtual scoped_ptr<base::DictionaryValue> ToValue() const OVERRIDE; 9858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 99558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch virtual std::string PrintForTest() const OVERRIDE; 10058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 10158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::string16& manifest_key() const { return manifest_key_; } 10258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const base::string16& manifest_specific() const { return manifest_specific_; } 10358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 10458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Keys used for retrieving JSON values. 10558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kManifestKeyKey[]; 10658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kManifestSpecificKey[]; 10758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 108558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch private: 109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE; 110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // If present, this indicates the feature in the manifest which caused the 11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // error. 11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::string16 manifest_key_; 11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // If present, this is a more-specific location of the error - for instance, 11558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // a specific permission which is incorrect, rather than simply "permissions". 11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) base::string16 manifest_specific_; 11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 118424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ManifestError); 119558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}; 120558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 121424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)class RuntimeError : public ExtensionError { 122558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch public: 12358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) RuntimeError(const std::string& extension_id, // optional, sometimes unknown. 12458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) bool from_incognito, 125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::string16& source, 126424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) const base::string16& message, 12758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const StackTrace& stack_trace, 12858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const GURL& context_url, 1290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) logging::LogSeverity level, 1300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_view_id, 1310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_process_id); 132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual ~RuntimeError(); 133558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) virtual scoped_ptr<base::DictionaryValue> ToValue() const OVERRIDE; 13558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 136558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch virtual std::string PrintForTest() const OVERRIDE; 137558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 13858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) const GURL& context_url() const { return context_url_; } 139558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch const StackTrace& stack_trace() const { return stack_trace_; } 1400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_view_id() const { return render_view_id_; } 1410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_process_id() const { return render_process_id_; } 14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Keys used for retrieving JSON values. 14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kColumnNumberKey[]; 14558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kContextUrlKey[]; 14658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kFunctionNameKey[]; 14758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kLineNumberKey[]; 14858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kStackTraceKey[]; 14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) static const char kUrlKey[]; 1500f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) static const char kRenderProcessIdKey[]; 1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) static const char kRenderViewIdKey[]; 15258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) 153558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch private: 154424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) virtual bool IsEqualImpl(const ExtensionError* rhs) const OVERRIDE; 155424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) 15658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // Since we piggy-back onto other error reporting systems (like V8 and 15758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // WebKit), the reported information may need to be cleaned up in order to be 15858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) // in a consistent format. 15958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) void CleanUpInit(); 160558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 16158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles) GURL context_url_; 162558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch StackTrace stack_trace_; 163558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // Keep track of the render process which caused the error in order to 1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // inspect the view later, if possible. 1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_view_id_; 1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) int render_process_id_; 1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(RuntimeError); 170558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch}; 171558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 172558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch} // namespace extensions 173558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch 174ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch#endif // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_ 175