Bug 1890793: Assert CallArgs::newTarget is not gray. r=spidermonkey-reviewers,sfink...
[gecko.git] / dom / fetch / ChannelInfo.cpp
blob71b9e3277b43a1953bdaabf9a6eb0717b64c7b39
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ChannelInfo.h"
8 #include "nsCOMPtr.h"
9 #include "nsContentUtils.h"
10 #include "nsIChannel.h"
11 #include "mozilla/dom/Document.h"
12 #include "nsIGlobalObject.h"
13 #include "nsIHttpChannel.h"
14 #include "nsSerializationHelper.h"
15 #include "mozilla/BasePrincipal.h"
16 #include "mozilla/net/HttpBaseChannel.h"
17 #include "nsNetUtil.h"
19 using namespace mozilla;
20 using namespace mozilla::dom;
22 void ChannelInfo::InitFromDocument(Document* aDoc) {
23 MOZ_ASSERT(NS_IsMainThread());
24 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
26 nsCOMPtr<nsITransportSecurityInfo> securityInfo(aDoc->GetSecurityInfo());
27 if (securityInfo) {
28 SetSecurityInfo(securityInfo);
31 mInited = true;
34 void ChannelInfo::InitFromChannel(nsIChannel* aChannel) {
35 MOZ_ASSERT(NS_IsMainThread());
36 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
38 nsCOMPtr<nsITransportSecurityInfo> securityInfo;
39 aChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
40 if (securityInfo) {
41 SetSecurityInfo(securityInfo);
44 mInited = true;
47 void ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal) {
48 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
49 MOZ_ASSERT(aGlobal);
51 MOZ_RELEASE_ASSERT(aGlobal->PrincipalOrNull()->IsSystemPrincipal());
53 mSecurityInfo = nullptr;
54 mInited = true;
57 void ChannelInfo::InitFromTransportSecurityInfo(
58 nsITransportSecurityInfo* aSecurityInfo) {
59 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
61 mSecurityInfo = aSecurityInfo;
62 mInited = true;
65 void ChannelInfo::SetSecurityInfo(nsITransportSecurityInfo* aSecurityInfo) {
66 MOZ_ASSERT(!mSecurityInfo, "security info should only be set once");
67 mSecurityInfo = aSecurityInfo;
70 nsresult ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel) {
71 MOZ_ASSERT(NS_IsMainThread());
72 MOZ_ASSERT(mInited);
74 if (mSecurityInfo) {
75 nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface(aChannel);
76 MOZ_ASSERT(httpChannel);
77 net::HttpBaseChannel* httpBaseChannel =
78 static_cast<net::HttpBaseChannel*>(httpChannel.get());
79 nsresult rv = httpBaseChannel->OverrideSecurityInfo(mSecurityInfo);
80 if (NS_WARN_IF(NS_FAILED(rv))) {
81 return rv;
85 return NS_OK;
88 already_AddRefed<nsITransportSecurityInfo> ChannelInfo::SecurityInfo() const {
89 // This may be called when mInited is false, for example if we try to store
90 // a synthesized Response object into the Cache. Uninitialized and empty
91 // ChannelInfo objects are indistinguishable at the IPC level, so this is
92 // fine.
93 nsCOMPtr<nsITransportSecurityInfo> securityInfo(mSecurityInfo);
94 return securityInfo.forget();