Source code

Revision control

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef nsSecCheckWrapChannel_h__
#define nsSecCheckWrapChannel_h__

#include "nsIHttpChannel.h"
#include "nsIHttpChannelInternal.h"
#include "nsIUploadChannel.h"
#include "nsIUploadChannel2.h"
#include "nsISecCheckWrapChannel.h"
#include "nsIWyciwygChannel.h"
#include "mozilla/LoadInfo.h"

namespace mozilla {
namespace net {

/*
 * The nsSecCheckWrapChannelBase wraps channels that do *not*
 *  * provide a newChannel2() implementation
 *  * provide get/setLoadInfo functions
 *
 * In order to perform security checks for channels
 *   a) before opening the channel, and
 *   b) after redirects
 * we are attaching a loadinfo object to every channel which
 * provides information about the content-type of the channel,
 * who initiated the load, etc.
 *
 * Addon created channels might *not* provide that loadInfo object for
 * some transition time before we mark the NewChannel-API as deprecated.
 * We do not want to break those addons hence we wrap such channels
 * using the provided wrapper in this class.
 *
 * Please note that the wrapper only forwards calls for
 *  * nsIRequest
 *  * nsIChannel
 *  * nsIHttpChannel
 *  * nsIHttpChannelInternal
 *  * nsIUploadChannel
 *  * nsIUploadChannel2
 *
 * In case any addon needs to query the inner channel this class
 * provides a readonly function to query the wrapped channel.
 *
 */

class nsSecCheckWrapChannelBase : public nsIHttpChannel,
                                  public nsIHttpChannelInternal,
                                  public nsISecCheckWrapChannel,
                                  public nsIUploadChannel,
                                  public nsIUploadChannel2 {
 public:
  NS_FORWARD_NSIHTTPCHANNEL(mHttpChannel->)
  NS_FORWARD_NSIHTTPCHANNELINTERNAL(mHttpChannelInternal->)
  NS_FORWARD_NSICHANNEL(mChannel->)
  NS_FORWARD_NSIREQUEST(mRequest->)
  NS_FORWARD_NSIUPLOADCHANNEL(mUploadChannel->)
  NS_FORWARD_NSIUPLOADCHANNEL2(mUploadChannel2->)
  NS_DECL_NSISECCHECKWRAPCHANNEL
  NS_DECL_ISUPPORTS

  explicit nsSecCheckWrapChannelBase(nsIChannel* aChannel);

 protected:
  virtual ~nsSecCheckWrapChannelBase() = default;

  nsCOMPtr<nsIChannel> mChannel;
  // We do a QI in the constructor to set the following pointers.
  nsCOMPtr<nsIHttpChannel> mHttpChannel;
  nsCOMPtr<nsIHttpChannelInternal> mHttpChannelInternal;
  nsCOMPtr<nsIRequest> mRequest;
  nsCOMPtr<nsIUploadChannel> mUploadChannel;
  nsCOMPtr<nsIUploadChannel2> mUploadChannel2;
};

/* We define a separate class here to make it clear that we're overriding
 * Get/SetLoadInfo as well as AsyncOpen2() and Open2(), rather that using
 * the forwarded implementations provided by NS_FORWARD_NSICHANNEL"
 */
class nsSecCheckWrapChannel : public nsSecCheckWrapChannelBase {
 public:
  NS_IMETHOD GetLoadInfo(nsILoadInfo** aLoadInfo) override;
  NS_IMETHOD SetLoadInfo(nsILoadInfo* aLoadInfo) override;

  NS_IMETHOD AsyncOpen2(nsIStreamListener* aListener) override;
  NS_IMETHOD Open2(nsIInputStream** aStream) override;

  nsSecCheckWrapChannel(nsIChannel* aChannel, nsILoadInfo* aLoadInfo);
  static already_AddRefed<nsIChannel> MaybeWrap(nsIChannel* aChannel,
                                                nsILoadInfo* aLoadInfo);

 protected:
  virtual ~nsSecCheckWrapChannel() = default;

  nsCOMPtr<nsILoadInfo> mLoadInfo;
};

}  // namespace net
}  // namespace mozilla

#endif  // nsSecCheckWrapChannel_h__