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