tor-browser

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

NetworkLoadHandler.h (2890B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 
      7 #ifndef mozilla_dom_workers_NetworkLoadHandler_h__
      8 #define mozilla_dom_workers_NetworkLoadHandler_h__
      9 
     10 #include "mozilla/dom/ScriptLoadHandler.h"
     11 #include "mozilla/dom/WorkerLoadContext.h"
     12 #include "mozilla/dom/WorkerRef.h"
     13 #include "nsIStreamLoader.h"
     14 
     15 namespace mozilla::dom::workerinternals::loader {
     16 
     17 class WorkerScriptLoader;
     18 
     19 /*
     20 * [DOMDOC] NetworkLoadHandler for Workers
     21 *
     22 * A LoadHandler is a ScriptLoader helper class that reacts to an
     23 * nsIStreamLoader's events for
     24 * loading JS scripts. It is primarily responsible for decoding the stream into
     25 * UTF8 or UTF16. Additionally, it takes care of any work that needs to follow
     26 * the completion of a stream. Every LoadHandler also manages additional tasks
     27 * for the type of load that it is doing.
     28 *
     29 * As part of worker loading we have an number of tasks that we need to take
     30 * care of after a successfully completed stream, including setting a final URI
     31 * on the WorkerPrivate if we have loaded a main script, or handling CSP issues.
     32 * These are handled in DataReceivedFromNetwork, and implement roughly the same
     33 * set of tasks as you will find in the CacheLoadhandler, which has a companion
     34 * method DataReceivedFromcache.
     35 *
     36 * In the worker context, the LoadHandler is run on the main thread, and all
     37 * work in this file ultimately is done by the main thread, including decoding.
     38 *
     39 */
     40 
     41 class NetworkLoadHandler final : public nsIStreamLoaderObserver,
     42                                 public nsIRequestObserver {
     43 public:
     44  NS_DECL_ISUPPORTS
     45 
     46  NetworkLoadHandler(WorkerScriptLoader* aLoader,
     47                     ThreadSafeRequestHandle* aRequestHandle);
     48 
     49  NS_IMETHOD
     50  OnStreamComplete(nsIStreamLoader* aLoader, nsISupports* aContext,
     51                   nsresult aStatus, uint32_t aStringLen,
     52                   const uint8_t* aString) override;
     53 
     54  nsresult DataReceivedFromNetwork(nsIStreamLoader* aLoader, nsresult aStatus,
     55                                   uint32_t aStringLen, const uint8_t* aString);
     56 
     57  NS_IMETHOD
     58  OnStartRequest(nsIRequest* aRequest) override;
     59 
     60  nsresult PrepareForRequest(nsIRequest* aRequest);
     61 
     62  NS_IMETHOD
     63  OnStopRequest(nsIRequest* aRequest, nsresult aStatusCode) override {
     64    // Nothing to do here!
     65    return NS_OK;
     66  }
     67 
     68 private:
     69  ~NetworkLoadHandler() = default;
     70 
     71  RefPtr<WorkerScriptLoader> mLoader;
     72  UniquePtr<ScriptDecoder> mDecoder;
     73  RefPtr<ThreadSafeWorkerRef> mWorkerRef;
     74  RefPtr<ThreadSafeRequestHandle> mRequestHandle;
     75 };
     76 
     77 }  // namespace mozilla::dom::workerinternals::loader
     78 
     79 #endif /* mozilla_dom_workers_NetworkLoadHandler_h__ */