InterceptedChannel.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set expandtab ts=2 sw=2 sts=2 cin: */
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef InterceptedChannel_h
  7. #define InterceptedChannel_h
  8. #include "nsINetworkInterceptController.h"
  9. #include "mozilla/RefPtr.h"
  10. #include "mozilla/Maybe.h"
  11. class nsICacheEntry;
  12. class nsInputStreamPump;
  13. class nsIStreamListener;
  14. namespace mozilla {
  15. namespace net {
  16. class nsHttpChannel;
  17. class HttpChannelChild;
  18. class nsHttpResponseHead;
  19. class InterceptStreamListener;
  20. // An object representing a channel that has been intercepted. This avoids complicating
  21. // the actual channel implementation with the details of synthesizing responses.
  22. class InterceptedChannelBase : public nsIInterceptedChannel {
  23. protected:
  24. // The interception controller to notify about the successful channel interception
  25. nsCOMPtr<nsINetworkInterceptController> mController;
  26. // The stream to write the body of the synthesized response
  27. nsCOMPtr<nsIOutputStream> mResponseBody;
  28. // Response head for use when synthesizing
  29. Maybe<nsAutoPtr<nsHttpResponseHead>> mSynthesizedResponseHead;
  30. nsCOMPtr<nsIConsoleReportCollector> mReportCollector;
  31. nsCOMPtr<nsISupports> mReleaseHandle;
  32. bool mClosed;
  33. void EnsureSynthesizedResponse();
  34. void DoNotifyController();
  35. nsresult DoSynthesizeStatus(uint16_t aStatus, const nsACString& aReason);
  36. nsresult DoSynthesizeHeader(const nsACString& aName, const nsACString& aValue);
  37. TimeStamp mLaunchServiceWorkerStart;
  38. TimeStamp mLaunchServiceWorkerEnd;
  39. TimeStamp mDispatchFetchEventStart;
  40. TimeStamp mDispatchFetchEventEnd;
  41. TimeStamp mHandleFetchEventStart;
  42. TimeStamp mHandleFetchEventEnd;
  43. virtual ~InterceptedChannelBase();
  44. public:
  45. explicit InterceptedChannelBase(nsINetworkInterceptController* aController);
  46. // Notify the interception controller that the channel has been intercepted
  47. // and prepare the response body output stream.
  48. virtual void NotifyController() = 0;
  49. NS_DECL_ISUPPORTS
  50. NS_IMETHOD GetResponseBody(nsIOutputStream** aOutput) override;
  51. NS_IMETHOD GetConsoleReportCollector(nsIConsoleReportCollector** aCollectorOut) override;
  52. NS_IMETHOD SetReleaseHandle(nsISupports* aHandle) override;
  53. NS_IMETHODIMP
  54. SetLaunchServiceWorkerStart(TimeStamp aTimeStamp) override
  55. {
  56. mLaunchServiceWorkerStart = aTimeStamp;
  57. return NS_OK;
  58. }
  59. NS_IMETHODIMP
  60. SetLaunchServiceWorkerEnd(TimeStamp aTimeStamp) override
  61. {
  62. mLaunchServiceWorkerEnd = aTimeStamp;
  63. return NS_OK;
  64. }
  65. NS_IMETHODIMP
  66. SetDispatchFetchEventStart(TimeStamp aTimeStamp) override
  67. {
  68. mDispatchFetchEventStart = aTimeStamp;
  69. return NS_OK;
  70. }
  71. NS_IMETHODIMP
  72. SetDispatchFetchEventEnd(TimeStamp aTimeStamp) override
  73. {
  74. mDispatchFetchEventEnd = aTimeStamp;
  75. return NS_OK;
  76. }
  77. NS_IMETHODIMP
  78. SetHandleFetchEventStart(TimeStamp aTimeStamp) override
  79. {
  80. mHandleFetchEventStart = aTimeStamp;
  81. return NS_OK;
  82. }
  83. NS_IMETHODIMP
  84. SetHandleFetchEventEnd(TimeStamp aTimeStamp) override
  85. {
  86. mHandleFetchEventEnd = aTimeStamp;
  87. return NS_OK;
  88. }
  89. NS_IMETHODIMP SaveTimeStampsToUnderlyingChannel() override;
  90. static already_AddRefed<nsIURI>
  91. SecureUpgradeChannelURI(nsIChannel* aChannel);
  92. };
  93. class InterceptedChannelChrome : public InterceptedChannelBase
  94. {
  95. // The actual channel being intercepted.
  96. RefPtr<nsHttpChannel> mChannel;
  97. // Writeable cache entry for use when synthesizing a response in a parent process
  98. nsCOMPtr<nsICacheEntry> mSynthesizedCacheEntry;
  99. // When a channel is intercepted, content decoding is disabled since the
  100. // ServiceWorker will have already extracted the decoded data. For parent
  101. // process channels we need to preserve the earlier value in case
  102. // ResetInterception is called.
  103. bool mOldApplyConversion;
  104. public:
  105. InterceptedChannelChrome(nsHttpChannel* aChannel,
  106. nsINetworkInterceptController* aController,
  107. nsICacheEntry* aEntry);
  108. NS_IMETHOD ResetInterception() override;
  109. NS_IMETHOD FinishSynthesizedResponse(const nsACString& aFinalURLSpec) override;
  110. NS_IMETHOD GetChannel(nsIChannel** aChannel) override;
  111. NS_IMETHOD GetSecureUpgradedChannelURI(nsIURI** aURI) override;
  112. NS_IMETHOD SynthesizeStatus(uint16_t aStatus, const nsACString& aReason) override;
  113. NS_IMETHOD SynthesizeHeader(const nsACString& aName, const nsACString& aValue) override;
  114. NS_IMETHOD Cancel(nsresult aStatus) override;
  115. NS_IMETHOD SetChannelInfo(mozilla::dom::ChannelInfo* aChannelInfo) override;
  116. NS_IMETHOD GetInternalContentPolicyType(nsContentPolicyType *aInternalContentPolicyType) override;
  117. virtual void NotifyController() override;
  118. };
  119. class InterceptedChannelContent : public InterceptedChannelBase
  120. {
  121. // The actual channel being intercepted.
  122. RefPtr<HttpChannelChild> mChannel;
  123. // Reader-side of the response body when synthesizing in a child proces
  124. nsCOMPtr<nsIInputStream> mSynthesizedInput;
  125. // Listener for the synthesized response to fix up the notifications before they reach
  126. // the actual channel.
  127. RefPtr<InterceptStreamListener> mStreamListener;
  128. // Set for intercepted channels that have gone through a secure upgrade.
  129. bool mSecureUpgrade;
  130. public:
  131. InterceptedChannelContent(HttpChannelChild* aChannel,
  132. nsINetworkInterceptController* aController,
  133. InterceptStreamListener* aListener,
  134. bool aSecureUpgrade);
  135. NS_IMETHOD ResetInterception() override;
  136. NS_IMETHOD FinishSynthesizedResponse(const nsACString& aFinalURLSpec) override;
  137. NS_IMETHOD GetChannel(nsIChannel** aChannel) override;
  138. NS_IMETHOD GetSecureUpgradedChannelURI(nsIURI** aURI) override;
  139. NS_IMETHOD SynthesizeStatus(uint16_t aStatus, const nsACString& aReason) override;
  140. NS_IMETHOD SynthesizeHeader(const nsACString& aName, const nsACString& aValue) override;
  141. NS_IMETHOD Cancel(nsresult aStatus) override;
  142. NS_IMETHOD SetChannelInfo(mozilla::dom::ChannelInfo* aChannelInfo) override;
  143. NS_IMETHOD GetInternalContentPolicyType(nsContentPolicyType *aInternalContentPolicyType) override;
  144. virtual void NotifyController() override;
  145. };
  146. } // namespace net
  147. } // namespace mozilla
  148. #endif // InterceptedChannel_h