2015-05-03 12:32:37 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2014-06-03 07:36:46 -07: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 "DataStoreService.h"
|
|
|
|
|
|
|
|
#include "DataStoreCallbacks.h"
|
|
|
|
#include "DataStoreDB.h"
|
|
|
|
#include "DataStoreRevision.h"
|
|
|
|
#include "mozilla/dom/DataStore.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "mozilla/dom/DataStoreBinding.h"
|
|
|
|
#include "mozilla/dom/DataStoreImplBinding.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "nsIDataStore.h"
|
|
|
|
|
2015-08-17 02:03:19 -07:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "mozilla/dom/ContentChild.h"
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "mozilla/dom/DOMError.h"
|
|
|
|
#include "mozilla/dom/indexedDB/IDBCursor.h"
|
|
|
|
#include "mozilla/dom/indexedDB/IDBObjectStore.h"
|
2014-09-26 16:21:57 -07:00
|
|
|
#include "mozilla/dom/indexedDB/IDBRequest.h"
|
|
|
|
#include "mozilla/dom/indexedDB/IDBTransaction.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "mozilla/dom/PermissionMessageUtils.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/unused.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
#include "mozIApplication.h"
|
|
|
|
#include "mozIApplicationClearPrivateDataParams.h"
|
|
|
|
#include "nsIAppsService.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMGlobalPropertyInitializer.h"
|
|
|
|
#include "nsIIOService.h"
|
2014-06-27 00:58:47 -07:00
|
|
|
#include "nsIMutableArray.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIPermissionManager.h"
|
|
|
|
#include "nsIScriptSecurityManager.h"
|
2014-06-27 00:58:47 -07:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2014-06-03 07:36:46 -07:00
|
|
|
#include "nsIUUIDGenerator.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsXULAppAPI.h"
|
|
|
|
|
|
|
|
#define ASSERT_PARENT_PROCESS() \
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess()); \
|
|
|
|
if (NS_WARN_IF(!XRE_IsParentProcess())) { \
|
2014-06-03 07:36:46 -07:00
|
|
|
return NS_ERROR_FAILURE; \
|
|
|
|
}
|
|
|
|
|
2015-08-17 02:03:19 -07:00
|
|
|
using mozilla::BasePrincipal;
|
2015-11-02 17:50:54 -08:00
|
|
|
using mozilla::PrincipalOriginAttributes;
|
2015-08-17 02:03:19 -07:00
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace indexedDB;
|
|
|
|
|
|
|
|
// This class contains all the information about a DataStore.
|
|
|
|
class DataStoreInfo
|
|
|
|
{
|
|
|
|
public:
|
2014-06-03 07:36:46 -07:00
|
|
|
DataStoreInfo()
|
|
|
|
: mReadOnly(true)
|
|
|
|
, mEnabled(false)
|
|
|
|
{}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
DataStoreInfo(const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly,
|
|
|
|
bool aEnabled)
|
2016-01-12 10:16:59 -08:00
|
|
|
: mReadOnly(true)
|
|
|
|
, mEnabled(false)
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
|
|
|
Init(aName, aOriginURL, aManifestURL, aReadOnly, aEnabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly,
|
|
|
|
bool aEnabled)
|
|
|
|
{
|
|
|
|
mName = aName;
|
|
|
|
mOriginURL = aOriginURL;
|
|
|
|
mManifestURL = aManifestURL;
|
|
|
|
mReadOnly = aReadOnly;
|
|
|
|
mEnabled = aEnabled;
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
void Update(const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
|
|
|
mName = aName;
|
|
|
|
mOriginURL = aOriginURL;
|
|
|
|
mManifestURL = aManifestURL;
|
|
|
|
mReadOnly = aReadOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Enable()
|
|
|
|
{
|
|
|
|
mEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString mName;
|
|
|
|
nsString mOriginURL;
|
|
|
|
nsString mManifestURL;
|
|
|
|
bool mReadOnly;
|
|
|
|
|
|
|
|
// A DataStore is enabled when it has its first revision.
|
|
|
|
bool mEnabled;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Singleton for DataStoreService.
|
|
|
|
StaticRefPtr<DataStoreService> gDataStoreService;
|
2015-09-17 22:49:24 -07:00
|
|
|
nsString gHomeScreenManifestURL;
|
2014-06-03 07:36:46 -07:00
|
|
|
static uint64_t gCounterID = 0;
|
|
|
|
|
|
|
|
typedef nsClassHashtable<nsUint32HashKey, DataStoreInfo> HashApp;
|
|
|
|
|
|
|
|
void
|
2016-01-30 09:05:36 -08:00
|
|
|
RejectPromise(nsPIDOMWindowInner* aWindow, Promise* aPromise, nsresult aRv)
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(NS_FAILED(aRv));
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DOMError> error;
|
2014-06-03 07:36:46 -07:00
|
|
|
if (aRv == NS_ERROR_DOM_SECURITY_ERR) {
|
|
|
|
error = new DOMError(aWindow, NS_LITERAL_STRING("SecurityError"),
|
|
|
|
NS_LITERAL_STRING("Access denied"));
|
|
|
|
} else {
|
|
|
|
error = new DOMError(aWindow, NS_LITERAL_STRING("InternalError"),
|
|
|
|
NS_LITERAL_STRING("An error occurred"));
|
|
|
|
}
|
|
|
|
|
2014-06-03 08:38:38 -07:00
|
|
|
aPromise->MaybeRejectBrokenly(error);
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DeleteDatabase(const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreDB> db = new DataStoreDB(aManifestURL, aName);
|
2014-06-03 07:36:46 -07:00
|
|
|
db->Delete();
|
|
|
|
}
|
|
|
|
|
2016-01-27 16:04:59 -08:00
|
|
|
static void
|
|
|
|
DeleteDataStoresHelper(nsClassHashtable<nsStringHashKey, HashApp>& aStores,
|
|
|
|
uint32_t aAppId)
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2016-01-27 16:04:59 -08:00
|
|
|
for (auto iter1 = aStores.Iter(); !iter1.Done(); iter1.Next()) {
|
|
|
|
nsAutoPtr<HashApp>& apps = iter1.Data();
|
|
|
|
for (auto iter2 = apps->Iter(); !iter2.Done(); iter2.Next()) {
|
|
|
|
if (aAppId == iter2.Key()) {
|
|
|
|
nsAutoPtr<DataStoreInfo>& info = iter2.Data();
|
|
|
|
DeleteDatabase(info->mName, info->mManifestURL);
|
|
|
|
iter2.Remove();
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2016-01-27 16:04:59 -08:00
|
|
|
if (apps->Count() == 0) {
|
|
|
|
iter1.Remove();
|
|
|
|
}
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GeneratePermissionName(nsAString& aPermission,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
{
|
2014-06-26 05:09:00 -07:00
|
|
|
aPermission.AssignLiteral("indexedDB-chrome-");
|
2014-06-03 07:36:46 -07:00
|
|
|
aPermission.Append(aName);
|
2014-06-26 05:09:00 -07:00
|
|
|
aPermission.Append('|');
|
2014-06-03 07:36:46 -07:00
|
|
|
aPermission.Append(aManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
ResetPermission(uint32_t aAppId, const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
const nsAString& aPermission,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ioService(do_GetService(NS_IOSERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = ioService->NewURI(NS_ConvertUTF16toUTF8(aOriginURL), nullptr, nullptr,
|
|
|
|
getter_AddRefs(uri));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-11-02 17:50:54 -08:00
|
|
|
PrincipalOriginAttributes attrs(aAppId, false);
|
2015-08-17 02:03:19 -07:00
|
|
|
nsCOMPtr<nsIPrincipal> principal =
|
|
|
|
BasePrincipal::CreateCodebasePrincipal(uri, attrs);
|
|
|
|
NS_ENSURE_TRUE(principal, NS_ERROR_FAILURE);
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPermissionManager> pm =
|
|
|
|
do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
|
|
|
|
if (!pm) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCString basePermission;
|
|
|
|
basePermission.Append(NS_ConvertUTF16toUTF8(aPermission));
|
|
|
|
|
|
|
|
// Write permission
|
|
|
|
{
|
|
|
|
nsCString permission;
|
|
|
|
permission.Append(basePermission);
|
2014-06-26 05:09:00 -07:00
|
|
|
permission.AppendLiteral("-write");
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
rv = pm->TestExactPermissionFromPrincipal(principal, permission.get(),
|
|
|
|
&perm);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aReadOnly && perm == nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
rv = pm->RemoveFromPrincipal(principal, permission.get());
|
|
|
|
}
|
|
|
|
else if (!aReadOnly && perm != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
rv = pm->AddFromPrincipal(principal, permission.get(),
|
|
|
|
nsIPermissionManager::ALLOW_ACTION,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read permission
|
|
|
|
{
|
|
|
|
nsCString permission;
|
|
|
|
permission.Append(basePermission);
|
2014-06-26 05:09:00 -07:00
|
|
|
permission.AppendLiteral("-read");
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
rv = pm->TestExactPermissionFromPrincipal(principal, permission.get(),
|
|
|
|
&perm);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perm != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
rv = pm->AddFromPrincipal(principal, permission.get(),
|
|
|
|
nsIPermissionManager::ALLOW_ACTION,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic permission
|
|
|
|
uint32_t perm = nsIPermissionManager::UNKNOWN_ACTION;
|
|
|
|
rv = pm->TestExactPermissionFromPrincipal(principal, basePermission.get(),
|
|
|
|
&perm);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (perm != nsIPermissionManager::ALLOW_ACTION) {
|
|
|
|
rv = pm->AddFromPrincipal(principal, basePermission.get(),
|
|
|
|
nsIPermissionManager::ALLOW_ACTION,
|
|
|
|
nsIPermissionManager::EXPIRE_NEVER, 0);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-17 22:49:24 -07:00
|
|
|
void
|
|
|
|
HomeScreenPrefCallback(const char* aPrefName, void* /* aClosure */)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = DataStoreService::Get();
|
2015-09-17 22:49:24 -07:00
|
|
|
if (!service) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
service->HomeScreenPrefChanged();
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
} /* anonymous namespace */
|
|
|
|
|
|
|
|
// A PendingRequest is created when a content code wants a list of DataStores
|
|
|
|
// but some of them are not enabled yet.
|
|
|
|
class PendingRequest
|
|
|
|
{
|
|
|
|
public:
|
2016-01-30 09:05:36 -08:00
|
|
|
void Init(nsPIDOMWindowInner* aWindow, Promise* aPromise,
|
2014-06-03 07:36:46 -07:00
|
|
|
const nsTArray<DataStoreInfo>& aStores,
|
|
|
|
const nsTArray<nsString>& aPendingDataStores)
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
2014-06-03 07:36:46 -07:00
|
|
|
mWindow = aWindow;
|
|
|
|
mPromise = aPromise;
|
|
|
|
mStores = aStores;
|
|
|
|
mPendingDataStores = aPendingDataStores;
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
2016-01-30 09:05:36 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> mWindow;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
2014-06-03 07:36:46 -07:00
|
|
|
nsTArray<DataStoreInfo> mStores;
|
|
|
|
|
|
|
|
// This array contains the list of manifestURLs of the DataStores that are
|
|
|
|
// not enabled yet.
|
|
|
|
nsTArray<nsString> mPendingDataStores;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This callback is used to enable a DataStore when its first revisionID is
|
|
|
|
// created.
|
2015-03-21 09:28:04 -07:00
|
|
|
class RevisionAddedEnableStoreCallback final :
|
2014-06-03 07:36:46 -07:00
|
|
|
public DataStoreRevisionCallback
|
|
|
|
{
|
2014-06-18 19:28:59 -07:00
|
|
|
private:
|
|
|
|
~RevisionAddedEnableStoreCallback() {}
|
2014-06-03 07:36:46 -07:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(RevisionAddedEnableStoreCallback);
|
|
|
|
|
|
|
|
RevisionAddedEnableStoreCallback(uint32_t aAppId,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
: mAppId(aAppId)
|
|
|
|
, mName(aName)
|
|
|
|
, mManifestURL(aManifestURL)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Run(const nsAString& aRevisionId)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = DataStoreService::Get();
|
2014-06-03 07:36:46 -07:00
|
|
|
MOZ_ASSERT(service);
|
|
|
|
|
|
|
|
service->EnableDataStore(mAppId, mName, mManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint32_t mAppId;
|
|
|
|
nsString mName;
|
|
|
|
nsString mManifestURL;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This DataStoreDBCallback is called when DataStoreDB opens the DataStore DB.
|
2014-08-03 07:48:19 -07:00
|
|
|
// Then the first revision will be created if it's needed.
|
2015-03-21 09:28:04 -07:00
|
|
|
class FirstRevisionIdCallback final : public DataStoreDBCallback
|
2015-04-16 04:25:42 -07:00
|
|
|
, public nsIDOMEventListener
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
|
|
|
public:
|
2015-04-16 04:25:42 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
FirstRevisionIdCallback(uint32_t aAppId, const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
: mAppId(aAppId)
|
|
|
|
, mName(aName)
|
|
|
|
, mManifestURL(aManifestURL)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 13:42:28 -07:00
|
|
|
Run(DataStoreDB* aDb, RunStatus aStatus) override
|
2014-06-03 07:36:46 -07:00
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
MOZ_ASSERT(aDb);
|
|
|
|
|
2014-08-03 07:48:19 -07:00
|
|
|
if (aStatus == Error) {
|
2014-06-03 07:36:46 -07:00
|
|
|
NS_WARNING("Failed to create the first revision.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-16 04:25:42 -07:00
|
|
|
ErrorResult error;
|
|
|
|
|
2014-08-03 07:48:19 -07:00
|
|
|
if (aStatus == Success) {
|
2015-04-16 04:25:42 -07:00
|
|
|
mTxn = aDb->Transaction();
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBObjectStore> store =
|
2015-04-16 04:25:42 -07:00
|
|
|
mTxn->ObjectStore(NS_LITERAL_STRING(DATASTOREDB_REVISION), error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
mRequest = store->OpenCursor(cx, JS::UndefinedHandleValue,
|
|
|
|
IDBCursorDirection::Prev, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = mRequest->EventTarget::AddEventListener(NS_LITERAL_STRING("success"),
|
|
|
|
this, false);
|
2014-08-03 07:48:19 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2015-04-16 04:25:42 -07:00
|
|
|
NS_WARNING("Failed to add an EventListener.");
|
|
|
|
return;
|
2014-08-03 07:48:19 -07:00
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-03 07:48:19 -07:00
|
|
|
// The DB has just been created.
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-04-16 04:25:42 -07:00
|
|
|
error = CreateFirstRevision(aDb->Transaction());
|
|
|
|
if (error.Failed()) {
|
|
|
|
NS_WARNING("Failed to add a revision to a DataStore.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
CreateFirstRevision(IDBTransaction* aTxn)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aTxn);
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
ErrorResult error;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBObjectStore> store =
|
2015-04-16 04:25:42 -07:00
|
|
|
aTxn->ObjectStore(NS_LITERAL_STRING(DATASTOREDB_REVISION), error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 06:18:51 -07:00
|
|
|
return error.StealNSResult();
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(store);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<RevisionAddedEnableStoreCallback> callback =
|
2014-06-03 07:36:46 -07:00
|
|
|
new RevisionAddedEnableStoreCallback(mAppId, mName, mManifestURL);
|
|
|
|
|
2014-08-03 07:48:19 -07:00
|
|
|
// Note: this cx is only used for rooting and AddRevision, neither of which
|
|
|
|
// actually care which compartment we're in.
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// If the revision doesn't exist, let's create it.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreRevision> revision = new DataStoreRevision();
|
2014-08-03 07:48:19 -07:00
|
|
|
nsresult rv = revision->AddRevision(cx, store, 0,
|
|
|
|
DataStoreRevision::RevisionVoid,
|
|
|
|
callback);
|
2015-04-16 04:25:42 -07:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIDOMEventListener
|
|
|
|
NS_IMETHOD
|
2015-04-16 13:42:28 -07:00
|
|
|
HandleEvent(nsIDOMEvent* aEvent) override
|
2015-04-16 04:25:42 -07:00
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2015-04-16 04:25:42 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBRequest> request;
|
2015-04-16 04:25:42 -07:00
|
|
|
request.swap(mRequest);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBTransaction> txn;
|
2015-04-16 04:25:42 -07:00
|
|
|
txn.swap(mTxn);
|
|
|
|
|
|
|
|
request->RemoveEventListener(NS_LITERAL_STRING("success"), this, false);
|
|
|
|
|
|
|
|
nsString type;
|
|
|
|
nsresult rv = aEvent->GetType(type);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
MOZ_ASSERT(type.EqualsASCII("success"));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
JS::Rooted<JS::Value> result(cx);
|
|
|
|
request->GetResult(cx, &result, error);
|
|
|
|
if (NS_WARN_IF(error.Failed())) {
|
2015-04-27 06:18:51 -07:00
|
|
|
return error.StealNSResult();
|
2014-08-03 07:48:19 -07:00
|
|
|
}
|
2015-04-16 04:25:42 -07:00
|
|
|
|
|
|
|
// This means that the content is a IDBCursor, so the first revision already
|
|
|
|
// exists.
|
|
|
|
if (result.isObject()) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
IDBCursor* cursor = nullptr;
|
|
|
|
error = UNWRAP_OBJECT(IDBCursor, &result.toObject(), cursor);
|
|
|
|
MOZ_ASSERT(!error.Failed());
|
|
|
|
#endif
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = DataStoreService::Get();
|
2015-04-16 04:25:42 -07:00
|
|
|
MOZ_ASSERT(service);
|
|
|
|
|
|
|
|
return service->EnableDataStore(mAppId, mName, mManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = CreateFirstRevision(txn);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-23 12:56:07 -07:00
|
|
|
~FirstRevisionIdCallback() {}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<IDBRequest> mRequest;
|
|
|
|
RefPtr<IDBTransaction> mTxn;
|
2015-04-16 04:25:42 -07:00
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
uint32_t mAppId;
|
|
|
|
nsString mName;
|
|
|
|
nsString mManifestURL;
|
|
|
|
};
|
|
|
|
|
2015-04-16 04:25:42 -07:00
|
|
|
NS_IMPL_ISUPPORTS(FirstRevisionIdCallback, nsIDOMEventListener)
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// This class calls the 'retrieveRevisionId' method of the DataStore object for
|
|
|
|
// any DataStore in the 'mResults' array. When all of them are called, the
|
|
|
|
// promise is resolved with 'mResults'.
|
|
|
|
// The reson why this has to be done is because DataStore are object that can be
|
|
|
|
// created in any thread and in any process. The first revision has been
|
|
|
|
// created, but they don't know its value yet.
|
|
|
|
class RetrieveRevisionsCounter
|
|
|
|
{
|
2014-06-18 19:28:59 -07:00
|
|
|
private:
|
|
|
|
~RetrieveRevisionsCounter() {}
|
2014-06-03 07:36:46 -07:00
|
|
|
public:
|
|
|
|
NS_INLINE_DECL_REFCOUNTING(RetrieveRevisionsCounter);
|
|
|
|
|
|
|
|
RetrieveRevisionsCounter(uint32_t aId, Promise* aPromise, uint32_t aCount)
|
|
|
|
: mPromise(aPromise)
|
|
|
|
, mId(aId)
|
|
|
|
, mCount(aCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
AppendDataStore(JSContext* aCx, DataStore* aDataStore,
|
|
|
|
nsIDataStore* aDataStoreIf)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mResults.AppendElement(aDataStore);
|
|
|
|
|
|
|
|
// DataStore will run this callback when the revisionID is retrieved.
|
|
|
|
JSFunction* func = js::NewFunctionWithReserved(aCx, JSCallback,
|
|
|
|
0 /* nargs */, 0 /* flags */,
|
2015-03-09 09:50:07 -07:00
|
|
|
nullptr);
|
2014-06-03 07:36:46 -07:00
|
|
|
if (!func) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
|
|
|
|
if (!obj) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use the ID to know which counter is this. The service keeps all of
|
|
|
|
// these counters alive with their own IDs in an hashtable.
|
|
|
|
js::SetFunctionNativeReserved(obj, 0, JS::Int32Value(mId));
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*obj));
|
|
|
|
nsresult rv = aDataStoreIf->RetrieveRevisionId(value);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool
|
|
|
|
JSCallback(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx,
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(), 0));
|
|
|
|
uint32_t id = value.toInt32();
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = DataStoreService::Get();
|
2014-06-03 07:36:46 -07:00
|
|
|
MOZ_ASSERT(service);
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<RetrieveRevisionsCounter> counter = service->GetCounter(id);
|
2014-06-03 07:36:46 -07:00
|
|
|
MOZ_ASSERT(counter);
|
|
|
|
|
|
|
|
// When all the callbacks are called, we can resolve the promise and remove
|
|
|
|
// the counter from the service.
|
|
|
|
--counter->mCount;
|
|
|
|
if (!counter->mCount) {
|
|
|
|
service->RemoveCounter(id);
|
|
|
|
counter->mPromise->MaybeResolve(counter->mResults);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
nsTArray<RefPtr<DataStore>> mResults;
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
uint32_t mId;
|
|
|
|
uint32_t mCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<DataStoreService>
|
|
|
|
DataStoreService::GetOrCreate()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!gDataStoreService) {
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = new DataStoreService();
|
2014-06-03 07:36:46 -07:00
|
|
|
if (NS_WARN_IF(NS_FAILED(service->Init()))) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gDataStoreService = service;
|
|
|
|
}
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = gDataStoreService.get();
|
2014-06-03 07:36:46 -07:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<DataStoreService>
|
|
|
|
DataStoreService::Get()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreService> service = gDataStoreService.get();
|
2014-06-03 07:36:46 -07:00
|
|
|
return service.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
DataStoreService::Shutdown()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (gDataStoreService) {
|
2015-07-03 18:29:00 -07:00
|
|
|
if (XRE_IsParentProcess()) {
|
2014-06-03 07:36:46 -07:00
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (obs) {
|
|
|
|
obs->RemoveObserver(gDataStoreService, "webapps-clear-data");
|
|
|
|
}
|
2015-09-17 22:49:24 -07:00
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
Preferences::UnregisterCallback(HomeScreenPrefCallback,
|
|
|
|
"dom.mozApps.homescreenURL",
|
|
|
|
nullptr);
|
|
|
|
NS_WARN_IF(NS_FAILED(rv));
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
gDataStoreService = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(DataStoreService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDataStoreService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDataStoreService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(DataStoreService)
|
|
|
|
NS_IMPL_RELEASE(DataStoreService)
|
|
|
|
|
|
|
|
DataStoreService::DataStoreService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
DataStoreService::~DataStoreService()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataStoreService::Init()
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
if (!XRE_IsParentProcess()) {
|
2014-06-03 07:36:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
|
|
|
|
if (!obs) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = obs->AddObserver(this, "webapps-clear-data", false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-09-17 22:49:24 -07:00
|
|
|
rv = Preferences::RegisterCallback(HomeScreenPrefCallback,
|
|
|
|
"dom.mozApps.homescreenURL",
|
|
|
|
nullptr);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataStoreService::InstallDataStore(uint32_t aAppId,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
|
|
|
ASSERT_PARENT_PROCESS()
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
if (!mStores.Get(aName, &apps)) {
|
|
|
|
apps = new HashApp();
|
|
|
|
mStores.Put(aName, apps);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataStoreInfo* info = nullptr;
|
|
|
|
if (!apps->Get(aAppId, &info)) {
|
|
|
|
info = new DataStoreInfo(aName, aOriginURL, aManifestURL, aReadOnly, false);
|
|
|
|
apps->Put(aAppId, info);
|
|
|
|
} else {
|
|
|
|
info->Update(aName, aOriginURL, aManifestURL, aReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = AddPermissions(aAppId, aName, aOriginURL, aManifestURL,
|
|
|
|
aReadOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Immediately create the first revision.
|
|
|
|
return CreateFirstRevisionId(aAppId, aName, aManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataStoreService::InstallAccessDataStore(uint32_t aAppId,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
|
|
|
ASSERT_PARENT_PROCESS()
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
if (!mAccessStores.Get(aName, &apps)) {
|
|
|
|
apps = new HashApp();
|
|
|
|
mAccessStores.Put(aName, apps);
|
|
|
|
}
|
|
|
|
|
|
|
|
DataStoreInfo* info = nullptr;
|
|
|
|
if (!apps->Get(aAppId, &info)) {
|
|
|
|
info = new DataStoreInfo(aName, aOriginURL, aManifestURL, aReadOnly, false);
|
|
|
|
apps->Put(aAppId, info);
|
|
|
|
} else {
|
|
|
|
info->Update(aName, aOriginURL, aManifestURL, aReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
return AddAccessPermissions(aAppId, aName, aOriginURL, aManifestURL,
|
|
|
|
aReadOnly);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-30 09:05:36 -08:00
|
|
|
DataStoreService::GetDataStores(mozIDOMWindow* aWindow,
|
2014-06-03 07:36:46 -07:00
|
|
|
const nsAString& aName,
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsAString& aOwner,
|
2014-06-03 07:36:46 -07:00
|
|
|
nsISupports** aDataStores)
|
|
|
|
{
|
|
|
|
// FIXME This will be a thread-safe method.
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2016-01-30 09:05:36 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = nsPIDOMWindowInner::From(aWindow);
|
2014-06-03 07:36:46 -07:00
|
|
|
if (!window) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(window);
|
2014-07-18 18:31:11 -07:00
|
|
|
ErrorResult rv;
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, rv);
|
2014-07-18 18:31:11 -07:00
|
|
|
if (rv.Failed()) {
|
2015-04-27 06:18:51 -07:00
|
|
|
return rv.StealNSResult();
|
2014-07-18 18:31:11 -07:00
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = window->GetDoc();
|
|
|
|
MOZ_ASSERT(document);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal = document->NodePrincipal();
|
|
|
|
MOZ_ASSERT(principal);
|
|
|
|
|
|
|
|
nsTArray<DataStoreInfo> stores;
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// If this request comes from the main process, we have access to the
|
|
|
|
// window, so we can skip the ipc communication.
|
2015-07-03 18:29:00 -07:00
|
|
|
if (XRE_IsParentProcess()) {
|
2014-06-03 07:36:46 -07:00
|
|
|
uint32_t appId;
|
2015-10-01 23:37:25 -07:00
|
|
|
rv = principal->GetAppId(&appId);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
RejectPromise(window, promise, rv.StealNSResult());
|
2014-06-03 07:36:46 -07:00
|
|
|
promise.forget(aDataStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-07-16 20:53:23 -07:00
|
|
|
rv = GetDataStoreInfos(aName, aOwner, appId, principal, stores);
|
2015-10-01 23:37:25 -07:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
RejectPromise(window, promise, rv.StealNSResult());
|
2014-06-03 07:36:46 -07:00
|
|
|
promise.forget(aDataStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-10-01 23:37:25 -07:00
|
|
|
} else {
|
2014-06-03 07:36:46 -07:00
|
|
|
// This method can be called in the child so we need to send a request
|
|
|
|
// to the parent and create DataStore object here.
|
2014-06-03 07:36:46 -07:00
|
|
|
ContentChild* contentChild = ContentChild::GetSingleton();
|
|
|
|
|
|
|
|
nsTArray<DataStoreSetting> array;
|
|
|
|
if (!contentChild->SendDataStoreGetStores(nsAutoString(aName),
|
2014-07-16 20:53:23 -07:00
|
|
|
nsAutoString(aOwner),
|
2014-06-03 07:36:46 -07:00
|
|
|
IPC::Principal(principal),
|
|
|
|
&array)) {
|
|
|
|
RejectPromise(window, promise, NS_ERROR_FAILURE);
|
|
|
|
promise.forget(aDataStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < array.Length(); ++i) {
|
|
|
|
DataStoreInfo* info = stores.AppendElement();
|
|
|
|
info->Init(array[i].name(), array[i].originURL(),
|
|
|
|
array[i].manifestURL(), array[i].readOnly(),
|
|
|
|
array[i].enabled());
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
GetDataStoresCreate(window, promise, stores);
|
2014-06-03 07:36:46 -07:00
|
|
|
promise.forget(aDataStores);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-30 09:05:36 -08:00
|
|
|
DataStoreService::GetDataStoresCreate(nsPIDOMWindowInner* aWindow,
|
|
|
|
Promise* aPromise,
|
2014-06-03 07:36:46 -07:00
|
|
|
const nsTArray<DataStoreInfo>& aStores)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!aStores.Length()) {
|
|
|
|
GetDataStoresResolve(aWindow, aPromise, aStores);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsString> pendingDataStores;
|
|
|
|
for (uint32_t i = 0; i < aStores.Length(); ++i) {
|
|
|
|
if (!aStores[i].mEnabled) {
|
|
|
|
pendingDataStores.AppendElement(aStores[i].mManifestURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pendingDataStores.Length()) {
|
|
|
|
GetDataStoresResolve(aWindow, aPromise, aStores);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PendingRequests* requests;
|
|
|
|
if (!mPendingRequests.Get(aStores[0].mName, &requests)) {
|
|
|
|
requests = new PendingRequests();
|
|
|
|
mPendingRequests.Put(aStores[0].mName, requests);
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
PendingRequest* request = requests->AppendElement();
|
|
|
|
request->Init(aWindow, aPromise, aStores, pendingDataStores);
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-01-30 09:05:36 -08:00
|
|
|
DataStoreService::GetDataStoresResolve(nsPIDOMWindowInner* aWindow,
|
2014-06-03 07:36:46 -07:00
|
|
|
Promise* aPromise,
|
|
|
|
const nsTArray<DataStoreInfo>& aStores)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (!aStores.Length()) {
|
2015-10-17 22:24:48 -07:00
|
|
|
nsTArray<RefPtr<DataStore>> results;
|
2014-06-03 07:36:46 -07:00
|
|
|
aPromise->MaybeResolve(results);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
|
|
|
|
// The counter will finish this task once all the DataStores will know their
|
|
|
|
// first revision Ids.
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<RetrieveRevisionsCounter> counter =
|
2014-06-03 07:36:46 -07:00
|
|
|
new RetrieveRevisionsCounter(++gCounterID, aPromise, aStores.Length());
|
|
|
|
mPendingCounters.Put(gCounterID, counter);
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aStores.Length(); ++i) {
|
|
|
|
nsCOMPtr<nsIDataStore> dataStore =
|
|
|
|
do_CreateInstance("@mozilla.org/dom/datastore;1");
|
|
|
|
if (NS_WARN_IF(!dataStore)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = dataStore->Init(aWindow, aStores[i].mName,
|
|
|
|
aStores[i].mManifestURL,
|
|
|
|
aStores[i].mReadOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedJS> xpcwrappedjs = do_QueryInterface(dataStore);
|
|
|
|
if (NS_WARN_IF(!xpcwrappedjs)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> dataStoreJS(cx, xpcwrappedjs->GetJSObject());
|
|
|
|
if (NS_WARN_IF(!dataStoreJS)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-08 08:57:19 -08:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aWindow);
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
JSAutoCompartment ac(cx, dataStoreJS);
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreImpl> dataStoreObj = new DataStoreImpl(dataStoreJS,
|
2015-01-08 08:57:19 -08:00
|
|
|
global);
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStore> exposedStore = new DataStore(aWindow);
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
exposedStore->SetDataStoreImpl(*dataStoreObj, error);
|
|
|
|
if (error.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-23 09:18:03 -07:00
|
|
|
JS::Rooted<JS::Value> exposedObject(cx);
|
|
|
|
if (!GetOrCreateDOMReflector(cx, exposedStore, &exposedObject)) {
|
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
return;
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
dataStore->SetExposedObject(exposedObject);
|
|
|
|
|
|
|
|
counter->AppendDataStore(cx, exposedStore, dataStore);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:37:25 -07:00
|
|
|
// This method populates 'aStores' with the list of DataStores with 'aName' as
|
2014-06-03 07:36:46 -07:00
|
|
|
// name and available for this 'aAppId'.
|
|
|
|
nsresult
|
|
|
|
DataStoreService::GetDataStoreInfos(const nsAString& aName,
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsAString& aOwner,
|
2014-06-03 07:36:46 -07:00
|
|
|
uint32_t aAppId,
|
2014-06-19 07:03:54 -07:00
|
|
|
nsIPrincipal* aPrincipal,
|
2014-06-03 07:36:46 -07:00
|
|
|
nsTArray<DataStoreInfo>& aStores)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1");
|
|
|
|
if (NS_WARN_IF(!appsService)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIApplication> app;
|
|
|
|
nsresult rv = appsService->GetAppByLocalId(aAppId, getter_AddRefs(app));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
2014-06-19 07:03:54 -07:00
|
|
|
if (!DataStoreService::CheckPermission(aPrincipal)) {
|
2014-06-03 07:36:46 -07:00
|
|
|
return NS_ERROR_DOM_SECURITY_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
aStores.Clear();
|
|
|
|
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
if (!mStores.Get(aName, &apps)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:37:25 -07:00
|
|
|
DataStoreInfo* appsInfo = nullptr;
|
|
|
|
if (apps->Get(aAppId, &appsInfo) &&
|
|
|
|
(aOwner.IsEmpty() || aOwner.Equals(appsInfo->mManifestURL))) {
|
2014-06-03 07:36:46 -07:00
|
|
|
DataStoreInfo* owned = aStores.AppendElement();
|
2015-10-01 23:37:25 -07:00
|
|
|
owned->Init(appsInfo->mName, appsInfo->mOriginURL, appsInfo->mManifestURL,
|
|
|
|
false, appsInfo->mEnabled);
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:59:52 -07:00
|
|
|
for (auto iter = apps->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
if (iter.Key() == aAppId) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:37:25 -07:00
|
|
|
DataStoreInfo* appInfo = iter.UserData();
|
|
|
|
MOZ_ASSERT(appInfo);
|
2015-09-30 03:59:52 -07:00
|
|
|
|
2015-10-01 23:37:25 -07:00
|
|
|
HashApp* accessApp;
|
|
|
|
if (!mAccessStores.Get(aName, &accessApp)) {
|
2015-09-30 03:59:52 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aOwner.IsEmpty() &&
|
2015-10-01 23:37:25 -07:00
|
|
|
!aOwner.Equals(appInfo->mManifestURL)) {
|
2015-09-30 03:59:52 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
DataStoreInfo* accessInfo = nullptr;
|
2015-10-01 23:37:25 -07:00
|
|
|
if (!accessApp->Get(aAppId, &accessInfo)) {
|
2015-09-30 03:59:52 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-01 23:37:25 -07:00
|
|
|
bool readOnly = appInfo->mReadOnly || accessInfo->mReadOnly;
|
2015-09-30 03:59:52 -07:00
|
|
|
DataStoreInfo* accessStore = aStores.AppendElement();
|
2015-10-01 23:37:25 -07:00
|
|
|
accessStore->Init(aName, appInfo->mOriginURL,
|
|
|
|
appInfo->mManifestURL, readOnly,
|
|
|
|
appInfo->mEnabled);
|
2015-09-30 03:59:52 -07:00
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-27 00:58:47 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
DataStoreService::GetAppManifestURLsForDataStore(const nsAString& aName,
|
|
|
|
nsIArray** aManifestURLs)
|
|
|
|
{
|
|
|
|
ASSERT_PARENT_PROCESS()
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
nsCOMPtr<nsIMutableArray> manifestURLs = do_CreateInstance(NS_ARRAY_CONTRACTID);
|
|
|
|
if (!manifestURLs) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
if (mStores.Get(aName, &apps)) {
|
2015-09-30 03:59:52 -07:00
|
|
|
for (auto iter = apps->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsCOMPtr<nsISupportsString> manifestURL(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
|
|
|
|
if (manifestURL) {
|
|
|
|
manifestURL->SetData(iter.UserData()->mManifestURL);
|
|
|
|
manifestURLs->AppendElement(manifestURL, false);
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 00:58:47 -07:00
|
|
|
}
|
|
|
|
if (mAccessStores.Get(aName, &apps)) {
|
2015-09-30 03:59:52 -07:00
|
|
|
for (auto iter = apps->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsCOMPtr<nsISupportsString> manifestURL(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
|
|
|
|
if (manifestURL) {
|
|
|
|
manifestURL->SetData(iter.UserData()->mManifestURL);
|
|
|
|
manifestURLs->AppendElement(manifestURL, false);
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 00:58:47 -07:00
|
|
|
}
|
|
|
|
|
2015-03-31 07:03:49 -07:00
|
|
|
manifestURLs.forget(aManifestURLs);
|
2014-06-27 00:58:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-19 07:03:54 -07:00
|
|
|
bool
|
|
|
|
DataStoreService::CheckPermission(nsIPrincipal* aPrincipal)
|
|
|
|
{
|
|
|
|
// First of all, the general pref has to be turned on.
|
|
|
|
bool enabled = false;
|
|
|
|
Preferences::GetBool("dom.datastore.enabled", &enabled);
|
|
|
|
if (!enabled) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just for testing, we can enable DataStore for any kind of app.
|
|
|
|
if (Preferences::GetBool("dom.testing.datastore_enabled_for_hosted_apps", false)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aPrincipal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t status;
|
|
|
|
if (NS_FAILED(aPrincipal->GetAppStatus(&status))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-07 15:19:14 -07:00
|
|
|
// Certified apps are always allowed.
|
|
|
|
if (status == nsIPrincipal::APP_STATUS_CERTIFIED) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status != nsIPrincipal::APP_STATUS_PRIVILEGED) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Privileged apps are allowed if they are the homescreen.
|
|
|
|
nsAdoptingString homescreen =
|
|
|
|
Preferences::GetString("dom.mozApps.homescreenURL");
|
|
|
|
if (!homescreen) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t appId;
|
|
|
|
nsresult rv = aPrincipal->GetAppId(&appId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1");
|
|
|
|
if (NS_WARN_IF(!appsService)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString manifestURL;
|
|
|
|
rv = appsService->GetManifestURLByLocalId(appId, manifestURL);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return manifestURL.Equals(homescreen);
|
2014-06-19 07:03:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataStoreService::CheckPermission(nsIPrincipal* aPrincipal,
|
|
|
|
bool* aResult)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
*aResult = DataStoreService::CheckPermission(aPrincipal);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// This method is called when an app with DataStores is deleted.
|
|
|
|
void
|
|
|
|
DataStoreService::DeleteDataStores(uint32_t aAppId)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2016-01-27 16:04:59 -08:00
|
|
|
DeleteDataStoresHelper(mStores, aAppId);
|
|
|
|
DeleteDataStoresHelper(mAccessStores, aAppId);
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
DataStoreService::Observe(nsISupports* aSubject,
|
|
|
|
const char* aTopic,
|
|
|
|
const char16_t* aData)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
if (strcmp(aTopic, "webapps-clear-data")) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIApplicationClearPrivateDataParams> params =
|
|
|
|
do_QueryInterface(aSubject);
|
|
|
|
MOZ_ASSERT(params);
|
|
|
|
|
|
|
|
// DataStore is explosed to apps, not browser content.
|
|
|
|
bool browserOnly;
|
|
|
|
nsresult rv = params->GetBrowserOnly(&browserOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (browserOnly) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t appId;
|
|
|
|
rv = params->GetAppId(&appId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
DeleteDataStores(appId);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataStoreService::AddPermissions(uint32_t aAppId,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
// This is the permission name.
|
|
|
|
nsString permission;
|
|
|
|
GeneratePermissionName(permission, aName, aManifestURL);
|
|
|
|
|
|
|
|
// When a new DataStore is installed, the permissions must be set for the
|
|
|
|
// owner app.
|
|
|
|
nsresult rv = ResetPermission(aAppId, aOriginURL, aManifestURL, permission,
|
|
|
|
aReadOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For any app that wants to have access to this DataStore we add the
|
|
|
|
// permissions.
|
|
|
|
HashApp* apps;
|
|
|
|
if (!mAccessStores.Get(aName, &apps)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:59:52 -07:00
|
|
|
for (auto iter = apps->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
DataStoreInfo* info = iter.UserData();
|
|
|
|
MOZ_ASSERT(info);
|
|
|
|
|
|
|
|
bool readOnly = aReadOnly || info->mReadOnly;
|
|
|
|
|
|
|
|
rv = ResetPermission(iter.Key(), info->mOriginURL,
|
|
|
|
info->mManifestURL,
|
|
|
|
permission, readOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataStoreService::AddAccessPermissions(uint32_t aAppId, const nsAString& aName,
|
|
|
|
const nsAString& aOriginURL,
|
|
|
|
const nsAString& aManifestURL,
|
|
|
|
bool aReadOnly)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
// When an app wants to have access to a DataStore, the permissions must be
|
|
|
|
// set.
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
if (!mStores.Get(aName, &apps)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-30 03:59:52 -07:00
|
|
|
for (auto iter = apps->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
DataStoreInfo* info = iter.UserData();
|
|
|
|
MOZ_ASSERT(info);
|
|
|
|
|
|
|
|
nsAutoString permission;
|
|
|
|
GeneratePermissionName(permission, aName, info->mManifestURL);
|
|
|
|
|
|
|
|
// ReadOnly is decided by the owner first.
|
|
|
|
bool readOnly = info->mReadOnly || aReadOnly;
|
|
|
|
|
|
|
|
nsresult rv = ResetPermission(aAppId, aOriginURL,
|
|
|
|
info->mManifestURL,
|
|
|
|
permission, readOnly);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2014-06-03 07:36:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This method starts the operation to create the first revision for a DataStore
|
|
|
|
// if needed.
|
|
|
|
nsresult
|
|
|
|
DataStoreService::CreateFirstRevisionId(uint32_t aAppId,
|
|
|
|
const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<DataStoreDB> db = new DataStoreDB(aManifestURL, aName);
|
2014-06-03 07:36:46 -07:00
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<FirstRevisionIdCallback> callback =
|
2014-06-03 07:36:46 -07:00
|
|
|
new FirstRevisionIdCallback(aAppId, aName, aManifestURL);
|
|
|
|
|
|
|
|
Sequence<nsString> dbs;
|
2015-05-28 11:07:44 -07:00
|
|
|
if (!dbs.AppendElement(NS_LITERAL_STRING(DATASTOREDB_REVISION), fallible)) {
|
2015-05-25 04:50:15 -07:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
return db->Open(IDBTransactionMode::Readwrite, dbs, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
DataStoreService::EnableDataStore(uint32_t aAppId, const nsAString& aName,
|
|
|
|
const nsAString& aManifestURL)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
{
|
|
|
|
HashApp* apps = nullptr;
|
|
|
|
DataStoreInfo* info = nullptr;
|
|
|
|
if (mStores.Get(aName, &apps) && apps->Get(aAppId, &info)) {
|
|
|
|
info->Enable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// Notify the child processes.
|
2015-07-03 18:29:00 -07:00
|
|
|
if (XRE_IsParentProcess()) {
|
2014-06-03 07:36:46 -07:00
|
|
|
nsTArray<ContentParent*> children;
|
|
|
|
ContentParent::GetAll(children);
|
|
|
|
for (uint32_t i = 0; i < children.Length(); i++) {
|
|
|
|
if (children[i]->NeedsDataStoreInfos()) {
|
2015-11-01 21:53:26 -08:00
|
|
|
Unused << children[i]->SendDataStoreNotify(aAppId, nsAutoString(aName),
|
2014-06-03 07:36:46 -07:00
|
|
|
nsAutoString(aManifestURL));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
// Maybe we have some pending request waiting for this DataStore.
|
|
|
|
PendingRequests* requests;
|
|
|
|
if (!mPendingRequests.Get(aName, &requests)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < requests->Length();) {
|
|
|
|
PendingRequest& request = requests->ElementAt(i);
|
|
|
|
nsTArray<nsString>::index_type pos =
|
|
|
|
request.mPendingDataStores.IndexOf(aManifestURL);
|
|
|
|
if (pos != request.mPendingDataStores.NoIndex) {
|
|
|
|
request.mPendingDataStores.RemoveElementAt(pos);
|
|
|
|
|
|
|
|
// No other pending dataStores.
|
|
|
|
if (request.mPendingDataStores.IsEmpty()) {
|
|
|
|
GetDataStoresResolve(request.mWindow, request.mPromise,
|
|
|
|
request.mStores);
|
|
|
|
requests->RemoveElementAt(i);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// No other pending requests for this name.
|
|
|
|
if (requests->IsEmpty()) {
|
|
|
|
mPendingRequests.Remove(aName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<RetrieveRevisionsCounter>
|
|
|
|
DataStoreService::GetCounter(uint32_t aId) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-17 22:24:48 -07:00
|
|
|
RefPtr<RetrieveRevisionsCounter> counter;
|
2014-06-03 07:36:46 -07:00
|
|
|
return mPendingCounters.Get(aId, getter_AddRefs(counter))
|
|
|
|
? counter.forget() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataStoreService::RemoveCounter(uint32_t aId)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mPendingCounters.Remove(aId);
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
nsresult
|
|
|
|
DataStoreService::GetDataStoresFromIPC(const nsAString& aName,
|
2014-07-16 20:53:23 -07:00
|
|
|
const nsAString& aOwner,
|
2014-06-03 07:36:46 -07:00
|
|
|
nsIPrincipal* aPrincipal,
|
|
|
|
nsTArray<DataStoreSetting>* aValue)
|
|
|
|
{
|
2015-07-03 18:29:00 -07:00
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
2014-06-03 07:36:46 -07:00
|
|
|
|
|
|
|
uint32_t appId;
|
|
|
|
nsresult rv = aPrincipal->GetAppId(&appId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<DataStoreInfo> stores;
|
2014-07-16 20:53:23 -07:00
|
|
|
rv = GetDataStoreInfos(aName, aOwner, appId, aPrincipal, stores);
|
2014-06-03 07:36:46 -07:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < stores.Length(); ++i) {
|
|
|
|
DataStoreSetting* data = aValue->AppendElement();
|
|
|
|
data->name() = stores[i].mName;
|
|
|
|
data->originURL() = stores[i].mOriginURL;
|
|
|
|
data->manifestURL() = stores[i].mManifestURL;
|
|
|
|
data->readOnly() = stores[i].mReadOnly;
|
|
|
|
data->enabled() = stores[i].mEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
nsresult
|
|
|
|
DataStoreService::GenerateUUID(nsAString& aID)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (!mUUIDGenerator) {
|
|
|
|
mUUIDGenerator = do_GetService("@mozilla.org/uuid-generator;1", &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsID id;
|
|
|
|
rv = mUUIDGenerator->GenerateUUIDInPlace(&id);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
char chars[NSID_LENGTH];
|
|
|
|
id.ToProvidedString(chars);
|
|
|
|
CopyASCIItoUTF16(chars, aID);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-17 22:49:24 -07:00
|
|
|
void
|
|
|
|
DataStoreService::HomeScreenPrefChanged()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(XRE_IsParentProcess() && NS_IsMainThread());
|
|
|
|
|
|
|
|
nsAdoptingString homescreen =
|
|
|
|
Preferences::GetString("dom.mozApps.homescreenURL");
|
|
|
|
if (homescreen == gHomeScreenManifestURL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove datastores of the old homescreen.
|
|
|
|
if (!gHomeScreenManifestURL.IsEmpty()) {
|
|
|
|
DeleteDataStoresIfNotAllowed(gHomeScreenManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
gHomeScreenManifestURL = homescreen;
|
|
|
|
if (gHomeScreenManifestURL.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add datastores for the new homescreen.
|
|
|
|
AddDataStoresIfAllowed(gHomeScreenManifestURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataStoreService::DeleteDataStoresIfNotAllowed(const nsAString& aManifestURL)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1");
|
|
|
|
if (NS_WARN_IF(!appsService)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIApplication> app;
|
|
|
|
nsresult rv = appsService->GetAppByManifestURL(aManifestURL,
|
|
|
|
getter_AddRefs(app));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !app) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t localId;
|
|
|
|
rv = app->GetLocalId(&localId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
rv = app->GetPrincipal(getter_AddRefs(principal));
|
|
|
|
|
|
|
|
// We delete all the dataStores for this app here.
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !principal ||
|
|
|
|
!CheckPermission(principal)) {
|
|
|
|
DeleteDataStores(localId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DataStoreService::AddDataStoresIfAllowed(const nsAString& aManifestURL)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAppsService> appsService =
|
|
|
|
do_GetService("@mozilla.org/AppsService;1");
|
|
|
|
if (NS_WARN_IF(!appsService)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<mozIApplication> app;
|
|
|
|
nsresult rv = appsService->GetAppByManifestURL(aManifestURL,
|
|
|
|
getter_AddRefs(app));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv)) || !app) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t localId;
|
|
|
|
rv = app->GetLocalId(&localId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = appsService->UpdateDataStoreEntriesFromLocalId(localId);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-03 07:36:46 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|