2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2007-06-27 00:13:01 +00:00
|
|
|
/* vim: set cindent tabstop=4 expandtab shiftwidth=4: */
|
2012-05-21 11:12:37 +00: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/. */
|
1999-04-06 20:37:24 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Implementation for the local store
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2002-02-01 00:24:27 +00:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsIOutputStream.h"
|
1999-04-06 20:37:24 +00:00
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsILocalStore.h"
|
|
|
|
#include "nsIRDFDataSource.h"
|
1999-06-24 00:22:58 +00:00
|
|
|
#include "nsIRDFRemoteDataSource.h"
|
1999-04-06 20:37:24 +00:00
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "plstr.h"
|
|
|
|
#include "rdf.h"
|
1999-10-05 04:12:58 +00:00
|
|
|
#include "nsCOMPtr.h"
|
2000-10-31 07:27:49 +00:00
|
|
|
#include "nsWeakPtr.h"
|
2000-08-22 00:14:24 +00:00
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2001-10-19 20:52:59 +00:00
|
|
|
#include "nsIObserver.h"
|
2001-03-11 22:12:21 +00:00
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsWeakReference.h"
|
2006-11-19 08:28:02 +00:00
|
|
|
#include "nsCRTGlue.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2006-08-08 18:18:50 +00:00
|
|
|
#include "nsEnumeratorUtils.h"
|
2008-03-05 20:07:55 +00:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
1999-07-30 23:42:41 +00:00
|
|
|
|
1999-04-06 20:37:24 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class LocalStoreImpl : public nsILocalStore,
|
1999-07-14 21:12:46 +00:00
|
|
|
public nsIRDFDataSource,
|
2001-03-11 22:12:21 +00:00
|
|
|
public nsIRDFRemoteDataSource,
|
|
|
|
public nsIObserver,
|
|
|
|
public nsSupportsWeakReference
|
1999-04-06 20:37:24 +00:00
|
|
|
{
|
2000-10-31 07:27:49 +00:00
|
|
|
protected:
|
1999-06-24 00:22:58 +00:00
|
|
|
nsCOMPtr<nsIRDFDataSource> mInner;
|
1999-04-06 20:37:24 +00:00
|
|
|
|
|
|
|
LocalStoreImpl();
|
|
|
|
virtual ~LocalStoreImpl();
|
1999-06-24 00:22:58 +00:00
|
|
|
nsresult Init();
|
2007-04-26 17:49:05 +00:00
|
|
|
nsresult CreateLocalStore(nsIFile* aFile);
|
2001-08-29 14:02:38 +00:00
|
|
|
nsresult LoadData();
|
1999-06-24 00:22:58 +00:00
|
|
|
|
2010-06-10 18:11:40 +00:00
|
|
|
friend nsresult
|
2001-02-22 03:01:34 +00:00
|
|
|
NS_NewLocalStore(nsISupports* aOuter, REFNSIID aIID, void** aResult);
|
1999-04-06 20:37:24 +00:00
|
|
|
|
2005-12-01 15:03:48 +00:00
|
|
|
nsCOMPtr<nsIRDFService> mRDFService;
|
2000-10-31 07:27:49 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
public:
|
1999-04-06 20:37:24 +00:00
|
|
|
// nsISupports interface
|
2008-03-05 20:07:55 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(LocalStoreImpl, nsILocalStore)
|
1999-04-06 20:37:24 +00:00
|
|
|
|
|
|
|
// nsILocalStore interface
|
|
|
|
|
|
|
|
// nsIRDFDataSource interface. Most of these are just delegated to
|
|
|
|
// the inner, in-memory datasource.
|
|
|
|
NS_IMETHOD GetURI(char* *aURI);
|
|
|
|
|
|
|
|
NS_IMETHOD GetSource(nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue,
|
1999-04-06 20:37:24 +00:00
|
|
|
nsIRDFResource** aSource) {
|
|
|
|
return mInner->GetSource(aProperty, aTarget, aTruthValue, aSource);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetSources(nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue,
|
1999-04-24 02:41:02 +00:00
|
|
|
nsISimpleEnumerator** aSources) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->GetSources(aProperty, aTarget, aTruthValue, aSources);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetTarget(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue,
|
1999-04-06 20:37:24 +00:00
|
|
|
nsIRDFNode** aTarget) {
|
|
|
|
return mInner->GetTarget(aSource, aProperty, aTruthValue, aTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD GetTargets(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue,
|
1999-04-24 02:41:02 +00:00
|
|
|
nsISimpleEnumerator** aTargets) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->GetTargets(aSource, aProperty, aTruthValue, aTargets);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Assert(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->Assert(aSource, aProperty, aTarget, aTruthValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Unassert(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget) {
|
|
|
|
return mInner->Unassert(aSource, aProperty, aTarget);
|
|
|
|
}
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
NS_IMETHOD Change(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aOldTarget,
|
|
|
|
nsIRDFNode* aNewTarget) {
|
|
|
|
return mInner->Change(aSource, aProperty, aOldTarget, aNewTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Move(nsIRDFResource* aOldSource,
|
|
|
|
nsIRDFResource* aNewSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget) {
|
|
|
|
return mInner->Move(aOldSource, aNewSource, aProperty, aTarget);
|
|
|
|
}
|
|
|
|
|
1999-04-06 20:37:24 +00:00
|
|
|
NS_IMETHOD HasAssertion(nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aTruthValue,
|
|
|
|
bool* hasAssertion) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->HasAssertion(aSource, aProperty, aTarget, aTruthValue, hasAssertion);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD AddObserver(nsIRDFObserver* aObserver) {
|
2007-04-10 22:05:41 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD RemoveObserver(nsIRDFObserver* aObserver) {
|
2007-04-10 22:05:41 +00:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *_retval) {
|
2000-07-19 03:58:25 +00:00
|
|
|
return mInner->HasArcIn(aNode, aArc, _retval);
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *_retval) {
|
2000-07-19 03:58:25 +00:00
|
|
|
return mInner->HasArcOut(aSource, aArc, _retval);
|
|
|
|
}
|
|
|
|
|
1999-04-06 20:37:24 +00:00
|
|
|
NS_IMETHOD ArcLabelsIn(nsIRDFNode* aNode,
|
1999-04-24 02:41:02 +00:00
|
|
|
nsISimpleEnumerator** aLabels) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->ArcLabelsIn(aNode, aLabels);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD ArcLabelsOut(nsIRDFResource* aSource,
|
1999-04-24 02:41:02 +00:00
|
|
|
nsISimpleEnumerator** aLabels) {
|
1999-04-06 20:37:24 +00:00
|
|
|
return mInner->ArcLabelsOut(aSource, aLabels);
|
|
|
|
}
|
|
|
|
|
1999-04-24 02:41:02 +00:00
|
|
|
NS_IMETHOD GetAllResources(nsISimpleEnumerator** aResult) {
|
|
|
|
return mInner->GetAllResources(aResult);
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
1999-06-26 01:09:02 +00:00
|
|
|
NS_IMETHOD GetAllCmds(nsIRDFResource* aSource,
|
|
|
|
nsISimpleEnumerator/*<nsIRDFResource>*/** aCommands);
|
|
|
|
|
1999-04-06 20:37:24 +00:00
|
|
|
NS_IMETHOD IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
|
|
|
|
nsIRDFResource* aCommand,
|
|
|
|
nsISupportsArray/*<nsIRDFResource>*/* aArguments,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aResult);
|
1999-04-06 20:37:24 +00:00
|
|
|
|
|
|
|
NS_IMETHOD DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources,
|
|
|
|
nsIRDFResource* aCommand,
|
|
|
|
nsISupportsArray/*<nsIRDFResource>*/* aArguments);
|
|
|
|
|
2003-05-23 12:03:40 +00:00
|
|
|
NS_IMETHOD BeginUpdateBatch() {
|
|
|
|
return mInner->BeginUpdateBatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD EndUpdateBatch() {
|
|
|
|
return mInner->EndUpdateBatch();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD GetLoaded(bool* _result);
|
2008-03-05 20:07:55 +00:00
|
|
|
NS_IMETHOD Init(const char *uri);
|
|
|
|
NS_IMETHOD Flush();
|
|
|
|
NS_IMETHOD FlushTo(const char *aURI);
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHOD Refresh(bool sync);
|
2008-03-05 20:07:55 +00:00
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
NS_DECL_NSIOBSERVER
|
1999-04-06 20:37:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
LocalStoreImpl::LocalStoreImpl(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalStoreImpl::~LocalStoreImpl(void)
|
|
|
|
{
|
2005-12-01 15:03:48 +00:00
|
|
|
if (mRDFService)
|
|
|
|
mRDFService->UnregisterDataSource(this);
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-10 18:11:40 +00:00
|
|
|
nsresult
|
2001-02-22 03:01:34 +00:00
|
|
|
NS_NewLocalStore(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
1999-04-06 20:37:24 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aOuter == nullptr, "no aggregation");
|
2001-02-22 03:01:34 +00:00
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aResult != nullptr, "null ptr");
|
1999-04-06 20:37:24 +00:00
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
LocalStoreImpl* impl = new LocalStoreImpl();
|
|
|
|
if (! impl)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2001-02-22 03:01:34 +00:00
|
|
|
NS_ADDREF(impl);
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
nsresult rv;
|
|
|
|
rv = impl->Init();
|
2001-02-22 03:01:34 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-05-25 15:58:09 +00:00
|
|
|
// Set up the result pointer
|
|
|
|
rv = impl->QueryInterface(aIID, aResult);
|
1999-07-30 23:42:41 +00:00
|
|
|
}
|
|
|
|
|
2001-02-22 03:01:34 +00:00
|
|
|
NS_RELEASE(impl);
|
|
|
|
return rv;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(LocalStoreImpl, mInner)
|
2011-03-06 11:11:31 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(LocalStoreImpl)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(LocalStoreImpl)
|
1999-04-06 20:37:24 +00:00
|
|
|
|
2008-03-05 20:07:55 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LocalStoreImpl)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsILocalStore)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRDFDataSource)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRDFRemoteDataSource)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsILocalStore)
|
|
|
|
NS_INTERFACE_MAP_END
|
1999-04-06 20:37:24 +00:00
|
|
|
|
|
|
|
// nsILocalStore interface
|
|
|
|
|
|
|
|
// nsIRDFDataSource interface
|
|
|
|
|
2000-01-27 02:24:20 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
LocalStoreImpl::GetLoaded(bool* _result)
|
2000-01-27 02:24:20 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(remote != nullptr, "not an nsIRDFRemoteDataSource");
|
2000-01-27 02:24:20 +00:00
|
|
|
if (! remote)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return remote->GetLoaded(_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-14 21:12:46 +00:00
|
|
|
LocalStoreImpl::Init(const char *uri)
|
|
|
|
{
|
|
|
|
return(NS_OK);
|
|
|
|
}
|
|
|
|
|
2000-01-27 02:24:20 +00:00
|
|
|
NS_IMETHODIMP
|
1999-07-14 21:12:46 +00:00
|
|
|
LocalStoreImpl::Flush()
|
|
|
|
{
|
1999-09-14 22:36:55 +00:00
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
|
2007-06-27 00:13:01 +00:00
|
|
|
// FIXME Bug 340242: Temporarily make this a warning rather than an
|
|
|
|
// assertion until we sort out the ordering of how we write
|
|
|
|
// everything to the localstore, flush it, and disconnect it when
|
|
|
|
// we're getting profile-change notifications.
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_WARN_IF_FALSE(remote != nullptr, "not an nsIRDFRemoteDataSource");
|
1999-09-14 22:36:55 +00:00
|
|
|
if (! remote)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return remote->Flush();
|
1999-07-14 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
2002-11-22 07:55:59 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
LocalStoreImpl::FlushTo(const char *aURI)
|
|
|
|
{
|
|
|
|
// Do not ever implement this (security)
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-01-27 02:24:20 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
LocalStoreImpl::Refresh(bool sync)
|
1999-07-14 21:12:46 +00:00
|
|
|
{
|
1999-09-14 22:36:55 +00:00
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ASSERTION(remote != nullptr, "not an nsIRDFRemoteDataSource");
|
1999-09-14 22:36:55 +00:00
|
|
|
if (! remote)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return remote->Refresh(sync);
|
1999-07-14 21:12:46 +00:00
|
|
|
}
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
nsresult
|
|
|
|
LocalStoreImpl::Init()
|
1999-04-06 20:37:24 +00:00
|
|
|
{
|
2001-08-29 14:02:38 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = LoadData();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-04-06 20:37:24 +00:00
|
|
|
|
2001-08-29 14:02:38 +00:00
|
|
|
// register this as a named data source with the RDF service
|
2005-12-01 15:03:48 +00:00
|
|
|
mRDFService = do_GetService(NS_RDF_CONTRACTID "/rdf-service;1", &rv);
|
2001-08-29 14:02:38 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mRDFService->RegisterDataSource(this, false);
|
2001-08-29 14:02:38 +00:00
|
|
|
|
|
|
|
// Register as an observer of profile changes
|
|
|
|
nsCOMPtr<nsIObserverService> obs =
|
2001-10-22 22:01:27 +00:00
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
2001-08-29 14:02:38 +00:00
|
|
|
|
|
|
|
if (obs) {
|
2011-10-17 14:59:28 +00:00
|
|
|
obs->AddObserver(this, "profile-before-change", true);
|
|
|
|
obs->AddObserver(this, "profile-do-change", true);
|
2001-08-29 14:02:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-04-26 17:49:05 +00:00
|
|
|
nsresult
|
|
|
|
LocalStoreImpl::CreateLocalStore(nsIFile* aFile)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = aFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIOutputStream> outStream;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), aFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
const char defaultRDF[] =
|
|
|
|
"<?xml version=\"1.0\"?>\n" \
|
|
|
|
"<RDF:RDF xmlns:RDF=\"" RDF_NAMESPACE_URI "\"\n" \
|
|
|
|
" xmlns:NC=\"" NC_NAMESPACE_URI "\">\n" \
|
|
|
|
" <!-- Empty -->\n" \
|
|
|
|
"</RDF:RDF>\n";
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count;
|
2007-04-26 17:49:05 +00:00
|
|
|
rv = outStream->Write(defaultRDF, sizeof(defaultRDF)-1, &count);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (count != sizeof(defaultRDF)-1)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// Okay, now see if the file exists _for real_. If it's still
|
|
|
|
// not there, it could be that the profile service gave us
|
|
|
|
// back a read-only directory. Whatever.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool fileExistsFlag = false;
|
2007-04-26 17:49:05 +00:00
|
|
|
aFile->Exists(&fileExistsFlag);
|
|
|
|
if (!fileExistsFlag)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-08-29 14:02:38 +00:00
|
|
|
nsresult
|
|
|
|
LocalStoreImpl::LoadData()
|
|
|
|
{
|
1999-04-06 20:37:24 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
1999-10-05 04:12:58 +00:00
|
|
|
// Look for localstore.rdf in the current profile
|
|
|
|
// directory. Bomb if we can't find it.
|
1999-08-04 02:19:55 +00:00
|
|
|
|
2000-08-22 00:14:24 +00:00
|
|
|
nsCOMPtr<nsIFile> aFile;
|
2002-01-18 07:20:36 +00:00
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile));
|
2002-01-15 05:20:37 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool fileExistsFlag = false;
|
2002-02-01 00:24:27 +00:00
|
|
|
(void)aFile->Exists(&fileExistsFlag);
|
|
|
|
if (!fileExistsFlag) {
|
|
|
|
// if file doesn't exist, create it
|
2007-04-26 17:49:05 +00:00
|
|
|
rv = CreateLocalStore(aFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-10-05 04:12:58 +00:00
|
|
|
}
|
1999-04-06 20:37:24 +00:00
|
|
|
|
2001-08-29 14:02:38 +00:00
|
|
|
mInner = do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource", &rv);
|
1999-04-06 20:37:24 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-08-29 14:02:38 +00:00
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-24 00:22:58 +00:00
|
|
|
|
2002-02-01 00:24:27 +00:00
|
|
|
nsCOMPtr<nsIURI> aURI;
|
|
|
|
rv = NS_NewFileURI(getter_AddRefs(aURI), aFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = aURI->GetSpec(spec);
|
2002-02-01 00:24:27 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-03-06 07:48:55 +00:00
|
|
|
rv = remote->Init(spec.get());
|
1999-04-06 20:37:24 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-05-25 15:58:09 +00:00
|
|
|
// Read the datasource synchronously.
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = remote->Refresh(true);
|
2007-04-26 17:49:05 +00:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Load failed, delete and recreate a fresh localstore
|
2011-10-17 14:59:28 +00:00
|
|
|
aFile->Remove(true);
|
2007-04-26 17:49:05 +00:00
|
|
|
rv = CreateLocalStore(aFile);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = remote->Refresh(true);
|
2007-04-26 17:49:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocalStoreImpl::GetURI(char* *aURI)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aURI != nullptr, "null ptr");
|
1999-04-06 20:37:24 +00:00
|
|
|
if (! aURI)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2006-11-19 08:28:02 +00:00
|
|
|
*aURI = NS_strdup("rdf:local-store");
|
1999-04-06 20:37:24 +00:00
|
|
|
if (! *aURI)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-26 01:09:02 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
LocalStoreImpl::GetAllCmds(nsIRDFResource* aSource,
|
|
|
|
nsISimpleEnumerator/*<nsIRDFResource>*/** aCommands)
|
|
|
|
{
|
1999-06-26 04:58:22 +00:00
|
|
|
return(NS_NewEmptyEnumerator(aCommands));
|
1999-06-26 01:09:02 +00:00
|
|
|
}
|
|
|
|
|
1999-04-06 20:37:24 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
LocalStoreImpl::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources,
|
|
|
|
nsIRDFResource* aCommand,
|
|
|
|
nsISupportsArray/*<nsIRDFResource>*/* aArguments,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool* aResult)
|
1999-04-06 20:37:24 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*aResult = true;
|
1999-04-08 00:43:11 +00:00
|
|
|
return NS_OK;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
LocalStoreImpl::DoCommand(nsISupportsArray* aSources,
|
|
|
|
nsIRDFResource* aCommand,
|
|
|
|
nsISupportsArray* aArguments)
|
|
|
|
{
|
1999-04-08 00:43:11 +00:00
|
|
|
// no-op
|
|
|
|
return NS_OK;
|
1999-04-06 20:37:24 +00:00
|
|
|
}
|
2001-03-11 22:12:21 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 15:02:17 +00:00
|
|
|
LocalStoreImpl::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData)
|
2001-03-11 22:12:21 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-10-19 20:52:59 +00:00
|
|
|
if (!nsCRT::strcmp(aTopic, "profile-before-change")) {
|
2001-08-29 14:02:38 +00:00
|
|
|
// Write out the old datasource's contents.
|
|
|
|
if (mInner) {
|
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner);
|
|
|
|
if (remote)
|
|
|
|
remote->Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an in-memory datasource for use while we're
|
|
|
|
// profile-less.
|
|
|
|
mInner = do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "in-memory-datasource");
|
2001-03-11 22:12:21 +00:00
|
|
|
|
2014-06-27 08:08:13 +00:00
|
|
|
if (!NS_strcmp(someData, MOZ_UTF16("shutdown-cleanse"))) {
|
2001-03-11 22:12:21 +00:00
|
|
|
nsCOMPtr<nsIFile> aFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = aFile->Remove(false);
|
2001-03-11 22:12:21 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-19 20:52:59 +00:00
|
|
|
else if (!nsCRT::strcmp(aTopic, "profile-do-change")) {
|
2001-08-29 14:02:38 +00:00
|
|
|
rv = LoadData();
|
2001-03-11 22:12:21 +00:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|