1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/uriloader/prefetch/OfflineCacheUpdateParent.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,263 @@ 1.4 +/* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "OfflineCacheUpdateParent.h" 1.10 + 1.11 +#include "mozilla/dom/TabParent.h" 1.12 +#include "mozilla/ipc/URIUtils.h" 1.13 +#include "mozilla/unused.h" 1.14 +#include "nsOfflineCacheUpdate.h" 1.15 +#include "nsIApplicationCache.h" 1.16 +#include "nsIScriptSecurityManager.h" 1.17 +#include "nsNetUtil.h" 1.18 +#include "nsContentUtils.h" 1.19 + 1.20 +using namespace mozilla::ipc; 1.21 +using mozilla::dom::TabParent; 1.22 + 1.23 +#if defined(PR_LOGGING) 1.24 +// 1.25 +// To enable logging (see prlog.h for full details): 1.26 +// 1.27 +// set NSPR_LOG_MODULES=nsOfflineCacheUpdate:5 1.28 +// set NSPR_LOG_FILE=offlineupdate.log 1.29 +// 1.30 +// this enables PR_LOG_ALWAYS level information and places all output in 1.31 +// the file offlineupdate.log 1.32 +// 1.33 +extern PRLogModuleInfo *gOfflineCacheUpdateLog; 1.34 +#endif 1.35 + 1.36 +#undef LOG 1.37 +#define LOG(args) PR_LOG(gOfflineCacheUpdateLog, 4, args) 1.38 + 1.39 +#undef LOG_ENABLED 1.40 +#define LOG_ENABLED() PR_LOG_TEST(gOfflineCacheUpdateLog, 4) 1.41 + 1.42 +namespace mozilla { 1.43 +namespace docshell { 1.44 + 1.45 +//----------------------------------------------------------------------------- 1.46 +// OfflineCacheUpdateParent::nsISupports 1.47 +//----------------------------------------------------------------------------- 1.48 + 1.49 +NS_IMPL_ISUPPORTS(OfflineCacheUpdateParent, 1.50 + nsIOfflineCacheUpdateObserver, 1.51 + nsILoadContext) 1.52 + 1.53 +//----------------------------------------------------------------------------- 1.54 +// OfflineCacheUpdateParent <public> 1.55 +//----------------------------------------------------------------------------- 1.56 + 1.57 +OfflineCacheUpdateParent::OfflineCacheUpdateParent(uint32_t aAppId, 1.58 + bool aIsInBrowser) 1.59 + : mIPCClosed(false) 1.60 + , mIsInBrowserElement(aIsInBrowser) 1.61 + , mAppId(aAppId) 1.62 +{ 1.63 + // Make sure the service has been initialized 1.64 + nsOfflineCacheUpdateService::EnsureService(); 1.65 + 1.66 + LOG(("OfflineCacheUpdateParent::OfflineCacheUpdateParent [%p]", this)); 1.67 +} 1.68 + 1.69 +OfflineCacheUpdateParent::~OfflineCacheUpdateParent() 1.70 +{ 1.71 + LOG(("OfflineCacheUpdateParent::~OfflineCacheUpdateParent [%p]", this)); 1.72 +} 1.73 + 1.74 +void 1.75 +OfflineCacheUpdateParent::ActorDestroy(ActorDestroyReason why) 1.76 +{ 1.77 + mIPCClosed = true; 1.78 +} 1.79 + 1.80 +nsresult 1.81 +OfflineCacheUpdateParent::Schedule(const URIParams& aManifestURI, 1.82 + const URIParams& aDocumentURI, 1.83 + const bool& stickDocument) 1.84 +{ 1.85 + LOG(("OfflineCacheUpdateParent::RecvSchedule [%p]", this)); 1.86 + 1.87 + nsRefPtr<nsOfflineCacheUpdate> update; 1.88 + nsCOMPtr<nsIURI> manifestURI = DeserializeURI(aManifestURI); 1.89 + if (!manifestURI) 1.90 + return NS_ERROR_FAILURE; 1.91 + 1.92 + nsOfflineCacheUpdateService* service = 1.93 + nsOfflineCacheUpdateService::EnsureService(); 1.94 + if (!service) 1.95 + return NS_ERROR_FAILURE; 1.96 + 1.97 + bool offlinePermissionAllowed = false; 1.98 + 1.99 + nsCOMPtr<nsIPrincipal> principal; 1.100 + nsContentUtils::GetSecurityManager()-> 1.101 + GetAppCodebasePrincipal(manifestURI, mAppId, mIsInBrowserElement, 1.102 + getter_AddRefs(principal)); 1.103 + 1.104 + nsresult rv = service->OfflineAppAllowed( 1.105 + principal, nullptr, &offlinePermissionAllowed); 1.106 + NS_ENSURE_SUCCESS(rv, rv); 1.107 + 1.108 + if (!offlinePermissionAllowed) 1.109 + return NS_ERROR_DOM_SECURITY_ERR; 1.110 + 1.111 + nsCOMPtr<nsIURI> documentURI = DeserializeURI(aDocumentURI); 1.112 + if (!documentURI) 1.113 + return NS_ERROR_FAILURE; 1.114 + 1.115 + if (!NS_SecurityCompareURIs(manifestURI, documentURI, false)) 1.116 + return NS_ERROR_DOM_SECURITY_ERR; 1.117 + 1.118 + service->FindUpdate(manifestURI, mAppId, mIsInBrowserElement, 1.119 + getter_AddRefs(update)); 1.120 + if (!update) { 1.121 + update = new nsOfflineCacheUpdate(); 1.122 + 1.123 + // Leave aDocument argument null. Only glues and children keep 1.124 + // document instances. 1.125 + rv = update->Init(manifestURI, documentURI, nullptr, nullptr, 1.126 + mAppId, mIsInBrowserElement); 1.127 + NS_ENSURE_SUCCESS(rv, rv); 1.128 + 1.129 + rv = update->Schedule(); 1.130 + NS_ENSURE_SUCCESS(rv, rv); 1.131 + } 1.132 + 1.133 + update->AddObserver(this, false); 1.134 + 1.135 + if (stickDocument) { 1.136 + nsCOMPtr<nsIURI> stickURI; 1.137 + documentURI->Clone(getter_AddRefs(stickURI)); 1.138 + update->StickDocument(stickURI); 1.139 + } 1.140 + 1.141 + return NS_OK; 1.142 +} 1.143 + 1.144 +NS_IMETHODIMP 1.145 +OfflineCacheUpdateParent::UpdateStateChanged(nsIOfflineCacheUpdate *aUpdate, uint32_t state) 1.146 +{ 1.147 + if (mIPCClosed) 1.148 + return NS_ERROR_UNEXPECTED; 1.149 + 1.150 + LOG(("OfflineCacheUpdateParent::StateEvent [%p]", this)); 1.151 + 1.152 + uint64_t byteProgress; 1.153 + aUpdate->GetByteProgress(&byteProgress); 1.154 + unused << SendNotifyStateEvent(state, byteProgress); 1.155 + 1.156 + if (state == nsIOfflineCacheUpdateObserver::STATE_FINISHED) { 1.157 + // Tell the child the particulars after the update has finished. 1.158 + // Sending the Finish event will release the child side of the protocol 1.159 + // and notify "offline-cache-update-completed" on the child process. 1.160 + bool isUpgrade; 1.161 + aUpdate->GetIsUpgrade(&isUpgrade); 1.162 + bool succeeded; 1.163 + aUpdate->GetSucceeded(&succeeded); 1.164 + 1.165 + unused << SendFinish(succeeded, isUpgrade); 1.166 + } 1.167 + 1.168 + return NS_OK; 1.169 +} 1.170 + 1.171 +NS_IMETHODIMP 1.172 +OfflineCacheUpdateParent::ApplicationCacheAvailable(nsIApplicationCache *aApplicationCache) 1.173 +{ 1.174 + if (mIPCClosed) 1.175 + return NS_ERROR_UNEXPECTED; 1.176 + 1.177 + NS_ENSURE_ARG(aApplicationCache); 1.178 + 1.179 + nsCString cacheClientId; 1.180 + aApplicationCache->GetClientID(cacheClientId); 1.181 + nsCString cacheGroupId; 1.182 + aApplicationCache->GetGroupID(cacheGroupId); 1.183 + 1.184 + unused << SendAssociateDocuments(cacheGroupId, cacheClientId); 1.185 + return NS_OK; 1.186 +} 1.187 + 1.188 +//----------------------------------------------------------------------------- 1.189 +// OfflineCacheUpdateParent::nsILoadContext 1.190 +//----------------------------------------------------------------------------- 1.191 + 1.192 +NS_IMETHODIMP 1.193 +OfflineCacheUpdateParent::GetAssociatedWindow(nsIDOMWindow * *aAssociatedWindow) 1.194 +{ 1.195 + return NS_ERROR_NOT_IMPLEMENTED; 1.196 +} 1.197 + 1.198 +NS_IMETHODIMP 1.199 +OfflineCacheUpdateParent::GetTopWindow(nsIDOMWindow * *aTopWindow) 1.200 +{ 1.201 + return NS_ERROR_NOT_IMPLEMENTED; 1.202 +} 1.203 + 1.204 +NS_IMETHODIMP 1.205 +OfflineCacheUpdateParent::GetTopFrameElement(nsIDOMElement** aElement) 1.206 +{ 1.207 + return NS_ERROR_NOT_IMPLEMENTED; 1.208 +} 1.209 + 1.210 +NS_IMETHODIMP 1.211 +OfflineCacheUpdateParent::IsAppOfType(uint32_t appType, bool *_retval) 1.212 +{ 1.213 + return NS_ERROR_NOT_IMPLEMENTED; 1.214 +} 1.215 + 1.216 +NS_IMETHODIMP 1.217 +OfflineCacheUpdateParent::GetIsContent(bool *aIsContent) 1.218 +{ 1.219 + return NS_ERROR_NOT_IMPLEMENTED; 1.220 +} 1.221 + 1.222 +NS_IMETHODIMP 1.223 +OfflineCacheUpdateParent::GetUsePrivateBrowsing(bool *aUsePrivateBrowsing) 1.224 +{ 1.225 + return NS_ERROR_NOT_IMPLEMENTED; 1.226 +} 1.227 +NS_IMETHODIMP 1.228 +OfflineCacheUpdateParent::SetUsePrivateBrowsing(bool aUsePrivateBrowsing) 1.229 +{ 1.230 + return NS_ERROR_NOT_IMPLEMENTED; 1.231 +} 1.232 + 1.233 +NS_IMETHODIMP 1.234 +OfflineCacheUpdateParent::SetPrivateBrowsing(bool aUsePrivateBrowsing) 1.235 +{ 1.236 + return NS_ERROR_NOT_IMPLEMENTED; 1.237 +} 1.238 + 1.239 +NS_IMETHODIMP 1.240 +OfflineCacheUpdateParent::GetUseRemoteTabs(bool *aUseRemoteTabs) 1.241 +{ 1.242 + return NS_ERROR_NOT_IMPLEMENTED; 1.243 +} 1.244 + 1.245 +NS_IMETHODIMP 1.246 +OfflineCacheUpdateParent::SetRemoteTabs(bool aUseRemoteTabs) 1.247 +{ 1.248 + return NS_ERROR_NOT_IMPLEMENTED; 1.249 +} 1.250 + 1.251 +NS_IMETHODIMP 1.252 +OfflineCacheUpdateParent::GetIsInBrowserElement(bool *aIsInBrowserElement) 1.253 +{ 1.254 + *aIsInBrowserElement = mIsInBrowserElement; 1.255 + return NS_OK; 1.256 +} 1.257 + 1.258 +NS_IMETHODIMP 1.259 +OfflineCacheUpdateParent::GetAppId(uint32_t *aAppId) 1.260 +{ 1.261 + *aAppId = mAppId; 1.262 + return NS_OK; 1.263 +} 1.264 + 1.265 +} // docshell 1.266 +} // mozilla