ExtensionChannelParent::Init is called for moz-extension loads draft
authorHaik Aftandilian <haftandilian@mozilla.com>
Thu, 16 Feb 2017 13:59:23 -0800
changeset 490176 690a9af9c635789f2d99eadf1903cd65b9029e21
parent 490175 a9d6d5c9b9b3da0a12f0a78032e9ab2518117a93
child 490177 33b710d0cf7fd1cf1823b186953b9e63ab4984e8
push id47029
push userhaftandilian@mozilla.com
push dateMon, 27 Feb 2017 20:23:07 +0000
milestone54.0a1
ExtensionChannelParent::Init is called for moz-extension loads MozReview-Commit-ID: EXeqE6oeIQ6
netwerk/ipc/NeckoChannelParams.ipdlh
netwerk/ipc/NeckoParent.cpp
netwerk/ipc/NeckoParent.h
netwerk/protocol/file/ExtensionChannelChild.cpp
netwerk/protocol/file/ExtensionChannelChild.h
netwerk/protocol/res/ExtensionProtocolHandler.cpp
--- a/netwerk/ipc/NeckoChannelParams.ipdlh
+++ b/netwerk/ipc/NeckoChannelParams.ipdlh
@@ -194,13 +194,13 @@ struct RtspChannelConnectArgs
 };
 
 //-----------------------------------------------------------------------------
 // Extension IPDL structs
 //-----------------------------------------------------------------------------
 struct ExtensionChannelCreationArgs
 {
   URIParams uri;
-  LoadInfoArgs loadInfo;
+  OptionalLoadInfoArgs loadInfo;
 };
 
 } // namespace ipc
 } // namespace mozilla
--- a/netwerk/ipc/NeckoParent.cpp
+++ b/netwerk/ipc/NeckoParent.cpp
@@ -500,16 +500,28 @@ NeckoParent::AllocPExtensionChannelParen
 bool
 NeckoParent::DeallocPExtensionChannelParent(PExtensionChannelParent* actor)
 {
   RefPtr<ExtensionChannelParent> p = dont_AddRef(static_cast<ExtensionChannelParent*>(actor));
   return true;
 }
 
 mozilla::ipc::IPCResult
+NeckoParent::RecvPExtensionChannelConstructor(
+                           PExtensionChannelParent* aActor,
+                           const ExtensionChannelCreationArgs& aOpenArgs)
+{
+  ExtensionChannelParent* p = static_cast<ExtensionChannelParent*>(aActor);
+  if (!p->Init(aOpenArgs)) {
+    return IPC_FAIL_NO_REASON(this);
+  }
+  return IPC_OK();
+}
+
+mozilla::ipc::IPCResult
 NeckoParent::RecvPDataChannelConstructor(PDataChannelParent* actor,
                                          const uint32_t& channelId)
 {
   DataChannelParent* p = static_cast<DataChannelParent*>(actor);
   DebugOnly<bool> rv = p->Init(channelId);
   MOZ_ASSERT(rv);
   return IPC_OK();
 }
--- a/netwerk/ipc/NeckoParent.h
+++ b/netwerk/ipc/NeckoParent.h
@@ -165,22 +165,23 @@ protected:
                                                             const nsresult& reason) override;
   virtual PWebSocketEventListenerParent*
     AllocPWebSocketEventListenerParent(const uint64_t& aInnerWindowID) override;
   virtual bool DeallocPWebSocketEventListenerParent(PWebSocketEventListenerParent*) override;
 
   virtual PDataChannelParent*
     AllocPDataChannelParent(const uint32_t& channelId) override;
   virtual bool DeallocPDataChannelParent(PDataChannelParent* parent) override;
+  virtual mozilla::ipc::IPCResult RecvPDataChannelConstructor(PDataChannelParent* aActor, const uint32_t& channelId) override;
 
   virtual PExtensionChannelParent* AllocPExtensionChannelParent(const ExtensionChannelCreationArgs& args) override;
   virtual bool DeallocPExtensionChannelParent(PExtensionChannelParent* parent) override;
-
-  virtual mozilla::ipc::IPCResult RecvPDataChannelConstructor(PDataChannelParent* aActor,
-                                                              const uint32_t& channelId) override;
+  virtual mozilla::ipc::IPCResult RecvPExtensionChannelConstructor(
+      PExtensionChannelParent* aActor,
+      const ExtensionChannelCreationArgs& aOpenArgs) override;
 
   virtual PRtspControllerParent* AllocPRtspControllerParent() override;
   virtual bool DeallocPRtspControllerParent(PRtspControllerParent*) override;
 
   virtual PRtspChannelParent*
     AllocPRtspChannelParent(const RtspChannelConnectArgs& aArgs)
                             override;
   virtual mozilla::ipc::IPCResult
--- a/netwerk/protocol/file/ExtensionChannelChild.cpp
+++ b/netwerk/protocol/file/ExtensionChannelChild.cpp
@@ -2,16 +2,17 @@
 /* vim: set ts=4 sw=4 sts=4 et tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "ExtensionChannelChild.h"
 
 #include "mozilla/Unused.h"
+#include "mozilla/ipc/URIUtils.h"
 #include "mozilla/net/NeckoChild.h"
 
 namespace mozilla {
 namespace net {
 
 NS_IMPL_ISUPPORTS_INHERITED(ExtensionChannelChild, nsBaseChannel, nsIChildChannel)
 
 ExtensionChannelChild::ExtensionChannelChild(nsIURI* aURI) : mIPCOpen(false)
@@ -66,10 +67,50 @@ ExtensionChannelChild::AddIPDLReference(
 void
 ExtensionChannelChild::ActorDestroy(ActorDestroyReason why)
 {
     MOZ_ASSERT(mIPCOpen);
     mIPCOpen = false;
     Release();
 }
 
+nsresult
+ExtensionChannelChild::AsyncOpen(nsIStreamListener* listener,
+                                 nsISupports* aContext)
+{
+    mListener = listener;
+    mListenerContext = aContext;
+
+    // add ourselves to the load group.
+    if (mLoadGroup)
+      mLoadGroup->AddRequest(this, nullptr);
+
+    nsresult rv;
+
+    ExtensionChannelCreationArgs args;
+    SerializeURI(nsBaseChannel::URI(), args.uri());
+
+    nsCOMPtr<nsILoadInfo> loadInfo;
+    GetLoadInfo(getter_AddRefs(loadInfo));
+    rv = mozilla::ipc::LoadInfoToLoadInfoArgs(loadInfo, &args.loadInfo());
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    gNeckoChild->SendPExtensionChannelConstructor(this, args);
+
+    // XXX Needed?
+    AddIPDLReference();
+
+    mWasOpened = true;
+
+    return rv;
+}
+
+nsresult
+ExtensionChannelChild::OpenContentStream(bool async,
+                                         nsIInputStream** stream,
+                                         nsIChannel** channel)
+{
+    MOZ_CRASH("Extension*Child* should never have OpenContentStream called!");
+    return NS_OK;
+}
+
 } // namespace net
 } // namespace mozilla
--- a/netwerk/protocol/file/ExtensionChannelChild.h
+++ b/netwerk/protocol/file/ExtensionChannelChild.h
@@ -21,16 +21,22 @@ class ExtensionChannelChild : public nsB
                             , public PExtensionChannelChild
 {
 public:
     NS_DECL_ISUPPORTS_INHERITED
     NS_DECL_NSICHILDCHANNEL
 
     explicit ExtensionChannelChild(nsIURI *uri);
 
+    NS_IMETHOD AsyncOpen(nsIStreamListener* listener,
+                         nsISupports* aContext) override;
+
+    nsresult OpenContentStream(bool async, nsIInputStream** stream,
+                               nsIChannel** channel) override;
+
 protected:
     virtual void ActorDestroy(ActorDestroyReason why) override;
 
 private:
     ~ExtensionChannelChild();
 
     void AddIPDLReference();
 
--- a/netwerk/protocol/res/ExtensionProtocolHandler.cpp
+++ b/netwerk/protocol/res/ExtensionProtocolHandler.cpp
@@ -1,15 +1,17 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "ExtensionProtocolHandler.h"
+#include "mozilla/net/ExtensionChannelChild.h"
+#include "mozilla/net/NeckoCommon.h"
 
 #include "nsIAddonPolicyService.h"
 #include "nsServiceManagerUtils.h"
 #include "nsIURL.h"
 #include "nsIChannel.h"
 #include "nsIStreamListener.h"
 #include "nsIRequestObserver.h"
 #include "nsIInputStreamChannel.h"
@@ -185,44 +187,71 @@ ExtensionProtocolHandler::SubstituteChan
                                         aLoadInfo);
   NS_ENSURE_SUCCESS(rv, rv);
 
   channel.swap(*result);
   return NS_OK;
 }
 
 /* Forwards to substituting protocol in the parent only */
-nsresult ExtensionProtocolHandler::GetScheme(nsACString & aScheme) {
+nsresult ExtensionProtocolHandler::GetScheme(nsACString & aScheme)
+{
   return SubstitutingProtocolHandler:: GetScheme(aScheme);
 }
-nsresult ExtensionProtocolHandler::GetDefaultPort(int32_t *aDefaultPort) {
+nsresult ExtensionProtocolHandler::GetDefaultPort(int32_t *aDefaultPort)
+{
   return SubstitutingProtocolHandler::GetDefaultPort(aDefaultPort);
 }
-nsresult ExtensionProtocolHandler::GetProtocolFlags(uint32_t *aProtocolFlags) {
+nsresult ExtensionProtocolHandler::GetProtocolFlags(uint32_t *aProtocolFlags)
+{
   return SubstitutingProtocolHandler::GetProtocolFlags(aProtocolFlags);
 }
 nsresult ExtensionProtocolHandler::NewURI(const nsACString & aSpec,
                                           const char * aOriginCharset,
                                           nsIURI *aBaseURI,
-                                          nsIURI * *_retval) {
+                                          nsIURI * *_retval)
+{
   return SubstitutingProtocolHandler::NewURI(aSpec, aOriginCharset,
                                              aBaseURI, _retval);
 }
 nsresult ExtensionProtocolHandler::AllowPort(int32_t port,
                                              const char * scheme,
-                                             bool *_retval) {
+                                             bool *_retval)
+{
   return SubstitutingProtocolHandler::AllowPort(port, scheme, _retval);
 }
 
 nsresult ExtensionProtocolHandler::NewChannel2(nsIURI *aURI,
                                                nsILoadInfo *aLoadinfo,
-                                               nsIChannel * *_retval) {
-  return SubstitutingProtocolHandler::NewChannel2(aURI, aLoadinfo, _retval);
+                                               nsIChannel * *_retval)
+{
+  // If we're in the parent process, use a direct channel
+  if (!IsNeckoChild()) {
+    return SubstitutingProtocolHandler::NewChannel2(aURI, aLoadinfo, _retval);
+  }
+
+  RefPtr<nsBaseChannel> channel;
+  channel = new ExtensionChannelChild(aURI);
+
+  nsresult rv = channel->Init();
+  if (NS_FAILED(rv)) {
+      return rv;
+  }
+
+  // set the loadInfo on the new channel
+  rv = channel->SetLoadInfo(aLoadinfo);
+  if (NS_FAILED(rv)) {
+      return rv;
+  }
+
+  channel.forget(_retval);
+  return rv;
 }
 
 
 nsresult ExtensionProtocolHandler::NewChannel(nsIURI *aURI,
-                                              nsIChannel **_retval) {
-  return NewChannel2(aURI, _retval);
+                                              nsIChannel **_retval)
+{
+  return NewChannel2(aURI, nullptr, _retval);
 }
 
 } // namespace net
 } // namespace mozilla