File: download_service_factory_helper.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (163 lines) | stat: -rw-r--r-- 8,043 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/download/content/factory/download_service_factory_helper.h"

#include <utility>

#include "base/files/file_path.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "components/download/content/factory/navigation_monitor_factory.h"
#include "components/download/content/internal/download_driver_impl.h"
#include "components/download/internal/background_service/client_set.h"
#include "components/download/internal/background_service/config.h"
#include "components/download/internal/background_service/controller_impl.h"
#include "components/download/internal/background_service/download_store.h"
#include "components/download/internal/background_service/empty_file_monitor.h"
#include "components/download/internal/background_service/file_monitor_impl.h"
#include "components/download/internal/background_service/in_memory_download_driver.h"
#include "components/download/internal/background_service/init_aware_background_download_service.h"
#include "components/download/internal/background_service/logger_impl.h"
#include "components/download/internal/background_service/model_impl.h"
#include "components/download/internal/background_service/noop_store.h"
#include "components/download/internal/background_service/proto/entry.pb.h"
#include "components/download/internal/background_service/scheduler/scheduler_impl.h"
#include "components/download/public/common/simple_download_manager_coordinator.h"
#include "components/download/public/task/empty_task_scheduler.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"

#if BUILDFLAG(IS_ANDROID)
#include "components/download/internal/background_service/android/battery_status_listener_android.h"
#include "components/download/network/android/network_status_listener_android.h"
#elif BUILDFLAG(IS_APPLE)
#include "components/download/internal/background_service/scheduler/battery_status_listener_mac.h"
#include "components/download/network/network_status_listener_impl.h"
#else
#include "components/download/internal/background_service/scheduler/battery_status_listener_impl.h"
#include "components/download/network/network_status_listener_impl.h"
#endif

namespace download {
namespace {
const base::FilePath::CharType kEntryDBStorageDir[] =
    FILE_PATH_LITERAL("EntryDB");
const base::FilePath::CharType kFilesStorageDir[] = FILE_PATH_LITERAL("Files");
}  // namespace

// Helper function to create download service with different implementation
// details.
std::unique_ptr<BackgroundDownloadService> CreateDownloadServiceInternal(
    SimpleFactoryKey* simple_factory_key,
    std::unique_ptr<DownloadClientMap> clients,
    std::unique_ptr<Configuration> config,
    std::unique_ptr<DownloadDriver> driver,
    std::unique_ptr<Store> store,
    std::unique_ptr<TaskScheduler> task_scheduler,
    std::unique_ptr<FileMonitor> file_monitor,
    network::NetworkConnectionTracker* network_connection_tracker,
    const base::FilePath& files_storage_dir) {
  auto client_set = std::make_unique<ClientSet>(std::move(clients));
  auto model = std::make_unique<ModelImpl>(std::move(store));

// Build platform network/battery status listener.
#if BUILDFLAG(IS_ANDROID)
  auto battery_listener = std::make_unique<BatteryStatusListenerAndroid>(
      config->battery_query_interval);
  auto network_listener = std::make_unique<NetworkStatusListenerAndroid>();
#elif BUILDFLAG(IS_APPLE)
  auto battery_listener = std::make_unique<BatteryStatusListenerMac>();
  auto network_listener =
      std::make_unique<NetworkStatusListenerImpl>(network_connection_tracker);
#else
  auto battery_listener = std::make_unique<BatteryStatusListenerImpl>(
      config->battery_query_interval);
  auto network_listener =
      std::make_unique<NetworkStatusListenerImpl>(network_connection_tracker);
#endif

  auto device_status_listener = std::make_unique<DeviceStatusListener>(
      config->network_startup_delay, config->network_change_delay,
      std::move(battery_listener), std::move(network_listener));
  NavigationMonitor* navigation_monitor =
      NavigationMonitorFactory::GetForKey(simple_factory_key);
  auto scheduler = std::make_unique<SchedulerImpl>(
      task_scheduler.get(), config.get(), client_set.get());
  auto logger = std::make_unique<LoggerImpl>();
  auto* logger_ptr = logger.get();
  auto controller = std::make_unique<ControllerImpl>(
      std::move(config), std::move(logger), logger_ptr, std::move(client_set),
      std::move(driver), std::move(model), std::move(device_status_listener),
      navigation_monitor, std::move(scheduler), std::move(task_scheduler),
      std::move(file_monitor), files_storage_dir);
  logger_ptr->SetLogSource(controller.get());

  return std::make_unique<InitAwareBackgroundDownloadService>(
      std::move(controller));
}

// Create download service for normal profile.
std::unique_ptr<BackgroundDownloadService> BuildDownloadService(
    SimpleFactoryKey* simple_factory_key,
    std::unique_ptr<DownloadClientMap> clients,
    network::NetworkConnectionTracker* network_connection_tracker,
    const base::FilePath& storage_dir,
    SimpleDownloadManagerCoordinator* download_manager_coordinator,
    leveldb_proto::ProtoDatabaseProvider* proto_db_provider,
    const scoped_refptr<base::SequencedTaskRunner>& background_task_runner,
    std::unique_ptr<TaskScheduler> task_scheduler) {
  auto config = Configuration::CreateFromFinch();

  auto driver =
      std::make_unique<DownloadDriverImpl>(download_manager_coordinator);

  auto entry_db_storage_dir = storage_dir.Append(kEntryDBStorageDir);

  auto entry_db = proto_db_provider->GetDB<protodb::Entry>(
      leveldb_proto::ProtoDbType::DOWNLOAD_STORE, entry_db_storage_dir,
      background_task_runner);
  auto store = std::make_unique<DownloadStore>(std::move(entry_db));

  auto files_storage_dir = storage_dir.Append(kFilesStorageDir);
  auto file_monitor = std::make_unique<FileMonitorImpl>(files_storage_dir,
                                                        background_task_runner);

  return CreateDownloadServiceInternal(
      simple_factory_key, std::move(clients), std::move(config),
      std::move(driver), std::move(store), std::move(task_scheduler),
      std::move(file_monitor), network_connection_tracker, files_storage_dir);
}

// Create download service for incognito mode without any database or file IO.
std::unique_ptr<BackgroundDownloadService> BuildInMemoryDownloadService(
    SimpleFactoryKey* simple_factory_key,
    std::unique_ptr<DownloadClientMap> clients,
    network::NetworkConnectionTracker* network_connection_tracker,
    const base::FilePath& storage_dir,
    BlobContextGetterFactoryPtr blob_context_getter_factory,
    scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
    URLLoaderFactoryGetterPtr url_loader_factory_getter) {
  auto config = Configuration::CreateFromFinch();
  auto download_factory =
      std::make_unique<InMemoryDownloadFactory>(io_task_runner);
  auto driver = std::make_unique<InMemoryDownloadDriver>(
      std::move(download_factory), std::move(blob_context_getter_factory),
      std::move(url_loader_factory_getter));
  auto store = std::make_unique<NoopStore>();
  auto task_scheduler = std::make_unique<EmptyTaskScheduler>();

  // TODO(xingliu): Remove |files_storage_dir| and |storage_dir| for incognito
  // mode. See https://crbug.com/810202.
  auto files_storage_dir = storage_dir.Append(kFilesStorageDir);
  auto file_monitor = std::make_unique<EmptyFileMonitor>();

  return CreateDownloadServiceInternal(
      simple_factory_key, std::move(clients), std::move(config),
      std::move(driver), std::move(store), std::move(task_scheduler),
      std::move(file_monitor), network_connection_tracker, files_storage_dir);
}

}  // namespace download