Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set cindent tabstop=4 expandtab shiftwidth=4: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | |
michael@0 | 9 | Implementation for the local store |
michael@0 | 10 | |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #include "nsNetUtil.h" |
michael@0 | 14 | #include "nsIURI.h" |
michael@0 | 15 | #include "nsIIOService.h" |
michael@0 | 16 | #include "nsIOutputStream.h" |
michael@0 | 17 | #include "nsIComponentManager.h" |
michael@0 | 18 | #include "nsILocalStore.h" |
michael@0 | 19 | #include "nsIRDFDataSource.h" |
michael@0 | 20 | #include "nsIRDFRemoteDataSource.h" |
michael@0 | 21 | #include "nsIRDFService.h" |
michael@0 | 22 | #include "nsIServiceManager.h" |
michael@0 | 23 | #include "nsRDFCID.h" |
michael@0 | 24 | #include "nsXPIDLString.h" |
michael@0 | 25 | #include "plstr.h" |
michael@0 | 26 | #include "rdf.h" |
michael@0 | 27 | #include "nsCOMPtr.h" |
michael@0 | 28 | #include "nsWeakPtr.h" |
michael@0 | 29 | #include "nsAppDirectoryServiceDefs.h" |
michael@0 | 30 | #include "nsIObserver.h" |
michael@0 | 31 | #include "nsIObserverService.h" |
michael@0 | 32 | #include "nsWeakReference.h" |
michael@0 | 33 | #include "nsCRTGlue.h" |
michael@0 | 34 | #include "nsCRT.h" |
michael@0 | 35 | #include "nsEnumeratorUtils.h" |
michael@0 | 36 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 37 | |
michael@0 | 38 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 39 | |
michael@0 | 40 | class LocalStoreImpl : public nsILocalStore, |
michael@0 | 41 | public nsIRDFDataSource, |
michael@0 | 42 | public nsIRDFRemoteDataSource, |
michael@0 | 43 | public nsIObserver, |
michael@0 | 44 | public nsSupportsWeakReference |
michael@0 | 45 | { |
michael@0 | 46 | protected: |
michael@0 | 47 | nsCOMPtr<nsIRDFDataSource> mInner; |
michael@0 | 48 | |
michael@0 | 49 | LocalStoreImpl(); |
michael@0 | 50 | virtual ~LocalStoreImpl(); |
michael@0 | 51 | nsresult Init(); |
michael@0 | 52 | nsresult CreateLocalStore(nsIFile* aFile); |
michael@0 | 53 | nsresult LoadData(); |
michael@0 | 54 | |
michael@0 | 55 | friend nsresult |
michael@0 | 56 | NS_NewLocalStore(nsISupports* aOuter, REFNSIID aIID, void** aResult); |
michael@0 | 57 | |
michael@0 | 58 | nsCOMPtr<nsIRDFService> mRDFService; |
michael@0 | 59 | |
michael@0 | 60 | public: |
michael@0 | 61 | // nsISupports interface |
michael@0 | 62 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 63 | NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(LocalStoreImpl, nsILocalStore) |
michael@0 | 64 | |
michael@0 | 65 | // nsILocalStore interface |
michael@0 | 66 | |
michael@0 | 67 | // nsIRDFDataSource interface. Most of these are just delegated to |
michael@0 | 68 | // the inner, in-memory datasource. |
michael@0 | 69 | NS_IMETHOD GetURI(char* *aURI); |
michael@0 | 70 | |
michael@0 | 71 | NS_IMETHOD GetSource(nsIRDFResource* aProperty, |
michael@0 | 72 | nsIRDFNode* aTarget, |
michael@0 | 73 | bool aTruthValue, |
michael@0 | 74 | nsIRDFResource** aSource) { |
michael@0 | 75 | return mInner->GetSource(aProperty, aTarget, aTruthValue, aSource); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | NS_IMETHOD GetSources(nsIRDFResource* aProperty, |
michael@0 | 79 | nsIRDFNode* aTarget, |
michael@0 | 80 | bool aTruthValue, |
michael@0 | 81 | nsISimpleEnumerator** aSources) { |
michael@0 | 82 | return mInner->GetSources(aProperty, aTarget, aTruthValue, aSources); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | NS_IMETHOD GetTarget(nsIRDFResource* aSource, |
michael@0 | 86 | nsIRDFResource* aProperty, |
michael@0 | 87 | bool aTruthValue, |
michael@0 | 88 | nsIRDFNode** aTarget) { |
michael@0 | 89 | return mInner->GetTarget(aSource, aProperty, aTruthValue, aTarget); |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_IMETHOD GetTargets(nsIRDFResource* aSource, |
michael@0 | 93 | nsIRDFResource* aProperty, |
michael@0 | 94 | bool aTruthValue, |
michael@0 | 95 | nsISimpleEnumerator** aTargets) { |
michael@0 | 96 | return mInner->GetTargets(aSource, aProperty, aTruthValue, aTargets); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | NS_IMETHOD Assert(nsIRDFResource* aSource, |
michael@0 | 100 | nsIRDFResource* aProperty, |
michael@0 | 101 | nsIRDFNode* aTarget, |
michael@0 | 102 | bool aTruthValue) { |
michael@0 | 103 | return mInner->Assert(aSource, aProperty, aTarget, aTruthValue); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | NS_IMETHOD Unassert(nsIRDFResource* aSource, |
michael@0 | 107 | nsIRDFResource* aProperty, |
michael@0 | 108 | nsIRDFNode* aTarget) { |
michael@0 | 109 | return mInner->Unassert(aSource, aProperty, aTarget); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | NS_IMETHOD Change(nsIRDFResource* aSource, |
michael@0 | 113 | nsIRDFResource* aProperty, |
michael@0 | 114 | nsIRDFNode* aOldTarget, |
michael@0 | 115 | nsIRDFNode* aNewTarget) { |
michael@0 | 116 | return mInner->Change(aSource, aProperty, aOldTarget, aNewTarget); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | NS_IMETHOD Move(nsIRDFResource* aOldSource, |
michael@0 | 120 | nsIRDFResource* aNewSource, |
michael@0 | 121 | nsIRDFResource* aProperty, |
michael@0 | 122 | nsIRDFNode* aTarget) { |
michael@0 | 123 | return mInner->Move(aOldSource, aNewSource, aProperty, aTarget); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | NS_IMETHOD HasAssertion(nsIRDFResource* aSource, |
michael@0 | 127 | nsIRDFResource* aProperty, |
michael@0 | 128 | nsIRDFNode* aTarget, |
michael@0 | 129 | bool aTruthValue, |
michael@0 | 130 | bool* hasAssertion) { |
michael@0 | 131 | return mInner->HasAssertion(aSource, aProperty, aTarget, aTruthValue, hasAssertion); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHOD AddObserver(nsIRDFObserver* aObserver) { |
michael@0 | 135 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | NS_IMETHOD RemoveObserver(nsIRDFObserver* aObserver) { |
michael@0 | 139 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | NS_IMETHOD HasArcIn(nsIRDFNode *aNode, nsIRDFResource *aArc, bool *_retval) { |
michael@0 | 143 | return mInner->HasArcIn(aNode, aArc, _retval); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | NS_IMETHOD HasArcOut(nsIRDFResource *aSource, nsIRDFResource *aArc, bool *_retval) { |
michael@0 | 147 | return mInner->HasArcOut(aSource, aArc, _retval); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | NS_IMETHOD ArcLabelsIn(nsIRDFNode* aNode, |
michael@0 | 151 | nsISimpleEnumerator** aLabels) { |
michael@0 | 152 | return mInner->ArcLabelsIn(aNode, aLabels); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | NS_IMETHOD ArcLabelsOut(nsIRDFResource* aSource, |
michael@0 | 156 | nsISimpleEnumerator** aLabels) { |
michael@0 | 157 | return mInner->ArcLabelsOut(aSource, aLabels); |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | NS_IMETHOD GetAllResources(nsISimpleEnumerator** aResult) { |
michael@0 | 161 | return mInner->GetAllResources(aResult); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | NS_IMETHOD GetAllCmds(nsIRDFResource* aSource, |
michael@0 | 165 | nsISimpleEnumerator/*<nsIRDFResource>*/** aCommands); |
michael@0 | 166 | |
michael@0 | 167 | NS_IMETHOD IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources, |
michael@0 | 168 | nsIRDFResource* aCommand, |
michael@0 | 169 | nsISupportsArray/*<nsIRDFResource>*/* aArguments, |
michael@0 | 170 | bool* aResult); |
michael@0 | 171 | |
michael@0 | 172 | NS_IMETHOD DoCommand(nsISupportsArray/*<nsIRDFResource>*/* aSources, |
michael@0 | 173 | nsIRDFResource* aCommand, |
michael@0 | 174 | nsISupportsArray/*<nsIRDFResource>*/* aArguments); |
michael@0 | 175 | |
michael@0 | 176 | NS_IMETHOD BeginUpdateBatch() { |
michael@0 | 177 | return mInner->BeginUpdateBatch(); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | NS_IMETHOD EndUpdateBatch() { |
michael@0 | 181 | return mInner->EndUpdateBatch(); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | NS_IMETHOD GetLoaded(bool* _result); |
michael@0 | 185 | NS_IMETHOD Init(const char *uri); |
michael@0 | 186 | NS_IMETHOD Flush(); |
michael@0 | 187 | NS_IMETHOD FlushTo(const char *aURI); |
michael@0 | 188 | NS_IMETHOD Refresh(bool sync); |
michael@0 | 189 | |
michael@0 | 190 | // nsIObserver |
michael@0 | 191 | NS_DECL_NSIOBSERVER |
michael@0 | 192 | }; |
michael@0 | 193 | |
michael@0 | 194 | //////////////////////////////////////////////////////////////////////// |
michael@0 | 195 | |
michael@0 | 196 | |
michael@0 | 197 | LocalStoreImpl::LocalStoreImpl(void) |
michael@0 | 198 | { |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | LocalStoreImpl::~LocalStoreImpl(void) |
michael@0 | 202 | { |
michael@0 | 203 | if (mRDFService) |
michael@0 | 204 | mRDFService->UnregisterDataSource(this); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | |
michael@0 | 208 | nsresult |
michael@0 | 209 | NS_NewLocalStore(nsISupports* aOuter, REFNSIID aIID, void** aResult) |
michael@0 | 210 | { |
michael@0 | 211 | NS_PRECONDITION(aOuter == nullptr, "no aggregation"); |
michael@0 | 212 | if (aOuter) |
michael@0 | 213 | return NS_ERROR_NO_AGGREGATION; |
michael@0 | 214 | |
michael@0 | 215 | NS_PRECONDITION(aResult != nullptr, "null ptr"); |
michael@0 | 216 | if (! aResult) |
michael@0 | 217 | return NS_ERROR_NULL_POINTER; |
michael@0 | 218 | |
michael@0 | 219 | LocalStoreImpl* impl = new LocalStoreImpl(); |
michael@0 | 220 | if (! impl) |
michael@0 | 221 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 222 | |
michael@0 | 223 | NS_ADDREF(impl); |
michael@0 | 224 | |
michael@0 | 225 | nsresult rv; |
michael@0 | 226 | rv = impl->Init(); |
michael@0 | 227 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 228 | // Set up the result pointer |
michael@0 | 229 | rv = impl->QueryInterface(aIID, aResult); |
michael@0 | 230 | } |
michael@0 | 231 | |
michael@0 | 232 | NS_RELEASE(impl); |
michael@0 | 233 | return rv; |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | NS_IMPL_CYCLE_COLLECTION(LocalStoreImpl, mInner) |
michael@0 | 237 | NS_IMPL_CYCLE_COLLECTING_ADDREF(LocalStoreImpl) |
michael@0 | 238 | NS_IMPL_CYCLE_COLLECTING_RELEASE(LocalStoreImpl) |
michael@0 | 239 | |
michael@0 | 240 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(LocalStoreImpl) |
michael@0 | 241 | NS_INTERFACE_MAP_ENTRY(nsILocalStore) |
michael@0 | 242 | NS_INTERFACE_MAP_ENTRY(nsIRDFDataSource) |
michael@0 | 243 | NS_INTERFACE_MAP_ENTRY(nsIRDFRemoteDataSource) |
michael@0 | 244 | NS_INTERFACE_MAP_ENTRY(nsIObserver) |
michael@0 | 245 | NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) |
michael@0 | 246 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsILocalStore) |
michael@0 | 247 | NS_INTERFACE_MAP_END |
michael@0 | 248 | |
michael@0 | 249 | // nsILocalStore interface |
michael@0 | 250 | |
michael@0 | 251 | // nsIRDFDataSource interface |
michael@0 | 252 | |
michael@0 | 253 | NS_IMETHODIMP |
michael@0 | 254 | LocalStoreImpl::GetLoaded(bool* _result) |
michael@0 | 255 | { |
michael@0 | 256 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner); |
michael@0 | 257 | NS_ASSERTION(remote != nullptr, "not an nsIRDFRemoteDataSource"); |
michael@0 | 258 | if (! remote) |
michael@0 | 259 | return NS_ERROR_UNEXPECTED; |
michael@0 | 260 | |
michael@0 | 261 | return remote->GetLoaded(_result); |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | |
michael@0 | 265 | NS_IMETHODIMP |
michael@0 | 266 | LocalStoreImpl::Init(const char *uri) |
michael@0 | 267 | { |
michael@0 | 268 | return(NS_OK); |
michael@0 | 269 | } |
michael@0 | 270 | |
michael@0 | 271 | NS_IMETHODIMP |
michael@0 | 272 | LocalStoreImpl::Flush() |
michael@0 | 273 | { |
michael@0 | 274 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner); |
michael@0 | 275 | // FIXME Bug 340242: Temporarily make this a warning rather than an |
michael@0 | 276 | // assertion until we sort out the ordering of how we write |
michael@0 | 277 | // everything to the localstore, flush it, and disconnect it when |
michael@0 | 278 | // we're getting profile-change notifications. |
michael@0 | 279 | NS_WARN_IF_FALSE(remote != nullptr, "not an nsIRDFRemoteDataSource"); |
michael@0 | 280 | if (! remote) |
michael@0 | 281 | return NS_ERROR_UNEXPECTED; |
michael@0 | 282 | |
michael@0 | 283 | return remote->Flush(); |
michael@0 | 284 | } |
michael@0 | 285 | |
michael@0 | 286 | NS_IMETHODIMP |
michael@0 | 287 | LocalStoreImpl::FlushTo(const char *aURI) |
michael@0 | 288 | { |
michael@0 | 289 | // Do not ever implement this (security) |
michael@0 | 290 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | NS_IMETHODIMP |
michael@0 | 294 | LocalStoreImpl::Refresh(bool sync) |
michael@0 | 295 | { |
michael@0 | 296 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner); |
michael@0 | 297 | NS_ASSERTION(remote != nullptr, "not an nsIRDFRemoteDataSource"); |
michael@0 | 298 | if (! remote) |
michael@0 | 299 | return NS_ERROR_UNEXPECTED; |
michael@0 | 300 | |
michael@0 | 301 | return remote->Refresh(sync); |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | nsresult |
michael@0 | 305 | LocalStoreImpl::Init() |
michael@0 | 306 | { |
michael@0 | 307 | nsresult rv; |
michael@0 | 308 | |
michael@0 | 309 | rv = LoadData(); |
michael@0 | 310 | if (NS_FAILED(rv)) return rv; |
michael@0 | 311 | |
michael@0 | 312 | // register this as a named data source with the RDF service |
michael@0 | 313 | mRDFService = do_GetService(NS_RDF_CONTRACTID "/rdf-service;1", &rv); |
michael@0 | 314 | if (NS_FAILED(rv)) return rv; |
michael@0 | 315 | |
michael@0 | 316 | mRDFService->RegisterDataSource(this, false); |
michael@0 | 317 | |
michael@0 | 318 | // Register as an observer of profile changes |
michael@0 | 319 | nsCOMPtr<nsIObserverService> obs = |
michael@0 | 320 | do_GetService("@mozilla.org/observer-service;1"); |
michael@0 | 321 | |
michael@0 | 322 | if (obs) { |
michael@0 | 323 | obs->AddObserver(this, "profile-before-change", true); |
michael@0 | 324 | obs->AddObserver(this, "profile-do-change", true); |
michael@0 | 325 | } |
michael@0 | 326 | |
michael@0 | 327 | return NS_OK; |
michael@0 | 328 | } |
michael@0 | 329 | |
michael@0 | 330 | nsresult |
michael@0 | 331 | LocalStoreImpl::CreateLocalStore(nsIFile* aFile) |
michael@0 | 332 | { |
michael@0 | 333 | nsresult rv; |
michael@0 | 334 | |
michael@0 | 335 | rv = aFile->Create(nsIFile::NORMAL_FILE_TYPE, 0666); |
michael@0 | 336 | if (NS_FAILED(rv)) return rv; |
michael@0 | 337 | |
michael@0 | 338 | nsCOMPtr<nsIOutputStream> outStream; |
michael@0 | 339 | rv = NS_NewLocalFileOutputStream(getter_AddRefs(outStream), aFile); |
michael@0 | 340 | if (NS_FAILED(rv)) return rv; |
michael@0 | 341 | |
michael@0 | 342 | const char defaultRDF[] = |
michael@0 | 343 | "<?xml version=\"1.0\"?>\n" \ |
michael@0 | 344 | "<RDF:RDF xmlns:RDF=\"" RDF_NAMESPACE_URI "\"\n" \ |
michael@0 | 345 | " xmlns:NC=\"" NC_NAMESPACE_URI "\">\n" \ |
michael@0 | 346 | " <!-- Empty -->\n" \ |
michael@0 | 347 | "</RDF:RDF>\n"; |
michael@0 | 348 | |
michael@0 | 349 | uint32_t count; |
michael@0 | 350 | rv = outStream->Write(defaultRDF, sizeof(defaultRDF)-1, &count); |
michael@0 | 351 | if (NS_FAILED(rv)) return rv; |
michael@0 | 352 | |
michael@0 | 353 | if (count != sizeof(defaultRDF)-1) |
michael@0 | 354 | return NS_ERROR_UNEXPECTED; |
michael@0 | 355 | |
michael@0 | 356 | // Okay, now see if the file exists _for real_. If it's still |
michael@0 | 357 | // not there, it could be that the profile service gave us |
michael@0 | 358 | // back a read-only directory. Whatever. |
michael@0 | 359 | bool fileExistsFlag = false; |
michael@0 | 360 | aFile->Exists(&fileExistsFlag); |
michael@0 | 361 | if (!fileExistsFlag) |
michael@0 | 362 | return NS_ERROR_UNEXPECTED; |
michael@0 | 363 | |
michael@0 | 364 | return NS_OK; |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | nsresult |
michael@0 | 368 | LocalStoreImpl::LoadData() |
michael@0 | 369 | { |
michael@0 | 370 | nsresult rv; |
michael@0 | 371 | |
michael@0 | 372 | // Look for localstore.rdf in the current profile |
michael@0 | 373 | // directory. Bomb if we can't find it. |
michael@0 | 374 | |
michael@0 | 375 | nsCOMPtr<nsIFile> aFile; |
michael@0 | 376 | rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile)); |
michael@0 | 377 | if (NS_FAILED(rv)) return rv; |
michael@0 | 378 | |
michael@0 | 379 | bool fileExistsFlag = false; |
michael@0 | 380 | (void)aFile->Exists(&fileExistsFlag); |
michael@0 | 381 | if (!fileExistsFlag) { |
michael@0 | 382 | // if file doesn't exist, create it |
michael@0 | 383 | rv = CreateLocalStore(aFile); |
michael@0 | 384 | if (NS_FAILED(rv)) return rv; |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | mInner = do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "xml-datasource", &rv); |
michael@0 | 388 | if (NS_FAILED(rv)) return rv; |
michael@0 | 389 | |
michael@0 | 390 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner, &rv); |
michael@0 | 391 | if (NS_FAILED(rv)) return rv; |
michael@0 | 392 | |
michael@0 | 393 | nsCOMPtr<nsIURI> aURI; |
michael@0 | 394 | rv = NS_NewFileURI(getter_AddRefs(aURI), aFile); |
michael@0 | 395 | if (NS_FAILED(rv)) return rv; |
michael@0 | 396 | |
michael@0 | 397 | nsAutoCString spec; |
michael@0 | 398 | rv = aURI->GetSpec(spec); |
michael@0 | 399 | if (NS_FAILED(rv)) return rv; |
michael@0 | 400 | |
michael@0 | 401 | rv = remote->Init(spec.get()); |
michael@0 | 402 | if (NS_FAILED(rv)) return rv; |
michael@0 | 403 | |
michael@0 | 404 | // Read the datasource synchronously. |
michael@0 | 405 | rv = remote->Refresh(true); |
michael@0 | 406 | |
michael@0 | 407 | if (NS_FAILED(rv)) { |
michael@0 | 408 | // Load failed, delete and recreate a fresh localstore |
michael@0 | 409 | aFile->Remove(true); |
michael@0 | 410 | rv = CreateLocalStore(aFile); |
michael@0 | 411 | if (NS_FAILED(rv)) return rv; |
michael@0 | 412 | |
michael@0 | 413 | rv = remote->Refresh(true); |
michael@0 | 414 | } |
michael@0 | 415 | |
michael@0 | 416 | return rv; |
michael@0 | 417 | } |
michael@0 | 418 | |
michael@0 | 419 | |
michael@0 | 420 | NS_IMETHODIMP |
michael@0 | 421 | LocalStoreImpl::GetURI(char* *aURI) |
michael@0 | 422 | { |
michael@0 | 423 | NS_PRECONDITION(aURI != nullptr, "null ptr"); |
michael@0 | 424 | if (! aURI) |
michael@0 | 425 | return NS_ERROR_NULL_POINTER; |
michael@0 | 426 | |
michael@0 | 427 | *aURI = NS_strdup("rdf:local-store"); |
michael@0 | 428 | if (! *aURI) |
michael@0 | 429 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 430 | |
michael@0 | 431 | return NS_OK; |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | |
michael@0 | 435 | NS_IMETHODIMP |
michael@0 | 436 | LocalStoreImpl::GetAllCmds(nsIRDFResource* aSource, |
michael@0 | 437 | nsISimpleEnumerator/*<nsIRDFResource>*/** aCommands) |
michael@0 | 438 | { |
michael@0 | 439 | return(NS_NewEmptyEnumerator(aCommands)); |
michael@0 | 440 | } |
michael@0 | 441 | |
michael@0 | 442 | NS_IMETHODIMP |
michael@0 | 443 | LocalStoreImpl::IsCommandEnabled(nsISupportsArray/*<nsIRDFResource>*/* aSources, |
michael@0 | 444 | nsIRDFResource* aCommand, |
michael@0 | 445 | nsISupportsArray/*<nsIRDFResource>*/* aArguments, |
michael@0 | 446 | bool* aResult) |
michael@0 | 447 | { |
michael@0 | 448 | *aResult = true; |
michael@0 | 449 | return NS_OK; |
michael@0 | 450 | } |
michael@0 | 451 | |
michael@0 | 452 | NS_IMETHODIMP |
michael@0 | 453 | LocalStoreImpl::DoCommand(nsISupportsArray* aSources, |
michael@0 | 454 | nsIRDFResource* aCommand, |
michael@0 | 455 | nsISupportsArray* aArguments) |
michael@0 | 456 | { |
michael@0 | 457 | // no-op |
michael@0 | 458 | return NS_OK; |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | NS_IMETHODIMP |
michael@0 | 462 | LocalStoreImpl::Observe(nsISupports *aSubject, const char *aTopic, const char16_t *someData) |
michael@0 | 463 | { |
michael@0 | 464 | nsresult rv = NS_OK; |
michael@0 | 465 | |
michael@0 | 466 | if (!nsCRT::strcmp(aTopic, "profile-before-change")) { |
michael@0 | 467 | // Write out the old datasource's contents. |
michael@0 | 468 | if (mInner) { |
michael@0 | 469 | nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(mInner); |
michael@0 | 470 | if (remote) |
michael@0 | 471 | remote->Flush(); |
michael@0 | 472 | } |
michael@0 | 473 | |
michael@0 | 474 | // Create an in-memory datasource for use while we're |
michael@0 | 475 | // profile-less. |
michael@0 | 476 | mInner = do_CreateInstance(NS_RDF_DATASOURCE_CONTRACTID_PREFIX "in-memory-datasource"); |
michael@0 | 477 | |
michael@0 | 478 | if (!nsCRT::strcmp(NS_ConvertUTF16toUTF8(someData).get(), "shutdown-cleanse")) { |
michael@0 | 479 | nsCOMPtr<nsIFile> aFile; |
michael@0 | 480 | rv = NS_GetSpecialDirectory(NS_APP_LOCALSTORE_50_FILE, getter_AddRefs(aFile)); |
michael@0 | 481 | if (NS_SUCCEEDED(rv)) |
michael@0 | 482 | rv = aFile->Remove(false); |
michael@0 | 483 | } |
michael@0 | 484 | } |
michael@0 | 485 | else if (!nsCRT::strcmp(aTopic, "profile-do-change")) { |
michael@0 | 486 | rv = LoadData(); |
michael@0 | 487 | } |
michael@0 | 488 | return rv; |
michael@0 | 489 | } |