nsConsoleService.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. * nsConsoleService class declaration.
  7. */
  8. #ifndef __nsconsoleservice_h__
  9. #define __nsconsoleservice_h__
  10. #include "mozilla/Attributes.h"
  11. #include "mozilla/Mutex.h"
  12. #include "nsInterfaceHashtable.h"
  13. #include "nsHashKeys.h"
  14. #include "nsIConsoleService.h"
  15. class nsConsoleService final : public nsIConsoleService,
  16. public nsIObserver
  17. {
  18. public:
  19. nsConsoleService();
  20. nsresult Init();
  21. NS_DECL_THREADSAFE_ISUPPORTS
  22. NS_DECL_NSICONSOLESERVICE
  23. NS_DECL_NSIOBSERVER
  24. void SetIsDelivering()
  25. {
  26. MOZ_ASSERT(NS_IsMainThread());
  27. MOZ_ASSERT(!mDeliveringMessage);
  28. mDeliveringMessage = true;
  29. }
  30. void SetDoneDelivering()
  31. {
  32. MOZ_ASSERT(NS_IsMainThread());
  33. MOZ_ASSERT(mDeliveringMessage);
  34. mDeliveringMessage = false;
  35. }
  36. // This is a variant of LogMessage which allows the caller to determine
  37. // if the message should be output to an OS-specific log.
  38. enum OutputMode {
  39. SuppressLog,
  40. OutputToLog
  41. };
  42. virtual nsresult LogMessageWithMode(nsIConsoleMessage* aMessage,
  43. OutputMode aOutputMode);
  44. typedef nsInterfaceHashtable<nsISupportsHashKey,
  45. nsIConsoleListener> ListenerHash;
  46. void CollectCurrentListeners(nsCOMArray<nsIConsoleListener>& aListeners);
  47. private:
  48. class MessageElement : public mozilla::LinkedListElement<MessageElement>
  49. {
  50. public:
  51. explicit MessageElement(nsIConsoleMessage* aMessage) : mMessage(aMessage)
  52. {}
  53. nsIConsoleMessage* Get()
  54. {
  55. return mMessage.get();
  56. }
  57. // Swap directly into an nsCOMPtr to avoid spurious refcount
  58. // traffic off the main thread in debug builds from
  59. // NSCAP_ASSERT_NO_QUERY_NEEDED().
  60. void swapMessage(nsCOMPtr<nsIConsoleMessage>& aRetVal)
  61. {
  62. mMessage.swap(aRetVal);
  63. }
  64. ~MessageElement();
  65. private:
  66. nsCOMPtr<nsIConsoleMessage> mMessage;
  67. MessageElement(const MessageElement&) = delete;
  68. MessageElement& operator=(const MessageElement&) = delete;
  69. MessageElement(MessageElement&&) = delete;
  70. MessageElement& operator=(MessageElement&&) = delete;
  71. };
  72. ~nsConsoleService();
  73. void ClearMessagesForWindowID(const uint64_t innerID);
  74. void ClearMessages();
  75. mozilla::LinkedList<MessageElement> mMessages;
  76. // The current size of mMessages.
  77. uint32_t mCurrentSize;
  78. // The maximum size of mMessages.
  79. uint32_t mMaximumSize;
  80. // Are we currently delivering a console message on the main thread? If
  81. // so, we suppress incoming messages on the main thread only, to avoid
  82. // infinite repitition.
  83. bool mDeliveringMessage;
  84. // Listeners to notify whenever a new message is logged.
  85. ListenerHash mListeners;
  86. // To serialize interesting methods.
  87. mozilla::Mutex mLock;
  88. };
  89. #endif /* __nsconsoleservice_h__ */