author Andrew Halberstadt <>
Thu, 25 May 2017 11:48:03 -0400
changeset 410946 ff64a7889c1fac56d48205fc2fad4eb5aa481a73
parent 400152 48d6ab7dafea23eeb87eeddf428a63b9a43db6fc
child 427632 0ae47966e150a75c69cc6b691529aa8c7126565c
permissions -rw-r--r--
Bug 1346025 - Move vendored python modules from /python to /third_party/python, r=ted This commit is a simple 'hg mv' and does not contain any file modifications. *** Bug 1346025 - Split vendored modules in python/ to third_party/python/, r=ted *** Bug 1346025 - Update references to moved python modules, r=ted MozReview-Commit-ID: A12RnIFtXju

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 */

#ifndef SimpleChannel_h
#define SimpleChannel_h

#include "mozilla/Result.h"
#include "mozilla/UniquePtr.h"
#include "nsCOMPtr.h"

class nsIChannel;
class nsIInputStream;
class nsILoadInfo;
class nsIRequest;
class nsIStreamListener;
class nsIURI;


namespace mozilla {

using InputStreamOrReason = Result<nsCOMPtr<nsIInputStream>, nsresult>;
using RequestOrReason = Result<nsCOMPtr<nsIRequest>, nsresult>;

namespace net {

class SimpleChannelCallbacks
  virtual InputStreamOrReason OpenContentStream(bool async, nsIChannel* channel) = 0;

  virtual RequestOrReason StartAsyncRead(nsIStreamListener* stream, nsIChannel* channel) = 0;

  virtual ~SimpleChannelCallbacks() {}

template <typename F1, typename F2, typename T>
class SimpleChannelCallbacksImpl final : public SimpleChannelCallbacks
  SimpleChannelCallbacksImpl(F1&& aStartAsyncRead, F2&& aOpenContentStream, T* context)
    : mStartAsyncRead(aStartAsyncRead)
    , mOpenContentStream(aOpenContentStream)
    , mContext(context)

  virtual ~SimpleChannelCallbacksImpl() {}

  virtual InputStreamOrReason OpenContentStream(bool async, nsIChannel* channel) override
    return mOpenContentStream(async, channel, mContext);

  virtual RequestOrReason StartAsyncRead(nsIStreamListener* listener, nsIChannel* channel) override
    return mStartAsyncRead(listener, channel, mContext);

  F1 mStartAsyncRead;
  F2 mOpenContentStream;
  RefPtr<T> mContext;

NS_NewSimpleChannelInternal(nsIURI* aURI, nsILoadInfo* aLoadInfo, UniquePtr<SimpleChannelCallbacks>&& aCallbacks);

} // namespace net
} // namespace mozilla

 * Creates a simple channel which wraps an input stream created by the given
 * callbacks. The callbacks are not called until the underlying AsyncOpen2 or
 * Open2 methods are called, and correspond to the nsBaseChannel::StartAsyncRead
 * and nsBaseChannel::OpenContentStream methods of the same names.
 * The last two arguments of each callback are the created channel instance,
 * and the ref-counted context object passed to NS_NewSimpleChannel. A strong
 * reference to that object is guaranteed to be kept alive until after a
 * callback successfully completes.
template <typename T, typename F1, typename F2>
inline already_AddRefed<nsIChannel>
NS_NewSimpleChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo, T* context, F1&& aStartAsyncRead, F2&& aOpenContentStream)
  using namespace mozilla;

  auto callbacks = MakeUnique<net::SimpleChannelCallbacksImpl<F1, F2, T>>(
      Move(aStartAsyncRead), Move(aOpenContentStream), context);

  return net::NS_NewSimpleChannelInternal(aURI, aLoadInfo, Move(callbacks));

template <typename T, typename F1>
inline already_AddRefed<nsIChannel>
NS_NewSimpleChannel(nsIURI* aURI, nsILoadInfo* aLoadInfo, T* context, F1&& aStartAsyncRead)
  using namespace mozilla;

  auto openContentStream = [] (bool async, nsIChannel* channel, T* context) {

  return NS_NewSimpleChannel(
      aURI, aLoadInfo, context, Move(aStartAsyncRead), Move(openContentStream));

#endif // SimpleChannel_h