Bug 1322748 add ability to get registered channelwrappers, r=kmag draft
authorShane Caraveo <scaraveo@mozilla.com>
Tue, 22 May 2018 14:19:57 -0400
changeset 798353 0b5193d7f4e9e7f27f9a7d622699b673781c3dd4
parent 798084 b75acf9652937ce79a9bf02de843c100db0e5ec7
child 798354 1c4b868d96d4997ef2b29e021ee0c508c2d7906b
child 798818 92da335bf64ced630fe1b3e1ee1687375f8c0ebe
child 798927 190eeeb925d32f4e9a50d26dbfa6bc0f0dfc2888
push id110729
push usermixedpuppy@gmail.com
push dateTue, 22 May 2018 18:20:17 +0000
reviewerskmag
bugs1322748
milestone62.0a1
Bug 1322748 add ability to get registered channelwrappers, r=kmag MozReview-Commit-ID: SphwWjzQuo
dom/chrome-webidl/ChannelWrapper.webidl
toolkit/components/extensions/webrequest/ChannelWrapper.cpp
toolkit/components/extensions/webrequest/ChannelWrapper.h
--- a/dom/chrome-webidl/ChannelWrapper.webidl
+++ b/dom/chrome-webidl/ChannelWrapper.webidl
@@ -45,16 +45,24 @@ enum MozContentPolicyType {
 interface ChannelWrapper : EventTarget {
   /**
    * Returns the wrapper instance for the given channel. The same wrapper is
    * always returned for a given channel.
    */
   static ChannelWrapper get(MozChannel channel);
 
   /**
+   * Returns the wrapper instance for the given channel. The same wrapper is
+   * always returned for a given channel.
+   */
+  static ChannelWrapper getRegisteredChannel(unsigned long long aChannelId,
+                                             WebExtensionPolicy extension,
+                                             TabParent? tabParent);
+
+  /**
    * A unique ID for for the requests which remains constant throughout the
    * redirect chain.
    */
   [Constant, StoreInSlot]
   readonly attribute unsigned long long id;
 
   // Not technically pure, since it's backed by a weak reference, but if JS
   // has a reference to the previous value, we can depend on it not being
--- a/toolkit/components/extensions/webrequest/ChannelWrapper.cpp
+++ b/toolkit/components/extensions/webrequest/ChannelWrapper.cpp
@@ -74,16 +74,34 @@ ChannelWrapper::Get(const GlobalObject& 
       Unused << props->SetPropertyAsInterface(CHANNELWRAPPER_PROP_KEY,
                                               wrapper);
     }
   }
 
   return wrapper.forget();
 }
 
+already_AddRefed<ChannelWrapper>
+ChannelWrapper::GetRegisteredChannel(const GlobalObject& global, uint64_t aChannelId, const WebExtensionPolicy& aAddon, nsITabParent* aTabParent)
+{
+  nsIContentParent* contentParent = nullptr;
+  if (TabParent* parent = static_cast<TabParent*>(aTabParent)) {
+    contentParent = static_cast<nsIContentParent*>(parent->Manager());
+  }
+
+  auto& webreq = WebRequestService::GetSingleton();
+
+  nsCOMPtr<nsITraceableChannel> channel = webreq.GetTraceableChannel(aChannelId, aAddon.Id(), contentParent);
+  if (!channel) {
+    return nullptr;
+  }
+  nsCOMPtr<nsIChannel> chan(do_QueryInterface(channel));
+  return ChannelWrapper::Get(global, chan);
+}
+
 void
 ChannelWrapper::SetChannel(nsIChannel* aChannel)
 {
   detail::ChannelHolder::SetChannel(aChannel);
   ClearCachedAttributes();
   ChannelWrapperBinding::ClearCachedFinalURIValue(this);
   ChannelWrapperBinding::ClearCachedFinalURLValue(this);
   mFinalURLInfo.reset();
--- a/toolkit/components/extensions/webrequest/ChannelWrapper.h
+++ b/toolkit/components/extensions/webrequest/ChannelWrapper.h
@@ -116,17 +116,17 @@ class ChannelWrapper final : public DOME
 public:
   MOZ_DECLARE_WEAKREFERENCE_TYPENAME(ChannelWrapper)
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED(ChannelWrapper, DOMEventTargetHelper)
 
   NS_DECLARE_STATIC_IID_ACCESSOR(NS_CHANNELWRAPPER_IID)
 
   static already_AddRefed<extensions::ChannelWrapper> Get(const dom::GlobalObject& global, nsIChannel* channel);
-
+  static already_AddRefed<extensions::ChannelWrapper> GetRegisteredChannel(const dom::GlobalObject& global, uint64_t aChannelId, const WebExtensionPolicy& aAddon, nsITabParent* aTabParent);
 
   uint64_t Id() const { return mId; }
 
   already_AddRefed<nsIChannel> GetChannel() const { return MaybeChannel(); }
 
   void SetChannel(nsIChannel* aChannel);