|
|
|
|
|
|
1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 |
/* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
| 3 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 5 |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#include "BackgroundSync.h" |
| 8 |
|
| 9 |
#include "mozilla/dom/Promise.h" |
| 10 |
#include "mozilla/dom/PromiseWorkerProxy.h" |
| 11 |
#include "mozilla/dom/BackgroundSyncBinding.h" |
| 12 |
#include "mozilla/ipc/BackgroundChild.h" |
| 13 |
#include "mozilla/ipc/BackgroundUtils.h" |
| 14 |
#include "mozilla/ipc/PBackgroundChild.h" |
| 15 |
#include "mozilla/Preferences.h" |
| 16 |
#include "nsIGlobalObject.h" |
| 17 |
#include "WorkerPrivate.h" |
| 18 |
|
| 19 |
namespace mozilla { |
| 20 |
|
| 21 |
using namespace ipc; |
| 22 |
|
| 23 |
namespace dom { |
| 24 |
|
| 25 |
using namespace workers; |
| 26 |
|
| 27 |
namespace backgroundsync { |
| 28 |
|
| 29 |
// BackgroundSync |
| 30 |
|
| 31 |
// static |
| 32 |
already_AddRefed<BackgroundSync> |
| 33 |
BackgroundSync::CreateOnMainThread(nsIGlobalObject* aGlobal, |
| 34 |
nsIPrincipal* aPrincipal, |
| 35 |
ErrorResult& aRv) |
| 36 |
{ |
| 37 |
MOZ_ASSERT(aGlobal); |
| 38 |
MOZ_ASSERT(aPrincipal); |
| 39 |
MOZ_ASSERT(NS_IsMainThread()); |
| 40 |
|
| 41 |
PrincipalInfo principalInfo; |
| 42 |
nsresult rv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo); |
| 43 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 44 |
aRv.Throw(rv); |
| 45 |
return nullptr; |
| 46 |
} |
| 47 |
|
| 48 |
RefPtr<BackgroundSync> ref = new BackgroundSync(aGlobal, principalInfo); |
| 49 |
return ref.forget(); |
| 50 |
} |
| 51 |
|
| 52 |
// static |
| 53 |
already_AddRefed<BackgroundSync> |
| 54 |
BackgroundSync::CreateOnWorker(WorkerPrivate* aWorkerPrivate) |
| 55 |
{ |
| 56 |
MOZ_ASSERT(aWorkerPrivate); |
| 57 |
aWorkerPrivate->AssertIsOnWorkerThread(); |
| 58 |
|
| 59 |
const PrincipalInfo& principalInfo = aWorkerPrivate->GetPrincipalInfo(); |
| 60 |
|
| 61 |
RefPtr<BackgroundSync> ref = new BackgroundSync(nullptr, |
| 62 |
principalInfo); |
| 63 |
return ref.forget(); |
| 64 |
} |
| 65 |
|
| 66 |
BackgroundSync::BackgroundSync(nsIGlobalObject* aGlobal, |
| 67 |
const PrincipalInfo& aPrincipalInfo) |
| 68 |
: mGlobal(aGlobal) |
| 69 |
, mPrincipalInfo(new PrincipalInfo(aPrincipalInfo)) |
| 70 |
{} |
| 71 |
|
| 72 |
BackgroundSync::~BackgroundSync() |
| 73 |
{} |
| 74 |
|
| 75 |
// Bindings methods. |
| 76 |
|
| 77 |
JSObject* |
| 78 |
BackgroundSync::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
| 79 |
{ |
| 80 |
return mozilla::dom::BackgroundSyncBinding::Wrap(aCx, this, aGivenProto); |
| 81 |
} |
| 82 |
|
| 83 |
// static |
| 84 |
bool |
| 85 |
BackgroundSync::PrefEnabled(JSContext* aCx, JSObject* aObj) |
| 86 |
{ |
| 87 |
using mozilla::dom::workers::WorkerPrivate; |
| 88 |
using mozilla::dom::workers::GetWorkerPrivateFromContext; |
| 89 |
|
| 90 |
// If we are on the main thread, then check the pref directly. |
| 91 |
if (NS_IsMainThread()) { |
| 92 |
return Preferences::GetBool("dom.background.sync.enabled", false); |
| 93 |
} |
| 94 |
|
| 95 |
// Otherwise check the pref via the worker private helper. |
| 96 |
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); |
| 97 |
if (!workerPrivate) { |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
return workerPrivate->BackgroundSyncEnabled(); |
| 102 |
} |
| 103 |
|
| 104 |
// WebIDL interace methods. |
| 105 |
|
| 106 |
already_AddRefed<Promise> |
| 107 |
BackgroundSync::Register(const nsAString& aName, ErrorResult& aRv) |
| 108 |
{ |
| 109 |
RefPtr<Promise> p = Promise::Create(mGlobal, aRv); |
| 110 |
if (NS_WARN_IF(aRv.Failed())) { |
| 111 |
return nullptr; |
| 112 |
} |
| 113 |
|
| 114 |
return p.forget(); |
| 115 |
} |
| 116 |
|
| 117 |
already_AddRefed<Promise> |
| 118 |
BackgroundSync::GetTags(ErrorResult& aRv) |
| 119 |
{ |
| 120 |
RefPtr<Promise> p = Promise::Create(mGlobal, aRv); |
| 121 |
if (NS_WARN_IF(aRv.Failed())) { |
| 122 |
return nullptr; |
| 123 |
} |
| 124 |
|
| 125 |
return p.forget(); |
| 126 |
} |
| 127 |
|
| 128 |
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(BackgroundSync, mGlobal) |
| 129 |
NS_IMPL_CYCLE_COLLECTING_ADDREF(BackgroundSync) |
| 130 |
NS_IMPL_CYCLE_COLLECTING_RELEASE(BackgroundSync) |
| 131 |
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(BackgroundSync) |
| 132 |
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
| 133 |
NS_INTERFACE_MAP_ENTRY(nsISupports) |
| 134 |
NS_INTERFACE_MAP_END |
| 135 |
|
| 136 |
} // namespace backgroundsync |
| 137 |
} // namespace dom |
| 138 |
} // namespace mozilla |