|
|
|
|
|
|
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 "SyncManager.h" |
| 8 |
|
| 9 |
#include "mozilla/dom/Promise.h" |
| 10 |
#include "mozilla/dom/PromiseWorkerProxy.h" |
| 11 |
#include "mozilla/dom/SyncManagerBinding.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 |
// SyncManager |
| 28 |
|
| 29 |
// static |
| 30 |
already_AddRefed<SyncManager> |
| 31 |
SyncManager::CreateOnMainThread(nsIGlobalObject* aGlobal, |
| 32 |
nsIPrincipal* aPrincipal, |
| 33 |
ErrorResult& aRv) |
| 34 |
{ |
| 35 |
MOZ_ASSERT(aGlobal); |
| 36 |
MOZ_ASSERT(aPrincipal); |
| 37 |
MOZ_ASSERT(NS_IsMainThread()); |
| 38 |
|
| 39 |
PrincipalInfo principalInfo; |
| 40 |
nsresult rv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo); |
| 41 |
if (NS_WARN_IF(NS_FAILED(rv))) { |
| 42 |
aRv.Throw(rv); |
| 43 |
return nullptr; |
| 44 |
} |
| 45 |
|
| 46 |
RefPtr<SyncManager> ref = new SyncManager(aGlobal, principalInfo); |
| 47 |
return ref.forget(); |
| 48 |
} |
| 49 |
|
| 50 |
// static |
| 51 |
already_AddRefed<SyncManager> |
| 52 |
SyncManager::CreateOnWorker(WorkerPrivate* aWorkerPrivate) |
| 53 |
{ |
| 54 |
MOZ_ASSERT(aWorkerPrivate); |
| 55 |
aWorkerPrivate->AssertIsOnWorkerThread(); |
| 56 |
|
| 57 |
const PrincipalInfo& principalInfo = aWorkerPrivate->GetPrincipalInfo(); |
| 58 |
|
| 59 |
RefPtr<SyncManager> ref = new SyncManager(nullptr, principalInfo); |
| 60 |
return ref.forget(); |
| 61 |
} |
| 62 |
|
| 63 |
SyncManager::SyncManager(nsIGlobalObject* aGlobal, |
| 64 |
const PrincipalInfo& aPrincipalInfo) |
| 65 |
: mGlobal(aGlobal) |
| 66 |
, mPrincipalInfo(new PrincipalInfo(aPrincipalInfo)) |
| 67 |
{} |
| 68 |
|
| 69 |
SyncManager::~SyncManager() |
| 70 |
{} |
| 71 |
|
| 72 |
// Bindings methods. |
| 73 |
|
| 74 |
JSObject* |
| 75 |
SyncManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
| 76 |
{ |
| 77 |
return SyncManagerBinding::Wrap(aCx, this, aGivenProto); |
| 78 |
} |
| 79 |
|
| 80 |
// static |
| 81 |
bool |
| 82 |
SyncManager::PrefEnabled(JSContext* aCx, JSObject* aObj) |
| 83 |
{ |
| 84 |
using mozilla::dom::workers::WorkerPrivate; |
| 85 |
using mozilla::dom::workers::GetWorkerPrivateFromContext; |
| 86 |
|
| 87 |
// If we are on the main thread, then check the pref directly. |
| 88 |
if (NS_IsMainThread()) { |
| 89 |
bool enabled = false; |
| 90 |
Preferences::GetBool("dom.background.sync.enabled", &enabled); |
| 91 |
return enabled; |
| 92 |
} |
| 93 |
|
| 94 |
// Otherwise check the pref via the worker private helper. |
| 95 |
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx); |
| 96 |
if (!workerPrivate) { |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
return workerPrivate->BackgroundSyncEnabled(); |
| 101 |
} |
| 102 |
|
| 103 |
// WebIDL interace methods. |
| 104 |
|
| 105 |
already_AddRefed<Promise> |
| 106 |
SyncManager::Register(const nsAString& aName, ErrorResult& aRv) |
| 107 |
{ |
| 108 |
RefPtr<Promise> p = Promise::Create(mGlobal, aRv); |
| 109 |
if (NS_WARN_IF(aRv.Failed())) { |
| 110 |
return nullptr; |
| 111 |
} |
| 112 |
|
| 113 |
return p.forget(); |
| 114 |
} |
| 115 |
|
| 116 |
already_AddRefed<Promise> |
| 117 |
SyncManager::GetTags(ErrorResult& aRv) |
| 118 |
{ |
| 119 |
RefPtr<Promise> p = Promise::Create(mGlobal, aRv); |
| 120 |
if (NS_WARN_IF(aRv.Failed())) { |
| 121 |
return nullptr; |
| 122 |
} |
| 123 |
|
| 124 |
return p.forget(); |
| 125 |
} |
| 126 |
|
| 127 |
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(SyncManager, mGlobal) |
| 128 |
NS_IMPL_CYCLE_COLLECTING_ADDREF(SyncManager) |
| 129 |
NS_IMPL_CYCLE_COLLECTING_RELEASE(SyncManager) |
| 130 |
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SyncManager) |
| 131 |
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
| 132 |
NS_INTERFACE_MAP_ENTRY(nsISupports) |
| 133 |
NS_INTERFACE_MAP_END |
| 134 |
|
| 135 |
} // namespace dom |
| 136 |
} // namespace mozilla |