2016-01-14 08:19:51 +03: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 "PushNotifier.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2016-03-28 23:33:20 +03:00
|
|
|
#include "nsICategoryManager.h"
|
2016-04-21 22:04:15 +03:00
|
|
|
#include "nsIPushErrorReporter.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2016-01-14 08:19:51 +03:00
|
|
|
#include "nsIXULRuntime.h"
|
2016-03-28 23:33:20 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsXPCOM.h"
|
2016-01-14 08:19:51 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/unused.h"
|
|
|
|
|
2016-02-28 00:54:11 +03:00
|
|
|
#include "mozilla/dom/BodyUtil.h"
|
2016-04-21 21:11:03 +03:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
2016-01-14 08:19:51 +03:00
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2016-03-28 23:33:20 +03:00
|
|
|
using workers::AssertIsOnMainThread;
|
2016-01-14 08:19:51 +03:00
|
|
|
using workers::ServiceWorkerManager;
|
|
|
|
|
|
|
|
PushNotifier::PushNotifier()
|
|
|
|
{}
|
|
|
|
|
|
|
|
PushNotifier::~PushNotifier()
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_0(PushNotifier)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushNotifier)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPushNotifier)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPushNotifier)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PushNotifier)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PushNotifier)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PushNotifier::NotifyPushWithData(const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2016-03-28 21:50:39 +03:00
|
|
|
const nsAString& aMessageId,
|
2016-01-14 08:19:51 +03:00
|
|
|
uint32_t aDataLen, uint8_t* aData)
|
|
|
|
{
|
|
|
|
nsTArray<uint8_t> data;
|
|
|
|
if (!data.SetCapacity(aDataLen, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
if (!data.InsertElementsAt(0, aData, aDataLen, fallible)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2016-03-28 21:50:39 +03:00
|
|
|
return NotifyPush(aScope, aPrincipal, aMessageId, Some(data));
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-03-28 21:50:39 +03:00
|
|
|
PushNotifier::NotifyPush(const nsACString& aScope, nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aMessageId)
|
2016-01-14 08:19:51 +03:00
|
|
|
{
|
2016-03-28 21:50:39 +03:00
|
|
|
return NotifyPush(aScope, aPrincipal, aMessageId, Nothing());
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PushNotifier::NotifySubscriptionChange(const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
{
|
2016-04-21 21:11:03 +03:00
|
|
|
nsresult rv = NotifySubscriptionChangeObservers(aScope);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
// Forward XPCOM observer notifications to the parent.
|
|
|
|
ContentChild* parentActor = ContentChild::GetSingleton();
|
|
|
|
if (!NS_WARN_IF(!parentActor)) {
|
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
!parentActor->SendNotifyPushSubscriptionChangeObservers(
|
|
|
|
PromiseFlatCString(aScope)));
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-21 21:11:03 +03:00
|
|
|
|
|
|
|
rv = NotifySubscriptionChangeWorkers(aScope, aPrincipal);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-04-21 22:04:15 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PushNotifier::NotifySubscriptionLost(const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
uint16_t aReason)
|
|
|
|
{
|
|
|
|
if (NS_WARN_IF(aReason < nsIPushErrorReporter::UNSUBSCRIBE_MANUAL ||
|
|
|
|
aReason > nsIPushErrorReporter::UNSUBSCRIBE_PERMISSION_REVOKED)) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
nsresult rv = NotifySubscriptionLostObservers(aScope, aReason);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
ContentChild* parentActor = ContentChild::GetSingleton();
|
|
|
|
if (!NS_WARN_IF(!parentActor)) {
|
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
!parentActor->SendNotifyPushSubscriptionLostObservers(
|
|
|
|
PromiseFlatCString(aScope), aReason));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:33:20 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PushNotifier::NotifyError(const nsACString& aScope, nsIPrincipal* aPrincipal,
|
|
|
|
const nsAString& aMessage, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (ShouldNotifyWorkers(aPrincipal)) {
|
|
|
|
// For service worker subscriptions, report the error to all clients.
|
|
|
|
NotifyErrorWorkers(aScope, aMessage, aFlags);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// For system subscriptions, log the error directly to the browser console.
|
|
|
|
return nsContentUtils::ReportToConsoleNonLocalized(aMessage,
|
|
|
|
aFlags,
|
|
|
|
NS_LITERAL_CSTRING("Push"),
|
|
|
|
nullptr, /* aDocument */
|
|
|
|
nullptr, /* aURI */
|
|
|
|
EmptyString(), /* aLine */
|
|
|
|
0, /* aLineNumber */
|
|
|
|
0, /* aColumnNumber */
|
|
|
|
nsContentUtils::eOMIT_LOCATION);
|
|
|
|
}
|
|
|
|
|
2016-01-14 08:19:51 +03:00
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifyPush(const nsACString& aScope, nsIPrincipal* aPrincipal,
|
2016-03-28 21:50:39 +03:00
|
|
|
const nsAString& aMessageId,
|
|
|
|
const Maybe<nsTArray<uint8_t>>& aData)
|
2016-01-14 08:19:51 +03:00
|
|
|
{
|
2016-04-21 21:11:03 +03:00
|
|
|
// Notify XPCOM observers in the current process.
|
|
|
|
nsresult rv = NotifyPushObservers(aScope, aData);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess()) {
|
|
|
|
// If we're in the content process, forward the notification to the parent.
|
|
|
|
// We don't need to do anything if we're already in the parent;
|
|
|
|
// `ContentChild::RecvPush` will notify content process observers.
|
|
|
|
ContentChild* parentActor = ContentChild::GetSingleton();
|
|
|
|
if (!NS_WARN_IF(!parentActor)) {
|
|
|
|
if (aData) {
|
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
!parentActor->SendNotifyPushObserversWithData(
|
|
|
|
PromiseFlatCString(aScope), PromiseFlatString(aMessageId),
|
|
|
|
aData.ref()));
|
|
|
|
} else {
|
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
!parentActor->SendNotifyPushObservers(
|
|
|
|
PromiseFlatCString(aScope), PromiseFlatString(aMessageId)));
|
|
|
|
}
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
}
|
2016-04-21 21:11:03 +03:00
|
|
|
|
|
|
|
rv = NotifyPushWorkers(aScope, aPrincipal, aMessageId, aData);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifyPushWorkers(const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal,
|
2016-03-28 21:50:39 +03:00
|
|
|
const nsAString& aMessageId,
|
|
|
|
const Maybe<nsTArray<uint8_t>>& aData)
|
2016-01-14 08:19:51 +03:00
|
|
|
{
|
2016-03-28 23:33:20 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-01-14 08:19:51 +03:00
|
|
|
if (!aPrincipal) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess() || !BrowserTabsRemoteAutostart()) {
|
|
|
|
// Notify the worker from the current process. Either we're running in
|
|
|
|
// the content process and received a message from the parent, or e10s
|
|
|
|
// is disabled.
|
2016-04-21 21:11:03 +03:00
|
|
|
if (!ShouldNotifyWorkers(aPrincipal)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-14 08:19:51 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsAutoCString originSuffix;
|
|
|
|
nsresult rv = aPrincipal->GetOriginSuffix(originSuffix);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-03-28 21:50:39 +03:00
|
|
|
return swm->SendPushEvent(originSuffix, aScope, aMessageId, aData);
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, we're in the parent and e10s is enabled. Broadcast the event
|
|
|
|
// to all content processes.
|
2016-01-25 15:47:06 +03:00
|
|
|
bool ok = true;
|
2016-01-14 08:19:51 +03:00
|
|
|
nsTArray<ContentParent*> contentActors;
|
|
|
|
ContentParent::GetAll(contentActors);
|
|
|
|
for (uint32_t i = 0; i < contentActors.Length(); ++i) {
|
|
|
|
if (aData) {
|
|
|
|
ok &= contentActors[i]->SendPushWithData(PromiseFlatCString(aScope),
|
2016-03-28 21:50:39 +03:00
|
|
|
IPC::Principal(aPrincipal), PromiseFlatString(aMessageId), aData.ref());
|
2016-01-14 08:19:51 +03:00
|
|
|
} else {
|
|
|
|
ok &= contentActors[i]->SendPush(PromiseFlatCString(aScope),
|
2016-03-28 21:50:39 +03:00
|
|
|
IPC::Principal(aPrincipal), PromiseFlatString(aMessageId));
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifySubscriptionChangeWorkers(const nsACString& aScope,
|
|
|
|
nsIPrincipal* aPrincipal)
|
|
|
|
{
|
2016-03-28 23:33:20 +03:00
|
|
|
AssertIsOnMainThread();
|
2016-01-14 08:19:51 +03:00
|
|
|
if (!aPrincipal) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess() || !BrowserTabsRemoteAutostart()) {
|
|
|
|
// Content process or e10s disabled.
|
2016-04-21 21:11:03 +03:00
|
|
|
if (!ShouldNotifyWorkers(aPrincipal)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-01-14 08:19:51 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (!swm) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
nsAutoCString originSuffix;
|
|
|
|
nsresult rv = aPrincipal->GetOriginSuffix(originSuffix);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return swm->SendPushSubscriptionChangeEvent(originSuffix, aScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent process, e10s enabled.
|
2016-01-25 15:47:06 +03:00
|
|
|
bool ok = true;
|
2016-01-14 08:19:51 +03:00
|
|
|
nsTArray<ContentParent*> contentActors;
|
|
|
|
ContentParent::GetAll(contentActors);
|
|
|
|
for (uint32_t i = 0; i < contentActors.Length(); ++i) {
|
|
|
|
ok &= contentActors[i]->SendPushSubscriptionChange(
|
|
|
|
PromiseFlatCString(aScope), IPC::Principal(aPrincipal));
|
|
|
|
}
|
|
|
|
return ok ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2016-03-28 23:33:20 +03:00
|
|
|
void
|
|
|
|
PushNotifier::NotifyErrorWorkers(const nsACString& aScope,
|
|
|
|
const nsAString& aMessage,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (XRE_IsContentProcess() || !BrowserTabsRemoteAutostart()) {
|
|
|
|
// Content process or e10s disabled.
|
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
|
|
|
if (swm) {
|
|
|
|
swm->ReportToAllClients(PromiseFlatCString(aScope),
|
|
|
|
PromiseFlatString(aMessage),
|
|
|
|
NS_ConvertUTF8toUTF16(aScope), /* aFilename */
|
|
|
|
EmptyString(), /* aLine */
|
|
|
|
0, /* aLineNumber */
|
|
|
|
0, /* aColumnNumber */
|
|
|
|
aFlags);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent process, e10s enabled.
|
|
|
|
nsTArray<ContentParent*> contentActors;
|
|
|
|
ContentParent::GetAll(contentActors);
|
|
|
|
if (!contentActors.IsEmpty()) {
|
|
|
|
// At least one content process active.
|
|
|
|
for (uint32_t i = 0; i < contentActors.Length(); ++i) {
|
|
|
|
Unused << NS_WARN_IF(
|
|
|
|
!contentActors[i]->SendPushError(PromiseFlatCString(aScope),
|
|
|
|
PromiseFlatString(aMessage), aFlags));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Report to the console if no content processes are active.
|
|
|
|
nsCOMPtr<nsIURI> scopeURI;
|
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(scopeURI), aScope);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(
|
|
|
|
nsContentUtils::ReportToConsoleNonLocalized(aMessage,
|
|
|
|
aFlags,
|
|
|
|
NS_LITERAL_CSTRING("Push"),
|
|
|
|
nullptr, /* aDocument */
|
|
|
|
scopeURI, /* aURI */
|
|
|
|
EmptyString(), /* aLine */
|
|
|
|
0, /* aLineNumber */
|
|
|
|
0, /* aColumnNumber */
|
|
|
|
nsContentUtils::eOMIT_LOCATION)));
|
|
|
|
}
|
|
|
|
|
2016-01-14 08:19:51 +03:00
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifyPushObservers(const nsACString& aScope,
|
2016-03-28 21:50:39 +03:00
|
|
|
const Maybe<nsTArray<uint8_t>>& aData)
|
2016-01-14 08:19:51 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPushMessage> message = nullptr;
|
|
|
|
if (aData) {
|
|
|
|
message = new PushMessage(aData.ref());
|
|
|
|
}
|
2016-03-07 08:46:10 +03:00
|
|
|
return DoNotifyObservers(message, OBSERVER_TOPIC_PUSH, aScope);
|
2016-01-14 08:19:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifySubscriptionChangeObservers(const nsACString& aScope)
|
2016-03-07 08:46:10 +03:00
|
|
|
{
|
|
|
|
return DoNotifyObservers(nullptr, OBSERVER_TOPIC_SUBSCRIPTION_CHANGE, aScope);
|
|
|
|
}
|
|
|
|
|
2016-04-21 22:04:15 +03:00
|
|
|
nsresult
|
|
|
|
PushNotifier::NotifySubscriptionLostObservers(const nsACString& aScope,
|
|
|
|
uint16_t aReason)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupportsPRUint16> wrapper =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_PRUINT16_CONTRACTID);
|
|
|
|
if (NS_WARN_IF(!wrapper || NS_FAILED(wrapper->SetData(aReason)))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
return DoNotifyObservers(wrapper, OBSERVER_TOPIC_SUBSCRIPTION_LOST, aScope);
|
|
|
|
}
|
|
|
|
|
2016-03-07 08:46:10 +03:00
|
|
|
nsresult
|
|
|
|
PushNotifier::DoNotifyObservers(nsISupports *aSubject, const char *aTopic,
|
|
|
|
const nsACString& aScope)
|
2016-01-14 08:19:51 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> obsService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (!obsService) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2016-03-07 08:46:10 +03:00
|
|
|
// If there's a service for this push category, make sure it is alive.
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan =
|
|
|
|
do_GetService(NS_CATEGORYMANAGER_CONTRACTID);
|
|
|
|
if (catMan) {
|
|
|
|
nsXPIDLCString contractId;
|
|
|
|
nsresult rv = catMan->GetCategoryEntry("push",
|
|
|
|
PromiseFlatCString(aScope).get(),
|
|
|
|
getter_Copies(contractId));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Ensure the service is created - we don't need to do anything with
|
|
|
|
// it though - we assume the service constructor attaches a listener.
|
|
|
|
nsCOMPtr<nsISupports> service = do_GetService(contractId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return obsService->NotifyObservers(aSubject, aTopic,
|
2016-01-14 08:19:51 +03:00
|
|
|
NS_ConvertUTF8toUTF16(aScope).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PushNotifier::ShouldNotifyWorkers(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
2016-04-26 20:20:08 +03:00
|
|
|
// System subscriptions use observer notifications instead of service worker
|
|
|
|
// events. The `testing.notifyWorkers` pref disables worker events for
|
2016-01-14 08:19:51 +03:00
|
|
|
// non-system subscriptions.
|
|
|
|
return !nsContentUtils::IsSystemPrincipal(aPrincipal) &&
|
|
|
|
Preferences::GetBool("dom.push.testing.notifyWorkers", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
PushMessage::PushMessage(const nsTArray<uint8_t>& aData)
|
|
|
|
: mData(aData)
|
|
|
|
{}
|
|
|
|
|
|
|
|
PushMessage::~PushMessage()
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_0(PushMessage)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PushMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPushMessage)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPushMessage)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PushMessage)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PushMessage)
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
PushMessage::EnsureDecodedText()
|
|
|
|
{
|
|
|
|
if (mData.IsEmpty() || !mDecodedText.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2016-02-28 00:54:11 +03:00
|
|
|
nsresult rv = BodyUtil::ConsumeText(
|
2016-01-14 08:19:51 +03:00
|
|
|
mData.Length(),
|
|
|
|
reinterpret_cast<uint8_t*>(mData.Elements()),
|
|
|
|
mDecodedText
|
|
|
|
);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
mDecodedText.Truncate();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PushMessage::Text(nsAString& aText)
|
|
|
|
{
|
|
|
|
nsresult rv = EnsureDecodedText();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
aText = mDecodedText;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PushMessage::Json(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JS::Value> aResult)
|
|
|
|
{
|
|
|
|
nsresult rv = EnsureDecodedText();
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
ErrorResult error;
|
2016-02-28 00:54:11 +03:00
|
|
|
BodyUtil::ConsumeJson(aCx, aResult, mDecodedText, error);
|
2016-01-14 08:19:51 +03:00
|
|
|
if (error.Failed()) {
|
|
|
|
return error.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PushMessage::Binary(uint32_t* aDataLen, uint8_t** aData)
|
|
|
|
{
|
|
|
|
if (!aDataLen || !aData) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
*aData = nullptr;
|
|
|
|
if (mData.IsEmpty()) {
|
|
|
|
*aDataLen = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
uint32_t length = mData.Length();
|
|
|
|
uint8_t* data = static_cast<uint8_t*>(NS_Alloc(length * sizeof(uint8_t)));
|
|
|
|
if (!data) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
memcpy(data, mData.Elements(), length * sizeof(uint8_t));
|
|
|
|
*aDataLen = length;
|
|
|
|
*aData = data;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|