2010-10-20 10:12:32 -07:00
|
|
|
/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -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/. */
|
2010-10-20 10:12:32 -07:00
|
|
|
|
|
|
|
#include "OfflineCacheUpdateGlue.h"
|
|
|
|
#include "nsOfflineCacheUpdate.h"
|
|
|
|
#include "mozilla/Services.h"
|
|
|
|
|
|
|
|
#include "nsIApplicationCache.h"
|
|
|
|
#include "nsIApplicationCacheChannel.h"
|
|
|
|
#include "nsIApplicationCacheContainer.h"
|
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIDocument.h"
|
2015-05-19 11:15:34 -07:00
|
|
|
#include "mozilla/Logging.h"
|
2010-10-20 10:12:32 -07:00
|
|
|
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=nsOfflineCacheUpdate:5
|
|
|
|
// set NSPR_LOG_FILE=offlineupdate.log
|
|
|
|
//
|
2015-06-03 15:25:57 -07:00
|
|
|
// this enables LogLevel::Info level information and places all output in
|
2010-10-20 10:12:32 -07:00
|
|
|
// the file offlineupdate.log
|
|
|
|
//
|
|
|
|
extern PRLogModuleInfo *gOfflineCacheUpdateLog;
|
2013-11-18 23:50:59 -08:00
|
|
|
|
|
|
|
#undef LOG
|
2015-06-03 15:25:57 -07:00
|
|
|
#define LOG(args) MOZ_LOG(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug, args)
|
2013-11-18 23:50:59 -08:00
|
|
|
|
|
|
|
#undef LOG_ENABLED
|
2015-06-03 15:25:57 -07:00
|
|
|
#define LOG_ENABLED() MOZ_LOG_TEST(gOfflineCacheUpdateLog, mozilla::LogLevel::Debug)
|
2010-10-20 10:12:32 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace docshell {
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// OfflineCacheUpdateGlue::nsISupports
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(OfflineCacheUpdateGlue,
|
|
|
|
nsIOfflineCacheUpdate,
|
|
|
|
nsIOfflineCacheUpdateObserver,
|
|
|
|
nsISupportsWeakReference)
|
2010-10-20 10:12:32 -07:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// OfflineCacheUpdateGlue <public>
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
OfflineCacheUpdateGlue::OfflineCacheUpdateGlue()
|
2014-07-16 07:16:30 -07:00
|
|
|
: mCoalesced(false)
|
2010-10-20 10:12:32 -07:00
|
|
|
{
|
|
|
|
LOG(("OfflineCacheUpdateGlue::OfflineCacheUpdateGlue [%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
OfflineCacheUpdateGlue::~OfflineCacheUpdateGlue()
|
|
|
|
{
|
|
|
|
LOG(("OfflineCacheUpdateGlue::~OfflineCacheUpdateGlue [%p]", this));
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIOfflineCacheUpdate*
|
|
|
|
OfflineCacheUpdateGlue::EnsureUpdate()
|
|
|
|
{
|
|
|
|
if (!mUpdate) {
|
|
|
|
mUpdate = new nsOfflineCacheUpdate();
|
|
|
|
LOG(("OfflineCacheUpdateGlue [%p] is using update [%p]", this, mUpdate.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mUpdate;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OfflineCacheUpdateGlue::Schedule()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
LOG(("Calling offline-cache-update-added"));
|
|
|
|
observerService->NotifyObservers(static_cast<nsIOfflineCacheUpdate*>(this),
|
|
|
|
"offline-cache-update-added",
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2010-10-20 10:12:32 -07:00
|
|
|
LOG(("Done offline-cache-update-added"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureUpdate())
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Do not use weak reference, we must survive!
|
2011-10-17 07:59:28 -07:00
|
|
|
mUpdate->AddObserver(this, false);
|
2010-10-20 10:12:32 -07:00
|
|
|
|
2014-07-16 07:16:30 -07:00
|
|
|
if (mCoalesced) // already scheduled
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-10-20 10:12:32 -07:00
|
|
|
return mUpdate->Schedule();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-10-02 09:13:52 -07:00
|
|
|
OfflineCacheUpdateGlue::Init(nsIURI *aManifestURI,
|
2010-10-20 10:12:32 -07:00
|
|
|
nsIURI *aDocumentURI,
|
2015-10-02 09:13:52 -07:00
|
|
|
nsIPrincipal* aLoadingPrincipal,
|
2012-06-04 07:12:24 -07:00
|
|
|
nsIDOMDocument *aDocument,
|
2012-07-30 23:36:00 -07:00
|
|
|
nsIFile *aCustomProfileDir,
|
2012-10-26 04:02:47 -07:00
|
|
|
uint32_t aAppID,
|
|
|
|
bool aInBrowser)
|
2010-10-20 10:12:32 -07:00
|
|
|
{
|
2014-07-16 07:16:30 -07:00
|
|
|
nsOfflineCacheUpdateService* service =
|
|
|
|
nsOfflineCacheUpdateService::EnsureService();
|
|
|
|
if (service) {
|
|
|
|
service->FindUpdate(aManifestURI, aAppID, aInBrowser, aCustomProfileDir,
|
|
|
|
getter_AddRefs(mUpdate));
|
|
|
|
mCoalesced = !!mUpdate;
|
|
|
|
}
|
|
|
|
|
2010-10-20 10:12:32 -07:00
|
|
|
if (!EnsureUpdate())
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
mDocumentURI = aDocumentURI;
|
2015-10-02 09:13:52 -07:00
|
|
|
mLoadingPrincipal = aLoadingPrincipal;
|
2010-10-20 10:12:32 -07:00
|
|
|
|
|
|
|
if (aDocument)
|
|
|
|
SetDocument(aDocument);
|
|
|
|
|
2014-07-16 07:16:30 -07:00
|
|
|
if (mCoalesced) { // already initialized
|
|
|
|
LOG(("OfflineCacheUpdateGlue %p coalesced with update %p", this, mUpdate.get()));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-10-02 09:13:52 -07:00
|
|
|
return mUpdate->Init(aManifestURI, aDocumentURI, aLoadingPrincipal, nullptr,
|
|
|
|
aCustomProfileDir, aAppID, aInBrowser);
|
2010-10-20 10:12:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
OfflineCacheUpdateGlue::SetDocument(nsIDOMDocument *aDocument)
|
|
|
|
{
|
|
|
|
// The design is one document for one cache update on the content process.
|
|
|
|
NS_ASSERTION(!mDocument,
|
|
|
|
"Setting more then a single document on an instance of OfflineCacheUpdateGlue");
|
|
|
|
|
|
|
|
LOG(("Document %p added to update glue %p", aDocument, this));
|
|
|
|
|
|
|
|
// Add document only if it was not loaded from an offline cache.
|
|
|
|
// If it were loaded from an offline cache then it has already
|
|
|
|
// been associated with it and must not be again cached as
|
|
|
|
// implicit (which are the reasons we collect documents here).
|
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryInterface(aDocument);
|
|
|
|
if (!document)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsIChannel* channel = document->GetChannel();
|
|
|
|
nsCOMPtr<nsIApplicationCacheChannel> appCacheChannel =
|
|
|
|
do_QueryInterface(channel);
|
|
|
|
if (!appCacheChannel)
|
|
|
|
return;
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool loadedFromAppCache;
|
2010-10-20 10:12:32 -07:00
|
|
|
appCacheChannel->GetLoadedFromApplicationCache(&loadedFromAppCache);
|
|
|
|
if (loadedFromAppCache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (EnsureUpdate()) {
|
|
|
|
mUpdate->StickDocument(mDocumentURI);
|
|
|
|
}
|
|
|
|
|
|
|
|
mDocument = aDocument;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
OfflineCacheUpdateGlue::UpdateStateChanged(nsIOfflineCacheUpdate *aUpdate, uint32_t state)
|
2010-10-20 10:12:32 -07:00
|
|
|
{
|
|
|
|
if (state == nsIOfflineCacheUpdateObserver::STATE_FINISHED) {
|
|
|
|
LOG(("OfflineCacheUpdateGlue got STATE_FINISHED [%p]", this));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
mozilla::services::GetObserverService();
|
|
|
|
if (observerService) {
|
|
|
|
LOG(("Calling offline-cache-update-completed"));
|
|
|
|
observerService->NotifyObservers(static_cast<nsIOfflineCacheUpdate*>(this),
|
|
|
|
"offline-cache-update-completed",
|
2012-07-30 07:20:58 -07:00
|
|
|
nullptr);
|
2010-10-20 10:12:32 -07:00
|
|
|
LOG(("Done offline-cache-update-completed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
aUpdate->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OfflineCacheUpdateGlue::ApplicationCacheAvailable(nsIApplicationCache *aApplicationCache)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aApplicationCache);
|
|
|
|
|
|
|
|
// Check that the document that requested this update was
|
|
|
|
// previously associated with an application cache. If not, it
|
|
|
|
// should be associated with the new one.
|
|
|
|
nsCOMPtr<nsIApplicationCacheContainer> container =
|
|
|
|
do_QueryInterface(mDocument);
|
|
|
|
if (!container)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIApplicationCache> existingCache;
|
|
|
|
nsresult rv = container->GetApplicationCache(getter_AddRefs(existingCache));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!existingCache) {
|
|
|
|
if (LOG_ENABLED()) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString clientID;
|
2010-10-20 10:12:32 -07:00
|
|
|
if (aApplicationCache) {
|
|
|
|
aApplicationCache->GetClientID(clientID);
|
|
|
|
}
|
|
|
|
LOG(("Update %p: associating app cache %s to document %p",
|
2010-10-20 19:30:31 -07:00
|
|
|
this, clientID.get(), mDocument.get()));
|
2010-10-20 10:12:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = container->SetApplicationCache(aApplicationCache);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 08:25:42 -07:00
|
|
|
} // namespace docshell
|
|
|
|
} // namespace mozilla
|