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"
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 "mozilla/ipc/ChannelInfo.h"
18 #include "nsNetUtil.h"
20 using namespace mozilla
;
21 using namespace mozilla::dom
;
23 void ChannelInfo::InitFromDocument(Document
* aDoc
) {
24 MOZ_ASSERT(NS_IsMainThread());
25 MOZ_ASSERT(!mInited
, "Cannot initialize the object twice");
27 nsCOMPtr
<nsISupports
> securityInfo
= aDoc
->GetSecurityInfo();
29 SetSecurityInfo(securityInfo
);
35 void ChannelInfo::InitFromChannel(nsIChannel
* aChannel
) {
36 MOZ_ASSERT(NS_IsMainThread());
37 MOZ_ASSERT(!mInited
, "Cannot initialize the object twice");
39 nsCOMPtr
<nsISupports
> securityInfo
;
40 aChannel
->GetSecurityInfo(getter_AddRefs(securityInfo
));
42 SetSecurityInfo(securityInfo
);
48 void ChannelInfo::InitFromChromeGlobal(nsIGlobalObject
* aGlobal
) {
49 MOZ_ASSERT(!mInited
, "Cannot initialize the object twice");
52 MOZ_RELEASE_ASSERT(aGlobal
->PrincipalOrNull()->IsSystemPrincipal());
54 mSecurityInfo
.Truncate();
58 void ChannelInfo::InitFromIPCChannelInfo(
59 const mozilla::ipc::IPCChannelInfo
& aChannelInfo
) {
60 MOZ_ASSERT(!mInited
, "Cannot initialize the object twice");
62 mSecurityInfo
= aChannelInfo
.securityInfo();
67 void ChannelInfo::SetSecurityInfo(nsISupports
* aSecurityInfo
) {
68 MOZ_ASSERT(mSecurityInfo
.IsEmpty(), "security info should only be set once");
69 nsCOMPtr
<nsISerializable
> serializable
= do_QueryInterface(aSecurityInfo
);
72 "A non-serializable object was passed to "
73 "InternalResponse::SetSecurityInfo");
76 NS_SerializeToString(serializable
, mSecurityInfo
);
79 nsresult
ChannelInfo::ResurrectInfoOnChannel(nsIChannel
* aChannel
) {
80 MOZ_ASSERT(NS_IsMainThread());
83 if (!mSecurityInfo
.IsEmpty()) {
84 nsCOMPtr
<nsISupports
> infoObj
;
85 nsresult rv
= NS_DeserializeObject(mSecurityInfo
, getter_AddRefs(infoObj
));
86 if (NS_WARN_IF(NS_FAILED(rv
))) {
89 nsCOMPtr
<nsIHttpChannel
> httpChannel
= do_QueryInterface(aChannel
);
90 MOZ_ASSERT(httpChannel
);
91 net::HttpBaseChannel
* httpBaseChannel
=
92 static_cast<net::HttpBaseChannel
*>(httpChannel
.get());
93 rv
= httpBaseChannel
->OverrideSecurityInfo(infoObj
);
94 if (NS_WARN_IF(NS_FAILED(rv
))) {
102 mozilla::ipc::IPCChannelInfo
ChannelInfo::AsIPCChannelInfo() const {
103 // This may be called when mInited is false, for example if we try to store
104 // a synthesized Response object into the Cache. Uninitialized and empty
105 // ChannelInfo objects are indistinguishable at the IPC level, so this is
108 IPCChannelInfo ipcInfo
;
110 ipcInfo
.securityInfo() = mSecurityInfo
;