Bug 1700051: part 26) Correct typo in comment of `mozInlineSpellWordUtil::BuildSoftTe...
[gecko.git] / dom / fetch / ChannelInfo.cpp
blob62e6287b0c08de4d4fe71be777eec6c51a881272
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 "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();
28 if (securityInfo) {
29 SetSecurityInfo(securityInfo);
32 mInited = true;
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));
41 if (securityInfo) {
42 SetSecurityInfo(securityInfo);
45 mInited = true;
48 void ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal) {
49 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
50 MOZ_ASSERT(aGlobal);
52 MOZ_RELEASE_ASSERT(aGlobal->PrincipalOrNull()->IsSystemPrincipal());
54 mSecurityInfo.Truncate();
55 mInited = true;
58 void ChannelInfo::InitFromIPCChannelInfo(
59 const mozilla::ipc::IPCChannelInfo& aChannelInfo) {
60 MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
62 mSecurityInfo = aChannelInfo.securityInfo();
64 mInited = true;
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);
70 if (!serializable) {
71 NS_WARNING(
72 "A non-serializable object was passed to "
73 "InternalResponse::SetSecurityInfo");
74 return;
76 NS_SerializeToString(serializable, mSecurityInfo);
79 nsresult ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel) {
80 MOZ_ASSERT(NS_IsMainThread());
81 MOZ_ASSERT(mInited);
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))) {
87 return 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))) {
95 return rv;
99 return NS_OK;
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
106 // fine.
108 IPCChannelInfo ipcInfo;
110 ipcInfo.securityInfo() = mSecurityInfo;
112 return ipcInfo;