Bug 1357473 - Update Console to the latest spec - part 6 - Console.count(), r=bgrins

This commit is contained in:
Andrea Marchesini 2017-04-20 13:52:41 +02:00
parent 62832535e3
commit 81bc08c5d9
3 changed files with 36 additions and 36 deletions

View File

@ -61,9 +61,6 @@
// This value is taken from ConsoleAPIStorage.js
#define STORAGE_MAX_EVENTS 1000
// Default label for console.count.
#define COUNT_DEFAULT_LABEL "default"
using namespace mozilla::dom::exceptions;
using namespace mozilla::dom::workers;
@ -989,18 +986,18 @@ Console::GroupEnd(const GlobalObject& aGlobal)
/* static */ void
Console::Time(const GlobalObject& aGlobal, const nsAString& aLabel)
{
TimeMethod(aGlobal, aLabel, MethodTime, NS_LITERAL_STRING("time"));
StringMethod(aGlobal, aLabel, MethodTime, NS_LITERAL_STRING("time"));
}
/* static */ void
Console::TimeEnd(const GlobalObject& aGlobal, const nsAString& aLabel)
{
TimeMethod(aGlobal, aLabel, MethodTimeEnd, NS_LITERAL_STRING("timeEnd"));
StringMethod(aGlobal, aLabel, MethodTimeEnd, NS_LITERAL_STRING("timeEnd"));
}
/* static */ void
Console::TimeMethod(const GlobalObject& aGlobal, const nsAString& aLabel,
MethodName aMethodName, const nsAString& aMethodString)
Console::StringMethod(const GlobalObject& aGlobal, const nsAString& aLabel,
MethodName aMethodName, const nsAString& aMethodString)
{
JSContext* cx = aGlobal.Context();
@ -1128,7 +1125,11 @@ Console::Assert(const GlobalObject& aGlobal, bool aCondition,
}
}
METHOD(Count, "count")
/* static */ void
Console::Count(const GlobalObject& aGlobal, const nsAString& aLabel)
{
StringMethod(aGlobal, aLabel, MethodCount, NS_LITERAL_STRING("count"));
}
/* static */ void
Console::NoopMethod(const GlobalObject& aGlobal)
@ -1378,8 +1379,10 @@ Console::MethodInternal(JSContext* aCx, MethodName aMethodName,
}
else if (aMethodName == MethodCount) {
callData->mCountValue = IncreaseCounter(aCx, aData,
callData->mCountLabel);
callData->mCountValue = IncreaseCounter(aCx, aData, callData->mCountLabel);
if (!callData->mCountValue) {
return;
}
}
if (NS_IsMainThread()) {
@ -2154,35 +2157,29 @@ Console::IncreaseCounter(JSContext* aCx, const Sequence<JS::Value>& aArguments,
ClearException ce(aCx);
nsAutoString label;
MOZ_ASSERT(!aArguments.IsEmpty());
if (aArguments.IsEmpty()) {
label.AssignLiteral(COUNT_DEFAULT_LABEL);
} else {
JS::Rooted<JS::Value> labelValue(aCx, aArguments[0]);
if (labelValue.isUndefined()) {
label.AssignLiteral(COUNT_DEFAULT_LABEL);
} else {
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, labelValue));
nsAutoJSString string;
if (jsString && string.init(aCx, jsString)) {
label = string;
}
}
JS::Rooted<JS::Value> labelValue(aCx, aArguments[0]);
JS::Rooted<JSString*> jsString(aCx, JS::ToString(aCx, labelValue));
if (!jsString) {
return 0; // We cannot continue.
}
nsAutoJSString string;
if (!string.init(aCx, jsString)) {
return 0; // We cannot continue.
}
aCountLabel = string;
uint32_t count = 0;
if (!mCounterRegistry.Get(label, &count) &&
if (!mCounterRegistry.Get(aCountLabel, &count) &&
mCounterRegistry.Count() >= MAX_PAGE_COUNTERS) {
return MAX_PAGE_COUNTERS;
}
++count;
mCounterRegistry.Put(label, count);
aCountLabel = label;
mCounterRegistry.Put(aCountLabel, count);
return count;
}

View File

@ -107,7 +107,7 @@ public:
const Sequence<JS::Value>& aData);
static void
Count(const GlobalObject& aGlobal, const Sequence<JS::Value>& aData);
Count(const GlobalObject& aGlobal, const nsAString& aLabel);
static void
Clear(const GlobalObject& aGlobal);
@ -181,8 +181,8 @@ private:
const nsAString& aString, const Sequence<JS::Value>& aData);
static void
TimeMethod(const GlobalObject& aGlobal, const nsAString& aLabel,
MethodName aMethodName, const nsAString& aMethodString);
StringMethod(const GlobalObject& aGlobal, const nsAString& aLabel,
MethodName aMethodName, const nsAString& aMethodString);
// This method must receive aCx and aArguments in the same JSCompartment.
void
@ -326,8 +326,11 @@ private:
// This method follows the same pattern as StartTimer: its runs on the owning
// thread and populate aCountLabel, used by CreateCounterValue. Returns
// MAX_PAGE_COUNTERS in case of error, otherwise the incremented counter
// value.
// 3 possible values:
// * MAX_PAGE_COUNTERS in case of error that has to be reported;
// * 0 in case of a CX exception. The operation cannot continue;
// * the incremented counter value.
// Params:
// * aCx - the JSContext rooting aData.
// * aData - the arguments received by the console.count() method.
// * aCountLabel - the label that will be populated by this method.

View File

@ -15,7 +15,7 @@ namespace console {
// Logging
void assert(optional boolean condition = false, any... data);
void clear();
void count(any... data); // Bug 1346326 already covers this
void count(optional DOMString label = "default");
void debug(any... data);
void error(any... data);
void info(any... data);