tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

BaseMediaResource.h (5742B)


      1 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef BaseMediaResource_h
      7 #define BaseMediaResource_h
      8 
      9 #include "MediaCache.h"
     10 #include "MediaResource.h"
     11 #include "MediaResourceCallback.h"
     12 #include "mozilla/dom/MediaDebugInfoBinding.h"
     13 #include "nsIChannel.h"
     14 #include "nsIStreamListener.h"
     15 #include "nsIURI.h"
     16 
     17 class nsIPrincipal;
     18 
     19 namespace mozilla {
     20 
     21 DDLoggedTypeDeclNameAndBase(BaseMediaResource, MediaResource);
     22 
     23 class BaseMediaResource : public MediaResource,
     24                          public DecoderDoctorLifeLogger<BaseMediaResource> {
     25 public:
     26  /**
     27   * Create a resource, reading data from the channel. Call on main thread only.
     28   * The caller must follow up by calling resource->Open().
     29   */
     30  static already_AddRefed<BaseMediaResource> Create(
     31      MediaResourceCallback* aCallback, nsIChannel* aChannel,
     32      bool aIsPrivateBrowsing);
     33 
     34  // Pass true to limit the amount of readahead data (specified by
     35  // "media.cache_readahead_limit") or false to read as much as the
     36  // cache size allows.
     37  virtual void ThrottleReadahead(bool bThrottle) {}
     38 
     39  // This is the client's estimate of the playback rate assuming
     40  // the media plays continuously. The cache can't guess this itself
     41  // because it doesn't know when the decoder was paused, buffering, etc.
     42  virtual void SetPlaybackRate(uint32_t aBytesPerSecond) = 0;
     43 
     44  // Get the estimated download rate in bytes per second (assuming no
     45  // pausing of the channel is requested by Gecko).
     46  // *aIsReliable is set to true if we think the estimate is useful.
     47  virtual double GetDownloadRate(bool* aIsReliable) = 0;
     48 
     49  // Moves any existing channel loads into or out of background. Background
     50  // loads don't block the load event. This also determines whether or not any
     51  // new loads initiated (for example to seek) will be in the background.
     52  void SetLoadInBackground(bool aLoadInBackground);
     53 
     54  // Suspend any downloads that are in progress.
     55  // If aCloseImmediately is set, resources should be released immediately
     56  // since we don't expect to resume again any time soon. Otherwise we
     57  // may resume again soon so resources should be held for a little
     58  // while.
     59  virtual void Suspend(bool aCloseImmediately) = 0;
     60 
     61  // Resume any downloads that have been suspended.
     62  virtual void Resume() = 0;
     63 
     64  // The mode is initially MODE_METADATA.
     65  virtual void SetReadMode(MediaCacheStream::ReadMode aMode) = 0;
     66 
     67  // Returns true if the resource can be seeked to unbuffered ranges, i.e.
     68  // for an HTTP network stream this returns true if HTTP1.1 Byte Range
     69  // requests are supported by the connection/server.
     70  virtual bool IsTransportSeekable() = 0;
     71 
     72  // Get the current principal for the channel
     73  virtual already_AddRefed<nsIPrincipal> GetCurrentPrincipal() = 0;
     74 
     75  // Return true if the loading of this resource required cross-origin
     76  // redirects.
     77  virtual bool HadCrossOriginRedirects() = 0;
     78 
     79  /**
     80   * Open the stream. This creates a stream listener and returns it in
     81   * aStreamListener; this listener needs to be notified of incoming data.
     82   */
     83  virtual nsresult Open(nsIStreamListener** aStreamListener) = 0;
     84 
     85  // If this returns false, then we shouldn't try to clone this MediaResource
     86  // because its underlying resources are not suitable for reuse (e.g.
     87  // because the underlying connection has been lost, or this resource
     88  // just can't be safely cloned). If this returns true, CloneData could
     89  // still fail. If this returns false, CloneData should not be called.
     90  virtual bool CanClone() { return false; }
     91 
     92  // Create a new stream of the same type that refers to the same URI
     93  // with a new channel. Any cached data associated with the original
     94  // stream should be accessible in the new stream too.
     95  virtual already_AddRefed<BaseMediaResource> CloneData(
     96      MediaResourceCallback* aCallback) {
     97    return nullptr;
     98  }
     99 
    100  // Returns true if the resource is a live stream.
    101  virtual bool IsLiveStream() const { return false; }
    102 
    103  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
    104    // Might be useful to track in the future:
    105    // - mChannel
    106    // - mURI (possibly owned, looks like just a ref from mChannel)
    107    // Not owned:
    108    // - mCallback
    109    return 0;
    110  }
    111 
    112  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
    113    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
    114  }
    115 
    116  virtual void GetDebugInfo(dom::MediaResourceDebugInfo& aInfo) {}
    117 
    118 protected:
    119  BaseMediaResource(MediaResourceCallback* aCallback, nsIChannel* aChannel,
    120                    nsIURI* aURI)
    121      : mCallback(aCallback),
    122        mChannel(aChannel),
    123        mURI(aURI),
    124        mLoadInBackground(false) {}
    125  virtual ~BaseMediaResource() = default;
    126 
    127  // Set the request's load flags to aFlags.  If the request is part of a
    128  // load group, the request is removed from the group, the flags are set, and
    129  // then the request is added back to the load group.
    130  nsresult ModifyLoadFlags(nsLoadFlags aFlags);
    131 
    132  RefPtr<MediaResourceCallback> mCallback;
    133 
    134  // Channel used to download the media data. Must be accessed
    135  // from the main thread only.
    136  nsCOMPtr<nsIChannel> mChannel;
    137 
    138  // URI in case the stream needs to be re-opened. Access from
    139  // main thread only.
    140  nsCOMPtr<nsIURI> mURI;
    141 
    142  // True if SetLoadInBackground() has been called with
    143  // aLoadInBackground = true, i.e. when the document load event is not
    144  // blocked by this resource, and all channel loads will be in the
    145  // background.
    146  bool mLoadInBackground;
    147 };
    148 
    149 }  // namespace mozilla
    150 
    151 #endif  // BaseMediaResource_h