2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-02-28 03:39:00 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/Console.h"
|
|
|
|
#include "mozilla/dom/ConsoleBinding.h"
|
|
|
|
|
2015-02-02 18:40:15 +03:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2014-02-28 03:39:00 +04:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2015-02-02 18:40:15 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2016-03-24 00:55:07 +03:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
2016-06-09 20:04:42 +03:00
|
|
|
#include "mozilla/dom/Performance.h"
|
2015-09-30 15:22:08 +03:00
|
|
|
#include "mozilla/dom/StructuredCloneHolder.h"
|
2014-04-16 06:58:44 +04:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2016-11-06 11:54:52 +03:00
|
|
|
#include "mozilla/dom/WorkletGlobalScope.h"
|
2014-04-16 06:58:44 +04:00
|
|
|
#include "mozilla/Maybe.h"
|
2014-02-28 03:39:00 +04:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
|
|
|
#include "nsDocument.h"
|
|
|
|
#include "nsDOMNavigationTiming.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsJSUtils.h"
|
2015-09-14 22:38:57 +03:00
|
|
|
#include "nsNetUtil.h"
|
2014-05-30 13:38:30 +04:00
|
|
|
#include "ScriptSettings.h"
|
2014-02-28 03:39:30 +04:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
2016-06-04 10:32:51 +03:00
|
|
|
#include "WorkerScope.h"
|
2016-08-04 21:35:13 +03:00
|
|
|
#include "xpcpublic.h"
|
2014-04-16 06:58:44 +04:00
|
|
|
#include "nsContentUtils.h"
|
2014-10-23 01:51:00 +04:00
|
|
|
#include "nsDocShell.h"
|
2015-04-03 22:37:52 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2015-08-31 15:56:37 +03:00
|
|
|
#include "mozilla/ConsoleTimelineMarker.h"
|
|
|
|
#include "mozilla/TimestampTimelineMarker.h"
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
#include "nsIConsoleAPIStorage.h"
|
|
|
|
#include "nsIDOMWindowUtils.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsILoadContext.h"
|
2015-03-30 20:45:39 +03:00
|
|
|
#include "nsIProgrammingLanguage.h"
|
2015-09-14 22:38:57 +03:00
|
|
|
#include "nsISensitiveInfoHiddenURI.h"
|
2014-02-28 03:39:00 +04:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIWebNavigation.h"
|
2015-01-06 18:45:00 +03:00
|
|
|
#include "nsIXPConnect.h"
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
// The maximum allowed number of concurrent timers per page.
|
|
|
|
#define MAX_PAGE_TIMERS 10000
|
|
|
|
|
2014-02-28 03:39:24 +04:00
|
|
|
// The maximum allowed number of concurrent counters per page.
|
|
|
|
#define MAX_PAGE_COUNTERS 10000
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
// The maximum stacktrace depth when populating the stacktrace array used for
|
|
|
|
// console.trace().
|
|
|
|
#define DEFAULT_MAX_STACKTRACE_DEPTH 200
|
|
|
|
|
2015-02-02 18:40:15 +03:00
|
|
|
// This tags are used in the Structured Clone Algorithm to move js values from
|
2014-02-28 03:39:30 +04:00
|
|
|
// worker thread to main thread
|
2015-02-05 21:50:43 +03:00
|
|
|
#define CONSOLE_TAG_BLOB JS_SCTAG_USER_MIN
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// This value is taken from ConsoleAPIStorage.js
|
2016-04-19 15:10:15 +03:00
|
|
|
#define STORAGE_MAX_EVENTS 1000
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
using namespace mozilla::dom::exceptions;
|
2014-02-28 03:39:30 +04:00
|
|
|
using namespace mozilla::dom::workers;
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2015-02-02 18:40:15 +03:00
|
|
|
struct
|
|
|
|
ConsoleStructuredCloneData
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> mParent;
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<BlobImpl>> mBlobs;
|
2015-02-02 18:40:15 +03:00
|
|
|
};
|
|
|
|
|
2014-02-28 03:39:30 +04:00
|
|
|
/**
|
|
|
|
* Console API in workers uses the Structured Clone Algorithm to move any value
|
|
|
|
* from the worker thread to the main-thread. Some object cannot be moved and,
|
|
|
|
* in these cases, we convert them to strings.
|
|
|
|
* It's not the best, but at least we are able to show something.
|
|
|
|
*/
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class ConsoleCallData final
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
public:
|
2016-02-07 13:29:12 +03:00
|
|
|
NS_INLINE_DECL_REFCOUNTING(ConsoleCallData)
|
2015-04-03 22:37:52 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
ConsoleCallData()
|
|
|
|
: mMethodName(Console::MethodLog)
|
2014-05-12 21:03:55 +04:00
|
|
|
, mTimeStamp(JS_Now() / PR_USEC_PER_MSEC)
|
2016-02-11 20:09:22 +03:00
|
|
|
, mStartTimerValue(0)
|
|
|
|
, mStartTimerStatus(false)
|
|
|
|
, mStopTimerDuration(0)
|
|
|
|
, mStopTimerStatus(false)
|
|
|
|
, mCountValue(MAX_PAGE_COUNTERS)
|
2015-01-06 18:45:00 +03:00
|
|
|
, mIDType(eUnknown)
|
|
|
|
, mOuterIDNumber(0)
|
|
|
|
, mInnerIDNumber(0)
|
2016-04-25 08:53:49 +03:00
|
|
|
, mStatus(eUnused)
|
2016-02-07 13:29:12 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mOwningThread(PR_GetCurrentThread())
|
|
|
|
#endif
|
2016-03-24 00:55:07 +03:00
|
|
|
{}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
bool
|
2014-04-17 00:51:07 +04:00
|
|
|
Initialize(JSContext* aCx, Console::MethodName aName,
|
2016-02-07 13:29:12 +03:00
|
|
|
const nsAString& aString,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
Console* aConsole)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-02-07 13:29:12 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(aConsole);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// We must be registered before doing any JS operation otherwise it can
|
|
|
|
// happen that mCopiedArguments are not correctly traced.
|
|
|
|
aConsole->StoreCallData(this);
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
mMethodName = aName;
|
|
|
|
mMethodString = aString;
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mGlobal = JS::CurrentGlobalOrNull(aCx);
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
for (uint32_t i = 0; i < aArguments.Length(); ++i) {
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!mCopiedArguments.AppendElement(aArguments[i]))) {
|
2016-03-24 00:55:07 +03:00
|
|
|
aConsole->UnstoreCallData(this);
|
2016-03-24 00:55:07 +03:00
|
|
|
return false;
|
2015-05-25 14:50:15 +03:00
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
return true;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
void
|
|
|
|
SetIDs(uint64_t aOuterID, uint64_t aInnerID)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIDType == eUnknown);
|
|
|
|
|
|
|
|
mOuterIDNumber = aOuterID;
|
|
|
|
mInnerIDNumber = aInnerID;
|
|
|
|
mIDType = eNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetIDs(const nsAString& aOuterID, const nsAString& aInnerID)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIDType == eUnknown);
|
|
|
|
|
|
|
|
mOuterIDString = aOuterID;
|
|
|
|
mInnerIDString = aInnerID;
|
|
|
|
mIDType = eString;
|
|
|
|
}
|
|
|
|
|
2016-04-01 14:55:40 +03:00
|
|
|
void
|
2017-01-12 19:38:48 +03:00
|
|
|
SetOriginAttributes(const OriginAttributes& aOriginAttributes)
|
2016-04-01 14:55:40 +03:00
|
|
|
{
|
|
|
|
mOriginAttributes = aOriginAttributes;
|
|
|
|
}
|
|
|
|
|
2017-02-27 00:13:45 +03:00
|
|
|
void
|
|
|
|
SetAddonId(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
nsAutoString addonId;
|
|
|
|
aPrincipal->GetAddonId(addonId);
|
|
|
|
|
|
|
|
mAddonId = addonId;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
bool
|
|
|
|
PopulateArgumentsSequence(Sequence<JS::Value>& aSequence) const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCopiedArguments.Length(); ++i) {
|
|
|
|
if (NS_WARN_IF(!aSequence.AppendElement(mCopiedArguments[i],
|
|
|
|
fallible))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
void
|
|
|
|
Trace(const TraceCallbacks& aCallbacks, void* aClosure)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
ConsoleCallData* tmp = this;
|
|
|
|
for (uint32_t i = 0; i < mCopiedArguments.Length(); ++i) {
|
2016-02-22 21:11:02 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mCopiedArguments[i])
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mGlobal);
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AssertIsOnOwningThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread);
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Heap<JSObject*> mGlobal;
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
// This is a copy of the arguments we received from the DOM bindings. Console
|
|
|
|
// object traces them because this ConsoleCallData calls
|
|
|
|
// RegisterConsoleCallData() in the Initialize().
|
|
|
|
nsTArray<JS::Heap<JS::Value>> mCopiedArguments;
|
2014-04-17 00:51:07 +04:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
Console::MethodName mMethodName;
|
|
|
|
int64_t mTimeStamp;
|
2016-02-11 20:09:22 +03:00
|
|
|
|
|
|
|
// These values are set in the owning thread and they contain the timestamp of
|
|
|
|
// when the new timer has started, the name of it and the status of the
|
|
|
|
// creation of it. If status is false, something went wrong. User
|
|
|
|
// DOMHighResTimeStamp instead mozilla::TimeStamp because we use
|
|
|
|
// monotonicTimer from Performance.now();
|
|
|
|
// They will be set on the owning thread and never touched again on that
|
2016-03-24 00:55:07 +03:00
|
|
|
// thread. They will be used in order to create a ConsoleTimerStart dictionary
|
|
|
|
// when console.time() is used.
|
2016-02-11 20:09:22 +03:00
|
|
|
DOMHighResTimeStamp mStartTimerValue;
|
|
|
|
nsString mStartTimerLabel;
|
|
|
|
bool mStartTimerStatus;
|
|
|
|
|
|
|
|
// These values are set in the owning thread and they contain the duration,
|
|
|
|
// the name and the status of the StopTimer method. If status is false,
|
|
|
|
// something went wrong. They will be set on the owning thread and never
|
2016-03-24 00:55:07 +03:00
|
|
|
// touched again on that thread. They will be used in order to create a
|
|
|
|
// ConsoleTimerEnd dictionary. This members are set when
|
2016-02-11 20:09:22 +03:00
|
|
|
// console.timeEnd() is called.
|
|
|
|
double mStopTimerDuration;
|
|
|
|
nsString mStopTimerLabel;
|
|
|
|
bool mStopTimerStatus;
|
|
|
|
|
|
|
|
// These 2 values are set by IncreaseCounter on the owning thread and they are
|
2016-03-24 00:55:07 +03:00
|
|
|
// used CreateCounterValue. These members are set when console.count() is
|
|
|
|
// called.
|
2016-02-11 20:09:22 +03:00
|
|
|
nsString mCountLabel;
|
|
|
|
uint32_t mCountValue;
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
// The concept of outerID and innerID is misleading because when a
|
|
|
|
// ConsoleCallData is created from a window, these are the window IDs, but
|
|
|
|
// when the object is created from a SharedWorker, a ServiceWorker or a
|
|
|
|
// subworker of a ChromeWorker these IDs are the type of worker and the
|
|
|
|
// filename of the callee.
|
|
|
|
// In Console.jsm the ID is 'jsm'.
|
|
|
|
enum {
|
|
|
|
eString,
|
|
|
|
eNumber,
|
|
|
|
eUnknown
|
|
|
|
} mIDType;
|
|
|
|
|
|
|
|
uint64_t mOuterIDNumber;
|
|
|
|
nsString mOuterIDString;
|
|
|
|
|
|
|
|
uint64_t mInnerIDNumber;
|
|
|
|
nsString mInnerIDString;
|
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes mOriginAttributes;
|
2016-04-01 14:55:40 +03:00
|
|
|
|
2017-02-27 00:13:45 +03:00
|
|
|
nsString mAddonId;
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
nsString mMethodString;
|
2014-04-16 06:58:44 +04:00
|
|
|
|
|
|
|
// Stack management is complicated, because we want to do it as
|
|
|
|
// lazily as possible. Therefore, we have the following behavior:
|
|
|
|
// 1) mTopStackFrame is initialized whenever we have any JS on the stack
|
|
|
|
// 2) mReifiedStack is initialized if we're created in a worker.
|
|
|
|
// 3) mStack is set (possibly to null if there is no JS on the stack) if
|
|
|
|
// we're created on main thread.
|
|
|
|
Maybe<ConsoleStackEntry> mTopStackFrame;
|
|
|
|
Maybe<nsTArray<ConsoleStackEntry>> mReifiedStack;
|
|
|
|
nsCOMPtr<nsIStackFrame> mStack;
|
2015-04-03 22:37:52 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// mStatus is about the lifetime of this object. Console must take care of
|
|
|
|
// keep it alive or not following this enumeration.
|
|
|
|
enum {
|
|
|
|
// If the object is created but it is owned by some runnable, this is its
|
|
|
|
// status. It can be deleted at any time.
|
|
|
|
eUnused,
|
|
|
|
|
|
|
|
// When a runnable takes ownership of a ConsoleCallData and send it to
|
|
|
|
// different thread, this is its status. Console cannot delete it at this
|
|
|
|
// time.
|
|
|
|
eInUse,
|
|
|
|
|
|
|
|
// When a runnable owns this ConsoleCallData, we can't delete it directly.
|
|
|
|
// instead, we mark it with this new status and we move it in
|
|
|
|
// mCallDataStoragePending list in order to keep it alive an trace it
|
|
|
|
// correctly. Once the runnable finishs its task, it will delete this object
|
|
|
|
// calling ReleaseCallData().
|
|
|
|
eToBeDeleted
|
|
|
|
} mStatus;
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
PRThread* mOwningThread;
|
|
|
|
#endif
|
|
|
|
|
2015-04-03 22:37:52 +03:00
|
|
|
private:
|
|
|
|
~ConsoleCallData()
|
2016-02-07 13:29:12 +03:00
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
2016-03-24 00:55:07 +03:00
|
|
|
MOZ_ASSERT(mStatus != eInUse);
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class is used to clear any exception at the end of this method.
|
|
|
|
class ClearException
|
|
|
|
{
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit ClearException(JSContext* aCx)
|
2014-02-28 03:39:00 +04:00
|
|
|
: mCx(aCx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~ClearException()
|
|
|
|
{
|
|
|
|
JS_ClearPendingException(mCx);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSContext* mCx;
|
|
|
|
};
|
|
|
|
|
2016-07-04 09:19:10 +03:00
|
|
|
class ConsoleRunnable : public WorkerProxyToMainThreadRunnable
|
2015-09-30 15:22:08 +03:00
|
|
|
, public StructuredCloneHolderBase
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-07 02:00:50 +03:00
|
|
|
explicit ConsoleRunnable(Console* aConsole)
|
2016-07-04 09:19:10 +03:00
|
|
|
: WorkerProxyToMainThreadRunnable(GetCurrentThreadWorkerPrivate())
|
2015-01-06 18:45:00 +03:00
|
|
|
, mConsole(aConsole)
|
2016-07-04 09:19:10 +03:00
|
|
|
{}
|
2014-02-28 03:39:30 +04:00
|
|
|
|
|
|
|
virtual
|
|
|
|
~ConsoleRunnable()
|
|
|
|
{
|
2015-09-30 15:22:08 +03:00
|
|
|
// Clear the StructuredCloneHolderBase class.
|
|
|
|
Clear();
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-03-24 00:55:07 +03:00
|
|
|
Dispatch(JSContext* aCx)
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
2016-07-04 09:19:10 +03:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!PreDispatch(aCx))) {
|
|
|
|
RunBackOnWorkerThread();
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-04 09:19:10 +03:00
|
|
|
if (NS_WARN_IF(!WorkerProxyToMainThreadRunnable::Dispatch())) {
|
2016-08-16 10:35:21 +03:00
|
|
|
// RunBackOnWorkerThread() will be called by
|
|
|
|
// WorkerProxyToMainThreadRunnable::Dispatch().
|
2016-07-04 09:19:10 +03:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-15 13:46:40 +03:00
|
|
|
|
2015-04-03 22:37:52 +03:00
|
|
|
return true;
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2016-07-04 09:19:10 +03:00
|
|
|
protected:
|
|
|
|
void
|
|
|
|
RunOnMainThread() override
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
// Walk up to our containing page
|
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowInner* window = wp->GetWindow();
|
2015-01-06 18:45:00 +03:00
|
|
|
if (!window) {
|
|
|
|
RunWindowless();
|
|
|
|
} else {
|
|
|
|
RunWithWindow(window);
|
|
|
|
}
|
2015-04-15 13:46:40 +03:00
|
|
|
}
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
RunWithWindow(nsPIDOMWindowInner* aWindow)
|
2015-01-06 18:45:00 +03:00
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
MOZ_ASSERT(aWindow);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
RefPtr<nsGlobalWindow> win = nsGlobalWindow::Cast(aWindow);
|
2015-01-06 18:45:00 +03:00
|
|
|
if (NS_WARN_IF(!jsapi.Init(win))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aWindow->IsInnerWindow());
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter* outerWindow = aWindow->GetOuterWindow();
|
2015-11-23 20:16:22 +03:00
|
|
|
if (NS_WARN_IF(!outerWindow)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-06 18:45:00 +03:00
|
|
|
|
|
|
|
RunConsole(jsapi.cx(), outerWindow, aWindow);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RunWindowless()
|
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
WorkerPrivate* wp = mWorkerPrivate;
|
|
|
|
while (wp->GetParent()) {
|
|
|
|
wp = wp->GetParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(!wp->GetWindow());
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
JS::Rooted<JSObject*> global(cx, mConsole->GetOrCreateSandbox(cx, wp->GetPrincipal()));
|
2015-01-06 18:45:00 +03:00
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The CreateSandbox call returns a proxy to the actual sandbox object. We
|
|
|
|
// don't need a proxy here.
|
|
|
|
global = js::UncheckedUnwrap(global);
|
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, global);
|
|
|
|
|
|
|
|
RunConsole(cx, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
|
2016-07-04 09:19:10 +03:00
|
|
|
void
|
|
|
|
RunBackOnWorkerThread() override
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
ReleaseData();
|
|
|
|
mConsole = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
// This method is called in the owning thread of the Console object.
|
2014-02-28 03:39:30 +04:00
|
|
|
virtual bool
|
2016-03-24 00:55:07 +03:00
|
|
|
PreDispatch(JSContext* aCx) = 0;
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
// This method is called in the main-thread.
|
2014-02-28 03:39:30 +04:00
|
|
|
virtual void
|
2016-01-30 20:05:36 +03:00
|
|
|
RunConsole(JSContext* aCx, nsPIDOMWindowOuter* aOuterWindow,
|
|
|
|
nsPIDOMWindowInner* aInnerWindow) = 0;
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
// This method is called in the owning thread of the Console object.
|
|
|
|
virtual void
|
|
|
|
ReleaseData() = 0;
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
virtual JSObject* CustomReadHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* aReader,
|
|
|
|
uint32_t aTag,
|
|
|
|
uint32_t aIndex) override
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (aTag == CONSOLE_TAG_BLOB) {
|
|
|
|
MOZ_ASSERT(mClonedData.mBlobs.Length() > aIndex);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob =
|
2015-07-19 10:54:41 +03:00
|
|
|
Blob::Create(mClonedData.mParent, mClonedData.mBlobs.ElementAt(aIndex));
|
|
|
|
if (!ToJSValue(aCx, blob, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_CRASH("No other tags are supported.");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
virtual bool CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
|
|
|
JS::Handle<JSObject*> aObj) override
|
2015-07-19 10:54:41 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Blob> blob;
|
2015-07-19 10:54:41 +03:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, aObj, blob)) &&
|
|
|
|
blob->Impl()->MayBeClonedToOtherThreads()) {
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS_WriteUint32Pair(aWriter, CONSOLE_TAG_BLOB,
|
|
|
|
mClonedData.mBlobs.Length()))) {
|
2015-07-19 10:54:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mClonedData.mBlobs.AppendElement(blob->Impl());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-19 11:26:00 +03:00
|
|
|
if (!JS_ObjectNotWritten(aWriter, aObj)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectOrNullValue(aObj));
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2015-07-19 10:54:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS_WriteString(aWriter, jsString))) {
|
2015-07-19 10:54:41 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-15 13:46:40 +03:00
|
|
|
// This must be released on the worker thread.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Console> mConsole;
|
2015-07-19 10:54:41 +03:00
|
|
|
|
|
|
|
ConsoleStructuredCloneData mClonedData;
|
2014-02-28 03:39:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable appends a CallData object into the Console queue running on
|
|
|
|
// the main-thread.
|
2015-03-21 19:28:04 +03:00
|
|
|
class ConsoleCallDataRunnable final : public ConsoleRunnable
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-06 18:45:00 +03:00
|
|
|
ConsoleCallDataRunnable(Console* aConsole,
|
|
|
|
ConsoleCallData* aCallData)
|
|
|
|
: ConsoleRunnable(aConsole)
|
|
|
|
, mCallData(aCallData)
|
2016-03-24 00:55:07 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCallData);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mCallData->AssertIsOnOwningThread();
|
|
|
|
}
|
2014-02-28 03:39:30 +04:00
|
|
|
|
|
|
|
private:
|
2016-03-24 00:55:07 +03:00
|
|
|
~ConsoleCallDataRunnable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mCallData);
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:30 +04:00
|
|
|
bool
|
2016-03-24 00:55:07 +03:00
|
|
|
PreDispatch(JSContext* aCx) override
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
2015-01-06 18:45:00 +03:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-03-24 00:55:07 +03:00
|
|
|
mCallData->AssertIsOnOwningThread();
|
2015-01-06 18:45:00 +03:00
|
|
|
|
2014-02-28 03:39:30 +04:00
|
|
|
ClearException ce(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> arguments(aCx,
|
2016-02-07 13:29:12 +03:00
|
|
|
JS_NewArrayObject(aCx, mCallData->mCopiedArguments.Length()));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!arguments)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-30 13:10:33 +04:00
|
|
|
JS::Rooted<JS::Value> arg(aCx);
|
2016-02-07 13:29:12 +03:00
|
|
|
for (uint32_t i = 0; i < mCallData->mCopiedArguments.Length(); ++i) {
|
|
|
|
arg = mCallData->mCopiedArguments[i];
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS_DefineElement(aCx, arguments, i, arg,
|
|
|
|
JSPROP_ENUMERATE))) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*arguments));
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!Write(aCx, value))) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mCallData->mStatus = ConsoleCallData::eInUse;
|
2014-02-28 03:39:30 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
RunConsole(JSContext* aCx, nsPIDOMWindowOuter* aOuterWindow,
|
|
|
|
nsPIDOMWindowInner* aInnerWindow) override
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
// The windows have to run in parallel.
|
|
|
|
MOZ_ASSERT(!!aOuterWindow == !!aInnerWindow);
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
if (aOuterWindow) {
|
|
|
|
mCallData->SetIDs(aOuterWindow->WindowID(), aInnerWindow->WindowID());
|
|
|
|
} else {
|
|
|
|
ConsoleStackEntry frame;
|
|
|
|
if (mCallData->mTopStackFrame) {
|
|
|
|
frame = *mCallData->mTopStackFrame;
|
|
|
|
}
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-11-05 23:03:31 +03:00
|
|
|
nsString id = frame.mFilename;
|
|
|
|
nsString innerID;
|
2015-01-06 18:45:00 +03:00
|
|
|
if (mWorkerPrivate->IsSharedWorker()) {
|
2015-11-05 23:03:31 +03:00
|
|
|
innerID = NS_LITERAL_STRING("SharedWorker");
|
2015-01-06 18:45:00 +03:00
|
|
|
} else if (mWorkerPrivate->IsServiceWorker()) {
|
2015-11-05 23:03:31 +03:00
|
|
|
innerID = NS_LITERAL_STRING("ServiceWorker");
|
|
|
|
// Use scope as ID so the webconsole can decide if the message should
|
|
|
|
// show up per tab
|
2017-03-25 02:56:48 +03:00
|
|
|
id.AssignWithConversion(mWorkerPrivate->ServiceWorkerScope());
|
2015-01-06 18:45:00 +03:00
|
|
|
} else {
|
2015-11-05 23:03:31 +03:00
|
|
|
innerID = NS_LITERAL_STRING("Worker");
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
2014-05-30 13:38:30 +04:00
|
|
|
|
2015-11-05 23:03:31 +03:00
|
|
|
mCallData->SetIDs(id, innerID);
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2015-02-02 18:40:15 +03:00
|
|
|
// Now we could have the correct window (if we are not window-less).
|
2015-07-19 10:54:41 +03:00
|
|
|
mClonedData.mParent = aInnerWindow;
|
2015-02-02 18:40:15 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
ProcessCallData(aCx);
|
2015-02-02 18:40:15 +03:00
|
|
|
|
2015-07-19 10:54:41 +03:00
|
|
|
mClonedData.mParent = nullptr;
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
virtual void
|
|
|
|
ReleaseData() override
|
|
|
|
{
|
2016-03-24 00:55:07 +03:00
|
|
|
mConsole->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (mCallData->mStatus == ConsoleCallData::eToBeDeleted) {
|
|
|
|
mConsole->ReleaseCallData(mCallData);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(mCallData->mStatus == ConsoleCallData::eInUse);
|
|
|
|
mCallData->mStatus = ConsoleCallData::eUnused;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
mCallData = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
void
|
|
|
|
ProcessCallData(JSContext* aCx)
|
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
ClearException ce(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> argumentsValue(aCx);
|
2015-07-19 10:54:41 +03:00
|
|
|
if (!Read(aCx, &argumentsValue)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(argumentsValue.isObject());
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
JS::Rooted<JSObject*> argumentsObj(aCx, &argumentsValue.toObject());
|
2014-02-28 03:39:30 +04:00
|
|
|
|
|
|
|
uint32_t length;
|
2015-01-06 18:45:00 +03:00
|
|
|
if (!JS_GetArrayLength(aCx, argumentsObj, &length)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
Sequence<JS::Value> values;
|
|
|
|
SequenceRooter<JS::Value> arguments(aCx, &values);
|
|
|
|
|
2014-02-28 03:39:30 +04:00
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-01-06 18:45:00 +03:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
if (!JS_GetElement(aCx, argumentsObj, i, &value)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
if (!values.AppendElement(value, fallible)) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
MOZ_ASSERT(values.Length() == length);
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mConsole->ProcessCallData(aCx, mCallData, values);
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ConsoleCallData> mCallData;
|
2014-02-28 03:39:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This runnable calls ProfileMethod() on the console on the main-thread.
|
2015-03-21 19:28:04 +03:00
|
|
|
class ConsoleProfileRunnable final : public ConsoleRunnable
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-06 18:45:00 +03:00
|
|
|
ConsoleProfileRunnable(Console* aConsole, const nsAString& aAction,
|
2014-02-28 03:39:30 +04:00
|
|
|
const Sequence<JS::Value>& aArguments)
|
2015-01-06 18:45:00 +03:00
|
|
|
: ConsoleRunnable(aConsole)
|
|
|
|
, mAction(aAction)
|
2014-02-28 03:39:30 +04:00
|
|
|
, mArguments(aArguments)
|
|
|
|
{
|
2015-01-06 18:45:00 +03:00
|
|
|
MOZ_ASSERT(aConsole);
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool
|
2016-03-24 00:55:07 +03:00
|
|
|
PreDispatch(JSContext* aCx) override
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
|
|
|
ClearException ce(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> arguments(aCx,
|
|
|
|
JS_NewArrayObject(aCx, mArguments.Length()));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!arguments)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-04-30 13:10:33 +04:00
|
|
|
JS::Rooted<JS::Value> arg(aCx);
|
2014-02-28 03:39:30 +04:00
|
|
|
for (uint32_t i = 0; i < mArguments.Length(); ++i) {
|
2014-04-30 13:10:33 +04:00
|
|
|
arg = mArguments[i];
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS_DefineElement(aCx, arguments, i, arg,
|
|
|
|
JSPROP_ENUMERATE))) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*arguments));
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!Write(aCx, value))) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-30 20:05:36 +03:00
|
|
|
RunConsole(JSContext* aCx, nsPIDOMWindowOuter* aOuterWindow,
|
|
|
|
nsPIDOMWindowInner* aInnerWindow) override
|
2014-02-28 03:39:30 +04:00
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
ClearException ce(aCx);
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-02-02 18:40:15 +03:00
|
|
|
// Now we could have the correct window (if we are not window-less).
|
2015-07-19 10:54:41 +03:00
|
|
|
mClonedData.mParent = aInnerWindow;
|
2015-02-02 18:40:15 +03:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
JS::Rooted<JS::Value> argumentsValue(aCx);
|
2015-07-19 10:54:41 +03:00
|
|
|
bool ok = Read(aCx, &argumentsValue);
|
|
|
|
mClonedData.mParent = nullptr;
|
2015-02-02 18:40:15 +03:00
|
|
|
|
|
|
|
if (!ok) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(argumentsValue.isObject());
|
2015-01-06 18:45:00 +03:00
|
|
|
JS::Rooted<JSObject*> argumentsObj(aCx, &argumentsValue.toObject());
|
2014-02-28 03:39:30 +04:00
|
|
|
|
|
|
|
uint32_t length;
|
2015-01-06 18:45:00 +03:00
|
|
|
if (!JS_GetArrayLength(aCx, argumentsObj, &length)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Sequence<JS::Value> arguments;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < length; ++i) {
|
2015-01-06 18:45:00 +03:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-02-28 03:39:30 +04:00
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
if (!JS_GetElement(aCx, argumentsObj, i, &value)) {
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!arguments.AppendElement(value, fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
mConsole->ProfileMethodInternal(aCx, mAction, arguments);
|
2014-02-28 03:39:30 +04:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
virtual void
|
|
|
|
ReleaseData() override
|
|
|
|
{}
|
|
|
|
|
2014-02-28 03:39:30 +04:00
|
|
|
nsString mAction;
|
2016-02-07 13:29:12 +03:00
|
|
|
|
|
|
|
// This is a reference of the sequence of arguments we receive from the DOM
|
|
|
|
// bindings and it's rooted by them. It's only used on the owning thread in
|
|
|
|
// PreDispatch().
|
|
|
|
const Sequence<JS::Value>& mArguments;
|
2014-02-28 03:39:30 +04:00
|
|
|
};
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Console)
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
// We don't need to traverse/unlink mStorage and mSandbox because they are not
|
2015-01-06 18:45:00 +03:00
|
|
|
// CCed objects and they are only used on the main thread, even when this
|
|
|
|
// Console object is used on workers.
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Console)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
|
2016-03-24 00:55:07 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsoleEventNotifier)
|
2016-03-24 00:55:07 +03:00
|
|
|
tmp->Shutdown();
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Console)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
|
2016-03-24 00:55:07 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsoleEventNotifier)
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Console)
|
2016-03-24 00:55:07 +03:00
|
|
|
for (uint32_t i = 0; i < tmp->mCallDataStorage.Length(); ++i) {
|
|
|
|
tmp->mCallDataStorage[i]->Trace(aCallbacks, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < tmp->mCallDataStoragePending.Length(); ++i) {
|
|
|
|
tmp->mCallDataStoragePending[i]->Trace(aCallbacks, aClosure);
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Console)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Console)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Console)
|
2014-02-28 03:39:17 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
2016-01-21 16:17:33 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2014-02-28 03:39:00 +04:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
/* static */ already_AddRefed<Console>
|
|
|
|
Console::Create(nsPIDOMWindowInner* aWindow, ErrorResult& aRv)
|
|
|
|
{
|
2016-04-12 13:12:07 +03:00
|
|
|
MOZ_ASSERT_IF(NS_IsMainThread(), aWindow);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
RefPtr<Console> console = new Console(aWindow);
|
|
|
|
console->Initialize(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return console.forget();
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
Console::Console(nsPIDOMWindowInner* aWindow)
|
2014-02-28 03:39:00 +04:00
|
|
|
: mWindow(aWindow)
|
2016-02-11 20:11:02 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mOwningThread(PR_GetCurrentThread())
|
|
|
|
#endif
|
2014-02-28 03:39:00 +04:00
|
|
|
, mOuterID(0)
|
|
|
|
, mInnerID(0)
|
2016-03-24 00:55:07 +03:00
|
|
|
, mStatus(eUnknown)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-04-12 13:12:07 +03:00
|
|
|
MOZ_ASSERT_IF(NS_IsMainThread(), aWindow);
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
if (mWindow) {
|
|
|
|
MOZ_ASSERT(mWindow->IsInnerWindow());
|
|
|
|
mInnerID = mWindow->WindowID();
|
|
|
|
|
2015-11-23 20:16:22 +03:00
|
|
|
// Without outerwindow any console message coming from this object will not
|
|
|
|
// shown in the devtools webconsole. But this should be fine because
|
|
|
|
// probably we are shutting down, or the window is CCed/GCed.
|
2016-01-30 20:05:36 +03:00
|
|
|
nsPIDOMWindowOuter* outerWindow = mWindow->GetOuterWindow();
|
2015-11-23 20:16:22 +03:00
|
|
|
if (outerWindow) {
|
|
|
|
mOuterID = outerWindow->WindowID();
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Console::~Console()
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
Shutdown();
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Console::Initialize(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(mStatus == eUnknown);
|
|
|
|
|
2014-02-28 03:39:17 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!obs)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRv = obs->AddObserver(this, "inner-window-destroyed", true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
2014-02-28 03:39:17 +04:00
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
aRv = obs->AddObserver(this, "memory-pressure", true);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:17 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mStatus = eInitialized;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void
|
|
|
|
Console::Shutdown()
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (mStatus == eUnknown || mStatus == eShuttingDown) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-06 18:45:00 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(this, "inner-window-destroyed");
|
2016-03-24 00:55:07 +03:00
|
|
|
obs->RemoveObserver(this, "memory-pressure");
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
NS_ReleaseOnMainThread(mStorage.forget());
|
|
|
|
NS_ReleaseOnMainThread(mSandbox.forget());
|
|
|
|
|
|
|
|
mTimerRegistry.Clear();
|
|
|
|
mCounterRegistry.Clear();
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
mCallDataStorage.Clear();
|
|
|
|
mCallDataStoragePending.Clear();
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
mStatus = eShuttingDown;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:17 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Console::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2014-02-28 03:39:17 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (!strcmp(aTopic, "inner-window-destroyed")) {
|
|
|
|
nsCOMPtr<nsISupportsPRUint64> wrapper = do_QueryInterface(aSubject);
|
|
|
|
NS_ENSURE_TRUE(wrapper, NS_ERROR_FAILURE);
|
2014-02-28 03:39:17 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
uint64_t innerID;
|
|
|
|
nsresult rv = wrapper->GetData(&innerID);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-02-28 03:39:17 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (innerID == mInnerID) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
2014-02-28 03:39:17 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(aTopic, "memory-pressure")) {
|
|
|
|
ClearStorage();
|
|
|
|
return NS_OK;
|
2014-02-28 03:39:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void
|
|
|
|
Console::ClearStorage()
|
|
|
|
{
|
|
|
|
mCallDataStorage.Clear();
|
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
#define METHOD(name, string) \
|
|
|
|
/* static */ void \
|
|
|
|
Console::name(const GlobalObject& aGlobal, const Sequence<JS::Value>& aData) \
|
|
|
|
{ \
|
|
|
|
Method(aGlobal, Method##name, NS_LITERAL_STRING(string), aData); \
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
METHOD(Log, "log")
|
|
|
|
METHOD(Info, "info")
|
|
|
|
METHOD(Warn, "warn")
|
|
|
|
METHOD(Error, "error")
|
|
|
|
METHOD(Exception, "exception")
|
|
|
|
METHOD(Debug, "debug")
|
2014-08-28 02:32:32 +04:00
|
|
|
METHOD(Table, "table")
|
2016-04-13 17:33:43 +03:00
|
|
|
METHOD(Clear, "clear")
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::Trace(const GlobalObject& aGlobal)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
const Sequence<JS::Value> data;
|
2016-06-04 10:32:51 +03:00
|
|
|
Method(aGlobal, MethodTrace, NS_LITERAL_STRING("trace"), data);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Displays an interactive listing of all the properties of an object.
|
|
|
|
METHOD(Dir, "dir");
|
2015-04-29 18:34:00 +03:00
|
|
|
METHOD(Dirxml, "dirxml");
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
METHOD(Group, "group")
|
|
|
|
METHOD(GroupCollapsed, "groupCollapsed")
|
|
|
|
METHOD(GroupEnd, "groupEnd")
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::Time(const GlobalObject& aGlobal, const JS::Handle<JS::Value> aTime)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
Sequence<JS::Value> data;
|
2016-06-04 10:32:51 +03:00
|
|
|
SequenceRooter<JS::Value> rooter(cx, &data);
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!aTime.isUndefined() && !data.AppendElement(aTime, fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
Method(aGlobal, MethodTime, NS_LITERAL_STRING("time"), data);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::TimeEnd(const GlobalObject& aGlobal, const JS::Handle<JS::Value> aTime)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
Sequence<JS::Value> data;
|
2016-06-04 10:32:51 +03:00
|
|
|
SequenceRooter<JS::Value> rooter(cx, &data);
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!aTime.isUndefined() && !data.AppendElement(aTime, fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
Method(aGlobal, MethodTimeEnd, NS_LITERAL_STRING("timeEnd"), data);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::TimeStamp(const GlobalObject& aGlobal,
|
|
|
|
const JS::Handle<JS::Value> aData)
|
2015-04-29 22:48:57 +03:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2015-04-29 22:48:57 +03:00
|
|
|
Sequence<JS::Value> data;
|
2016-06-04 10:32:51 +03:00
|
|
|
SequenceRooter<JS::Value> rooter(cx, &data);
|
2015-04-29 22:48:57 +03:00
|
|
|
|
2015-05-28 21:07:44 +03:00
|
|
|
if (aData.isString() && !data.AppendElement(aData, fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return;
|
2015-04-29 22:48:57 +03:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
Method(aGlobal, MethodTimeStamp, NS_LITERAL_STRING("timeStamp"), data);
|
2015-04-29 22:48:57 +03:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::Profile(const GlobalObject& aGlobal, const Sequence<JS::Value>& aData)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
ProfileMethod(aGlobal, NS_LITERAL_STRING("profile"), aData);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::ProfileEnd(const GlobalObject& aGlobal,
|
|
|
|
const Sequence<JS::Value>& aData)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
ProfileMethod(aGlobal, NS_LITERAL_STRING("profileEnd"), aData);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::ProfileMethod(const GlobalObject& aGlobal, const nsAString& aAction,
|
2014-04-29 10:49:57 +04:00
|
|
|
const Sequence<JS::Value>& aData)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
RefPtr<Console> console = GetConsole(aGlobal);
|
|
|
|
if (!console) {
|
2016-05-03 11:19:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
console->ProfileMethodInternal(cx, aAction, aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Console::ProfileMethodInternal(JSContext* aCx, const nsAString& aAction,
|
|
|
|
const Sequence<JS::Value>& aData)
|
|
|
|
{
|
2014-02-28 03:39:30 +04:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// Here we are in a worker thread.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ConsoleProfileRunnable> runnable =
|
2015-01-06 18:45:00 +03:00
|
|
|
new ConsoleProfileRunnable(this, aAction, aData);
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
runnable->Dispatch(aCx);
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 10:49:57 +04:00
|
|
|
ClearException ce(aCx);
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
RootedDictionary<ConsoleProfileEvent> event(aCx);
|
|
|
|
event.mAction = aAction;
|
|
|
|
|
|
|
|
event.mArguments.Construct();
|
|
|
|
Sequence<JS::Value>& sequence = event.mArguments.Value();
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aData.Length(); ++i) {
|
2015-05-28 21:07:44 +03:00
|
|
|
if (!sequence.AppendElement(aData[i], fallible)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> eventValue(aCx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(aCx, event, &eventValue)) {
|
2014-02-28 03:39:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> eventObj(aCx, &eventValue.toObject());
|
|
|
|
MOZ_ASSERT(eventObj);
|
|
|
|
|
2014-04-29 10:49:57 +04:00
|
|
|
if (!JS_DefineProperty(aCx, eventObj, "wrappedJSObject", eventValue,
|
|
|
|
JSPROP_ENUMERATE)) {
|
2014-02-28 03:39:00 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:35:13 +03:00
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
2014-02-28 03:39:00 +04:00
|
|
|
nsCOMPtr<nsISupports> wrapper;
|
|
|
|
const nsIID& iid = NS_GET_IID(nsISupports);
|
|
|
|
|
|
|
|
if (NS_FAILED(xpc->WrapJS(aCx, eventObj, iid, getter_AddRefs(wrapper)))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
2014-02-28 03:39:00 +04:00
|
|
|
if (obs) {
|
|
|
|
obs->NotifyObservers(wrapper, "console-api-profiler", nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::Assert(const GlobalObject& aGlobal, bool aCondition,
|
2014-02-28 03:39:00 +04:00
|
|
|
const Sequence<JS::Value>& aData)
|
|
|
|
{
|
|
|
|
if (!aCondition) {
|
2016-06-04 10:32:51 +03:00
|
|
|
Method(aGlobal, MethodAssert, NS_LITERAL_STRING("assert"), aData);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:24 +04:00
|
|
|
METHOD(Count, "count")
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::NoopMethod(const GlobalObject& aGlobal)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
namespace {
|
|
|
|
|
2014-04-16 06:58:44 +04:00
|
|
|
nsresult
|
2016-03-22 20:50:31 +03:00
|
|
|
StackFrameToStackEntry(JSContext* aCx, nsIStackFrame* aStackFrame,
|
2016-03-22 20:50:31 +03:00
|
|
|
ConsoleStackEntry& aStackEntry)
|
2014-04-16 06:58:44 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStackFrame);
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = aStackFrame->GetFilename(aCx, aStackEntry.mFilename);
|
2014-04-16 06:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
int32_t lineNumber;
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = aStackFrame->GetLineNumber(aCx, &lineNumber);
|
2014-04-16 06:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aStackEntry.mLineNumber = lineNumber;
|
|
|
|
|
2014-09-22 22:28:55 +04:00
|
|
|
int32_t columnNumber;
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = aStackFrame->GetColumnNumber(aCx, &columnNumber);
|
2014-09-22 22:28:55 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aStackEntry.mColumnNumber = columnNumber;
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = aStackFrame->GetName(aCx, aStackEntry.mFunctionName);
|
2014-04-16 06:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-21 09:40:00 +03:00
|
|
|
nsString cause;
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = aStackFrame->GetAsyncCause(aCx, cause);
|
2015-10-21 09:40:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!cause.IsEmpty()) {
|
|
|
|
aStackEntry.mAsyncCause.Construct(cause);
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
aStackEntry.mLanguage = nsIProgrammingLanguage::JAVASCRIPT;
|
2014-04-16 06:58:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-03-22 20:50:31 +03:00
|
|
|
ReifyStack(JSContext* aCx, nsIStackFrame* aStack,
|
|
|
|
nsTArray<ConsoleStackEntry>& aRefiedStack)
|
2014-04-16 06:58:44 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStackFrame> stack(aStack);
|
|
|
|
|
|
|
|
while (stack) {
|
2016-03-22 20:50:31 +03:00
|
|
|
ConsoleStackEntry& data = *aRefiedStack.AppendElement();
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = StackFrameToStackEntry(aCx, stack, data);
|
2014-04-16 06:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStackFrame> caller;
|
2016-03-22 20:50:35 +03:00
|
|
|
rv = stack->GetCaller(aCx, getter_AddRefs(caller));
|
2014-04-16 06:58:44 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-21 09:40:00 +03:00
|
|
|
if (!caller) {
|
2016-03-22 20:50:31 +03:00
|
|
|
rv = stack->GetAsyncCaller(aCx, getter_AddRefs(caller));
|
2015-10-21 09:40:00 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2014-04-16 06:58:44 +04:00
|
|
|
stack.swap(caller);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
// Queue a call to a console method. See the CALL_DELAY constant.
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ void
|
|
|
|
Console::Method(const GlobalObject& aGlobal, MethodName aMethodName,
|
2014-02-28 03:39:00 +04:00
|
|
|
const nsAString& aMethodString,
|
|
|
|
const Sequence<JS::Value>& aData)
|
|
|
|
{
|
2016-06-04 10:32:51 +03:00
|
|
|
RefPtr<Console> console = GetConsole(aGlobal);
|
|
|
|
if (!console) {
|
2016-05-03 11:19:39 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
console->MethodInternal(aGlobal.Context(), aMethodName, aMethodString,
|
|
|
|
aData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Console::MethodInternal(JSContext* aCx, MethodName aMethodName,
|
|
|
|
const nsAString& aMethodString,
|
|
|
|
const Sequence<JS::Value>& aData)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ConsoleCallData> callData(new ConsoleCallData());
|
2014-03-03 04:20:34 +04:00
|
|
|
|
2014-04-29 10:49:57 +04:00
|
|
|
ClearException ce(aCx);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!callData->Initialize(aCx, aMethodName, aMethodString,
|
|
|
|
aData, this))) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes oa;
|
2016-12-16 13:57:24 +03:00
|
|
|
|
2016-12-17 00:08:31 +03:00
|
|
|
if (mWindow) {
|
2016-04-01 14:55:40 +03:00
|
|
|
// Save the principal's OriginAttributes in the console event data
|
|
|
|
// so that we will be able to filter messages by origin attributes.
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mWindow);
|
|
|
|
if (NS_WARN_IF(!sop)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = sop->GetPrincipal();
|
|
|
|
if (NS_WARN_IF(!principal)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-19 13:30:29 +03:00
|
|
|
oa = principal->OriginAttributesRef();
|
2017-02-27 00:13:45 +03:00
|
|
|
callData->SetAddonId(principal);
|
2016-12-17 00:08:31 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!nsContentUtils::IsSystemPrincipal(principal)) {
|
|
|
|
nsCOMPtr<nsIWebNavigation> webNav = do_GetInterface(mWindow);
|
|
|
|
if (webNav) {
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = do_QueryInterface(webNav);
|
|
|
|
MOZ_ASSERT(loadContext);
|
|
|
|
|
|
|
|
bool pb;
|
|
|
|
if (NS_SUCCEEDED(loadContext->GetUsePrivateBrowsing(&pb))) {
|
|
|
|
MOZ_ASSERT(pb == !!oa.mPrivateBrowsingId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
oa = workerPrivate->GetOriginAttributes();
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-12-17 00:08:31 +03:00
|
|
|
callData->SetOriginAttributes(oa);
|
|
|
|
|
2016-07-22 06:41:02 +03:00
|
|
|
JS::StackCapture captureMode = ShouldIncludeStackTrace(aMethodName) ?
|
|
|
|
JS::StackCapture(JS::MaxFrames(DEFAULT_MAX_STACKTRACE_DEPTH)) :
|
|
|
|
JS::StackCapture(JS::FirstSubsumedFrame(aCx));
|
|
|
|
nsCOMPtr<nsIStackFrame> stack = CreateStack(aCx, mozilla::Move(captureMode));
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2016-03-22 20:50:31 +03:00
|
|
|
if (stack) {
|
|
|
|
callData->mTopStackFrame.emplace();
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = StackFrameToStackEntry(aCx, stack,
|
2016-03-22 20:50:31 +03:00
|
|
|
*callData->mTopStackFrame);
|
2016-03-22 23:10:45 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-22 20:50:31 +03:00
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2014-04-16 06:58:44 +04:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
callData->mStack = stack;
|
|
|
|
} else {
|
|
|
|
// nsIStackFrame is not threadsafe, so we need to snapshot it now,
|
|
|
|
// before we post our runnable to the main thread.
|
2014-08-14 02:39:41 +04:00
|
|
|
callData->mReifiedStack.emplace();
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = ReifyStack(aCx, stack, *callData->mReifiedStack);
|
2014-04-16 06:58:44 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
DOMHighResTimeStamp monotonicTimer;
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
// Monotonic timer for 'time' and 'timeEnd'
|
2015-04-29 22:48:57 +03:00
|
|
|
if (aMethodName == MethodTime ||
|
|
|
|
aMethodName == MethodTimeEnd ||
|
|
|
|
aMethodName == MethodTimeStamp) {
|
2014-05-06 13:50:12 +04:00
|
|
|
if (mWindow) {
|
2016-01-30 20:05:36 +03:00
|
|
|
nsGlobalWindow *win = nsGlobalWindow::Cast(mWindow);
|
2014-05-06 13:50:12 +04:00
|
|
|
MOZ_ASSERT(win);
|
|
|
|
|
2016-06-09 20:04:42 +03:00
|
|
|
RefPtr<Performance> performance = win->GetPerformance();
|
2014-09-06 06:42:33 +04:00
|
|
|
if (!performance) {
|
2014-05-06 13:50:12 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
monotonicTimer = performance->Now();
|
2014-10-23 01:51:00 +04:00
|
|
|
|
|
|
|
nsDocShell* docShell = static_cast<nsDocShell*>(mWindow->GetDocShell());
|
2015-10-22 00:10:05 +03:00
|
|
|
RefPtr<TimelineConsumers> timelines = TimelineConsumers::Get();
|
|
|
|
bool isTimelineRecording = timelines && timelines->HasConsumer(docShell);
|
2014-10-23 01:51:00 +04:00
|
|
|
|
2015-10-22 00:10:05 +03:00
|
|
|
// The 'timeStamp' recordings do not need an argument; use empty string
|
|
|
|
// if no arguments passed in.
|
2015-04-29 22:48:57 +03:00
|
|
|
if (isTimelineRecording && aMethodName == MethodTimeStamp) {
|
2015-10-22 00:10:05 +03:00
|
|
|
JS::Rooted<JS::Value> value(aCx, aData.Length() == 0
|
|
|
|
? JS_GetEmptyStringValue(aCx)
|
|
|
|
: aData[0]);
|
2015-04-29 22:48:57 +03:00
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
|
2015-10-22 00:10:05 +03:00
|
|
|
|
2015-04-29 22:48:57 +03:00
|
|
|
nsAutoJSString key;
|
|
|
|
if (jsString) {
|
|
|
|
key.init(aCx, jsString);
|
|
|
|
}
|
|
|
|
|
2015-10-22 00:10:05 +03:00
|
|
|
timelines->AddMarkerForDocShell(docShell, Move(
|
|
|
|
MakeUnique<TimestampTimelineMarker>(key)));
|
2015-04-29 22:48:57 +03:00
|
|
|
}
|
2015-10-22 00:10:05 +03:00
|
|
|
// For `console.time(foo)` and `console.timeEnd(foo)`.
|
2015-04-29 22:48:57 +03:00
|
|
|
else if (isTimelineRecording && aData.Length() == 1) {
|
2014-10-23 01:51:00 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx, aData[0]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
|
2015-10-22 00:10:05 +03:00
|
|
|
|
2014-10-23 01:51:00 +04:00
|
|
|
if (jsString) {
|
|
|
|
nsAutoJSString key;
|
|
|
|
if (key.init(aCx, jsString)) {
|
2015-10-22 00:10:05 +03:00
|
|
|
timelines->AddMarkerForDocShell(docShell, Move(
|
|
|
|
MakeUnique<ConsoleTimelineMarker>(
|
|
|
|
key, aMethodName == MethodTime ? MarkerTracingType::START
|
|
|
|
: MarkerTracingType::END)));
|
2014-10-23 01:51:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 13:50:12 +04:00
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
2016-12-16 11:08:05 +03:00
|
|
|
monotonicTimer = workerPrivate->TimeStampToDOMHighRes(TimeStamp::Now());
|
2014-05-06 13:50:12 +04:00
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
if (aMethodName == MethodTime && !aData.IsEmpty()) {
|
|
|
|
callData->mStartTimerStatus = StartTimer(aCx, aData[0],
|
|
|
|
monotonicTimer,
|
|
|
|
callData->mStartTimerLabel,
|
|
|
|
&callData->mStartTimerValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (aMethodName == MethodTimeEnd && !aData.IsEmpty()) {
|
|
|
|
callData->mStopTimerStatus = StopTimer(aCx, aData[0],
|
|
|
|
monotonicTimer,
|
|
|
|
callData->mStopTimerLabel,
|
|
|
|
&callData->mStopTimerDuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (aMethodName == MethodCount) {
|
|
|
|
ConsoleStackEntry frame;
|
|
|
|
if (callData->mTopStackFrame) {
|
|
|
|
frame = *callData->mTopStackFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
callData->mCountValue = IncreaseCounter(aCx, frame, aData,
|
|
|
|
callData->mCountLabel);
|
|
|
|
}
|
|
|
|
|
2015-01-06 18:45:00 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
callData->SetIDs(mOuterID, mInnerID);
|
2016-03-24 00:55:07 +03:00
|
|
|
ProcessCallData(aCx, callData, aData);
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
// Just because we don't want to expose
|
|
|
|
// retrieveConsoleEvents/setConsoleEventHandler to main-thread, we can
|
|
|
|
// cleanup the mCallDataStorage:
|
|
|
|
UnstoreCallData(callData);
|
2014-02-28 03:39:30 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// We do this only in workers for now.
|
2016-03-24 00:55:07 +03:00
|
|
|
NotifyHandler(aCx, aData, callData);
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ConsoleCallDataRunnable> runnable =
|
2015-01-06 18:45:00 +03:00
|
|
|
new ConsoleCallDataRunnable(this, callData);
|
2016-09-02 10:12:24 +03:00
|
|
|
Unused << NS_WARN_IF(!runnable->Dispatch(aCx));
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-04-16 06:58:44 +04:00
|
|
|
// We store information to lazily compute the stack in the reserved slots of
|
|
|
|
// LazyStackGetter. The first slot always stores a JS object: it's either the
|
|
|
|
// JS wrapper of the nsIStackFrame or the actual reified stack representation.
|
|
|
|
// The second slot is a PrivateValue() holding an nsIStackFrame* when we haven't
|
|
|
|
// reified the stack yet, or an UndefinedValue() otherwise.
|
|
|
|
enum {
|
|
|
|
SLOT_STACKOBJ,
|
|
|
|
SLOT_RAW_STACK
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
LazyStackGetter(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
JS::Rooted<JSObject*> callee(aCx, &args.callee());
|
|
|
|
|
|
|
|
JS::Value v = js::GetFunctionNativeReserved(&args.callee(), SLOT_RAW_STACK);
|
|
|
|
if (v.isUndefined()) {
|
|
|
|
// Already reified.
|
|
|
|
args.rval().set(js::GetFunctionNativeReserved(callee, SLOT_STACKOBJ));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIStackFrame* stack = reinterpret_cast<nsIStackFrame*>(v.toPrivate());
|
|
|
|
nsTArray<ConsoleStackEntry> reifiedStack;
|
2016-03-22 20:50:31 +03:00
|
|
|
nsresult rv = ReifyStack(aCx, stack, reifiedStack);
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2014-04-16 06:58:44 +04:00
|
|
|
Throw(aCx, rv);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> stackVal(aCx);
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, reifiedStack, &stackVal))) {
|
2014-04-16 06:58:44 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(stackVal.isObject());
|
|
|
|
|
|
|
|
js::SetFunctionNativeReserved(callee, SLOT_STACKOBJ, stackVal);
|
|
|
|
js::SetFunctionNativeReserved(callee, SLOT_RAW_STACK, JS::UndefinedValue());
|
|
|
|
|
|
|
|
args.rval().set(stackVal);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
void
|
2016-03-24 00:55:07 +03:00
|
|
|
Console::ProcessCallData(JSContext* aCx, ConsoleCallData* aData,
|
2016-02-07 13:29:12 +03:00
|
|
|
const Sequence<JS::Value>& aArguments)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-02-11 20:11:02 +03:00
|
|
|
MOZ_ASSERT(aData);
|
2014-03-03 04:20:34 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JS::Value> eventValue(aCx);
|
|
|
|
|
|
|
|
// We want to create a console event object and pass it to our
|
|
|
|
// nsIConsoleAPIStorage implementation. We want to define some accessor
|
|
|
|
// properties on this object, and those will need to keep an nsIStackFrame
|
|
|
|
// alive. But nsIStackFrame cannot be wrapped in an untrusted scope. And
|
|
|
|
// further, passing untrusted objects to system code is likely to run afoul of
|
|
|
|
// Object Xrays. So we want to wrap in a system-principal scope here. But
|
|
|
|
// which one? We could cheat and try to get the underlying JSObject* of
|
|
|
|
// mStorage, but that's a bit fragile. Instead, we just use the junk scope,
|
|
|
|
// with explicit permission from the XPConnect module owner. If you're
|
|
|
|
// tempted to do that anywhere else, talk to said module owner first.
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// aCx and aArguments are in the same compartment.
|
2016-03-24 11:29:08 +03:00
|
|
|
if (NS_WARN_IF(!PopulateConsoleNotificationInTheTargetScope(aCx, aArguments,
|
|
|
|
xpc::PrivilegedJunkScope(),
|
|
|
|
&eventValue, aData))) {
|
2016-03-24 00:55:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStorage) {
|
|
|
|
mStorage = do_GetService("@mozilla.org/consoleAPI-storage;1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mStorage) {
|
|
|
|
NS_WARNING("Failed to get the ConsoleAPIStorage service.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString innerID, outerID;
|
|
|
|
|
|
|
|
MOZ_ASSERT(aData->mIDType != ConsoleCallData::eUnknown);
|
|
|
|
if (aData->mIDType == ConsoleCallData::eString) {
|
|
|
|
outerID = aData->mOuterIDString;
|
|
|
|
innerID = aData->mInnerIDString;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aData->mIDType == ConsoleCallData::eNumber);
|
|
|
|
outerID.AppendInt(aData->mOuterIDNumber);
|
|
|
|
innerID.AppendInt(aData->mInnerIDNumber);
|
|
|
|
}
|
|
|
|
|
2016-04-13 17:33:43 +03:00
|
|
|
if (aData->mMethodName == MethodClear) {
|
2016-09-02 10:12:24 +03:00
|
|
|
DebugOnly<nsresult> rv = mStorage->ClearEvents(innerID);
|
|
|
|
NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "ClearEvents failed");
|
2016-04-13 17:33:43 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_FAILED(mStorage->RecordEvent(innerID, outerID, eventValue))) {
|
|
|
|
NS_WARNING("Failed to record a console event.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2016-03-24 11:29:08 +03:00
|
|
|
Console::PopulateConsoleNotificationInTheTargetScope(JSContext* aCx,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
JSObject* aTargetScope,
|
|
|
|
JS::MutableHandle<JS::Value> aEventValue,
|
|
|
|
ConsoleCallData* aData) const
|
2016-03-24 00:55:07 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCx);
|
|
|
|
MOZ_ASSERT(aData);
|
2016-03-24 00:55:07 +03:00
|
|
|
MOZ_ASSERT(aTargetScope);
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2016-03-24 05:15:05 +03:00
|
|
|
JS::Rooted<JSObject*> targetScope(aCx, aTargetScope);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
ConsoleStackEntry frame;
|
|
|
|
if (aData->mTopStackFrame) {
|
|
|
|
frame = *aData->mTopStackFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearException ce(aCx);
|
|
|
|
RootedDictionary<ConsoleEvent> event(aCx);
|
|
|
|
|
2016-04-01 14:55:40 +03:00
|
|
|
// Save the principal's OriginAttributes in the console event data
|
|
|
|
// so that we will be able to filter messages by origin attributes.
|
|
|
|
JS::Rooted<JS::Value> originAttributesValue(aCx);
|
|
|
|
if (ToJSValue(aCx, aData->mOriginAttributes, &originAttributesValue)) {
|
|
|
|
event.mOriginAttributes = originAttributesValue;
|
|
|
|
}
|
|
|
|
|
2017-02-27 00:13:45 +03:00
|
|
|
event.mAddonId = aData->mAddonId;
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
event.mID.Construct();
|
|
|
|
event.mInnerID.Construct();
|
2015-01-06 18:45:00 +03:00
|
|
|
|
|
|
|
if (aData->mIDType == ConsoleCallData::eString) {
|
|
|
|
event.mID.Value().SetAsString() = aData->mOuterIDString;
|
|
|
|
event.mInnerID.Value().SetAsString() = aData->mInnerIDString;
|
2016-03-24 00:55:07 +03:00
|
|
|
} else if (aData->mIDType == ConsoleCallData::eNumber) {
|
2015-03-17 14:13:00 +03:00
|
|
|
event.mID.Value().SetAsUnsignedLongLong() = aData->mOuterIDNumber;
|
|
|
|
event.mInnerID.Value().SetAsUnsignedLongLong() = aData->mInnerIDNumber;
|
2016-03-24 00:55:07 +03:00
|
|
|
} else {
|
|
|
|
// aData->mIDType can be eUnknown when we dispatch notifications via
|
2016-03-24 00:55:07 +03:00
|
|
|
// mConsoleEventNotifier.
|
2016-03-24 00:55:07 +03:00
|
|
|
event.mID.Value().SetAsUnsignedLongLong() = 0;
|
|
|
|
event.mInnerID.Value().SetAsUnsignedLongLong() = 0;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 04:20:34 +04:00
|
|
|
event.mLevel = aData->mMethodString;
|
2014-02-28 03:39:00 +04:00
|
|
|
event.mFilename = frame.mFilename;
|
2015-09-14 22:38:57 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> filenameURI;
|
|
|
|
nsAutoCString pass;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_IsMainThread() &&
|
|
|
|
NS_SUCCEEDED(NS_NewURI(getter_AddRefs(filenameURI), frame.mFilename)) &&
|
2015-09-14 22:38:57 +03:00
|
|
|
NS_SUCCEEDED(filenameURI->GetPassword(pass)) && !pass.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsISensitiveInfoHiddenURI> safeURI = do_QueryInterface(filenameURI);
|
|
|
|
nsAutoCString spec;
|
|
|
|
if (safeURI &&
|
|
|
|
NS_SUCCEEDED(safeURI->GetSensitiveInfoHiddenSpec(spec))) {
|
|
|
|
CopyUTF8toUTF16(spec, event.mFilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
event.mLineNumber = frame.mLineNumber;
|
2014-09-22 22:28:55 +04:00
|
|
|
event.mColumnNumber = frame.mColumnNumber;
|
2014-02-28 03:39:00 +04:00
|
|
|
event.mFunctionName = frame.mFunctionName;
|
2014-03-03 04:20:34 +04:00
|
|
|
event.mTimeStamp = aData->mTimeStamp;
|
2016-12-17 00:08:31 +03:00
|
|
|
event.mPrivate = !!aData->mOriginAttributes.mPrivateBrowsingId;
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2014-03-03 04:20:34 +04:00
|
|
|
switch (aData->mMethodName) {
|
2014-02-28 03:39:00 +04:00
|
|
|
case MethodLog:
|
|
|
|
case MethodInfo:
|
|
|
|
case MethodWarn:
|
|
|
|
case MethodError:
|
|
|
|
case MethodException:
|
|
|
|
case MethodDebug:
|
|
|
|
case MethodAssert:
|
2017-04-11 16:14:40 +03:00
|
|
|
case MethodGroup:
|
|
|
|
case MethodGroupCollapsed:
|
|
|
|
case MethodGroupEnd:
|
2014-02-28 03:39:00 +04:00
|
|
|
event.mArguments.Construct();
|
2014-04-10 18:59:21 +04:00
|
|
|
event.mStyles.Construct();
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!ProcessArguments(aCx, aArguments,
|
|
|
|
event.mArguments.Value(),
|
|
|
|
event.mStyles.Value()))) {
|
|
|
|
return false;
|
2015-05-25 14:50:15 +03:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
event.mArguments.Construct();
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!ArgumentsToValueList(aArguments,
|
|
|
|
event.mArguments.Value()))) {
|
|
|
|
return false;
|
2015-05-25 14:50:15 +03:00
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-04-16 06:58:44 +04:00
|
|
|
if (aData->mMethodName == MethodGroup ||
|
|
|
|
aData->mMethodName == MethodGroupCollapsed ||
|
|
|
|
aData->mMethodName == MethodGroupEnd) {
|
2017-04-11 16:14:40 +03:00
|
|
|
ComposeGroupName(aCx, event.mArguments.Value(), event.mGroupName);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
else if (aData->mMethodName == MethodTime && !aArguments.IsEmpty()) {
|
2016-03-24 00:55:07 +03:00
|
|
|
event.mTimer = CreateStartTimerValue(aCx, aData->mStartTimerLabel,
|
2016-02-11 20:09:22 +03:00
|
|
|
aData->mStartTimerStatus);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
else if (aData->mMethodName == MethodTimeEnd && !aArguments.IsEmpty()) {
|
2016-03-24 00:55:07 +03:00
|
|
|
event.mTimer = CreateStopTimerValue(aCx, aData->mStopTimerLabel,
|
2016-02-11 20:09:22 +03:00
|
|
|
aData->mStopTimerDuration,
|
|
|
|
aData->mStopTimerStatus);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-03-03 04:20:34 +04:00
|
|
|
else if (aData->mMethodName == MethodCount) {
|
2016-03-24 00:55:07 +03:00
|
|
|
event.mCounter = CreateCounterValue(aCx, aData->mCountLabel,
|
2016-02-11 20:09:22 +03:00
|
|
|
aData->mCountValue);
|
2014-02-28 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 05:15:05 +03:00
|
|
|
JSAutoCompartment ac2(aCx, targetScope);
|
2014-04-16 23:09:23 +04:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, event, aEventValue))) {
|
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JSObject*> eventObj(aCx, &aEventValue.toObject());
|
|
|
|
if (NS_WARN_IF(!JS_DefineProperty(aCx, eventObj, "wrappedJSObject", eventObj,
|
|
|
|
JSPROP_ENUMERATE))) {
|
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-10-20 12:34:00 +04:00
|
|
|
if (ShouldIncludeStackTrace(aData->mMethodName)) {
|
2014-04-16 06:58:44 +04:00
|
|
|
// Now define the "stacktrace" property on eventObj. There are two cases
|
|
|
|
// here. Either we came from a worker and have a reified stack, or we want
|
|
|
|
// to define a getter that will lazily reify the stack.
|
2014-08-14 02:39:41 +04:00
|
|
|
if (aData->mReifiedStack) {
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JS::Value> stacktrace(aCx);
|
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, *aData->mReifiedStack, &stacktrace)) ||
|
|
|
|
NS_WARN_IF(!JS_DefineProperty(aCx, eventObj, "stacktrace", stacktrace,
|
|
|
|
JSPROP_ENUMERATE))) {
|
|
|
|
return false;
|
2014-04-16 06:58:44 +04:00
|
|
|
}
|
|
|
|
} else {
|
2016-03-24 00:55:07 +03:00
|
|
|
JSFunction* fun = js::NewFunctionWithReserved(aCx, LazyStackGetter, 0, 0,
|
2015-03-09 19:50:07 +03:00
|
|
|
"stacktrace");
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!fun)) {
|
|
|
|
return false;
|
2014-04-16 06:58:44 +04:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JSObject*> funObj(aCx, JS_GetFunctionObject(fun));
|
2014-04-16 06:58:44 +04:00
|
|
|
|
|
|
|
// We want to store our stack in the function and have it stay alive. But
|
|
|
|
// we also need sane access to the C++ nsIStackFrame. So store both a JS
|
|
|
|
// wrapper and the raw pointer: the former will keep the latter alive.
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JS::Value> stackVal(aCx);
|
|
|
|
nsresult rv = nsContentUtils::WrapNative(aCx, aData->mStack,
|
2014-04-16 06:58:44 +04:00
|
|
|
&stackVal);
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
2014-04-16 06:58:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
js::SetFunctionNativeReserved(funObj, SLOT_STACKOBJ, stackVal);
|
|
|
|
js::SetFunctionNativeReserved(funObj, SLOT_RAW_STACK,
|
|
|
|
JS::PrivateValue(aData->mStack.get()));
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS_DefineProperty(aCx, eventObj, "stacktrace",
|
|
|
|
JS::UndefinedHandleValue,
|
|
|
|
JSPROP_ENUMERATE | JSPROP_SHARED |
|
|
|
|
JSPROP_GETTER | JSPROP_SETTER,
|
|
|
|
JS_DATA_TO_FUNC_PTR(JSNative, funObj.get()),
|
|
|
|
nullptr))) {
|
|
|
|
return false;
|
2014-04-16 06:58:44 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
return true;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2015-05-04 18:28:25 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper method for ProcessArguments. Flushes output, if non-empty, to aSequence.
|
2015-05-25 14:50:15 +03:00
|
|
|
bool
|
|
|
|
FlushOutput(JSContext* aCx, Sequence<JS::Value>& aSequence, nsString &aOutput)
|
2015-05-04 18:28:25 +03:00
|
|
|
{
|
2015-05-25 14:50:15 +03:00
|
|
|
if (!aOutput.IsEmpty()) {
|
2015-05-04 18:28:25 +03:00
|
|
|
JS::Rooted<JSString*> str(aCx, JS_NewUCStringCopyN(aCx,
|
2015-05-25 14:50:15 +03:00
|
|
|
aOutput.get(),
|
|
|
|
aOutput.Length()));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!str)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aSequence.AppendElement(JS::StringValue(str), fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2015-05-04 18:28:25 +03:00
|
|
|
}
|
|
|
|
|
2015-05-25 14:50:15 +03:00
|
|
|
aOutput.Truncate();
|
2015-05-04 18:28:25 +03:00
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
|
|
|
return true;
|
2015-05-04 18:28:25 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-05-04 18:28:25 +03:00
|
|
|
|
2015-05-25 14:50:15 +03:00
|
|
|
bool
|
2014-02-28 03:39:00 +04:00
|
|
|
Console::ProcessArguments(JSContext* aCx,
|
2016-02-07 13:29:12 +03:00
|
|
|
const Sequence<JS::Value>& aData,
|
2014-04-10 18:59:21 +04:00
|
|
|
Sequence<JS::Value>& aSequence,
|
2016-02-13 19:08:17 +03:00
|
|
|
Sequence<nsString>& aStyles) const
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
if (aData.IsEmpty()) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return true;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aData.Length() == 1 || !aData[0].isString()) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return ArgumentsToValueList(aData, aSequence);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> format(aCx, aData[0]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, format));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString string;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!string.init(aCx, jsString))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsString::const_iterator start, end;
|
|
|
|
string.BeginReading(start);
|
|
|
|
string.EndReading(end);
|
|
|
|
|
|
|
|
nsString output;
|
|
|
|
uint32_t index = 1;
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
if (*start != '%') {
|
|
|
|
output.Append(*start);
|
|
|
|
++start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
++start;
|
2014-03-05 07:22:32 +04:00
|
|
|
if (start == end) {
|
|
|
|
output.Append('%');
|
|
|
|
break;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
if (*start == '%') {
|
|
|
|
output.Append(*start);
|
|
|
|
++start;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:12 +04:00
|
|
|
nsAutoString tmp;
|
|
|
|
tmp.Append('%');
|
|
|
|
|
|
|
|
int32_t integer = -1;
|
|
|
|
int32_t mantissa = -1;
|
|
|
|
|
|
|
|
// Let's parse %<number>.<number> for %d and %f
|
|
|
|
if (*start >= '0' && *start <= '9') {
|
|
|
|
integer = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
integer = integer * 10 + *start - '0';
|
|
|
|
tmp.Append(*start);
|
|
|
|
++start;
|
2014-03-05 07:22:32 +04:00
|
|
|
} while (*start >= '0' && *start <= '9' && start != end);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start == end) {
|
|
|
|
output.Append(tmp);
|
|
|
|
break;
|
2014-02-28 03:39:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*start == '.') {
|
|
|
|
tmp.Append(*start);
|
|
|
|
++start;
|
|
|
|
|
2014-03-05 07:22:32 +04:00
|
|
|
if (start == end) {
|
|
|
|
output.Append(tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:12 +04:00
|
|
|
// '.' must be followed by a number.
|
|
|
|
if (*start < '0' || *start > '9') {
|
|
|
|
output.Append(tmp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
mantissa = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
mantissa = mantissa * 10 + *start - '0';
|
|
|
|
tmp.Append(*start);
|
|
|
|
++start;
|
2014-03-05 07:22:32 +04:00
|
|
|
} while (*start >= '0' && *start <= '9' && start != end);
|
|
|
|
|
|
|
|
if (start == end) {
|
|
|
|
output.Append(tmp);
|
|
|
|
break;
|
|
|
|
}
|
2014-02-28 03:39:12 +04:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
char ch = *start;
|
2014-02-28 03:39:12 +04:00
|
|
|
tmp.Append(ch);
|
2014-02-28 03:39:00 +04:00
|
|
|
++start;
|
|
|
|
|
|
|
|
switch (ch) {
|
|
|
|
case 'o':
|
2014-04-10 18:59:21 +04:00
|
|
|
case 'O':
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!FlushOutput(aCx, aSequence, output))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> v(aCx);
|
|
|
|
if (index < aData.Length()) {
|
|
|
|
v = aData[index++];
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aSequence.AppendElement(v, fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-04-10 18:59:21 +04:00
|
|
|
case 'c':
|
|
|
|
{
|
2015-09-11 17:44:06 +03:00
|
|
|
// If there isn't any output but there's already a style, then
|
|
|
|
// discard the previous style and use the next one instead.
|
|
|
|
if (output.IsEmpty() && !aStyles.IsEmpty()) {
|
|
|
|
aStyles.TruncateLength(aStyles.Length() - 1);
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!FlushOutput(aCx, aSequence, output))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-10 18:59:21 +04:00
|
|
|
|
|
|
|
if (index < aData.Length()) {
|
|
|
|
JS::Rooted<JS::Value> v(aCx, aData[index++]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, v));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-04-10 18:59:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int32_t diff = aSequence.Length() - aStyles.Length();
|
|
|
|
if (diff > 0) {
|
|
|
|
for (int32_t i = 0; i < diff; i++) {
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aStyles.AppendElement(NullString(), fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-10 18:59:21 +04:00
|
|
|
}
|
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
2016-02-13 19:08:17 +03:00
|
|
|
nsAutoJSString string;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!string.init(aCx, jsString))) {
|
2016-02-13 19:08:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aStyles.AppendElement(string, fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-04-10 18:59:21 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
case 's':
|
|
|
|
if (index < aData.Length()) {
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aData[index++]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString v;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!v.init(aCx, jsString))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
output.Append(v);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
if (index < aData.Length()) {
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aData[index++]);
|
|
|
|
|
|
|
|
int32_t v;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS::ToInt32(aCx, value, &v))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:12 +04:00
|
|
|
nsCString format;
|
|
|
|
MakeFormatString(format, integer, mantissa, 'd');
|
|
|
|
output.AppendPrintf(format.get(), v);
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
if (index < aData.Length()) {
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aData[index++]);
|
|
|
|
|
|
|
|
double v;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!JS::ToNumber(aCx, value, &v))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-01-28 01:22:45 +03:00
|
|
|
// nspr returns "nan", but we want to expose it as "NaN"
|
|
|
|
if (std::isnan(v)) {
|
|
|
|
output.AppendFloat(v);
|
|
|
|
} else {
|
|
|
|
nsCString format;
|
|
|
|
MakeFormatString(format, integer, mantissa, 'f');
|
|
|
|
output.AppendPrintf(format.get(), v);
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-02-28 03:39:12 +04:00
|
|
|
output.Append(tmp);
|
2014-02-28 03:39:00 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!FlushOutput(aCx, aSequence, output))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
|
2015-05-04 18:28:25 +03:00
|
|
|
// Discard trailing style element if there is no output to apply it to.
|
|
|
|
if (aStyles.Length() > aSequence.Length()) {
|
|
|
|
aStyles.TruncateLength(aSequence.Length());
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// The rest of the array, if unused by the format string.
|
|
|
|
for (; index < aData.Length(); ++index) {
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aSequence.AppendElement(aData[index], fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
|
|
|
return true;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:12 +04:00
|
|
|
void
|
|
|
|
Console::MakeFormatString(nsCString& aFormat, int32_t aInteger,
|
2016-02-07 13:29:12 +03:00
|
|
|
int32_t aMantissa, char aCh) const
|
2014-02-28 03:39:12 +04:00
|
|
|
{
|
2014-05-22 07:48:52 +04:00
|
|
|
aFormat.Append('%');
|
2014-02-28 03:39:12 +04:00
|
|
|
if (aInteger >= 0) {
|
|
|
|
aFormat.AppendInt(aInteger);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMantissa >= 0) {
|
2014-05-22 07:48:52 +04:00
|
|
|
aFormat.Append('.');
|
2014-02-28 03:39:12 +04:00
|
|
|
aFormat.AppendInt(aMantissa);
|
|
|
|
}
|
|
|
|
|
|
|
|
aFormat.Append(aCh);
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
void
|
|
|
|
Console::ComposeGroupName(JSContext* aCx,
|
2016-02-07 13:29:12 +03:00
|
|
|
const Sequence<JS::Value>& aData,
|
|
|
|
nsAString& aName) const
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aData.Length(); ++i) {
|
|
|
|
if (i != 0) {
|
|
|
|
aName.AppendASCII(" ");
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aData[i]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, value));
|
|
|
|
if (!jsString) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString string;
|
2014-02-28 03:39:00 +04:00
|
|
|
if (!string.init(aCx, jsString)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aName.Append(string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
bool
|
2014-02-28 03:39:00 +04:00
|
|
|
Console::StartTimer(JSContext* aCx, const JS::Value& aName,
|
2016-02-11 20:09:22 +03:00
|
|
|
DOMHighResTimeStamp aTimestamp,
|
|
|
|
nsAString& aTimerLabel,
|
|
|
|
DOMHighResTimeStamp* aTimerValue)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
2016-02-11 20:09:22 +03:00
|
|
|
MOZ_ASSERT(aTimerValue);
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
*aTimerValue = 0;
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(mTimerRegistry.Count() >= MAX_PAGE_TIMERS)) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> name(aCx, aName);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, name));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
nsAutoJSString label;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!label.init(aCx, jsString))) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:23 +03:00
|
|
|
DOMHighResTimeStamp entry = 0;
|
2016-02-11 20:09:22 +03:00
|
|
|
if (!mTimerRegistry.Get(label, &entry)) {
|
|
|
|
mTimerRegistry.Put(label, aTimestamp);
|
2014-02-28 03:39:00 +04:00
|
|
|
} else {
|
|
|
|
aTimestamp = entry;
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
aTimerLabel = label;
|
|
|
|
*aTimerValue = aTimestamp;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
Console::CreateStartTimerValue(JSContext* aCx, const nsAString& aTimerLabel,
|
|
|
|
bool aTimerStatus) const
|
|
|
|
{
|
|
|
|
if (!aTimerStatus) {
|
|
|
|
RootedDictionary<ConsoleTimerError> error(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!ToJSValue(aCx, error, &value)) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
RootedDictionary<ConsoleTimerStart> timer(aCx);
|
|
|
|
|
|
|
|
timer.mName = aTimerLabel;
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(aCx, timer, &value)) {
|
2014-02-28 03:39:00 +04:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
bool
|
2014-02-28 03:39:00 +04:00
|
|
|
Console::StopTimer(JSContext* aCx, const JS::Value& aName,
|
2016-02-11 20:09:22 +03:00
|
|
|
DOMHighResTimeStamp aTimestamp,
|
|
|
|
nsAString& aTimerLabel,
|
|
|
|
double* aTimerDuration)
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
2016-02-11 20:09:22 +03:00
|
|
|
MOZ_ASSERT(aTimerDuration);
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
*aTimerDuration = 0;
|
2016-02-07 13:29:12 +03:00
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
JS::Rooted<JS::Value> name(aCx, aName);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, name));
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!jsString)) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString key;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!key.init(aCx, jsString))) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:08:23 +03:00
|
|
|
DOMHighResTimeStamp entry = 0;
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!mTimerRegistry.Get(key, &entry))) {
|
2016-02-11 20:09:22 +03:00
|
|
|
return false;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mTimerRegistry.Remove(key);
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
aTimerLabel = key;
|
|
|
|
*aTimerDuration = aTimestamp - entry;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
Console::CreateStopTimerValue(JSContext* aCx, const nsAString& aLabel,
|
|
|
|
double aDuration, bool aStatus) const
|
|
|
|
{
|
|
|
|
if (!aStatus) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
RootedDictionary<ConsoleTimerEnd> timer(aCx);
|
2016-02-11 20:09:22 +03:00
|
|
|
timer.mName = aLabel;
|
|
|
|
timer.mDuration = aDuration;
|
2014-02-28 03:39:00 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(aCx, timer, &value)) {
|
2014-02-28 03:39:00 +04:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-05-25 14:50:15 +03:00
|
|
|
bool
|
2016-02-07 13:29:12 +03:00
|
|
|
Console::ArgumentsToValueList(const Sequence<JS::Value>& aData,
|
|
|
|
Sequence<JS::Value>& aSequence) const
|
2014-02-28 03:39:00 +04:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < aData.Length(); ++i) {
|
2016-03-24 00:55:07 +03:00
|
|
|
if (NS_WARN_IF(!aSequence.AppendElement(aData[i], fallible))) {
|
2015-05-25 14:50:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
2015-05-25 14:50:15 +03:00
|
|
|
|
|
|
|
return true;
|
2014-02-28 03:39:00 +04:00
|
|
|
}
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
uint32_t
|
2014-02-28 03:39:24 +04:00
|
|
|
Console::IncreaseCounter(JSContext* aCx, const ConsoleStackEntry& aFrame,
|
2016-02-11 20:09:22 +03:00
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
nsAString& aCountLabel)
|
2014-02-28 03:39:24 +04:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2014-02-28 03:39:24 +04:00
|
|
|
ClearException ce(aCx);
|
|
|
|
|
|
|
|
nsAutoString key;
|
|
|
|
nsAutoString label;
|
|
|
|
|
|
|
|
if (!aArguments.IsEmpty()) {
|
|
|
|
JS::Rooted<JS::Value> labelValue(aCx, aArguments[0]);
|
|
|
|
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, labelValue));
|
|
|
|
|
2014-07-05 19:30:54 +04:00
|
|
|
nsAutoJSString string;
|
2014-02-28 03:39:24 +04:00
|
|
|
if (jsString && string.init(aCx, jsString)) {
|
|
|
|
label = string;
|
|
|
|
key = string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.IsEmpty()) {
|
|
|
|
key.Append(aFrame.mFilename);
|
2014-05-22 07:48:50 +04:00
|
|
|
key.Append(':');
|
2014-02-28 03:39:24 +04:00
|
|
|
key.AppendInt(aFrame.mLineNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t count = 0;
|
2016-02-11 20:09:22 +03:00
|
|
|
if (!mCounterRegistry.Get(key, &count) &&
|
|
|
|
mCounterRegistry.Count() >= MAX_PAGE_COUNTERS) {
|
|
|
|
return MAX_PAGE_COUNTERS;
|
2014-02-28 03:39:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
++count;
|
|
|
|
mCounterRegistry.Put(key, count);
|
|
|
|
|
2016-02-11 20:09:22 +03:00
|
|
|
aCountLabel = label;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value
|
|
|
|
Console::CreateCounterValue(JSContext* aCx, const nsAString& aCountLabel,
|
|
|
|
uint32_t aCountValue) const
|
|
|
|
{
|
|
|
|
ClearException ce(aCx);
|
|
|
|
|
|
|
|
if (aCountValue == MAX_PAGE_COUNTERS) {
|
|
|
|
RootedDictionary<ConsoleCounterError> error(aCx);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!ToJSValue(aCx, error, &value)) {
|
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:24 +04:00
|
|
|
RootedDictionary<ConsoleCounter> data(aCx);
|
2016-02-11 20:09:22 +03:00
|
|
|
data.mLabel = aCountLabel;
|
|
|
|
data.mCount = aCountValue;
|
2014-02-28 03:39:24 +04:00
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2014-06-11 23:38:55 +04:00
|
|
|
if (!ToJSValue(aCx, data, &value)) {
|
2014-02-28 03:39:24 +04:00
|
|
|
return JS::UndefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2014-04-11 00:02:37 +04:00
|
|
|
bool
|
2016-02-07 13:29:12 +03:00
|
|
|
Console::ShouldIncludeStackTrace(MethodName aMethodName) const
|
2014-04-11 00:02:37 +04:00
|
|
|
{
|
|
|
|
switch (aMethodName) {
|
|
|
|
case MethodError:
|
|
|
|
case MethodException:
|
|
|
|
case MethodAssert:
|
|
|
|
case MethodTrace:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
JSObject*
|
2015-01-06 18:45:00 +03:00
|
|
|
Console::GetOrCreateSandbox(JSContext* aCx, nsIPrincipal* aPrincipal)
|
|
|
|
{
|
2016-02-11 20:09:22 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-01-06 18:45:00 +03:00
|
|
|
|
|
|
|
if (!mSandbox) {
|
|
|
|
nsIXPConnect* xpc = nsContentUtils::XPConnect();
|
|
|
|
MOZ_ASSERT(xpc, "This should never be null!");
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
JS::Rooted<JSObject*> sandbox(aCx);
|
|
|
|
nsresult rv = xpc->CreateSandbox(aCx, aPrincipal, sandbox.address());
|
2015-01-06 18:45:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-07-21 21:31:44 +03:00
|
|
|
|
|
|
|
mSandbox = new JSObjectHolder(aCx, sandbox);
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
|
|
|
|
2015-07-21 21:31:44 +03:00
|
|
|
return mSandbox->GetJSObject();
|
2015-01-06 18:45:00 +03:00
|
|
|
}
|
|
|
|
|
2016-02-07 13:29:12 +03:00
|
|
|
void
|
2016-03-24 00:55:07 +03:00
|
|
|
Console::StoreCallData(ConsoleCallData* aCallData)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
MOZ_ASSERT(aCallData);
|
|
|
|
MOZ_ASSERT(!mCallDataStorage.Contains(aCallData));
|
|
|
|
MOZ_ASSERT(!mCallDataStoragePending.Contains(aCallData));
|
|
|
|
|
|
|
|
mCallDataStorage.AppendElement(aCallData);
|
|
|
|
|
|
|
|
if (mCallDataStorage.Length() > STORAGE_MAX_EVENTS) {
|
|
|
|
RefPtr<ConsoleCallData> callData = mCallDataStorage[0];
|
|
|
|
mCallDataStorage.RemoveElementAt(0);
|
|
|
|
|
|
|
|
MOZ_ASSERT(callData->mStatus != ConsoleCallData::eToBeDeleted);
|
|
|
|
|
|
|
|
// We cannot delete this object now because we have to trace its JSValues
|
|
|
|
// until the pending operation (ConsoleCallDataRunnable) is completed.
|
|
|
|
if (callData->mStatus == ConsoleCallData::eInUse) {
|
|
|
|
callData->mStatus = ConsoleCallData::eToBeDeleted;
|
|
|
|
mCallDataStoragePending.AppendElement(callData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Console::UnstoreCallData(ConsoleCallData* aCallData)
|
2016-02-07 13:29:12 +03:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
MOZ_ASSERT(aCallData);
|
2016-06-08 13:49:51 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
MOZ_ASSERT(!mCallDataStoragePending.Contains(aCallData));
|
|
|
|
|
2016-06-08 13:49:51 +03:00
|
|
|
// It can be that mCallDataStorage has been already cleaned in case the
|
|
|
|
// processing of the argument of some Console methods triggers the
|
|
|
|
// window.close().
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mCallDataStorage.RemoveElement(aCallData);
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-24 00:55:07 +03:00
|
|
|
Console::ReleaseCallData(ConsoleCallData* aCallData)
|
2016-02-07 13:29:12 +03:00
|
|
|
{
|
2016-02-11 20:11:02 +03:00
|
|
|
AssertIsOnOwningThread();
|
2016-03-24 00:55:07 +03:00
|
|
|
MOZ_ASSERT(aCallData);
|
|
|
|
MOZ_ASSERT(aCallData->mStatus == ConsoleCallData::eToBeDeleted);
|
|
|
|
MOZ_ASSERT(mCallDataStoragePending.Contains(aCallData));
|
2016-02-11 20:11:02 +03:00
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
mCallDataStoragePending.RemoveElement(aCallData);
|
2016-02-07 13:29:12 +03:00
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void
|
2016-03-24 00:55:07 +03:00
|
|
|
Console::NotifyHandler(JSContext* aCx, const Sequence<JS::Value>& aArguments,
|
2016-03-24 00:55:07 +03:00
|
|
|
ConsoleCallData* aCallData) const
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aCallData);
|
|
|
|
|
|
|
|
if (!mConsoleEventNotifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
2016-03-24 00:55:07 +03:00
|
|
|
|
2016-11-15 08:25:37 +03:00
|
|
|
JS::Rooted<JSObject*> callable(aCx, mConsoleEventNotifier->CallableOrNull());
|
|
|
|
if (NS_WARN_IF(!callable)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// aCx and aArguments are in the same compartment because this method is
|
|
|
|
// called directly when a Console.something() runs.
|
|
|
|
// mConsoleEventNotifier->Callable() is the scope where value will be sent to.
|
2016-03-24 11:29:08 +03:00
|
|
|
if (NS_WARN_IF(!PopulateConsoleNotificationInTheTargetScope(aCx, aArguments,
|
2016-11-15 08:25:37 +03:00
|
|
|
callable,
|
2016-03-24 11:29:08 +03:00
|
|
|
&value,
|
|
|
|
aCallData))) {
|
2016-03-24 00:55:07 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> ignored(aCx);
|
|
|
|
mConsoleEventNotifier->Call(value, &ignored);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Console::RetrieveConsoleEvents(JSContext* aCx, nsTArray<JS::Value>& aEvents,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// We don't want to expose this functionality to main-thread yet.
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JSObject*> targetScope(aCx, JS::CurrentGlobalOrNull(aCx));
|
2016-03-24 00:55:07 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mCallDataStorage.Length(); ++i) {
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
JS::Rooted<JSObject*> sequenceScope(aCx, mCallDataStorage[i]->mGlobal);
|
|
|
|
JSAutoCompartment ac(aCx, sequenceScope);
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
Sequence<JS::Value> sequence;
|
|
|
|
SequenceRooter<JS::Value> arguments(aCx, &sequence);
|
|
|
|
|
|
|
|
if (!mCallDataStorage[i]->PopulateArgumentsSequence(sequence)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
// Here we have aCx and sequence in the same compartment.
|
|
|
|
// targetScope is the destination scope and value will be populated in its
|
|
|
|
// compartment.
|
2016-03-24 11:29:08 +03:00
|
|
|
if (NS_WARN_IF(!PopulateConsoleNotificationInTheTargetScope(aCx, sequence,
|
|
|
|
targetScope,
|
|
|
|
&value,
|
|
|
|
mCallDataStorage[i]))) {
|
2016-03-24 00:55:07 +03:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aEvents.AppendElement(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-03-29 17:56:10 +03:00
|
|
|
Console::SetConsoleEventHandler(AnyCallback* aHandler)
|
2016-03-24 00:55:07 +03:00
|
|
|
{
|
|
|
|
AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
// We don't want to expose this functionality to main-thread yet.
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
|
|
|
|
2016-03-29 17:56:10 +03:00
|
|
|
mConsoleEventNotifier = aHandler;
|
2016-03-24 00:55:07 +03:00
|
|
|
}
|
|
|
|
|
2016-02-11 20:11:02 +03:00
|
|
|
void
|
|
|
|
Console::AssertIsOnOwningThread() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mOwningThread);
|
|
|
|
MOZ_ASSERT(PR_GetCurrentThread() == mOwningThread);
|
|
|
|
}
|
|
|
|
|
2016-05-03 11:19:39 +03:00
|
|
|
bool
|
|
|
|
Console::IsShuttingDown() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mStatus != eUnknown);
|
|
|
|
return mStatus == eShuttingDown;
|
|
|
|
}
|
|
|
|
|
2016-06-04 10:32:51 +03:00
|
|
|
/* static */ already_AddRefed<Console>
|
|
|
|
Console::GetConsole(const GlobalObject& aGlobal)
|
|
|
|
{
|
2016-11-06 11:54:52 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
RefPtr<Console> console = GetConsoleInternal(aGlobal, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed()) || !console) {
|
|
|
|
rv.SuppressException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
console->AssertIsOnOwningThread();
|
|
|
|
|
|
|
|
if (console->IsShuttingDown()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return console.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ Console*
|
|
|
|
Console::GetConsoleInternal(const GlobalObject& aGlobal, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
// Worklet
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<WorkletGlobalScope> workletScope =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (workletScope) {
|
|
|
|
return workletScope->GetConsole(aRv);
|
|
|
|
}
|
|
|
|
}
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
// Window
|
2016-06-04 10:32:51 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> innerWindow =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (NS_WARN_IF(!innerWindow)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindow* window = nsGlobalWindow::Cast(innerWindow);
|
2016-11-06 11:54:52 +03:00
|
|
|
return window->GetConsole(aRv);
|
|
|
|
}
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
// Workers
|
|
|
|
MOZ_ASSERT(!NS_IsMainThread());
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
JSContext* cx = aGlobal.Context();
|
|
|
|
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(cx);
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
WorkerGlobalScope* scope = workerPrivate->GlobalScope();
|
|
|
|
MOZ_ASSERT(scope);
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
// Normal worker scope.
|
|
|
|
if (scope == global) {
|
|
|
|
return scope->GetConsole(aRv);
|
2016-06-04 10:32:51 +03:00
|
|
|
}
|
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
// Debugger worker scope
|
|
|
|
else {
|
|
|
|
WorkerDebuggerGlobalScope* debuggerScope =
|
|
|
|
workerPrivate->DebuggerGlobalScope();
|
|
|
|
MOZ_ASSERT(debuggerScope);
|
|
|
|
MOZ_ASSERT(debuggerScope == global, "Which kind of global do we have?");
|
2016-06-04 10:32:51 +03:00
|
|
|
|
2016-11-06 11:54:52 +03:00
|
|
|
return debuggerScope->GetConsole(aRv);
|
2016-06-04 10:32:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 03:39:00 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|