2015-03-02 16:20:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/cache/AutoUtils.h"
|
|
|
|
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-04-29 18:59:43 +03:00
|
|
|
#include "mozilla/dom/InternalHeaders.h"
|
|
|
|
#include "mozilla/dom/InternalRequest.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/CacheParent.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/CacheStreamControlParent.h"
|
|
|
|
#include "mozilla/dom/cache/ReadStream.h"
|
|
|
|
#include "mozilla/dom/cache/SavedTypes.h"
|
|
|
|
#include "mozilla/dom/cache/StreamList.h"
|
|
|
|
#include "mozilla/dom/cache/TypeUtils.h"
|
2016-05-15 20:32:09 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/ipc/PBackgroundParent.h"
|
2015-04-29 18:59:43 +03:00
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsHttp.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2015-04-16 22:00:15 +03:00
|
|
|
using mozilla::dom::cache::CacheReadStream;
|
|
|
|
using mozilla::dom::cache::CacheReadStreamOrVoid;
|
2016-05-15 20:32:09 +03:00
|
|
|
using mozilla::ipc::AutoIPCStream;
|
|
|
|
using mozilla::ipc::PBackgroundParent;
|
|
|
|
|
|
|
|
namespace {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
enum CleanupAction
|
|
|
|
{
|
2015-03-22 09:52:12 +03:00
|
|
|
Forget,
|
|
|
|
Delete
|
2015-03-02 16:20:00 +03:00
|
|
|
};
|
|
|
|
|
2015-03-22 09:52:12 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupChild(CacheReadStream& aReadStream, CleanupAction aAction)
|
2015-03-22 09:52:12 +03:00
|
|
|
{
|
2016-05-15 20:32:09 +03:00
|
|
|
// fds cleaned up by mStreamCleanupList
|
2017-03-14 14:28:58 +03:00
|
|
|
// PChildToParentStream actors cleaned up by mStreamCleanupList
|
2015-03-22 09:52:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupChild(CacheReadStreamOrVoid& aReadStreamOrVoid, CleanupAction aAction)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:15 +03:00
|
|
|
if (aReadStreamOrVoid.type() == CacheReadStreamOrVoid::Tvoid_t) {
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupChild(aReadStreamOrVoid.get_CacheReadStream(), aAction);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// --------------------------------------------
|
|
|
|
|
|
|
|
AutoChildOpArgs::AutoChildOpArgs(TypeUtils* aTypeUtils,
|
2016-05-15 20:32:09 +03:00
|
|
|
const CacheOpArgs& aOpArgs,
|
|
|
|
uint32_t aEntryCount)
|
2015-03-02 16:20:00 +03:00
|
|
|
: mTypeUtils(aTypeUtils)
|
2015-04-16 22:00:15 +03:00
|
|
|
, mOpArgs(aOpArgs)
|
2015-03-02 16:20:00 +03:00
|
|
|
, mSent(false)
|
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mTypeUtils);
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aEntryCount != 0);
|
|
|
|
// We are using AutoIPCStream objects to cleanup target IPCStream
|
|
|
|
// structures embedded in our CacheOpArgs. These IPCStream structs
|
|
|
|
// must not move once we attach our AutoIPCStream to them. Therefore,
|
|
|
|
// its important that any arrays containing streams are pre-sized for
|
|
|
|
// the number of entries we have in order to avoid realloc moving
|
|
|
|
// things around on us.
|
|
|
|
if (mOpArgs.type() == CacheOpArgs::TCachePutAllArgs) {
|
|
|
|
CachePutAllArgs& args = mOpArgs.get_CachePutAllArgs();
|
|
|
|
args.requestResponseList().SetCapacity(aEntryCount);
|
|
|
|
} else {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aEntryCount == 1);
|
2016-05-15 20:32:09 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoChildOpArgs::~AutoChildOpArgs()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupAction action = mSent ? Forget : Delete;
|
2015-04-15 10:55:53 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
switch(mOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TCacheMatchArgs:
|
|
|
|
{
|
|
|
|
CacheMatchArgs& args = mOpArgs.get_CacheMatchArgs();
|
|
|
|
CleanupChild(args.request().body(), action);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheMatchAllArgs:
|
|
|
|
{
|
|
|
|
CacheMatchAllArgs& args = mOpArgs.get_CacheMatchAllArgs();
|
2015-04-16 22:00:15 +03:00
|
|
|
if (args.requestOrVoid().type() == CacheRequestOrVoid::Tvoid_t) {
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupChild(args.requestOrVoid().get_CacheRequest().body(), action);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCachePutAllArgs:
|
|
|
|
{
|
|
|
|
CachePutAllArgs& args = mOpArgs.get_CachePutAllArgs();
|
|
|
|
auto& list = args.requestResponseList();
|
|
|
|
for (uint32_t i = 0; i < list.Length(); ++i) {
|
|
|
|
CleanupChild(list[i].request().body(), action);
|
|
|
|
CleanupChild(list[i].response().body(), action);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheDeleteArgs:
|
|
|
|
{
|
|
|
|
CacheDeleteArgs& args = mOpArgs.get_CacheDeleteArgs();
|
|
|
|
CleanupChild(args.request().body(), action);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheKeysArgs:
|
|
|
|
{
|
|
|
|
CacheKeysArgs& args = mOpArgs.get_CacheKeysArgs();
|
2015-04-16 22:00:15 +03:00
|
|
|
if (args.requestOrVoid().type() == CacheRequestOrVoid::Tvoid_t) {
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
CleanupChild(args.requestOrVoid().get_CacheRequest().body(), action);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TStorageMatchArgs:
|
|
|
|
{
|
|
|
|
StorageMatchArgs& args = mOpArgs.get_StorageMatchArgs();
|
|
|
|
CleanupChild(args.request().body(), action);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Other types do not need cleanup
|
|
|
|
break;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
2016-05-15 20:32:09 +03:00
|
|
|
|
|
|
|
mStreamCleanupList.Clear();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoChildOpArgs::Add(InternalRequest* aRequest, BodyAction aBodyAction,
|
2015-04-29 18:59:43 +03:00
|
|
|
SchemeAction aSchemeAction, ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
switch(mOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TCacheMatchArgs:
|
|
|
|
{
|
|
|
|
CacheMatchArgs& args = mOpArgs.get_CacheMatchArgs();
|
2015-04-16 22:00:15 +03:00
|
|
|
mTypeUtils->ToCacheRequest(args.request(), aRequest, aBodyAction,
|
2016-05-15 20:32:09 +03:00
|
|
|
aSchemeAction, mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheMatchAllArgs:
|
|
|
|
{
|
|
|
|
CacheMatchAllArgs& args = mOpArgs.get_CacheMatchAllArgs();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(args.requestOrVoid().type() == CacheRequestOrVoid::Tvoid_t);
|
2015-04-16 22:00:15 +03:00
|
|
|
args.requestOrVoid() = CacheRequest();
|
|
|
|
mTypeUtils->ToCacheRequest(args.requestOrVoid().get_CacheRequest(),
|
2016-05-15 20:32:09 +03:00
|
|
|
aRequest, aBodyAction, aSchemeAction,
|
|
|
|
mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheDeleteArgs:
|
|
|
|
{
|
|
|
|
CacheDeleteArgs& args = mOpArgs.get_CacheDeleteArgs();
|
2015-04-16 22:00:15 +03:00
|
|
|
mTypeUtils->ToCacheRequest(args.request(), aRequest, aBodyAction,
|
2016-05-15 20:32:09 +03:00
|
|
|
aSchemeAction, mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheKeysArgs:
|
|
|
|
{
|
|
|
|
CacheKeysArgs& args = mOpArgs.get_CacheKeysArgs();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(args.requestOrVoid().type() == CacheRequestOrVoid::Tvoid_t);
|
2015-04-16 22:00:15 +03:00
|
|
|
args.requestOrVoid() = CacheRequest();
|
|
|
|
mTypeUtils->ToCacheRequest(args.requestOrVoid().get_CacheRequest(),
|
2016-05-15 20:32:09 +03:00
|
|
|
aRequest, aBodyAction, aSchemeAction,
|
|
|
|
mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TStorageMatchArgs:
|
|
|
|
{
|
|
|
|
StorageMatchArgs& args = mOpArgs.get_StorageMatchArgs();
|
2015-04-16 22:00:15 +03:00
|
|
|
mTypeUtils->ToCacheRequest(args.request(), aRequest, aBodyAction,
|
2016-05-15 20:32:09 +03:00
|
|
|
aSchemeAction, mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Cache args type cannot send a Request!");
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool
|
|
|
|
MatchInPutList(InternalRequest* aRequest,
|
|
|
|
const nsTArray<CacheRequestResponse>& aPutList)
|
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRequest);
|
2015-04-29 18:59:43 +03:00
|
|
|
|
|
|
|
// This method implements the SW spec QueryCache algorithm against an
|
|
|
|
// in memory array of Request/Response objects. This essentially the
|
|
|
|
// same algorithm that is implemented in DBSchema.cpp. Unfortunately
|
|
|
|
// we cannot unify them because when operating against the real database
|
|
|
|
// we don't want to load all request/response objects into memory.
|
|
|
|
|
|
|
|
// Note, we can skip the check for a invalid request method because
|
|
|
|
// Cache should only call into here with a GET or HEAD.
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoCString method;
|
|
|
|
aRequest->GetMethod(method);
|
|
|
|
MOZ_ASSERT(method.LowerCaseEqualsLiteral("get") ||
|
|
|
|
method.LowerCaseEqualsLiteral("head"));
|
|
|
|
#endif
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> requestHeaders = aRequest->Headers();
|
2015-04-29 18:59:43 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aPutList.Length(); ++i) {
|
|
|
|
const CacheRequest& cachedRequest = aPutList[i].request();
|
|
|
|
const CacheResponse& cachedResponse = aPutList[i].response();
|
|
|
|
|
|
|
|
nsAutoCString url;
|
|
|
|
aRequest->GetURL(url);
|
|
|
|
|
2015-06-17 03:39:05 +03:00
|
|
|
nsAutoCString requestUrl(cachedRequest.urlWithoutQuery());
|
|
|
|
requestUrl.Append(cachedRequest.urlQuery());
|
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
// If the URLs don't match, then just skip to the next entry.
|
2015-06-17 03:39:05 +03:00
|
|
|
if (url != requestUrl) {
|
2015-04-29 18:59:43 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> cachedRequestHeaders =
|
2015-04-29 18:59:43 +03:00
|
|
|
TypeUtils::ToInternalHeaders(cachedRequest.headers());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<InternalHeaders> cachedResponseHeaders =
|
2015-04-29 18:59:43 +03:00
|
|
|
TypeUtils::ToInternalHeaders(cachedResponse.headers());
|
|
|
|
|
2016-10-10 20:20:14 +03:00
|
|
|
nsCString varyHeaders;
|
2015-04-29 18:59:43 +03:00
|
|
|
ErrorResult rv;
|
2016-10-10 20:20:14 +03:00
|
|
|
cachedResponseHeaders->Get(NS_LITERAL_CSTRING("vary"), varyHeaders, rv);
|
2015-04-29 18:59:43 +03:00
|
|
|
MOZ_ALWAYS_TRUE(!rv.Failed());
|
|
|
|
|
|
|
|
// Assume the vary headers match until we find a conflict
|
|
|
|
bool varyHeadersMatch = true;
|
|
|
|
|
2016-10-10 20:20:14 +03:00
|
|
|
char* rawBuffer = varyHeaders.BeginWriting();
|
|
|
|
char* token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer);
|
|
|
|
for (; token;
|
|
|
|
token = nsCRT::strtok(rawBuffer, NS_HTTP_HEADER_SEPS, &rawBuffer)) {
|
|
|
|
nsDependentCString header(token);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!header.EqualsLiteral("*"),
|
|
|
|
"We should have already caught this in "
|
|
|
|
"TypeUtils::ToPCacheResponseWithoutBody()");
|
2016-10-10 20:20:14 +03:00
|
|
|
|
|
|
|
ErrorResult headerRv;
|
|
|
|
nsAutoCString value;
|
|
|
|
requestHeaders->Get(header, value, headerRv);
|
|
|
|
if (NS_WARN_IF(headerRv.Failed())) {
|
|
|
|
headerRv.SuppressException();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(value.IsEmpty());
|
2015-04-29 18:59:43 +03:00
|
|
|
}
|
|
|
|
|
2016-10-10 20:20:14 +03:00
|
|
|
nsAutoCString cachedValue;
|
|
|
|
cachedRequestHeaders->Get(header, cachedValue, headerRv);
|
|
|
|
if (NS_WARN_IF(headerRv.Failed())) {
|
|
|
|
headerRv.SuppressException();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(cachedValue.IsEmpty());
|
2016-10-10 20:20:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value != cachedValue) {
|
|
|
|
varyHeadersMatch = false;
|
2015-04-29 18:59:43 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL was equal and all vary headers match!
|
|
|
|
if (varyHeadersMatch) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-29 18:59:43 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
void
|
2017-08-11 04:04:55 +03:00
|
|
|
AutoChildOpArgs::Add(JSContext* aCx, InternalRequest* aRequest,
|
|
|
|
BodyAction aBodyAction, SchemeAction aSchemeAction,
|
|
|
|
Response& aResponse, ErrorResult& aRv)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
switch(mOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TCachePutAllArgs:
|
|
|
|
{
|
|
|
|
CachePutAllArgs& args = mOpArgs.get_CachePutAllArgs();
|
|
|
|
|
2015-04-29 18:59:43 +03:00
|
|
|
// Throw an error if a request/response pair would mask another
|
|
|
|
// request/response pair in the same PutAll operation. This is
|
|
|
|
// step 2.3.2.3 from the "Batch Cache Operations" spec algorithm.
|
|
|
|
if (MatchInPutList(aRequest, args.requestResponseList())) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
// Ensure that we don't realloc the array since this can result
|
|
|
|
// in our AutoIPCStream objects to reference the wrong memory
|
|
|
|
// location. This should never happen and is a UAF if it does.
|
|
|
|
// Therefore make this a release assertion.
|
|
|
|
MOZ_RELEASE_ASSERT(args.requestResponseList().Length() <
|
|
|
|
args.requestResponseList().Capacity());
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// The FileDescriptorSetChild asserts in its destructor that all fds have
|
|
|
|
// been removed. The copy constructor, however, simply duplicates the
|
|
|
|
// fds without removing any. This means each temporary and copy must be
|
|
|
|
// explicitly cleaned up.
|
|
|
|
//
|
|
|
|
// Avoid a lot of this hassle by making sure we only create one here. On
|
|
|
|
// error we remove it.
|
|
|
|
CacheRequestResponse& pair = *args.requestResponseList().AppendElement();
|
|
|
|
pair.request().body() = void_t();
|
|
|
|
pair.response().body() = void_t();
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
mTypeUtils->ToCacheRequest(pair.request(), aRequest, aBodyAction,
|
2016-05-15 20:32:09 +03:00
|
|
|
aSchemeAction, mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
if (!aRv.Failed()) {
|
2017-08-11 04:04:55 +03:00
|
|
|
mTypeUtils->ToCacheResponse(aCx, pair.response(), aResponse,
|
2016-05-15 20:32:09 +03:00
|
|
|
mStreamCleanupList, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
CleanupChild(pair.request().body(), Delete);
|
|
|
|
args.requestResponseList().RemoveElementAt(
|
|
|
|
args.requestResponseList().Length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Cache args type cannot send a Request/Response pair!");
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheOpArgs&
|
|
|
|
AutoChildOpArgs::SendAsOpArgs()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-03-02 16:20:00 +03:00
|
|
|
mSent = true;
|
2016-05-15 20:32:09 +03:00
|
|
|
for (UniquePtr<AutoIPCStream>& autoStream : mStreamCleanupList) {
|
|
|
|
autoStream->TakeValue();
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
return mOpArgs;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoParentOpResult::AutoParentOpResult(mozilla::ipc::PBackgroundParent* aManager,
|
2016-05-15 20:32:09 +03:00
|
|
|
const CacheOpResult& aOpResult,
|
|
|
|
uint32_t aEntryCount)
|
2015-04-16 22:00:15 +03:00
|
|
|
: mManager(aManager)
|
|
|
|
, mOpResult(aOpResult)
|
|
|
|
, mStreamControl(nullptr)
|
|
|
|
, mSent(false)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mManager);
|
2016-05-15 20:32:09 +03:00
|
|
|
MOZ_RELEASE_ASSERT(aEntryCount != 0);
|
|
|
|
// We are using AutoIPCStream objects to cleanup target IPCStream
|
|
|
|
// structures embedded in our CacheOpArgs. These IPCStream structs
|
|
|
|
// must not move once we attach our AutoIPCStream to them. Therefore,
|
|
|
|
// its important that any arrays containing streams are pre-sized for
|
|
|
|
// the number of entries we have in order to avoid realloc moving
|
|
|
|
// things around on us.
|
|
|
|
if (mOpResult.type() == CacheOpResult::TCacheMatchAllResult) {
|
|
|
|
CacheMatchAllResult& result = mOpResult.get_CacheMatchAllResult();
|
|
|
|
result.responseList().SetCapacity(aEntryCount);
|
|
|
|
} else if (mOpResult.type() == CacheOpResult::TCacheKeysResult) {
|
|
|
|
CacheKeysResult& result = mOpResult.get_CacheKeysResult();
|
|
|
|
result.requestList().SetCapacity(aEntryCount);
|
|
|
|
} else {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aEntryCount == 1);
|
2016-05-15 20:32:09 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoParentOpResult::~AutoParentOpResult()
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2015-03-22 09:52:12 +03:00
|
|
|
CleanupAction action = mSent ? Forget : Delete;
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
switch (mOpResult.type()) {
|
|
|
|
case CacheOpResult::TStorageOpenResult:
|
|
|
|
{
|
|
|
|
StorageOpenResult& result = mOpResult.get_StorageOpenResult();
|
|
|
|
if (action == Forget || result.actorParent() == nullptr) {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << PCacheParent::Send__delete__(result.actorParent());
|
2015-11-26 10:26:32 +03:00
|
|
|
break;
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
default:
|
2016-05-15 20:32:09 +03:00
|
|
|
// other types do not need additional clean up
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == Delete && mStreamControl) {
|
2015-11-02 08:53:26 +03:00
|
|
|
Unused << PCacheStreamControlParent::Send__delete__(mStreamControl);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
2016-05-15 20:32:09 +03:00
|
|
|
|
|
|
|
mStreamCleanupList.Clear();
|
2015-04-14 00:05:57 +03:00
|
|
|
}
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-04-14 22:21:14 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoParentOpResult::Add(CacheId aOpenedCacheId, Manager* aManager)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mOpResult.type() == CacheOpResult::TStorageOpenResult);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mOpResult.get_StorageOpenResult().actorParent() == nullptr);
|
2015-04-16 22:00:15 +03:00
|
|
|
mOpResult.get_StorageOpenResult().actorParent() =
|
|
|
|
mManager->SendPCacheConstructor(new CacheParent(aManager, aOpenedCacheId));
|
2015-04-14 22:21:14 +03:00
|
|
|
}
|
2015-04-14 01:18:19 +03:00
|
|
|
|
2015-04-15 03:11:19 +03:00
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoParentOpResult::Add(const SavedResponse& aSavedResponse,
|
|
|
|
StreamList* aStreamList)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
switch (mOpResult.type()) {
|
|
|
|
case CacheOpResult::TCacheMatchResult:
|
|
|
|
{
|
|
|
|
CacheMatchResult& result = mOpResult.get_CacheMatchResult();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(result.responseOrVoid().type() == CacheResponseOrVoid::Tvoid_t);
|
2015-04-16 22:00:15 +03:00
|
|
|
result.responseOrVoid() = aSavedResponse.mValue;
|
|
|
|
SerializeResponseBody(aSavedResponse, aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
&result.responseOrVoid().get_CacheResponse());
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TCacheMatchAllResult:
|
|
|
|
{
|
|
|
|
CacheMatchAllResult& result = mOpResult.get_CacheMatchAllResult();
|
2016-05-15 20:32:09 +03:00
|
|
|
// Ensure that we don't realloc the array since this can result
|
|
|
|
// in our AutoIPCStream objects to reference the wrong memory
|
|
|
|
// location. This should never happen and is a UAF if it does.
|
|
|
|
// Therefore make this a release assertion.
|
|
|
|
MOZ_RELEASE_ASSERT(result.responseList().Length() <
|
|
|
|
result.responseList().Capacity());
|
2015-04-16 22:00:15 +03:00
|
|
|
result.responseList().AppendElement(aSavedResponse.mValue);
|
|
|
|
SerializeResponseBody(aSavedResponse, aStreamList,
|
|
|
|
&result.responseList().LastElement());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpResult::TStorageMatchResult:
|
|
|
|
{
|
|
|
|
StorageMatchResult& result = mOpResult.get_StorageMatchResult();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(result.responseOrVoid().type() == CacheResponseOrVoid::Tvoid_t);
|
2015-04-16 22:00:15 +03:00
|
|
|
result.responseOrVoid() = aSavedResponse.mValue;
|
|
|
|
SerializeResponseBody(aSavedResponse, aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
&result.responseOrVoid().get_CacheResponse());
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Cache result type cannot handle returning a Response!");
|
|
|
|
}
|
2015-04-15 03:11:19 +03:00
|
|
|
}
|
2015-04-15 01:27:48 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void
|
|
|
|
AutoParentOpResult::Add(const SavedRequest& aSavedRequest,
|
|
|
|
StreamList* aStreamList)
|
2015-04-14 01:18:19 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-04-14 22:21:14 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
switch (mOpResult.type()) {
|
|
|
|
case CacheOpResult::TCacheKeysResult:
|
|
|
|
{
|
|
|
|
CacheKeysResult& result = mOpResult.get_CacheKeysResult();
|
2016-05-15 20:32:09 +03:00
|
|
|
// Ensure that we don't realloc the array since this can result
|
|
|
|
// in our AutoIPCStream objects to reference the wrong memory
|
|
|
|
// location. This should never happen and is a UAF if it does.
|
|
|
|
// Therefore make this a release assertion.
|
|
|
|
MOZ_RELEASE_ASSERT(result.requestList().Length() <
|
|
|
|
result.requestList().Capacity());
|
2015-04-16 22:00:15 +03:00
|
|
|
result.requestList().AppendElement(aSavedRequest.mValue);
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheRequest& request = result.requestList().LastElement();
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
if (!aSavedRequest.mHasBodyId) {
|
|
|
|
request.body() = void_t();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
request.body() = CacheReadStream();
|
2015-04-16 22:00:15 +03:00
|
|
|
SerializeReadStream(aSavedRequest.mBodyId, aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
&request.body().get_CacheReadStream());
|
2015-04-16 22:00:15 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
MOZ_CRASH("Cache result type cannot handle returning a Request!");
|
|
|
|
}
|
|
|
|
}
|
2015-04-15 10:55:53 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
const CacheOpResult&
|
|
|
|
AutoParentOpResult::SendAsOpResult()
|
2015-04-15 01:27:48 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-04-16 22:00:15 +03:00
|
|
|
mSent = true;
|
2016-05-15 20:32:09 +03:00
|
|
|
for (UniquePtr<AutoIPCStream>& autoStream : mStreamCleanupList) {
|
|
|
|
autoStream->TakeValue();
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
return mOpResult;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
2015-04-15 03:11:19 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void
|
|
|
|
AutoParentOpResult::SerializeResponseBody(const SavedResponse& aSavedResponse,
|
|
|
|
StreamList* aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheResponse* aResponseOut)
|
2015-04-15 10:55:53 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aResponseOut);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
if (!aSavedResponse.mHasBodyId) {
|
|
|
|
aResponseOut->body() = void_t();
|
|
|
|
return;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
aResponseOut->body() = CacheReadStream();
|
2015-04-16 22:00:15 +03:00
|
|
|
SerializeReadStream(aSavedResponse.mBodyId, aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
&aResponseOut->body().get_CacheReadStream());
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-16 22:00:15 +03:00
|
|
|
AutoParentOpResult::SerializeReadStream(const nsID& aId, StreamList* aStreamList,
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheReadStream* aReadStreamOut)
|
2015-03-02 16:20:00 +03:00
|
|
|
{
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aStreamList);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aReadStreamOut);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mSent);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream = aStreamList->Extract(aId);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(stream);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
if (!mStreamControl) {
|
|
|
|
mStreamControl = static_cast<CacheStreamControlParent*>(
|
|
|
|
mManager->SendPCacheStreamControlConstructor(new CacheStreamControlParent()));
|
|
|
|
|
|
|
|
// If this failed, then the child process is gone. Warn and allow actor
|
|
|
|
// cleanup to proceed as normal.
|
|
|
|
if (!mStreamControl) {
|
|
|
|
NS_WARNING("Cache failed to create stream control actor.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aStreamList->SetStreamControl(mStreamControl);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ReadStream> readStream = ReadStream::Create(mStreamControl,
|
2016-05-15 20:32:09 +03:00
|
|
|
aId, stream);
|
2016-05-12 17:29:20 +03:00
|
|
|
ErrorResult rv;
|
2016-05-15 20:32:09 +03:00
|
|
|
readStream->Serialize(aReadStreamOut, mStreamCleanupList, rv);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!rv.Failed());
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cache
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|