1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * nsConsoleService class declaration.
11 #ifndef __nsconsoleservice_h__
12 #define __nsconsoleservice_h__
17 #include "mozilla/Assertions.h"
18 #include "mozilla/LinkedList.h"
19 #include "mozilla/Mutex.h"
21 #include "MainThreadUtils.h"
23 #include "nsInterfaceHashtable.h"
24 #include "nsHashKeys.h"
26 #include "nsIConsoleListener.h"
27 #include "nsIConsoleMessage.h"
28 #include "nsIConsoleService.h"
29 #include "nsIObserver.h"
30 #include "nsISupports.h"
35 class nsConsoleService final
: public nsIConsoleService
, public nsIObserver
{
40 NS_DECL_THREADSAFE_ISUPPORTS
41 NS_DECL_NSICONSOLESERVICE
44 void SetIsDelivering() {
45 MOZ_ASSERT(NS_IsMainThread());
46 MOZ_ASSERT(!mDeliveringMessage
);
47 mDeliveringMessage
= true;
50 void SetDoneDelivering() {
51 MOZ_ASSERT(NS_IsMainThread());
52 MOZ_ASSERT(mDeliveringMessage
);
53 mDeliveringMessage
= false;
56 typedef nsInterfaceHashtable
<nsISupportsHashKey
, nsIConsoleListener
>
58 void CollectCurrentListeners(nsCOMArray
<nsIConsoleListener
>& aListeners
);
61 class MessageElement
: public mozilla::LinkedListElement
<MessageElement
> {
63 explicit MessageElement(nsIConsoleMessage
* aMessage
) : mMessage(aMessage
) {}
65 nsIConsoleMessage
* Get() { return mMessage
.get(); }
67 // Swap directly into an nsCOMPtr to avoid spurious refcount
68 // traffic off the main thread in debug builds from
69 // NSCAP_ASSERT_NO_QUERY_NEEDED().
70 void swapMessage(nsCOMPtr
<nsIConsoleMessage
>& aRetVal
) {
71 mMessage
.swap(aRetVal
);
77 nsCOMPtr
<nsIConsoleMessage
> mMessage
;
79 MessageElement(const MessageElement
&) = delete;
80 MessageElement
& operator=(const MessageElement
&) = delete;
81 MessageElement(MessageElement
&&) = delete;
82 MessageElement
& operator=(MessageElement
&&) = delete;
87 nsresult
MaybeForwardScriptError(nsIConsoleMessage
* aMessage
, bool* sent
);
89 void ClearMessagesForWindowID(const uint64_t innerID
);
90 void ClearMessages() MOZ_REQUIRES(mLock
);
92 mozilla::LinkedList
<MessageElement
> mMessages
MOZ_GUARDED_BY(mLock
);
94 // The current size of mMessages.
95 uint32_t mCurrentSize
MOZ_GUARDED_BY(mLock
);
97 // The maximum size of mMessages.
98 const uint32_t mMaximumSize
;
100 // Are we currently delivering a console message on the main thread? If
101 // so, we suppress incoming messages on the main thread only, to avoid
102 // infinite repitition.
103 // Only touched on MainThread
104 bool mDeliveringMessage
;
106 // Listeners to notify whenever a new message is logged.
107 ListenerHash mListeners
MOZ_GUARDED_BY(mLock
);
109 // To serialize interesting methods.
110 mozilla::Mutex mLock
;
113 #endif /* __nsconsoleservice_h__ */