1 // Copyright 2017 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_DOWNLOAD_INTERNAL_COMMON_DOWNLOAD_WORKER_H_ 6 #define COMPONENTS_DOWNLOAD_INTERNAL_COMMON_DOWNLOAD_WORKER_H_ 7 8 #include <memory> 9 10 #include "base/macros.h" 11 #include "base/memory/scoped_refptr.h" 12 #include "base/memory/weak_ptr.h" 13 #include "components/download/public/common/download_export.h" 14 #include "components/download/public/common/download_url_parameters.h" 15 #include "components/download/public/common/url_download_handler.h" 16 #include "mojo/public/cpp/bindings/pending_remote.h" 17 #include "services/device/public/mojom/wake_lock_provider.mojom.h" 18 19 namespace download { 20 21 // Helper class used to send subsequent range requests to fetch slices of the 22 // file after handling response of the original non-range request. 23 // TODO(xingliu): we should consider to reuse this class for single connection 24 // download. 25 class COMPONENTS_DOWNLOAD_EXPORT DownloadWorker 26 : public UrlDownloadHandler::Delegate { 27 public: 28 class Delegate { 29 public: 30 // Called when the the input stream is established after server response is 31 // handled. The stream contains data starts from |offset| of the 32 // destination file. 33 virtual void OnInputStreamReady( 34 DownloadWorker* worker, 35 std::unique_ptr<InputStream> input_stream, 36 std::unique_ptr<DownloadCreateInfo> download_create_info) = 0; 37 }; 38 39 DownloadWorker(DownloadWorker::Delegate* delegate, int64_t offset); 40 virtual ~DownloadWorker(); 41 offset()42 int64_t offset() const { return offset_; } 43 44 // Send network request to ask for a download. 45 void SendRequest( 46 std::unique_ptr<DownloadUrlParameters> params, 47 URLLoaderFactoryProvider* url_loader_factory_provider, 48 mojo::PendingRemote<device::mojom::WakeLockProvider> wake_lock_provider); 49 50 // Download operations. 51 void Pause(); 52 void Resume(); 53 void Cancel(bool user_cancel); 54 55 private: 56 // UrlDownloader::Delegate implementation. 57 void OnUrlDownloadStarted( 58 std::unique_ptr<DownloadCreateInfo> create_info, 59 std::unique_ptr<InputStream> input_stream, 60 URLLoaderFactoryProvider::URLLoaderFactoryProviderPtr 61 url_loader_factory_provider, 62 UrlDownloadHandler* downloader, 63 DownloadUrlParameters::OnStartedCallback callback) override; 64 void OnUrlDownloadStopped(UrlDownloadHandler* downloader) override; 65 void OnUrlDownloadHandlerCreated( 66 UrlDownloadHandler::UniqueUrlDownloadHandlerPtr downloader) override; 67 68 DownloadWorker::Delegate* const delegate_; 69 70 // The starting position of the content for this worker to download. 71 int64_t offset_; 72 73 // States of the worker. 74 bool is_paused_; 75 bool is_canceled_; 76 77 // Used to handle the url request. Live and die on IO thread. 78 UrlDownloadHandler::UniqueUrlDownloadHandlerPtr url_download_handler_; 79 80 base::WeakPtrFactory<DownloadWorker> weak_factory_{this}; 81 82 DISALLOW_COPY_AND_ASSIGN(DownloadWorker); 83 }; 84 85 } // namespace download 86 87 #endif // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_WORKER_H_ 88