browser_process_impl.cc 13 KB

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