GeckoViewContentChannelParent.cpp (6584B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=4 sw=2 sts=2 et 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 "GeckoViewContentChannelParent.h" 8 9 #include "GeckoViewContentChannel.h" 10 #include "mozilla/dom/ContentParent.h" 11 #include "mozilla/ipc/URIUtils.h" 12 #include "mozilla/net/NeckoParent.h" 13 #include "mozilla/NotNull.h" 14 #include "nsIAuthPromptProvider.h" 15 #include "nsISecureBrowserUI.h" 16 17 namespace mozilla::net { 18 19 NS_IMPL_ISUPPORTS(GeckoViewContentChannelParent, nsIParentChannel, 20 nsIStreamListener, nsIInterfaceRequestor) 21 22 GeckoViewContentChannelParent::GeckoViewContentChannelParent( 23 dom::BrowserParent* aIframeEmbedding, nsILoadContext* aLoadContext) 24 : mLoadContext(aLoadContext), mBrowserParent(aIframeEmbedding) {} 25 26 NS_IMETHODIMP 27 GeckoViewContentChannelParent::SetParentListener( 28 ParentChannelListener* aListener) { 29 // Nothing to do. 30 return NS_OK; 31 } 32 33 NS_IMETHODIMP 34 GeckoViewContentChannelParent::NotifyClassificationFlags( 35 uint32_t aClassificationFlags, bool aIsThirdParty) { 36 // Nothing to do. 37 return NS_OK; 38 } 39 40 NS_IMETHODIMP 41 GeckoViewContentChannelParent::SetClassifierMatchedInfo( 42 const nsACString& aList, const nsACString& aProvider, 43 const nsACString& aFullHash) { 44 // nothing to do 45 return NS_OK; 46 } 47 48 NS_IMETHODIMP 49 GeckoViewContentChannelParent::SetClassifierMatchedTrackingInfo( 50 const nsACString& aLists, const nsACString& aFullHashes) { 51 // nothing to do 52 return NS_OK; 53 } 54 55 NS_IMETHODIMP 56 GeckoViewContentChannelParent::Delete() { 57 if (!CanSend()) { 58 return NS_ERROR_UNEXPECTED; 59 } 60 (void)SendDeleteSelf(); 61 return NS_OK; 62 } 63 64 NS_IMETHODIMP 65 GeckoViewContentChannelParent::GetRemoteType(nsACString& aRemoteType) { 66 if (!CanSend()) { 67 return NS_ERROR_UNEXPECTED; 68 } 69 70 dom::PContentParent* pcp = Manager()->Manager(); 71 aRemoteType = static_cast<dom::ContentParent*>(pcp)->GetRemoteType(); 72 return NS_OK; 73 } 74 75 NS_IMETHODIMP 76 GeckoViewContentChannelParent::OnStartRequest(nsIRequest* aRequest) { 77 if (!CanSend()) { 78 return NS_ERROR_UNEXPECTED; 79 } 80 nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest); 81 82 nsCString contentType; 83 channel->GetContentType(contentType); 84 85 nsresult channelStatus = NS_OK; 86 channel->GetStatus(&channelStatus); 87 88 nsCString entityID; 89 90 nsCOMPtr<nsIURI> uri; 91 channel->GetURI(getter_AddRefs(uri)); 92 if (MOZ_UNLIKELY(!uri)) { 93 return NS_ERROR_UNEXPECTED; 94 } 95 96 (void)SendOnStartRequest(channelStatus, contentType, entityID, 97 WrapNotNull(uri)); 98 99 return NS_OK; 100 } 101 102 NS_IMETHODIMP 103 GeckoViewContentChannelParent::OnStopRequest(nsIRequest* aRequest, 104 nsresult aStatusCode) { 105 if (!CanSend() || !SendOnStopRequest(aStatusCode)) { 106 return NS_ERROR_UNEXPECTED; 107 } 108 return NS_OK; 109 } 110 111 NS_IMETHODIMP 112 GeckoViewContentChannelParent::OnDataAvailable(nsIRequest* aRequest, 113 nsIInputStream* aInputStream, 114 uint64_t aOffset, 115 uint32_t aCount) { 116 if (!CanSend()) { 117 return NS_ERROR_UNEXPECTED; 118 } 119 nsCString data; 120 nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount); 121 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 122 return rv; 123 } 124 125 nsresult channelStatus = NS_OK; 126 mChannel->GetStatus(&channelStatus); 127 128 (void)SendOnDataAvailable(channelStatus, data, aOffset, aCount); 129 130 return NS_OK; 131 } 132 133 NS_IMETHODIMP 134 GeckoViewContentChannelParent::GetInterface(const nsIID& uuid, void** result) { 135 if (uuid.Equals(NS_GET_IID(nsIAuthPromptProvider)) || 136 uuid.Equals(NS_GET_IID(nsISecureBrowserUI))) { 137 if (mBrowserParent) { 138 return mBrowserParent->QueryInterface(uuid, result); 139 } 140 } 141 142 // Only support nsILoadContext if child channel's callbacks did too 143 if (uuid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) { 144 nsCOMPtr<nsILoadContext> copy = mLoadContext; 145 copy.forget(result); 146 return NS_OK; 147 } 148 149 return QueryInterface(uuid, result); 150 } 151 152 bool GeckoViewContentChannelParent::Init( 153 const GeckoViewContentChannelOpenArgs& aArgs) { 154 nsresult rv = NS_OK; 155 156 auto scopeExit = MakeScopeExit([&rv, this] { 157 if (NS_FAILED(rv)) { 158 (void)SendOnAsyncOpenFailed(rv); 159 } 160 }); 161 162 nsCOMPtr<nsIURI> uri = ipc::DeserializeURI(aArgs.uri()); 163 164 nsAutoCString remoteType; 165 rv = GetRemoteType(remoteType); 166 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 167 return false; 168 } 169 170 nsCOMPtr<nsILoadInfo> loadInfo; 171 rv = mozilla::ipc::LoadInfoArgsToLoadInfo(aArgs.loadInfo(), remoteType, 172 getter_AddRefs(loadInfo)); 173 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 174 return false; 175 } 176 177 nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv)); 178 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 179 return false; 180 } 181 182 nsCOMPtr<nsIChannel> channel; 183 rv = NS_NewChannelInternal(getter_AddRefs(channel), uri, loadInfo, nullptr, 184 nullptr, nullptr, aArgs.loadFlags(), ios); 185 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 186 return false; 187 } 188 189 rv = channel->AsyncOpen(this); 190 if (MOZ_UNLIKELY(NS_FAILED(rv))) { 191 return false; 192 } 193 194 mChannel = channel; 195 196 return true; 197 } 198 199 bool GeckoViewContentChannelParent::Init( 200 const GeckoViewContentChannelConnectArgs& aArgs) { 201 nsCOMPtr<nsIChannel> channel; 202 nsresult rv = 203 NS_LinkRedirectChannels(aArgs.channelId(), this, getter_AddRefs(channel)); 204 if (NS_SUCCEEDED(rv)) { 205 mChannel = channel; 206 } 207 return true; 208 } 209 210 bool GeckoViewContentChannelParent::Init( 211 const GeckoViewContentChannelArgs& aArgs) { 212 switch (aArgs.type()) { 213 case GeckoViewContentChannelArgs::TGeckoViewContentChannelOpenArgs: 214 return Init(aArgs.get_GeckoViewContentChannelOpenArgs()); 215 case GeckoViewContentChannelArgs::TGeckoViewContentChannelConnectArgs: 216 return Init(aArgs.get_GeckoViewContentChannelConnectArgs()); 217 default: 218 return false; 219 } 220 } 221 222 mozilla::ipc::IPCResult GeckoViewContentChannelParent::RecvCancel( 223 const nsresult& status) { 224 if (mChannel) { 225 mChannel->Cancel(status); 226 } 227 return IPC_OK(); 228 } 229 230 mozilla::ipc::IPCResult GeckoViewContentChannelParent::RecvSuspend() { 231 if (mChannel) { 232 mChannel->Suspend(); 233 } 234 return IPC_OK(); 235 } 236 237 mozilla::ipc::IPCResult GeckoViewContentChannelParent::RecvResume() { 238 if (mChannel) { 239 mChannel->Resume(); 240 } 241 return IPC_OK(); 242 } 243 244 } // namespace mozilla::net