extension_error.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #ifndef EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_ERROR_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include <vector>
  9. #include "base/compiler_specific.h"
  10. #include "base/logging.h"
  11. #include "extensions/common/stack_frame.h"
  12. #include "url/gurl.h"
  13. namespace extensions {
  14. class ExtensionError {
  15. public:
  16. enum Type {
  17. MANIFEST_ERROR = 0,
  18. RUNTIME_ERROR,
  19. INTERNAL_ERROR,
  20. NUM_ERROR_TYPES, // Put new values above this.
  21. };
  22. ExtensionError(const ExtensionError&) = delete;
  23. ExtensionError& operator=(const ExtensionError&) = delete;
  24. virtual ~ExtensionError();
  25. virtual std::string GetDebugString() const;
  26. // Return true if this error and |rhs| are considered equal, and should be
  27. // grouped together.
  28. bool IsEqual(const ExtensionError* rhs) const;
  29. Type type() const { return type_; }
  30. const std::string& extension_id() const { return extension_id_; }
  31. int id() const { return id_; }
  32. void set_id(int id) { id_ = id; }
  33. bool from_incognito() const { return from_incognito_; }
  34. logging::LogSeverity level() const { return level_; }
  35. const std::u16string& source() const { return source_; }
  36. const std::u16string& message() const { return message_; }
  37. size_t occurrences() const { return occurrences_; }
  38. void set_occurrences(size_t occurrences) { occurrences_ = occurrences; }
  39. protected:
  40. ExtensionError(Type type,
  41. const std::string& extension_id,
  42. bool from_incognito,
  43. logging::LogSeverity level,
  44. const std::u16string& source,
  45. const std::u16string& message);
  46. virtual bool IsEqualImpl(const ExtensionError* rhs) const = 0;
  47. // Which type of error this is.
  48. Type type_;
  49. // The ID of the extension which caused the error.
  50. std::string extension_id_;
  51. // The id of this particular error. This can be zero if the id is never set.
  52. int id_;
  53. // Whether or not the error was caused while incognito.
  54. bool from_incognito_;
  55. // The severity level of the error.
  56. logging::LogSeverity level_;
  57. // The source for the error; this can be a script, web page, or manifest file.
  58. // This is stored as a string (rather than a url) since it can be a Chrome
  59. // script file (e.g., event_bindings.js).
  60. std::u16string source_;
  61. // The error message itself.
  62. std::u16string message_;
  63. // The number of times this error has occurred.
  64. size_t occurrences_;
  65. };
  66. class ManifestError : public ExtensionError {
  67. public:
  68. ManifestError(const std::string& extension_id,
  69. const std::u16string& message,
  70. const std::u16string& manifest_key,
  71. const std::u16string& manifest_specific);
  72. ManifestError(const ManifestError&) = delete;
  73. ManifestError& operator=(const ManifestError&) = delete;
  74. ~ManifestError() override;
  75. std::string GetDebugString() const override;
  76. const std::u16string& manifest_key() const { return manifest_key_; }
  77. const std::u16string& manifest_specific() const { return manifest_specific_; }
  78. private:
  79. bool IsEqualImpl(const ExtensionError* rhs) const override;
  80. // If present, this indicates the feature in the manifest which caused the
  81. // error.
  82. std::u16string manifest_key_;
  83. // If present, this is a more-specific location of the error - for instance,
  84. // a specific permission which is incorrect, rather than simply "permissions".
  85. std::u16string manifest_specific_;
  86. };
  87. class RuntimeError : public ExtensionError {
  88. public:
  89. RuntimeError(const std::string& extension_id, // optional, sometimes unknown.
  90. bool from_incognito,
  91. const std::u16string& source,
  92. const std::u16string& message,
  93. const StackTrace& stack_trace,
  94. const GURL& context_url,
  95. logging::LogSeverity level,
  96. int render_frame_id,
  97. int render_process_id);
  98. RuntimeError(const RuntimeError&) = delete;
  99. RuntimeError& operator=(const RuntimeError&) = delete;
  100. ~RuntimeError() override;
  101. std::string GetDebugString() const override;
  102. const GURL& context_url() const { return context_url_; }
  103. const StackTrace& stack_trace() const { return stack_trace_; }
  104. int render_frame_id() const { return render_frame_id_; }
  105. int render_process_id() const { return render_process_id_; }
  106. private:
  107. bool IsEqualImpl(const ExtensionError* rhs) const override;
  108. // Since we piggy-back onto other error reporting systems (like V8 and
  109. // WebKit), the reported information may need to be cleaned up in order to be
  110. // in a consistent format.
  111. void CleanUpInit();
  112. GURL context_url_;
  113. StackTrace stack_trace_;
  114. // Keep track of the render process which caused the error in order to
  115. // inspect the frame later, if possible.
  116. int render_frame_id_;
  117. int render_process_id_;
  118. };
  119. class InternalError : public ExtensionError {
  120. public:
  121. InternalError(const std::string& extension_id,
  122. const std::u16string& message,
  123. logging::LogSeverity level);
  124. InternalError(const InternalError&) = delete;
  125. InternalError& operator=(const InternalError&) = delete;
  126. ~InternalError() override;
  127. std::string GetDebugString() const override;
  128. private:
  129. bool IsEqualImpl(const ExtensionError* rhs) const override;
  130. };
  131. } // namespace extensions
  132. #endif // EXTENSIONS_BROWSER_EXTENSION_ERROR_H_