browser_process_impl.cc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // Copyright (c) 2012 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 "shell/browser/browser_process_impl.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/command_line.h"
  8. #include "base/files/file_path.h"
  9. #include "base/path_service.h"
  10. #include "chrome/browser/browser_process.h"
  11. #include "chrome/common/chrome_paths.h"
  12. #include "chrome/common/chrome_switches.h"
  13. #include "components/os_crypt/async/browser/key_provider.h"
  14. #include "components/os_crypt/async/browser/os_crypt_async.h"
  15. #include "components/os_crypt/sync/os_crypt.h"
  16. #include "components/prefs/in_memory_pref_store.h"
  17. #include "components/prefs/json_pref_store.h"
  18. #include "components/prefs/overlay_user_pref_store.h"
  19. #include "components/prefs/pref_registry.h"
  20. #include "components/prefs/pref_registry_simple.h"
  21. #include "components/prefs/pref_service_factory.h"
  22. #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
  23. #include "components/proxy_config/proxy_config_dictionary.h"
  24. #include "components/proxy_config/proxy_config_pref_names.h"
  25. #include "content/public/browser/child_process_security_policy.h"
  26. #include "content/public/browser/network_quality_observer_factory.h"
  27. #include "content/public/browser/network_service_instance.h"
  28. #include "content/public/common/content_switches.h"
  29. #include "electron/fuses.h"
  30. #include "extensions/common/constants.h"
  31. #include "net/proxy_resolution/proxy_config.h"
  32. #include "net/proxy_resolution/proxy_config_service.h"
  33. #include "net/proxy_resolution/proxy_config_with_annotation.h"
  34. #include "services/device/public/cpp/geolocation/geolocation_manager.h"
  35. #include "services/network/public/cpp/network_switches.h"
  36. #include "shell/browser/net/resolve_proxy_helper.h"
  37. #include "shell/common/electron_paths.h"
  38. #include "shell/common/thread_restrictions.h"
  39. #if BUILDFLAG(ENABLE_PRINTING)
  40. #include "chrome/browser/printing/print_job_manager.h"
  41. #endif
  42. BrowserProcessImpl::BrowserProcessImpl() {
  43. g_browser_process = this;
  44. }
  45. BrowserProcessImpl::~BrowserProcessImpl() {
  46. g_browser_process = nullptr;
  47. }
  48. // static
  49. void BrowserProcessImpl::ApplyProxyModeFromCommandLine(
  50. ValueMapPrefStore* pref_store) {
  51. if (!pref_store)
  52. return;
  53. auto* command_line = base::CommandLine::ForCurrentProcess();
  54. if (command_line->HasSwitch(switches::kNoProxyServer)) {
  55. pref_store->SetValue(proxy_config::prefs::kProxy,
  56. base::Value(ProxyConfigDictionary::CreateDirect()),
  57. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  58. } else if (command_line->HasSwitch(switches::kProxyPacUrl)) {
  59. std::string pac_script_url =
  60. command_line->GetSwitchValueASCII(switches::kProxyPacUrl);
  61. pref_store->SetValue(proxy_config::prefs::kProxy,
  62. base::Value(ProxyConfigDictionary::CreatePacScript(
  63. pac_script_url, false /* pac_mandatory */)),
  64. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  65. } else if (command_line->HasSwitch(switches::kProxyAutoDetect)) {
  66. pref_store->SetValue(proxy_config::prefs::kProxy,
  67. base::Value(ProxyConfigDictionary::CreateAutoDetect()),
  68. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  69. } else if (command_line->HasSwitch(switches::kProxyServer)) {
  70. std::string proxy_server =
  71. command_line->GetSwitchValueASCII(switches::kProxyServer);
  72. std::string bypass_list =
  73. command_line->GetSwitchValueASCII(switches::kProxyBypassList);
  74. pref_store->SetValue(proxy_config::prefs::kProxy,
  75. base::Value(ProxyConfigDictionary::CreateFixedServers(
  76. proxy_server, bypass_list)),
  77. WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
  78. }
  79. }
  80. BuildState* BrowserProcessImpl::GetBuildState() {
  81. NOTIMPLEMENTED();
  82. return nullptr;
  83. }
  84. void BrowserProcessImpl::PostEarlyInitialization() {
  85. PrefServiceFactory prefs_factory;
  86. auto pref_registry = base::MakeRefCounted<PrefRegistrySimple>();
  87. PrefProxyConfigTrackerImpl::RegisterPrefs(pref_registry.get());
  88. #if BUILDFLAG(IS_WIN)
  89. OSCrypt::RegisterLocalPrefs(pref_registry.get());
  90. #endif
  91. in_memory_pref_store_ = base::MakeRefCounted<ValueMapPrefStore>();
  92. ApplyProxyModeFromCommandLine(in_memory_pref_store());
  93. prefs_factory.set_command_line_prefs(in_memory_pref_store());
  94. // Only use a persistent prefs store when cookie encryption is enabled as that
  95. // is the only key that needs it
  96. base::FilePath prefs_path;
  97. CHECK(base::PathService::Get(electron::DIR_SESSION_DATA, &prefs_path));
  98. prefs_path = prefs_path.Append(FILE_PATH_LITERAL("Local State"));
  99. electron::ScopedAllowBlockingForElectron allow_blocking;
  100. scoped_refptr<JsonPrefStore> user_pref_store =
  101. base::MakeRefCounted<JsonPrefStore>(prefs_path);
  102. user_pref_store->ReadPrefs();
  103. prefs_factory.set_user_prefs(user_pref_store);
  104. local_state_ = prefs_factory.Create(std::move(pref_registry));
  105. }
  106. void BrowserProcessImpl::PreCreateThreads() {
  107. // chrome-extension:// URLs are safe to request anywhere, but may only
  108. // commit (including in iframes) in extension processes.
  109. content::ChildProcessSecurityPolicy::GetInstance()
  110. ->RegisterWebSafeIsolatedScheme(extensions::kExtensionScheme, true);
  111. // Must be created before the IOThread.
  112. // Once IOThread class is no longer needed,
  113. // this can be created on first use.
  114. if (!SystemNetworkContextManager::GetInstance())
  115. SystemNetworkContextManager::CreateInstance(local_state_.get());
  116. }
  117. void BrowserProcessImpl::PreMainMessageLoopRun() {
  118. CreateNetworkQualityObserver();
  119. CreateOSCryptAsync();
  120. }
  121. void BrowserProcessImpl::PostMainMessageLoopRun() {
  122. if (local_state_)
  123. local_state_->CommitPendingWrite();
  124. // This expects to be destroyed before the task scheduler is torn down.
  125. SystemNetworkContextManager::DeleteInstance();
  126. }
  127. bool BrowserProcessImpl::IsShuttingDown() {
  128. return false;
  129. }
  130. metrics_services_manager::MetricsServicesManager*
  131. BrowserProcessImpl::GetMetricsServicesManager() {
  132. return nullptr;
  133. }
  134. metrics::MetricsService* BrowserProcessImpl::metrics_service() {
  135. return nullptr;
  136. }
  137. ProfileManager* BrowserProcessImpl::profile_manager() {
  138. return nullptr;
  139. }
  140. PrefService* BrowserProcessImpl::local_state() {
  141. DCHECK(local_state_.get());
  142. return local_state_.get();
  143. }
  144. scoped_refptr<network::SharedURLLoaderFactory>
  145. BrowserProcessImpl::shared_url_loader_factory() {
  146. return system_network_context_manager()->GetSharedURLLoaderFactory();
  147. }
  148. variations::VariationsService* BrowserProcessImpl::variations_service() {
  149. return nullptr;
  150. }
  151. BrowserProcessPlatformPart* BrowserProcessImpl::platform_part() {
  152. return nullptr;
  153. }
  154. extensions::EventRouterForwarder*
  155. BrowserProcessImpl::extension_event_router_forwarder() {
  156. return nullptr;
  157. }
  158. NotificationUIManager* BrowserProcessImpl::notification_ui_manager() {
  159. return nullptr;
  160. }
  161. NotificationPlatformBridge* BrowserProcessImpl::notification_platform_bridge() {
  162. return nullptr;
  163. }
  164. SystemNetworkContextManager*
  165. BrowserProcessImpl::system_network_context_manager() {
  166. DCHECK(SystemNetworkContextManager::GetInstance());
  167. return SystemNetworkContextManager::GetInstance();
  168. }
  169. network::NetworkQualityTracker* BrowserProcessImpl::network_quality_tracker() {
  170. return nullptr;
  171. }
  172. embedder_support::OriginTrialsSettingsStorage*
  173. BrowserProcessImpl::GetOriginTrialsSettingsStorage() {
  174. return &origin_trials_settings_storage_;
  175. }
  176. policy::ChromeBrowserPolicyConnector*
  177. BrowserProcessImpl::browser_policy_connector() {
  178. return nullptr;
  179. }
  180. policy::PolicyService* BrowserProcessImpl::policy_service() {
  181. return nullptr;
  182. }
  183. IconManager* BrowserProcessImpl::icon_manager() {
  184. return nullptr;
  185. }
  186. GpuModeManager* BrowserProcessImpl::gpu_mode_manager() {
  187. return nullptr;
  188. }
  189. printing::PrintPreviewDialogController*
  190. BrowserProcessImpl::print_preview_dialog_controller() {
  191. return nullptr;
  192. }
  193. printing::BackgroundPrintingManager*
  194. BrowserProcessImpl::background_printing_manager() {
  195. return nullptr;
  196. }
  197. IntranetRedirectDetector* BrowserProcessImpl::intranet_redirect_detector() {
  198. return nullptr;
  199. }
  200. DownloadStatusUpdater* BrowserProcessImpl::download_status_updater() {
  201. return nullptr;
  202. }
  203. DownloadRequestLimiter* BrowserProcessImpl::download_request_limiter() {
  204. return nullptr;
  205. }
  206. BackgroundModeManager* BrowserProcessImpl::background_mode_manager() {
  207. return nullptr;
  208. }
  209. StatusTray* BrowserProcessImpl::status_tray() {
  210. return nullptr;
  211. }
  212. safe_browsing::SafeBrowsingService*
  213. BrowserProcessImpl::safe_browsing_service() {
  214. return nullptr;
  215. }
  216. subresource_filter::RulesetService*
  217. BrowserProcessImpl::subresource_filter_ruleset_service() {
  218. return nullptr;
  219. }
  220. component_updater::ComponentUpdateService*
  221. BrowserProcessImpl::component_updater() {
  222. return nullptr;
  223. }
  224. MediaFileSystemRegistry* BrowserProcessImpl::media_file_system_registry() {
  225. return nullptr;
  226. }
  227. WebRtcLogUploader* BrowserProcessImpl::webrtc_log_uploader() {
  228. return nullptr;
  229. }
  230. network_time::NetworkTimeTracker* BrowserProcessImpl::network_time_tracker() {
  231. return nullptr;
  232. }
  233. gcm::GCMDriver* BrowserProcessImpl::gcm_driver() {
  234. return nullptr;
  235. }
  236. resource_coordinator::ResourceCoordinatorParts*
  237. BrowserProcessImpl::resource_coordinator_parts() {
  238. return nullptr;
  239. }
  240. resource_coordinator::TabManager* BrowserProcessImpl::GetTabManager() {
  241. return nullptr;
  242. }
  243. SerialPolicyAllowedPorts* BrowserProcessImpl::serial_policy_allowed_ports() {
  244. return nullptr;
  245. }
  246. HidSystemTrayIcon* BrowserProcessImpl::hid_system_tray_icon() {
  247. return nullptr;
  248. }
  249. UsbSystemTrayIcon* BrowserProcessImpl::usb_system_tray_icon() {
  250. return nullptr;
  251. }
  252. os_crypt_async::OSCryptAsync* BrowserProcessImpl::os_crypt_async() {
  253. return os_crypt_async_.get();
  254. }
  255. void BrowserProcessImpl::SetSystemLocale(const std::string& locale) {
  256. system_locale_ = locale;
  257. }
  258. const std::string& BrowserProcessImpl::GetSystemLocale() const {
  259. return system_locale_;
  260. }
  261. electron::ResolveProxyHelper* BrowserProcessImpl::GetResolveProxyHelper() {
  262. if (!resolve_proxy_helper_) {
  263. resolve_proxy_helper_ = base::MakeRefCounted<electron::ResolveProxyHelper>(
  264. system_network_context_manager()->GetContext());
  265. }
  266. return resolve_proxy_helper_.get();
  267. }
  268. #if BUILDFLAG(IS_LINUX)
  269. void BrowserProcessImpl::SetLinuxStorageBackend(
  270. os_crypt::SelectedLinuxBackend selected_backend) {
  271. switch (selected_backend) {
  272. case os_crypt::SelectedLinuxBackend::BASIC_TEXT:
  273. selected_linux_storage_backend_ = "basic_text";
  274. break;
  275. case os_crypt::SelectedLinuxBackend::GNOME_LIBSECRET:
  276. selected_linux_storage_backend_ = "gnome_libsecret";
  277. break;
  278. case os_crypt::SelectedLinuxBackend::KWALLET:
  279. selected_linux_storage_backend_ = "kwallet";
  280. break;
  281. case os_crypt::SelectedLinuxBackend::KWALLET5:
  282. selected_linux_storage_backend_ = "kwallet5";
  283. break;
  284. case os_crypt::SelectedLinuxBackend::KWALLET6:
  285. selected_linux_storage_backend_ = "kwallet6";
  286. break;
  287. case os_crypt::SelectedLinuxBackend::DEFER:
  288. NOTREACHED();
  289. break;
  290. }
  291. }
  292. #endif // BUILDFLAG(IS_LINUX)
  293. void BrowserProcessImpl::SetApplicationLocale(const std::string& locale) {
  294. locale_ = locale;
  295. }
  296. const std::string& BrowserProcessImpl::GetApplicationLocale() {
  297. return locale_;
  298. }
  299. printing::PrintJobManager* BrowserProcessImpl::print_job_manager() {
  300. #if BUILDFLAG(ENABLE_PRINTING)
  301. if (!print_job_manager_)
  302. print_job_manager_ = std::make_unique<printing::PrintJobManager>();
  303. return print_job_manager_.get();
  304. #else
  305. return nullptr;
  306. #endif
  307. }
  308. StartupData* BrowserProcessImpl::startup_data() {
  309. return nullptr;
  310. }
  311. network::NetworkQualityTracker* BrowserProcessImpl::GetNetworkQualityTracker() {
  312. if (!network_quality_tracker_) {
  313. network_quality_tracker_ = std::make_unique<network::NetworkQualityTracker>(
  314. base::BindRepeating(&content::GetNetworkService));
  315. }
  316. return network_quality_tracker_.get();
  317. }
  318. void BrowserProcessImpl::CreateNetworkQualityObserver() {
  319. DCHECK(!network_quality_observer_);
  320. network_quality_observer_ =
  321. content::CreateNetworkQualityObserver(GetNetworkQualityTracker());
  322. DCHECK(network_quality_observer_);
  323. }
  324. void BrowserProcessImpl::CreateOSCryptAsync() {
  325. // source: https://chromium-review.googlesource.com/c/chromium/src/+/4455776
  326. // For now, initialize OSCryptAsync with no providers. This delegates all
  327. // encryption operations to OSCrypt.
  328. // TODO(crbug.com/1373092): Add providers behind features, as support for them
  329. // is added.
  330. os_crypt_async_ = std::make_unique<os_crypt_async::OSCryptAsync>(
  331. std::vector<
  332. std::pair<size_t, std::unique_ptr<os_crypt_async::KeyProvider>>>());
  333. // Trigger async initialization of OSCrypt key providers.
  334. std::ignore = os_crypt_async_->GetInstance(base::DoNothing());
  335. }