netwerk/protocol/wyciwyg/WyciwygChannelParent.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "nsWyciwyg.h"
     7 #include "mozilla/net/WyciwygChannelParent.h"
     8 #include "nsWyciwygChannel.h"
     9 #include "nsNetUtil.h"
    10 #include "nsCharsetSource.h"
    11 #include "nsISerializable.h"
    12 #include "nsSerializationHelper.h"
    13 #include "mozilla/ipc/URIUtils.h"
    14 #include "mozilla/net/NeckoParent.h"
    15 #include "SerializedLoadContext.h"
    17 using namespace mozilla::ipc;
    19 namespace mozilla {
    20 namespace net {
    22 WyciwygChannelParent::WyciwygChannelParent()
    23  : mIPCClosed(false)
    24  , mReceivedAppData(false)
    25 {
    26 #if defined(PR_LOGGING)
    27   if (!gWyciwygLog)
    28     gWyciwygLog = PR_NewLogModule("nsWyciwygChannel");
    29 #endif
    30 }
    32 WyciwygChannelParent::~WyciwygChannelParent()
    33 {
    34 }
    36 void
    37 WyciwygChannelParent::ActorDestroy(ActorDestroyReason why)
    38 {
    39   // We may still have refcount>0 if the channel hasn't called OnStopRequest
    40   // yet, but we must not send any more msgs to child.
    41   mIPCClosed = true;
    43   // We need to force the cycle to break here
    44   mChannel->SetNotificationCallbacks(nullptr);
    45 }
    47 //-----------------------------------------------------------------------------
    48 // WyciwygChannelParent::nsISupports
    49 //-----------------------------------------------------------------------------
    51 NS_IMPL_ISUPPORTS(WyciwygChannelParent,
    52                   nsIStreamListener,
    53                   nsIInterfaceRequestor,
    54                   nsIRequestObserver)
    56 //-----------------------------------------------------------------------------
    57 // WyciwygChannelParent::PWyciwygChannelParent
    58 //-----------------------------------------------------------------------------
    60 bool
    61 WyciwygChannelParent::RecvInit(const URIParams& aURI)
    62 {
    63   nsresult rv;
    65   nsCOMPtr<nsIURI> uri = DeserializeURI(aURI);
    66   if (!uri)
    67     return false;
    69   nsCString uriSpec;
    70   uri->GetSpec(uriSpec);
    71   LOG(("WyciwygChannelParent RecvInit [this=%p uri=%s]\n",
    72        this, uriSpec.get()));
    74   nsCOMPtr<nsIIOService> ios(do_GetIOService(&rv));
    75   if (NS_FAILED(rv))
    76     return SendCancelEarly(rv);
    78   nsCOMPtr<nsIChannel> chan;
    79   rv = NS_NewChannel(getter_AddRefs(chan), uri, ios);
    80   if (NS_FAILED(rv))
    81     return SendCancelEarly(rv);
    83   mChannel = do_QueryInterface(chan, &rv);
    84   if (NS_FAILED(rv))
    85     return SendCancelEarly(rv);
    87   return true;
    88 }
    90 bool
    91 WyciwygChannelParent::RecvAppData(const IPC::SerializedLoadContext& loadContext,
    92                                   PBrowserParent* parent)
    93 {
    94   LOG(("WyciwygChannelParent RecvAppData [this=%p]\n", this));
    96   if (!SetupAppData(loadContext, parent))
    97     return false;
    99   mChannel->SetNotificationCallbacks(this);
   100   return true;
   101 }
   103 bool
   104 WyciwygChannelParent::SetupAppData(const IPC::SerializedLoadContext& loadContext,
   105                                    PBrowserParent* aParent)
   106 {
   107   if (!mChannel)
   108     return true;
   110   const char* error = NeckoParent::CreateChannelLoadContext(aParent,
   111                                                             Manager()->Manager(),
   112                                                             loadContext,
   113                                                             mLoadContext);
   114   if (error) {
   115     printf_stderr("WyciwygChannelParent::SetupAppData: FATAL ERROR: %s\n",
   116                   error);
   117     return false;
   118   }
   120   if (!mLoadContext && loadContext.IsPrivateBitValid()) {
   121     nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(mChannel);
   122     if (pbChannel)
   123       pbChannel->SetPrivate(loadContext.mUsePrivateBrowsing);
   124   }
   126   mReceivedAppData = true;
   127   return true;
   128 }
   130 bool
   131 WyciwygChannelParent::RecvAsyncOpen(const URIParams& aOriginal,
   132                                     const uint32_t& aLoadFlags,
   133                                     const IPC::SerializedLoadContext& loadContext,
   134                                     PBrowserParent* aParent)
   135 {
   136   nsCOMPtr<nsIURI> original = DeserializeURI(aOriginal);
   137   if (!original)
   138     return false;
   140   LOG(("WyciwygChannelParent RecvAsyncOpen [this=%p]\n", this));
   142   if (!mChannel)
   143     return true;
   145   nsresult rv;
   147   rv = mChannel->SetOriginalURI(original);
   148   if (NS_FAILED(rv))
   149     return SendCancelEarly(rv);
   151   rv = mChannel->SetLoadFlags(aLoadFlags);
   152   if (NS_FAILED(rv))
   153     return SendCancelEarly(rv);
   155   if (!mReceivedAppData && !SetupAppData(loadContext, aParent)) {
   156     return false;
   157   }
   159   rv = mChannel->SetNotificationCallbacks(this);
   160   if (NS_FAILED(rv))
   161     return SendCancelEarly(rv);
   163   rv = mChannel->AsyncOpen(this, nullptr);
   164   if (NS_FAILED(rv))
   165     return SendCancelEarly(rv);
   167   return true;
   168 }
   170 bool
   171 WyciwygChannelParent::RecvWriteToCacheEntry(const nsString& data)
   172 {
   173   if (!mReceivedAppData) {
   174     printf_stderr("WyciwygChannelParent::RecvWriteToCacheEntry: FATAL ERROR: didn't receive app data\n");
   175     return false;
   176   }
   178   if (mChannel)
   179     mChannel->WriteToCacheEntry(data);
   181   return true;
   182 }
   184 bool
   185 WyciwygChannelParent::RecvCloseCacheEntry(const nsresult& reason)
   186 {
   187   if (mChannel) {
   188     mChannel->CloseCacheEntry(reason);
   189   }
   191   return true;
   192 }
   194 bool
   195 WyciwygChannelParent::RecvSetCharsetAndSource(const int32_t& aCharsetSource,
   196                                               const nsCString& aCharset)
   197 {
   198   if (mChannel)
   199     mChannel->SetCharsetAndSource(aCharsetSource, aCharset);
   201   return true;
   202 }
   204 bool
   205 WyciwygChannelParent::RecvSetSecurityInfo(const nsCString& aSecurityInfo)
   206 {
   207   if (mChannel) {
   208     nsCOMPtr<nsISupports> securityInfo;
   209     NS_DeserializeObject(aSecurityInfo, getter_AddRefs(securityInfo));
   210     mChannel->SetSecurityInfo(securityInfo);
   211   }
   213   return true;
   214 }
   216 bool
   217 WyciwygChannelParent::RecvCancel(const nsresult& aStatusCode)
   218 {
   219   if (mChannel)
   220     mChannel->Cancel(aStatusCode);
   221   return true;
   222 }
   224 //-----------------------------------------------------------------------------
   225 // WyciwygChannelParent::nsIRequestObserver
   226 //-----------------------------------------------------------------------------
   228 NS_IMETHODIMP
   229 WyciwygChannelParent::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
   230 {
   231   LOG(("WyciwygChannelParent::OnStartRequest [this=%p]\n", this));
   233   nsresult rv;
   235   nsCOMPtr<nsIWyciwygChannel> chan = do_QueryInterface(aRequest, &rv);
   236   NS_ENSURE_SUCCESS(rv, rv);
   238   nsresult status;
   239   chan->GetStatus(&status);
   241   int64_t contentLength = -1;
   242   chan->GetContentLength(&contentLength);
   244   int32_t charsetSource = kCharsetUninitialized;
   245   nsAutoCString charset;
   246   chan->GetCharsetAndSource(&charsetSource, charset);
   248   nsCOMPtr<nsISupports> securityInfo;
   249   chan->GetSecurityInfo(getter_AddRefs(securityInfo));
   250   nsCString secInfoStr;
   251   if (securityInfo) {
   252     nsCOMPtr<nsISerializable> serializable = do_QueryInterface(securityInfo);
   253     if (serializable)
   254       NS_SerializeToString(serializable, secInfoStr);
   255     else {
   256       NS_ERROR("Can't serialize security info");
   257       return NS_ERROR_UNEXPECTED;
   258     }
   259   }
   261   if (mIPCClosed ||
   262       !SendOnStartRequest(status, contentLength, charsetSource, charset, secInfoStr)) {
   263     return NS_ERROR_UNEXPECTED;
   264   }
   266   return NS_OK;
   267 }
   269 NS_IMETHODIMP
   270 WyciwygChannelParent::OnStopRequest(nsIRequest *aRequest,
   271                                     nsISupports *aContext,
   272                                     nsresult aStatusCode)
   273 {
   274   LOG(("WyciwygChannelParent::OnStopRequest: [this=%p status=%ul]\n",
   275        this, aStatusCode));
   277   if (mIPCClosed || !SendOnStopRequest(aStatusCode)) {
   278     return NS_ERROR_UNEXPECTED;
   279   }
   281   return NS_OK;
   282 }
   284 //-----------------------------------------------------------------------------
   285 // WyciwygChannelParent::nsIStreamListener
   286 //-----------------------------------------------------------------------------
   288 NS_IMETHODIMP
   289 WyciwygChannelParent::OnDataAvailable(nsIRequest *aRequest,
   290                                       nsISupports *aContext,
   291                                       nsIInputStream *aInputStream,
   292                                       uint64_t aOffset,
   293                                       uint32_t aCount)
   294 {
   295   LOG(("WyciwygChannelParent::OnDataAvailable [this=%p]\n", this));
   297   nsCString data;
   298   nsresult rv = NS_ReadInputStreamToString(aInputStream, data, aCount);
   299   if (NS_FAILED(rv))
   300     return rv;
   302   if (mIPCClosed || !SendOnDataAvailable(data, aOffset)) {
   303     return NS_ERROR_UNEXPECTED;
   304   }
   306   return NS_OK;
   307 }
   309 //-----------------------------------------------------------------------------
   310 // WyciwygChannelParent::nsIInterfaceRequestor
   311 //-----------------------------------------------------------------------------
   313 NS_IMETHODIMP
   314 WyciwygChannelParent::GetInterface(const nsIID& uuid, void** result)
   315 {
   316   // Only support nsILoadContext if child channel's callbacks did too
   317   if (uuid.Equals(NS_GET_IID(nsILoadContext)) && mLoadContext) {
   318     NS_ADDREF(mLoadContext);
   319     *result = static_cast<nsILoadContext*>(mLoadContext);
   320     return NS_OK;
   321   }
   323   return QueryInterface(uuid, result);
   324 }
   327 }} // mozilla::net

mercurial