Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/ChannelInfo.h"
8 : #include "nsCOMPtr.h"
9 : #include "nsContentUtils.h"
10 : #include "nsIChannel.h"
11 : #include "nsIDocument.h"
12 : #include "nsIHttpChannel.h"
13 : #include "nsSerializationHelper.h"
14 : #include "mozilla/net/HttpBaseChannel.h"
15 : #include "mozilla/ipc/ChannelInfo.h"
16 : #include "nsNetUtil.h"
17 :
18 : using namespace mozilla;
19 : using namespace mozilla::dom;
20 :
21 : void
22 0 : ChannelInfo::InitFromDocument(nsIDocument* aDoc)
23 : {
24 0 : MOZ_ASSERT(NS_IsMainThread());
25 0 : MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
26 :
27 0 : nsCOMPtr<nsISupports> securityInfo = aDoc->GetSecurityInfo();
28 0 : if (securityInfo) {
29 0 : SetSecurityInfo(securityInfo);
30 : }
31 :
32 0 : mInited = true;
33 0 : }
34 :
35 : void
36 2 : ChannelInfo::InitFromChannel(nsIChannel* aChannel)
37 : {
38 2 : MOZ_ASSERT(NS_IsMainThread());
39 2 : MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
40 :
41 4 : nsCOMPtr<nsISupports> securityInfo;
42 2 : aChannel->GetSecurityInfo(getter_AddRefs(securityInfo));
43 2 : if (securityInfo) {
44 0 : SetSecurityInfo(securityInfo);
45 : }
46 :
47 2 : mInited = true;
48 2 : }
49 :
50 : void
51 0 : ChannelInfo::InitFromChromeGlobal(nsIGlobalObject* aGlobal)
52 : {
53 0 : MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
54 0 : MOZ_ASSERT(aGlobal);
55 :
56 0 : MOZ_RELEASE_ASSERT(
57 : nsContentUtils::IsSystemPrincipal(aGlobal->PrincipalOrNull()));
58 :
59 0 : mSecurityInfo.Truncate();
60 0 : mInited = true;
61 0 : }
62 :
63 : void
64 0 : ChannelInfo::InitFromIPCChannelInfo(const mozilla::ipc::IPCChannelInfo& aChannelInfo)
65 : {
66 0 : MOZ_ASSERT(!mInited, "Cannot initialize the object twice");
67 :
68 0 : mSecurityInfo = aChannelInfo.securityInfo();
69 :
70 0 : mInited = true;
71 0 : }
72 :
73 : void
74 0 : ChannelInfo::SetSecurityInfo(nsISupports* aSecurityInfo)
75 : {
76 0 : MOZ_ASSERT(mSecurityInfo.IsEmpty(), "security info should only be set once");
77 0 : nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aSecurityInfo);
78 0 : if (!serializable) {
79 0 : NS_WARNING("A non-serializable object was passed to InternalResponse::SetSecurityInfo");
80 0 : return;
81 : }
82 0 : NS_SerializeToString(serializable, mSecurityInfo);
83 : }
84 :
85 : nsresult
86 0 : ChannelInfo::ResurrectInfoOnChannel(nsIChannel* aChannel)
87 : {
88 0 : MOZ_ASSERT(NS_IsMainThread());
89 0 : MOZ_ASSERT(mInited);
90 :
91 0 : if (!mSecurityInfo.IsEmpty()) {
92 0 : nsCOMPtr<nsISupports> infoObj;
93 0 : nsresult rv = NS_DeserializeObject(mSecurityInfo, getter_AddRefs(infoObj));
94 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
95 0 : return rv;
96 : }
97 : nsCOMPtr<nsIHttpChannel> httpChannel =
98 0 : do_QueryInterface(aChannel);
99 0 : MOZ_ASSERT(httpChannel);
100 : net::HttpBaseChannel* httpBaseChannel =
101 0 : static_cast<net::HttpBaseChannel*>(httpChannel.get());
102 0 : rv = httpBaseChannel->OverrideSecurityInfo(infoObj);
103 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
104 0 : return rv;
105 : }
106 : }
107 :
108 0 : return NS_OK;
109 : }
110 :
111 : mozilla::ipc::IPCChannelInfo
112 0 : ChannelInfo::AsIPCChannelInfo() const
113 : {
114 : // This may be called when mInited is false, for example if we try to store
115 : // a synthesized Response object into the Cache. Uninitialized and empty
116 : // ChannelInfo objects are indistinguishable at the IPC level, so this is
117 : // fine.
118 :
119 0 : IPCChannelInfo ipcInfo;
120 :
121 0 : ipcInfo.securityInfo() = mSecurityInfo;
122 :
123 0 : return ipcInfo;
124 : }
|