2017-07-14 11:58:56 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "nsCodeCoverage.h"
|
|
|
|
#include "mozilla/CodeCoverageHandler.h"
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2018-06-22 16:31:59 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2017-07-14 11:58:56 +03:00
|
|
|
|
2020-01-29 15:05:47 +03:00
|
|
|
using mozilla::dom::ContentParent;
|
|
|
|
using mozilla::dom::Promise;
|
2017-07-14 11:58:56 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsCodeCoverage, nsICodeCoverage)
|
|
|
|
|
|
|
|
nsCodeCoverage::nsCodeCoverage() {}
|
|
|
|
|
|
|
|
nsCodeCoverage::~nsCodeCoverage() {}
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
enum RequestType { Flush };
|
2018-06-22 16:31:59 +03:00
|
|
|
|
|
|
|
class ProcessCount final {
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(ProcessCount);
|
|
|
|
|
|
|
|
public:
|
2018-06-30 01:14:21 +03:00
|
|
|
explicit ProcessCount(uint32_t c) : mCount(c) {}
|
2018-06-22 16:31:59 +03:00
|
|
|
operator uint32_t() const { return mCount; }
|
|
|
|
ProcessCount& operator--() {
|
|
|
|
mCount--;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ProcessCount() {}
|
|
|
|
uint32_t mCount;
|
|
|
|
};
|
|
|
|
|
2020-01-29 15:05:47 +03:00
|
|
|
namespace {
|
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
nsresult Request(JSContext* cx, Promise** aPromise, RequestType requestType) {
|
2017-07-14 11:58:56 +03:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess());
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-08-01 12:25:50 +03:00
|
|
|
nsIGlobalObject* global = xpc::CurrentNativeGlobal(cx);
|
2018-06-22 16:31:59 +03:00
|
|
|
if (NS_WARN_IF(!global)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:05:47 +03:00
|
|
|
mozilla::ErrorResult result;
|
2018-06-22 16:31:59 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t processCount = 0;
|
2017-07-14 11:58:56 +03:00
|
|
|
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
2020-01-29 15:05:47 +03:00
|
|
|
mozilla::Unused << cp;
|
2018-06-22 16:31:59 +03:00
|
|
|
++processCount;
|
2018-06-22 16:31:59 +03:00
|
|
|
}
|
2017-07-14 11:58:56 +03:00
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
if (requestType == RequestType::Flush) {
|
2020-01-29 15:05:47 +03:00
|
|
|
mozilla::CodeCoverageHandler::FlushCounters();
|
2018-06-22 16:31:59 +03:00
|
|
|
}
|
2018-06-22 16:31:59 +03:00
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
if (processCount == 0) {
|
|
|
|
promise->MaybeResolveWithUndefined();
|
|
|
|
} else {
|
|
|
|
RefPtr<ProcessCount> processCountHolder(new ProcessCount(processCount));
|
2018-06-22 16:31:59 +03:00
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
auto resolve = [processCountHolder, promise](bool unused) {
|
|
|
|
if (--(*processCountHolder) == 0) {
|
|
|
|
promise->MaybeResolveWithUndefined();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-29 15:05:47 +03:00
|
|
|
auto reject = [promise](mozilla::ipc::ResponseRejectReason&& aReason) {
|
2018-06-22 16:31:59 +03:00
|
|
|
promise->MaybeReject(NS_ERROR_FAILURE);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (auto* cp : ContentParent::AllProcesses(ContentParent::eLive)) {
|
2018-10-15 11:36:45 +03:00
|
|
|
if (requestType == RequestType::Flush) {
|
|
|
|
cp->SendFlushCodeCoverageCounters(resolve, reject);
|
2018-06-22 16:31:59 +03:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 16:57:10 +03:00
|
|
|
}
|
|
|
|
|
2018-06-22 16:31:59 +03:00
|
|
|
promise.forget(aPromise);
|
2018-06-27 16:57:10 +03:00
|
|
|
return NS_OK;
|
2018-06-22 16:31:59 +03:00
|
|
|
}
|
2018-06-22 16:31:59 +03:00
|
|
|
|
2020-01-29 15:05:47 +03:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2018-10-15 11:36:45 +03:00
|
|
|
NS_IMETHODIMP nsCodeCoverage::FlushCounters(JSContext* cx, Promise** aPromise) {
|
|
|
|
return Request(cx, aPromise, RequestType::Flush);
|
2018-06-22 16:31:59 +03:00
|
|
|
}
|