uriloader/prefetch/OfflineCacheUpdateParent.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "OfflineCacheUpdateParent.h"
michael@0 7
michael@0 8 #include "mozilla/dom/TabParent.h"
michael@0 9 #include "mozilla/ipc/URIUtils.h"
michael@0 10 #include "mozilla/unused.h"
michael@0 11 #include "nsOfflineCacheUpdate.h"
michael@0 12 #include "nsIApplicationCache.h"
michael@0 13 #include "nsIScriptSecurityManager.h"
michael@0 14 #include "nsNetUtil.h"
michael@0 15 #include "nsContentUtils.h"
michael@0 16
michael@0 17 using namespace mozilla::ipc;
michael@0 18 using mozilla::dom::TabParent;
michael@0 19
michael@0 20 #if defined(PR_LOGGING)
michael@0 21 //
michael@0 22 // To enable logging (see prlog.h for full details):
michael@0 23 //
michael@0 24 // set NSPR_LOG_MODULES=nsOfflineCacheUpdate:5
michael@0 25 // set NSPR_LOG_FILE=offlineupdate.log
michael@0 26 //
michael@0 27 // this enables PR_LOG_ALWAYS level information and places all output in
michael@0 28 // the file offlineupdate.log
michael@0 29 //
michael@0 30 extern PRLogModuleInfo *gOfflineCacheUpdateLog;
michael@0 31 #endif
michael@0 32
michael@0 33 #undef LOG
michael@0 34 #define LOG(args) PR_LOG(gOfflineCacheUpdateLog, 4, args)
michael@0 35
michael@0 36 #undef LOG_ENABLED
michael@0 37 #define LOG_ENABLED() PR_LOG_TEST(gOfflineCacheUpdateLog, 4)
michael@0 38
michael@0 39 namespace mozilla {
michael@0 40 namespace docshell {
michael@0 41
michael@0 42 //-----------------------------------------------------------------------------
michael@0 43 // OfflineCacheUpdateParent::nsISupports
michael@0 44 //-----------------------------------------------------------------------------
michael@0 45
michael@0 46 NS_IMPL_ISUPPORTS(OfflineCacheUpdateParent,
michael@0 47 nsIOfflineCacheUpdateObserver,
michael@0 48 nsILoadContext)
michael@0 49
michael@0 50 //-----------------------------------------------------------------------------
michael@0 51 // OfflineCacheUpdateParent <public>
michael@0 52 //-----------------------------------------------------------------------------
michael@0 53
michael@0 54 OfflineCacheUpdateParent::OfflineCacheUpdateParent(uint32_t aAppId,
michael@0 55 bool aIsInBrowser)
michael@0 56 : mIPCClosed(false)
michael@0 57 , mIsInBrowserElement(aIsInBrowser)
michael@0 58 , mAppId(aAppId)
michael@0 59 {
michael@0 60 // Make sure the service has been initialized
michael@0 61 nsOfflineCacheUpdateService::EnsureService();
michael@0 62
michael@0 63 LOG(("OfflineCacheUpdateParent::OfflineCacheUpdateParent [%p]", this));
michael@0 64 }
michael@0 65
michael@0 66 OfflineCacheUpdateParent::~OfflineCacheUpdateParent()
michael@0 67 {
michael@0 68 LOG(("OfflineCacheUpdateParent::~OfflineCacheUpdateParent [%p]", this));
michael@0 69 }
michael@0 70
michael@0 71 void
michael@0 72 OfflineCacheUpdateParent::ActorDestroy(ActorDestroyReason why)
michael@0 73 {
michael@0 74 mIPCClosed = true;
michael@0 75 }
michael@0 76
michael@0 77 nsresult
michael@0 78 OfflineCacheUpdateParent::Schedule(const URIParams& aManifestURI,
michael@0 79 const URIParams& aDocumentURI,
michael@0 80 const bool& stickDocument)
michael@0 81 {
michael@0 82 LOG(("OfflineCacheUpdateParent::RecvSchedule [%p]", this));
michael@0 83
michael@0 84 nsRefPtr<nsOfflineCacheUpdate> update;
michael@0 85 nsCOMPtr<nsIURI> manifestURI = DeserializeURI(aManifestURI);
michael@0 86 if (!manifestURI)
michael@0 87 return NS_ERROR_FAILURE;
michael@0 88
michael@0 89 nsOfflineCacheUpdateService* service =
michael@0 90 nsOfflineCacheUpdateService::EnsureService();
michael@0 91 if (!service)
michael@0 92 return NS_ERROR_FAILURE;
michael@0 93
michael@0 94 bool offlinePermissionAllowed = false;
michael@0 95
michael@0 96 nsCOMPtr<nsIPrincipal> principal;
michael@0 97 nsContentUtils::GetSecurityManager()->
michael@0 98 GetAppCodebasePrincipal(manifestURI, mAppId, mIsInBrowserElement,
michael@0 99 getter_AddRefs(principal));
michael@0 100
michael@0 101 nsresult rv = service->OfflineAppAllowed(
michael@0 102 principal, nullptr, &offlinePermissionAllowed);
michael@0 103 NS_ENSURE_SUCCESS(rv, rv);
michael@0 104
michael@0 105 if (!offlinePermissionAllowed)
michael@0 106 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 107
michael@0 108 nsCOMPtr<nsIURI> documentURI = DeserializeURI(aDocumentURI);
michael@0 109 if (!documentURI)
michael@0 110 return NS_ERROR_FAILURE;
michael@0 111
michael@0 112 if (!NS_SecurityCompareURIs(manifestURI, documentURI, false))
michael@0 113 return NS_ERROR_DOM_SECURITY_ERR;
michael@0 114
michael@0 115 service->FindUpdate(manifestURI, mAppId, mIsInBrowserElement,
michael@0 116 getter_AddRefs(update));
michael@0 117 if (!update) {
michael@0 118 update = new nsOfflineCacheUpdate();
michael@0 119
michael@0 120 // Leave aDocument argument null. Only glues and children keep
michael@0 121 // document instances.
michael@0 122 rv = update->Init(manifestURI, documentURI, nullptr, nullptr,
michael@0 123 mAppId, mIsInBrowserElement);
michael@0 124 NS_ENSURE_SUCCESS(rv, rv);
michael@0 125
michael@0 126 rv = update->Schedule();
michael@0 127 NS_ENSURE_SUCCESS(rv, rv);
michael@0 128 }
michael@0 129
michael@0 130 update->AddObserver(this, false);
michael@0 131
michael@0 132 if (stickDocument) {
michael@0 133 nsCOMPtr<nsIURI> stickURI;
michael@0 134 documentURI->Clone(getter_AddRefs(stickURI));
michael@0 135 update->StickDocument(stickURI);
michael@0 136 }
michael@0 137
michael@0 138 return NS_OK;
michael@0 139 }
michael@0 140
michael@0 141 NS_IMETHODIMP
michael@0 142 OfflineCacheUpdateParent::UpdateStateChanged(nsIOfflineCacheUpdate *aUpdate, uint32_t state)
michael@0 143 {
michael@0 144 if (mIPCClosed)
michael@0 145 return NS_ERROR_UNEXPECTED;
michael@0 146
michael@0 147 LOG(("OfflineCacheUpdateParent::StateEvent [%p]", this));
michael@0 148
michael@0 149 uint64_t byteProgress;
michael@0 150 aUpdate->GetByteProgress(&byteProgress);
michael@0 151 unused << SendNotifyStateEvent(state, byteProgress);
michael@0 152
michael@0 153 if (state == nsIOfflineCacheUpdateObserver::STATE_FINISHED) {
michael@0 154 // Tell the child the particulars after the update has finished.
michael@0 155 // Sending the Finish event will release the child side of the protocol
michael@0 156 // and notify "offline-cache-update-completed" on the child process.
michael@0 157 bool isUpgrade;
michael@0 158 aUpdate->GetIsUpgrade(&isUpgrade);
michael@0 159 bool succeeded;
michael@0 160 aUpdate->GetSucceeded(&succeeded);
michael@0 161
michael@0 162 unused << SendFinish(succeeded, isUpgrade);
michael@0 163 }
michael@0 164
michael@0 165 return NS_OK;
michael@0 166 }
michael@0 167
michael@0 168 NS_IMETHODIMP
michael@0 169 OfflineCacheUpdateParent::ApplicationCacheAvailable(nsIApplicationCache *aApplicationCache)
michael@0 170 {
michael@0 171 if (mIPCClosed)
michael@0 172 return NS_ERROR_UNEXPECTED;
michael@0 173
michael@0 174 NS_ENSURE_ARG(aApplicationCache);
michael@0 175
michael@0 176 nsCString cacheClientId;
michael@0 177 aApplicationCache->GetClientID(cacheClientId);
michael@0 178 nsCString cacheGroupId;
michael@0 179 aApplicationCache->GetGroupID(cacheGroupId);
michael@0 180
michael@0 181 unused << SendAssociateDocuments(cacheGroupId, cacheClientId);
michael@0 182 return NS_OK;
michael@0 183 }
michael@0 184
michael@0 185 //-----------------------------------------------------------------------------
michael@0 186 // OfflineCacheUpdateParent::nsILoadContext
michael@0 187 //-----------------------------------------------------------------------------
michael@0 188
michael@0 189 NS_IMETHODIMP
michael@0 190 OfflineCacheUpdateParent::GetAssociatedWindow(nsIDOMWindow * *aAssociatedWindow)
michael@0 191 {
michael@0 192 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 193 }
michael@0 194
michael@0 195 NS_IMETHODIMP
michael@0 196 OfflineCacheUpdateParent::GetTopWindow(nsIDOMWindow * *aTopWindow)
michael@0 197 {
michael@0 198 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 199 }
michael@0 200
michael@0 201 NS_IMETHODIMP
michael@0 202 OfflineCacheUpdateParent::GetTopFrameElement(nsIDOMElement** aElement)
michael@0 203 {
michael@0 204 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 205 }
michael@0 206
michael@0 207 NS_IMETHODIMP
michael@0 208 OfflineCacheUpdateParent::IsAppOfType(uint32_t appType, bool *_retval)
michael@0 209 {
michael@0 210 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 211 }
michael@0 212
michael@0 213 NS_IMETHODIMP
michael@0 214 OfflineCacheUpdateParent::GetIsContent(bool *aIsContent)
michael@0 215 {
michael@0 216 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 217 }
michael@0 218
michael@0 219 NS_IMETHODIMP
michael@0 220 OfflineCacheUpdateParent::GetUsePrivateBrowsing(bool *aUsePrivateBrowsing)
michael@0 221 {
michael@0 222 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 223 }
michael@0 224 NS_IMETHODIMP
michael@0 225 OfflineCacheUpdateParent::SetUsePrivateBrowsing(bool aUsePrivateBrowsing)
michael@0 226 {
michael@0 227 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 228 }
michael@0 229
michael@0 230 NS_IMETHODIMP
michael@0 231 OfflineCacheUpdateParent::SetPrivateBrowsing(bool aUsePrivateBrowsing)
michael@0 232 {
michael@0 233 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 234 }
michael@0 235
michael@0 236 NS_IMETHODIMP
michael@0 237 OfflineCacheUpdateParent::GetUseRemoteTabs(bool *aUseRemoteTabs)
michael@0 238 {
michael@0 239 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 240 }
michael@0 241
michael@0 242 NS_IMETHODIMP
michael@0 243 OfflineCacheUpdateParent::SetRemoteTabs(bool aUseRemoteTabs)
michael@0 244 {
michael@0 245 return NS_ERROR_NOT_IMPLEMENTED;
michael@0 246 }
michael@0 247
michael@0 248 NS_IMETHODIMP
michael@0 249 OfflineCacheUpdateParent::GetIsInBrowserElement(bool *aIsInBrowserElement)
michael@0 250 {
michael@0 251 *aIsInBrowserElement = mIsInBrowserElement;
michael@0 252 return NS_OK;
michael@0 253 }
michael@0 254
michael@0 255 NS_IMETHODIMP
michael@0 256 OfflineCacheUpdateParent::GetAppId(uint32_t *aAppId)
michael@0 257 {
michael@0 258 *aAppId = mAppId;
michael@0 259 return NS_OK;
michael@0 260 }
michael@0 261
michael@0 262 } // docshell
michael@0 263 } // mozilla

mercurial