nsPrefetchService.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. #ifndef nsPrefetchService_h__
  5. #define nsPrefetchService_h__
  6. #include "nsCPrefetchService.h"
  7. #include "nsIObserver.h"
  8. #include "nsIInterfaceRequestor.h"
  9. #include "nsIChannelEventSink.h"
  10. #include "nsIRedirectResultListener.h"
  11. #include "nsIWebProgressListener.h"
  12. #include "nsIStreamListener.h"
  13. #include "nsIChannel.h"
  14. #include "nsIURI.h"
  15. #include "nsWeakReference.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsAutoPtr.h"
  18. #include "mozilla/Attributes.h"
  19. #include <deque>
  20. class nsPrefetchService;
  21. class nsPrefetchNode;
  22. //-----------------------------------------------------------------------------
  23. // nsPrefetchService
  24. //-----------------------------------------------------------------------------
  25. class nsPrefetchService final : public nsIPrefetchService
  26. , public nsIWebProgressListener
  27. , public nsIObserver
  28. , public nsSupportsWeakReference
  29. {
  30. public:
  31. NS_DECL_ISUPPORTS
  32. NS_DECL_NSIPREFETCHSERVICE
  33. NS_DECL_NSIWEBPROGRESSLISTENER
  34. NS_DECL_NSIOBSERVER
  35. nsPrefetchService();
  36. nsresult Init();
  37. void ProcessNextURI(nsPrefetchNode *aFinished);
  38. void NotifyLoadRequested(nsPrefetchNode *node);
  39. void NotifyLoadCompleted(nsPrefetchNode *node);
  40. void DispatchEvent(nsPrefetchNode *node, bool aSuccess);
  41. private:
  42. ~nsPrefetchService();
  43. nsresult Prefetch(nsIURI *aURI,
  44. nsIURI *aReferrerURI,
  45. nsIDOMNode *aSource,
  46. bool aExplicit);
  47. void AddProgressListener();
  48. void RemoveProgressListener();
  49. nsresult EnqueueURI(nsIURI *aURI, nsIURI *aReferrerURI,
  50. nsIDOMNode *aSource, nsPrefetchNode **node);
  51. void EmptyQueue();
  52. void StartPrefetching();
  53. void StopPrefetching();
  54. std::deque<RefPtr<nsPrefetchNode>> mQueue;
  55. nsTArray<RefPtr<nsPrefetchNode>> mCurrentNodes;
  56. int32_t mMaxParallelism;
  57. int32_t mStopCount;
  58. // true if pending document loads have ever reached zero.
  59. int32_t mHaveProcessed;
  60. bool mDisabled;
  61. // In usual case prefetch does not start until all normal loads are done.
  62. // Aggressive mode ignores normal loads and just start prefetch ASAP.
  63. // It's mainly for testing purpose and discoraged for normal use;
  64. // see https://bugzilla.mozilla.org/show_bug.cgi?id=1281415 for details.
  65. bool mAggressive;
  66. };
  67. //-----------------------------------------------------------------------------
  68. // nsPrefetchNode
  69. //-----------------------------------------------------------------------------
  70. class nsPrefetchNode final : public nsIStreamListener
  71. , public nsIInterfaceRequestor
  72. , public nsIChannelEventSink
  73. , public nsIRedirectResultListener
  74. {
  75. public:
  76. NS_DECL_ISUPPORTS
  77. NS_DECL_NSIREQUESTOBSERVER
  78. NS_DECL_NSISTREAMLISTENER
  79. NS_DECL_NSIINTERFACEREQUESTOR
  80. NS_DECL_NSICHANNELEVENTSINK
  81. NS_DECL_NSIREDIRECTRESULTLISTENER
  82. nsPrefetchNode(nsPrefetchService *aPrefetchService,
  83. nsIURI *aURI,
  84. nsIURI *aReferrerURI,
  85. nsIDOMNode *aSource);
  86. nsresult OpenChannel();
  87. nsresult CancelChannel(nsresult error);
  88. nsCOMPtr<nsIURI> mURI;
  89. nsCOMPtr<nsIURI> mReferrerURI;
  90. nsTArray<nsCOMPtr<nsIWeakReference>> mSources;
  91. private:
  92. ~nsPrefetchNode() {}
  93. RefPtr<nsPrefetchService> mService;
  94. nsCOMPtr<nsIChannel> mChannel;
  95. nsCOMPtr<nsIChannel> mRedirectChannel;
  96. int64_t mBytesRead;
  97. bool mShouldFireLoadEvent;
  98. };
  99. #endif // !nsPrefetchService_h__