background_download_service_factory.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright 2021 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. #include "weblayer/browser/background_download_service_factory.h"
  5. #include "base/files/file_path.h"
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/no_destructor.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "base/task/single_thread_task_runner.h"
  10. #include "base/task/task_traits.h"
  11. #include "base/task/thread_pool.h"
  12. #include "components/background_fetch/download_client.h"
  13. #include "components/download/content/factory/download_service_factory_helper.h"
  14. #include "components/download/content/factory/navigation_monitor_factory.h"
  15. #include "components/download/public/background_service/background_download_service.h"
  16. #include "components/download/public/background_service/basic_task_scheduler.h"
  17. #include "components/download/public/background_service/blob_context_getter_factory.h"
  18. #include "components/download/public/background_service/clients.h"
  19. #include "components/download/public/common/simple_download_manager_coordinator.h"
  20. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  21. #include "components/leveldb_proto/public/proto_database_provider.h"
  22. #include "content/public/browser/browser_context.h"
  23. #include "content/public/browser/browser_task_traits.h"
  24. #include "content/public/browser/browser_thread.h"
  25. #include "content/public/browser/download_manager.h"
  26. #include "content/public/browser/network_service_instance.h"
  27. #include "content/public/browser/storage_partition.h"
  28. #include "weblayer/browser/browser_context_impl.h"
  29. #include "weblayer/browser/profile_impl.h"
  30. #include "weblayer/browser/system_network_context_manager.h"
  31. namespace weblayer {
  32. namespace {
  33. // Like BackgroundDownloadServiceFactory, this is a
  34. // BrowserContextKeyedServiceFactory although the Chrome version is a
  35. // SimpleKeyedServiceFactory.
  36. class SimpleDownloadManagerCoordinatorFactory
  37. : public BrowserContextKeyedServiceFactory {
  38. public:
  39. // Returns singleton instance of SimpleDownloadManagerCoordinatorFactory.
  40. static SimpleDownloadManagerCoordinatorFactory* GetInstance() {
  41. static base::NoDestructor<SimpleDownloadManagerCoordinatorFactory> instance;
  42. return instance.get();
  43. }
  44. static download::SimpleDownloadManagerCoordinator* GetForBrowserContext(
  45. content::BrowserContext* context) {
  46. return static_cast<download::SimpleDownloadManagerCoordinator*>(
  47. GetInstance()->GetServiceForBrowserContext(context, true));
  48. }
  49. SimpleDownloadManagerCoordinatorFactory(
  50. const SimpleDownloadManagerCoordinatorFactory& other) = delete;
  51. SimpleDownloadManagerCoordinatorFactory& operator=(
  52. const SimpleDownloadManagerCoordinatorFactory& other) = delete;
  53. private:
  54. friend class base::NoDestructor<SimpleDownloadManagerCoordinatorFactory>;
  55. SimpleDownloadManagerCoordinatorFactory()
  56. : BrowserContextKeyedServiceFactory(
  57. "SimpleDownloadManagerCoordinator",
  58. BrowserContextDependencyManager::GetInstance()) {}
  59. ~SimpleDownloadManagerCoordinatorFactory() override = default;
  60. // BrowserContextKeyedService:
  61. KeyedService* BuildServiceInstanceFor(
  62. content::BrowserContext* context) const override {
  63. auto* service_instance = new download::SimpleDownloadManagerCoordinator(
  64. {}, !context->IsOffTheRecord());
  65. service_instance->SetSimpleDownloadManager(context->GetDownloadManager(),
  66. true);
  67. return service_instance;
  68. }
  69. content::BrowserContext* GetBrowserContextToUse(
  70. content::BrowserContext* context) const override {
  71. return context;
  72. }
  73. };
  74. // An implementation of BlobContextGetterFactory that returns the
  75. // BlobStorageContext without delay (since WebLayer must be in "full browser"
  76. // mode).
  77. class DownloadBlobContextGetterFactory
  78. : public download::BlobContextGetterFactory {
  79. public:
  80. explicit DownloadBlobContextGetterFactory(
  81. content::BrowserContext* browser_context)
  82. : browser_context_(browser_context) {}
  83. DownloadBlobContextGetterFactory(const DownloadBlobContextGetterFactory&) =
  84. delete;
  85. DownloadBlobContextGetterFactory& operator=(
  86. const DownloadBlobContextGetterFactory&) = delete;
  87. ~DownloadBlobContextGetterFactory() override = default;
  88. private:
  89. // download::BlobContextGetterFactory:
  90. void RetrieveBlobContextGetter(
  91. download::BlobContextGetterCallback callback) override {
  92. std::move(callback).Run(browser_context_->GetBlobStorageContext());
  93. }
  94. raw_ptr<content::BrowserContext> browser_context_;
  95. };
  96. } // namespace
  97. // static
  98. download::BackgroundDownloadService*
  99. BackgroundDownloadServiceFactory::GetForBrowserContext(
  100. content::BrowserContext* browser_context) {
  101. return static_cast<download::BackgroundDownloadService*>(
  102. GetInstance()->GetServiceForBrowserContext(browser_context,
  103. /*create=*/true));
  104. }
  105. // static
  106. BackgroundDownloadServiceFactory*
  107. BackgroundDownloadServiceFactory::GetInstance() {
  108. static base::NoDestructor<BackgroundDownloadServiceFactory> factory;
  109. return factory.get();
  110. }
  111. BackgroundDownloadServiceFactory::BackgroundDownloadServiceFactory()
  112. : BrowserContextKeyedServiceFactory(
  113. "BackgroundDownloadServiceFactory",
  114. BrowserContextDependencyManager::GetInstance()) {
  115. DependsOn(SimpleDownloadManagerCoordinatorFactory::GetInstance());
  116. DependsOn(download::NavigationMonitorFactory::GetInstance());
  117. }
  118. KeyedService* BackgroundDownloadServiceFactory::BuildServiceInstanceFor(
  119. content::BrowserContext* context) const {
  120. SimpleFactoryKey* key = ProfileImpl::FromBrowserContext(context)
  121. ->GetBrowserContext()
  122. ->simple_factory_key();
  123. auto clients = std::make_unique<download::DownloadClientMap>();
  124. clients->insert(std::make_pair(
  125. download::DownloadClient::BACKGROUND_FETCH,
  126. std::make_unique<background_fetch::DownloadClient>(context)));
  127. // Build in memory download service for an off the record context.
  128. if (context->IsOffTheRecord()) {
  129. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner =
  130. content::GetIOThreadTaskRunner({});
  131. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory =
  132. SystemNetworkContextManager::GetInstance()->GetSharedURLLoaderFactory();
  133. return download::BuildInMemoryDownloadService(
  134. key, std::move(clients), content::GetNetworkConnectionTracker(),
  135. base::FilePath(),
  136. std::make_unique<DownloadBlobContextGetterFactory>(context),
  137. io_task_runner, url_loader_factory)
  138. .release();
  139. }
  140. // Build download service for a regular browsing context.
  141. base::FilePath storage_dir;
  142. if (!context->IsOffTheRecord() && !context->GetPath().empty()) {
  143. const base::FilePath::CharType kDownloadServiceStorageDirname[] =
  144. FILE_PATH_LITERAL("Download Service");
  145. storage_dir = context->GetPath().Append(kDownloadServiceStorageDirname);
  146. }
  147. scoped_refptr<base::SequencedTaskRunner> background_task_runner =
  148. base::ThreadPool::CreateSequencedTaskRunner(
  149. {base::MayBlock(), base::TaskPriority::BEST_EFFORT});
  150. leveldb_proto::ProtoDatabaseProvider* proto_db_provider =
  151. context->GetDefaultStoragePartition()->GetProtoDatabaseProvider();
  152. return download::BuildDownloadService(
  153. key, std::move(clients), content::GetNetworkConnectionTracker(),
  154. storage_dir,
  155. SimpleDownloadManagerCoordinatorFactory::GetForBrowserContext(
  156. context),
  157. proto_db_provider, background_task_runner,
  158. std::make_unique<download::BasicTaskScheduler>(base::BindRepeating(
  159. [](content::BrowserContext* context) {
  160. return BackgroundDownloadServiceFactory::
  161. GetForBrowserContext(context);
  162. },
  163. context)))
  164. .release();
  165. }
  166. content::BrowserContext*
  167. BackgroundDownloadServiceFactory::GetBrowserContextToUse(
  168. content::BrowserContext* context) const {
  169. return context;
  170. }
  171. } // namespace weblayer