electron_browser_context.cc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/electron_browser_context.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <optional>
  8. #include <utility>
  9. #include "base/barrier_closure.h"
  10. #include "base/base_paths.h"
  11. #include "base/command_line.h"
  12. #include "base/containers/to_vector.h"
  13. #include "base/files/file_path.h"
  14. #include "base/no_destructor.h"
  15. #include "base/path_service.h"
  16. #include "base/strings/escape.h"
  17. #include "base/strings/string_number_conversions.h"
  18. #include "base/strings/string_util.h"
  19. #include "chrome/browser/predictors/preconnect_manager.h"
  20. #include "chrome/common/chrome_paths.h"
  21. #include "chrome/common/pref_names.h"
  22. #include "components/keyed_service/content/browser_context_dependency_manager.h"
  23. #include "components/prefs/json_pref_store.h"
  24. #include "components/prefs/pref_registry_simple.h"
  25. #include "components/prefs/pref_service.h"
  26. #include "components/prefs/pref_service_factory.h"
  27. #include "components/prefs/value_map_pref_store.h"
  28. #include "components/proxy_config/pref_proxy_config_tracker_impl.h"
  29. #include "components/proxy_config/proxy_config_pref_names.h"
  30. #include "content/browser/blob_storage/chrome_blob_storage_context.h" // nogncheck
  31. #include "content/public/browser/browser_thread.h"
  32. #include "content/public/browser/cors_origin_pattern_setter.h"
  33. #include "content/public/browser/host_zoom_map.h"
  34. #include "content/public/browser/render_process_host.h"
  35. #include "content/public/browser/shared_cors_origin_access_list.h"
  36. #include "content/public/browser/storage_partition.h"
  37. #include "content/public/browser/web_contents_media_capture_id.h"
  38. #include "gin/arguments.h"
  39. #include "media/audio/audio_device_description.h"
  40. #include "services/network/public/cpp/features.h"
  41. #include "services/network/public/cpp/url_loader_factory_builder.h"
  42. #include "services/network/public/cpp/wrapper_shared_url_loader_factory.h"
  43. #include "services/network/public/mojom/network_context.mojom.h"
  44. #include "shell/browser/cookie_change_notifier.h"
  45. #include "shell/browser/electron_browser_client.h"
  46. #include "shell/browser/electron_browser_main_parts.h"
  47. #include "shell/browser/electron_download_manager_delegate.h"
  48. #include "shell/browser/electron_permission_manager.h"
  49. #include "shell/browser/file_system_access/file_system_access_permission_context_factory.h"
  50. #include "shell/browser/media/media_device_id_salt.h"
  51. #include "shell/browser/net/resolve_proxy_helper.h"
  52. #include "shell/browser/protocol_registry.h"
  53. #include "shell/browser/serial/serial_chooser_context.h"
  54. #include "shell/browser/special_storage_policy.h"
  55. #include "shell/browser/ui/inspectable_web_contents.h"
  56. #include "shell/browser/ui/webui/accessibility_ui.h"
  57. #include "shell/browser/web_contents_permission_helper.h"
  58. #include "shell/browser/web_view_manager.h"
  59. #include "shell/browser/zoom_level_delegate.h"
  60. #include "shell/common/application_info.h"
  61. #include "shell/common/electron_constants.h"
  62. #include "shell/common/electron_paths.h"
  63. #include "shell/common/gin_converters/frame_converter.h"
  64. #include "shell/common/gin_helper/dictionary.h"
  65. #include "shell/common/gin_helper/error_thrower.h"
  66. #include "shell/common/options_switches.h"
  67. #include "shell/common/thread_restrictions.h"
  68. #include "third_party/blink/public/common/page/page_zoom.h"
  69. #include "third_party/blink/public/mojom/media/capture_handle_config.mojom.h"
  70. #include "third_party/blink/public/mojom/mediastream/media_stream.mojom.h"
  71. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  72. #include "extensions/browser/browser_context_keyed_service_factories.h"
  73. #include "extensions/browser/extension_pref_store.h"
  74. #include "extensions/browser/extension_pref_value_map_factory.h"
  75. #include "extensions/browser/extension_prefs.h"
  76. #include "extensions/browser/permissions_manager.h"
  77. #include "extensions/browser/pref_names.h"
  78. #include "extensions/common/extension_api.h"
  79. #include "shell/browser/extensions/electron_browser_context_keyed_service_factories.h"
  80. #include "shell/browser/extensions/electron_extension_system.h"
  81. #include "shell/browser/extensions/electron_extension_system_factory.h"
  82. #include "shell/browser/extensions/electron_extensions_browser_client.h"
  83. #include "shell/common/extensions/electron_extensions_client.h"
  84. #endif // BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  85. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
  86. BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  87. #include "components/pref_registry/pref_registry_syncable.h"
  88. #include "components/user_prefs/user_prefs.h"
  89. #endif
  90. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  91. #include "base/i18n/rtl.h"
  92. #include "components/language/core/browser/language_prefs.h"
  93. #include "components/spellcheck/browser/pref_names.h"
  94. #include "components/spellcheck/common/spellcheck_common.h"
  95. #endif
  96. using content::BrowserThread;
  97. namespace electron {
  98. namespace {
  99. // Copied from chrome/browser/media/webrtc/desktop_capture_devices_util.cc.
  100. media::mojom::CaptureHandlePtr CreateCaptureHandle(
  101. content::WebContents* capturer,
  102. const url::Origin& capturer_origin,
  103. const content::DesktopMediaID& captured_id) {
  104. if (capturer_origin.opaque()) {
  105. return nullptr;
  106. }
  107. content::RenderFrameHost* const captured_rfh =
  108. content::RenderFrameHost::FromID(
  109. captured_id.web_contents_id.render_process_id,
  110. captured_id.web_contents_id.main_render_frame_id);
  111. if (!captured_rfh || !captured_rfh->IsActive()) {
  112. return nullptr;
  113. }
  114. content::WebContents* const captured =
  115. content::WebContents::FromRenderFrameHost(captured_rfh);
  116. if (!captured) {
  117. return nullptr;
  118. }
  119. const auto& captured_config = captured->GetCaptureHandleConfig();
  120. if (!captured_config.all_origins_permitted &&
  121. std::ranges::none_of(
  122. captured_config.permitted_origins,
  123. [capturer_origin](const url::Origin& permitted_origin) {
  124. return capturer_origin.IsSameOriginWith(permitted_origin);
  125. })) {
  126. return nullptr;
  127. }
  128. // Observing CaptureHandle when either the capturing or the captured party
  129. // is incognito is disallowed, except for self-capture.
  130. if (capturer->GetPrimaryMainFrame() != captured->GetPrimaryMainFrame()) {
  131. if (capturer->GetBrowserContext()->IsOffTheRecord() ||
  132. captured->GetBrowserContext()->IsOffTheRecord()) {
  133. return nullptr;
  134. }
  135. }
  136. if (!captured_config.expose_origin &&
  137. captured_config.capture_handle.empty()) {
  138. return nullptr;
  139. }
  140. auto result = media::mojom::CaptureHandle::New();
  141. if (captured_config.expose_origin) {
  142. result->origin = captured->GetPrimaryMainFrame()->GetLastCommittedOrigin();
  143. }
  144. result->capture_handle = captured_config.capture_handle;
  145. return result;
  146. }
  147. // Copied from chrome/browser/media/webrtc/desktop_capture_devices_util.cc.
  148. std::optional<int> GetZoomLevel(content::WebContents* capturer,
  149. const url::Origin& capturer_origin,
  150. const content::DesktopMediaID& captured_id) {
  151. content::RenderFrameHost* const captured_rfh =
  152. content::RenderFrameHost::FromID(
  153. captured_id.web_contents_id.render_process_id,
  154. captured_id.web_contents_id.main_render_frame_id);
  155. if (!captured_rfh || !captured_rfh->IsActive()) {
  156. return std::nullopt;
  157. }
  158. content::WebContents* const captured_wc =
  159. content::WebContents::FromRenderFrameHost(captured_rfh);
  160. if (!captured_wc) {
  161. return std::nullopt;
  162. }
  163. double zoom_level = blink::ZoomLevelToZoomFactor(
  164. content::HostZoomMap::GetZoomLevel(captured_wc));
  165. return std::round(100 * zoom_level);
  166. }
  167. // Copied from chrome/browser/media/webrtc/desktop_capture_devices_util.cc.
  168. media::mojom::DisplayMediaInformationPtr
  169. DesktopMediaIDToDisplayMediaInformation(
  170. content::WebContents* capturer,
  171. const url::Origin& capturer_origin,
  172. const content::DesktopMediaID& media_id) {
  173. media::mojom::DisplayCaptureSurfaceType display_surface =
  174. media::mojom::DisplayCaptureSurfaceType::MONITOR;
  175. bool logical_surface = true;
  176. media::mojom::CursorCaptureType cursor =
  177. media::mojom::CursorCaptureType::NEVER;
  178. #if defined(USE_AURA)
  179. const bool uses_aura =
  180. media_id.window_id != content::DesktopMediaID::kNullId ? true : false;
  181. #else
  182. const bool uses_aura = false;
  183. #endif // defined(USE_AURA)
  184. media::mojom::CaptureHandlePtr capture_handle;
  185. int zoom_level = 100;
  186. switch (media_id.type) {
  187. case content::DesktopMediaID::TYPE_SCREEN:
  188. display_surface = media::mojom::DisplayCaptureSurfaceType::MONITOR;
  189. cursor = uses_aura ? media::mojom::CursorCaptureType::MOTION
  190. : media::mojom::CursorCaptureType::ALWAYS;
  191. break;
  192. case content::DesktopMediaID::TYPE_WINDOW:
  193. display_surface = media::mojom::DisplayCaptureSurfaceType::WINDOW;
  194. cursor = uses_aura ? media::mojom::CursorCaptureType::MOTION
  195. : media::mojom::CursorCaptureType::ALWAYS;
  196. break;
  197. case content::DesktopMediaID::TYPE_WEB_CONTENTS:
  198. display_surface = media::mojom::DisplayCaptureSurfaceType::BROWSER;
  199. cursor = media::mojom::CursorCaptureType::MOTION;
  200. capture_handle = CreateCaptureHandle(capturer, capturer_origin, media_id);
  201. zoom_level =
  202. GetZoomLevel(capturer, capturer_origin, media_id).value_or(100);
  203. break;
  204. case content::DesktopMediaID::TYPE_NONE:
  205. break;
  206. }
  207. return media::mojom::DisplayMediaInformation::New(
  208. display_surface, logical_surface, cursor, std::move(capture_handle),
  209. zoom_level);
  210. }
  211. // Convert string to lower case and escape it.
  212. std::string MakePartitionName(const std::string& input) {
  213. return base::EscapePath(base::ToLowerASCII(input));
  214. }
  215. [[nodiscard]] content::DesktopMediaID GetAudioDesktopMediaId(
  216. const std::vector<std::string>& audio_device_ids) {
  217. // content::MediaStreamRequest provides a vector of ids
  218. // to allow user preference to influence which stream is
  219. // returned. This is a WIP upstream, so for now just use
  220. // the first device in the list.
  221. // Xref: https://chromium-review.googlesource.com/c/chromium/src/+/5132210
  222. if (!audio_device_ids.empty())
  223. return content::DesktopMediaID::Parse(audio_device_ids.front());
  224. return {};
  225. }
  226. bool DoesDeviceMatch(const base::Value& device,
  227. const base::Value& device_to_compare,
  228. const blink::PermissionType permission_type) {
  229. if (permission_type ==
  230. static_cast<blink::PermissionType>(
  231. WebContentsPermissionHelper::PermissionType::HID) ||
  232. permission_type ==
  233. static_cast<blink::PermissionType>(
  234. WebContentsPermissionHelper::PermissionType::USB)) {
  235. if (device.GetDict().FindInt(kDeviceVendorIdKey) !=
  236. device_to_compare.GetDict().FindInt(kDeviceVendorIdKey) ||
  237. device.GetDict().FindInt(kDeviceProductIdKey) !=
  238. device_to_compare.GetDict().FindInt(kDeviceProductIdKey)) {
  239. return false;
  240. }
  241. const auto* serial_number =
  242. device_to_compare.GetDict().FindString(kDeviceSerialNumberKey);
  243. const auto* device_serial_number =
  244. device.GetDict().FindString(kDeviceSerialNumberKey);
  245. if (serial_number && device_serial_number &&
  246. *device_serial_number == *serial_number)
  247. return true;
  248. } else if (permission_type ==
  249. static_cast<blink::PermissionType>(
  250. WebContentsPermissionHelper::PermissionType::SERIAL)) {
  251. #if BUILDFLAG(IS_WIN)
  252. const auto* instance_id = device.GetDict().FindString(kDeviceInstanceIdKey);
  253. const auto* port_instance_id =
  254. device_to_compare.GetDict().FindString(kDeviceInstanceIdKey);
  255. if (instance_id && port_instance_id && *instance_id == *port_instance_id)
  256. return true;
  257. #else
  258. const auto* serial_number = device.GetDict().FindString(kSerialNumberKey);
  259. const auto* port_serial_number =
  260. device_to_compare.GetDict().FindString(kSerialNumberKey);
  261. if (device.GetDict().FindInt(kVendorIdKey) !=
  262. device_to_compare.GetDict().FindInt(kVendorIdKey) ||
  263. device.GetDict().FindInt(kProductIdKey) !=
  264. device_to_compare.GetDict().FindInt(kProductIdKey) ||
  265. (serial_number && port_serial_number &&
  266. *port_serial_number != *serial_number)) {
  267. return false;
  268. }
  269. #if BUILDFLAG(IS_MAC)
  270. const auto* usb_driver_key = device.GetDict().FindString(kUsbDriverKey);
  271. const auto* port_usb_driver_key =
  272. device_to_compare.GetDict().FindString(kUsbDriverKey);
  273. if (usb_driver_key && port_usb_driver_key &&
  274. *usb_driver_key != *port_usb_driver_key) {
  275. return false;
  276. }
  277. #endif // BUILDFLAG(IS_MAC)
  278. return true;
  279. #endif // BUILDFLAG(IS_WIN)
  280. }
  281. return false;
  282. }
  283. // partition_id => browser_context
  284. struct PartitionKey {
  285. PartitionKey(const std::string_view partition, bool in_memory)
  286. : type_{Type::Partition}, location_{partition}, in_memory_{in_memory} {}
  287. explicit PartitionKey(const base::FilePath& file_path)
  288. : type_{Type::Path},
  289. location_{file_path.AsUTF8Unsafe()},
  290. in_memory_{false} {}
  291. friend auto operator<=>(const PartitionKey&, const PartitionKey&) = default;
  292. private:
  293. enum class Type { Partition, Path };
  294. Type type_;
  295. std::string location_;
  296. bool in_memory_;
  297. };
  298. [[nodiscard]] auto& ContextMap() {
  299. static base::NoDestructor<
  300. std::map<PartitionKey, std::unique_ptr<ElectronBrowserContext>>>
  301. map;
  302. return *map;
  303. }
  304. } // namespace
  305. // static
  306. std::vector<ElectronBrowserContext*> ElectronBrowserContext::BrowserContexts() {
  307. return base::ToVector(ContextMap(),
  308. [](auto& iter) { return iter.second.get(); });
  309. }
  310. bool ElectronBrowserContext::IsValidContext(const void* context) {
  311. return std::ranges::any_of(ContextMap(), [context](const auto& iter) {
  312. return iter.second.get() == context;
  313. });
  314. }
  315. // static
  316. void ElectronBrowserContext::DestroyAllContexts() {
  317. auto& map = ContextMap();
  318. // Avoid UAF by destroying the default context last. See ba629e3 for info.
  319. const auto extracted = map.extract(PartitionKey{"", false});
  320. map.clear();
  321. }
  322. ElectronBrowserContext::ElectronBrowserContext(
  323. const PartitionOrPath partition_location,
  324. bool in_memory,
  325. base::Value::Dict options)
  326. : in_memory_pref_store_(new ValueMapPrefStore),
  327. storage_policy_(base::MakeRefCounted<SpecialStoragePolicy>()),
  328. protocol_registry_(base::WrapUnique(new ProtocolRegistry)),
  329. in_memory_(in_memory),
  330. ssl_config_(network::mojom::SSLConfig::New()) {
  331. // Read options.
  332. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  333. use_cache_ = !command_line->HasSwitch(switches::kDisableHttpCache);
  334. if (auto use_cache_opt = options.FindBool("cache")) {
  335. use_cache_ = use_cache_opt.value();
  336. }
  337. base::StringToInt(command_line->GetSwitchValueASCII(switches::kDiskCacheSize),
  338. &max_cache_size_);
  339. if (auto* path_value = std::get_if<std::reference_wrapper<const std::string>>(
  340. &partition_location)) {
  341. base::PathService::Get(DIR_SESSION_DATA, &path_);
  342. const std::string& partition_loc = path_value->get();
  343. if (!in_memory && !partition_loc.empty()) {
  344. path_ = path_.Append(FILE_PATH_LITERAL("Partitions"))
  345. .Append(base::FilePath::FromUTF8Unsafe(
  346. MakePartitionName(partition_loc)));
  347. }
  348. } else if (auto* filepath_partition =
  349. std::get_if<std::reference_wrapper<const base::FilePath>>(
  350. &partition_location)) {
  351. const base::FilePath& partition_path = filepath_partition->get();
  352. path_ = std::move(partition_path);
  353. }
  354. BrowserContextDependencyManager::GetInstance()->MarkBrowserContextLive(this);
  355. // Initialize Pref Registry.
  356. InitPrefs();
  357. cookie_change_notifier_ = std::make_unique<CookieChangeNotifier>(this);
  358. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  359. if (!in_memory_) {
  360. BrowserContextDependencyManager::GetInstance()
  361. ->CreateBrowserContextServices(this);
  362. auto* extension_system = static_cast<extensions::ElectronExtensionSystem*>(
  363. extensions::ExtensionSystem::Get(this));
  364. extension_system->InitForRegularProfile(true /* extensions_enabled */);
  365. extension_system->FinishInitialization();
  366. }
  367. #endif
  368. }
  369. ElectronBrowserContext::~ElectronBrowserContext() {
  370. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  371. NotifyWillBeDestroyed();
  372. // Notify any keyed services of browser context destruction.
  373. BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
  374. this);
  375. ShutdownStoragePartitions();
  376. }
  377. void ElectronBrowserContext::InitPrefs() {
  378. auto prefs_path = GetPath().Append(FILE_PATH_LITERAL("Preferences"));
  379. ScopedAllowBlockingForElectron allow_blocking;
  380. PrefServiceFactory prefs_factory;
  381. scoped_refptr<JsonPrefStore> pref_store =
  382. base::MakeRefCounted<JsonPrefStore>(prefs_path);
  383. pref_store->ReadPrefs(); // Synchronous.
  384. prefs_factory.set_user_prefs(pref_store);
  385. prefs_factory.set_command_line_prefs(in_memory_pref_store());
  386. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  387. if (!in_memory_) {
  388. auto* ext_pref_store = new ExtensionPrefStore(
  389. ExtensionPrefValueMapFactory::GetForBrowserContext(this),
  390. IsOffTheRecord());
  391. prefs_factory.set_extension_prefs(ext_pref_store);
  392. }
  393. #endif
  394. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
  395. BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  396. auto registry = base::MakeRefCounted<user_prefs::PrefRegistrySyncable>();
  397. #else
  398. auto registry = base::MakeRefCounted<PrefRegistrySimple>();
  399. #endif
  400. registry->RegisterFilePathPref(prefs::kSelectFileLastDirectory,
  401. base::FilePath());
  402. base::FilePath download_dir;
  403. base::PathService::Get(chrome::DIR_DEFAULT_DOWNLOADS, &download_dir);
  404. registry->RegisterFilePathPref(prefs::kDownloadDefaultDirectory,
  405. download_dir);
  406. registry->RegisterDictionaryPref(prefs::kDevToolsFileSystemPaths);
  407. InspectableWebContents::RegisterPrefs(registry.get());
  408. MediaDeviceIDSalt::RegisterPrefs(registry.get());
  409. ZoomLevelDelegate::RegisterPrefs(registry.get());
  410. PrefProxyConfigTrackerImpl::RegisterPrefs(registry.get());
  411. ElectronAccessibilityUIMessageHandler::RegisterPrefs(registry.get());
  412. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  413. if (!in_memory_)
  414. extensions::ExtensionPrefs::RegisterProfilePrefs(registry.get());
  415. extensions::PermissionsManager::RegisterProfilePrefs(registry.get());
  416. #endif
  417. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  418. BrowserContextDependencyManager::GetInstance()
  419. ->RegisterProfilePrefsForServices(registry.get());
  420. language::LanguagePrefs::RegisterProfilePrefs(registry.get());
  421. #endif
  422. prefs_ = prefs_factory.Create(registry.get());
  423. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS) || \
  424. BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  425. user_prefs::UserPrefs::Set(this, prefs_.get());
  426. #endif
  427. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  428. // No configured dictionaries, the default will be en-US
  429. if (prefs()->GetList(spellcheck::prefs::kSpellCheckDictionaries).empty()) {
  430. std::string default_code = spellcheck::GetCorrespondingSpellCheckLanguage(
  431. base::i18n::GetConfiguredLocale());
  432. if (!default_code.empty()) {
  433. base::Value::List language_codes;
  434. language_codes.Append(default_code);
  435. prefs()->Set(spellcheck::prefs::kSpellCheckDictionaries,
  436. base::Value(std::move(language_codes)));
  437. }
  438. }
  439. #endif
  440. // Unique uuid for global shortcuts.
  441. registry->RegisterStringPref(electron::kElectronGlobalShortcutsUuid,
  442. std::string());
  443. }
  444. void ElectronBrowserContext::SetUserAgent(const std::string& user_agent) {
  445. user_agent_ = user_agent;
  446. }
  447. base::FilePath ElectronBrowserContext::GetPath() {
  448. return path_;
  449. }
  450. bool ElectronBrowserContext::IsOffTheRecord() {
  451. return in_memory_;
  452. }
  453. std::string ElectronBrowserContext::GetMediaDeviceIDSalt() {
  454. if (!media_device_id_salt_)
  455. media_device_id_salt_ = std::make_unique<MediaDeviceIDSalt>(prefs_.get());
  456. return media_device_id_salt_->GetSalt();
  457. }
  458. std::unique_ptr<content::ZoomLevelDelegate>
  459. ElectronBrowserContext::CreateZoomLevelDelegate(
  460. const base::FilePath& partition_path) {
  461. if (!IsOffTheRecord()) {
  462. return std::make_unique<ZoomLevelDelegate>(prefs(), partition_path);
  463. }
  464. return {};
  465. }
  466. content::DownloadManagerDelegate*
  467. ElectronBrowserContext::GetDownloadManagerDelegate() {
  468. if (!download_manager_delegate_) {
  469. download_manager_delegate_ =
  470. std::make_unique<ElectronDownloadManagerDelegate>(GetDownloadManager());
  471. }
  472. return download_manager_delegate_.get();
  473. }
  474. content::BrowserPluginGuestManager* ElectronBrowserContext::GetGuestManager() {
  475. if (!guest_manager_)
  476. guest_manager_ = std::make_unique<WebViewManager>();
  477. return guest_manager_.get();
  478. }
  479. content::PlatformNotificationService*
  480. ElectronBrowserContext::GetPlatformNotificationService() {
  481. return ElectronBrowserClient::Get()->GetPlatformNotificationService();
  482. }
  483. content::PermissionControllerDelegate*
  484. ElectronBrowserContext::GetPermissionControllerDelegate() {
  485. if (!permission_manager_)
  486. permission_manager_ = std::make_unique<ElectronPermissionManager>();
  487. return permission_manager_.get();
  488. }
  489. storage::SpecialStoragePolicy*
  490. ElectronBrowserContext::GetSpecialStoragePolicy() {
  491. return storage_policy_.get();
  492. }
  493. std::string ElectronBrowserContext::GetUserAgent() const {
  494. return user_agent_.value_or(ElectronBrowserClient::Get()->GetUserAgent());
  495. }
  496. predictors::PreconnectManager* ElectronBrowserContext::GetPreconnectManager() {
  497. if (!preconnect_manager_) {
  498. preconnect_manager_ =
  499. std::make_unique<predictors::PreconnectManager>(nullptr, this);
  500. }
  501. return preconnect_manager_.get();
  502. }
  503. scoped_refptr<network::SharedURLLoaderFactory>
  504. ElectronBrowserContext::GetURLLoaderFactory() {
  505. if (url_loader_factory_)
  506. return url_loader_factory_;
  507. network::URLLoaderFactoryBuilder factory_builder;
  508. // Consult the embedder.
  509. mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>
  510. header_client;
  511. static_cast<content::ContentBrowserClient*>(ElectronBrowserClient::Get())
  512. ->WillCreateURLLoaderFactory(
  513. this, nullptr, -1,
  514. content::ContentBrowserClient::URLLoaderFactoryType::kNavigation,
  515. url::Origin(), net::IsolationInfo(), std::nullopt,
  516. ukm::kInvalidSourceIdObj, factory_builder, &header_client, nullptr,
  517. nullptr, nullptr, nullptr);
  518. network::mojom::URLLoaderFactoryParamsPtr params =
  519. network::mojom::URLLoaderFactoryParams::New();
  520. params->header_client = std::move(header_client);
  521. params->process_id = network::mojom::kBrowserProcessId;
  522. params->is_trusted = true;
  523. params->is_orb_enabled = false;
  524. // The tests of net module would fail if this setting is true, it seems that
  525. // the non-NetworkService implementation always has web security enabled.
  526. params->disable_web_security = false;
  527. auto* storage_partition = GetDefaultStoragePartition();
  528. url_loader_factory_ =
  529. std::move(factory_builder)
  530. .Finish(storage_partition->GetNetworkContext(), std::move(params));
  531. return url_loader_factory_;
  532. }
  533. content::PushMessagingService*
  534. ElectronBrowserContext::GetPushMessagingService() {
  535. return nullptr;
  536. }
  537. content::SSLHostStateDelegate*
  538. ElectronBrowserContext::GetSSLHostStateDelegate() {
  539. return nullptr;
  540. }
  541. content::BackgroundFetchDelegate*
  542. ElectronBrowserContext::GetBackgroundFetchDelegate() {
  543. return nullptr;
  544. }
  545. content::BackgroundSyncController*
  546. ElectronBrowserContext::GetBackgroundSyncController() {
  547. return nullptr;
  548. }
  549. content::BrowsingDataRemoverDelegate*
  550. ElectronBrowserContext::GetBrowsingDataRemoverDelegate() {
  551. return nullptr;
  552. }
  553. content::ClientHintsControllerDelegate*
  554. ElectronBrowserContext::GetClientHintsControllerDelegate() {
  555. return nullptr;
  556. }
  557. content::StorageNotificationService*
  558. ElectronBrowserContext::GetStorageNotificationService() {
  559. return nullptr;
  560. }
  561. content::ReduceAcceptLanguageControllerDelegate*
  562. ElectronBrowserContext::GetReduceAcceptLanguageControllerDelegate() {
  563. // Needs implementation
  564. // Refs https://chromium-review.googlesource.com/c/chromium/src/+/3687391
  565. return nullptr;
  566. }
  567. content::FileSystemAccessPermissionContext*
  568. ElectronBrowserContext::GetFileSystemAccessPermissionContext() {
  569. return FileSystemAccessPermissionContextFactory::GetForBrowserContext(this);
  570. }
  571. ResolveProxyHelper* ElectronBrowserContext::GetResolveProxyHelper() {
  572. if (!resolve_proxy_helper_) {
  573. resolve_proxy_helper_ = base::MakeRefCounted<ResolveProxyHelper>(this);
  574. }
  575. return resolve_proxy_helper_.get();
  576. }
  577. network::mojom::SSLConfigPtr ElectronBrowserContext::GetSSLConfig() {
  578. return ssl_config_.Clone();
  579. }
  580. void ElectronBrowserContext::SetSSLConfig(network::mojom::SSLConfigPtr config) {
  581. ssl_config_ = std::move(config);
  582. if (ssl_config_client_) {
  583. ssl_config_client_->OnSSLConfigUpdated(ssl_config_.Clone());
  584. }
  585. }
  586. void ElectronBrowserContext::SetSSLConfigClient(
  587. mojo::Remote<network::mojom::SSLConfigClient> client) {
  588. ssl_config_client_ = std::move(client);
  589. }
  590. void ElectronBrowserContext::SetDisplayMediaRequestHandler(
  591. DisplayMediaRequestHandler handler) {
  592. display_media_request_handler_ = handler;
  593. }
  594. void ElectronBrowserContext::DisplayMediaDeviceChosen(
  595. const content::MediaStreamRequest& request,
  596. content::MediaResponseCallback callback,
  597. gin::Arguments* args) {
  598. blink::mojom::StreamDevicesSetPtr stream_devices_set =
  599. blink::mojom::StreamDevicesSet::New();
  600. v8::Local<v8::Value> result;
  601. if (!args->GetNext(&result) || result->IsNullOrUndefined()) {
  602. std::move(callback).Run(
  603. blink::mojom::StreamDevicesSet(),
  604. blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
  605. return;
  606. }
  607. gin_helper::Dictionary result_dict;
  608. if (!gin::ConvertFromV8(args->isolate(), result, &result_dict)) {
  609. gin_helper::ErrorThrower(args->isolate())
  610. .ThrowTypeError(
  611. "Display Media Request streams callback must be called with null "
  612. "or a valid object");
  613. std::move(callback).Run(
  614. blink::mojom::StreamDevicesSet(),
  615. blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
  616. return;
  617. }
  618. stream_devices_set->stream_devices.emplace_back(
  619. blink::mojom::StreamDevices::New());
  620. blink::mojom::StreamDevices& devices = *stream_devices_set->stream_devices[0];
  621. bool video_requested =
  622. request.video_type != blink::mojom::MediaStreamType::NO_SERVICE;
  623. bool audio_requested =
  624. request.audio_type != blink::mojom::MediaStreamType::NO_SERVICE;
  625. bool has_video = false;
  626. if (video_requested && result_dict.Has("video")) {
  627. gin_helper::Dictionary video_dict;
  628. std::string id;
  629. std::string name;
  630. content::RenderFrameHost* rfh;
  631. if (result_dict.Get("video", &video_dict) && video_dict.Get("id", &id) &&
  632. video_dict.Get("name", &name)) {
  633. blink::MediaStreamDevice video_device(request.video_type, id, name);
  634. video_device.display_media_info = DesktopMediaIDToDisplayMediaInformation(
  635. nullptr, url::Origin::Create(request.security_origin),
  636. content::DesktopMediaID::Parse(video_device.id));
  637. devices.video_device = video_device;
  638. } else if (result_dict.Get("video", &rfh)) {
  639. auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
  640. blink::MediaStreamDevice video_device(
  641. request.video_type,
  642. content::WebContentsMediaCaptureId(
  643. rfh->GetProcess()->GetDeprecatedID(), rfh->GetRoutingID())
  644. .ToString(),
  645. base::UTF16ToUTF8(web_contents->GetTitle()));
  646. video_device.display_media_info = DesktopMediaIDToDisplayMediaInformation(
  647. web_contents, url::Origin::Create(request.security_origin),
  648. content::DesktopMediaID::Parse(video_device.id));
  649. devices.video_device = video_device;
  650. } else {
  651. gin_helper::ErrorThrower(args->isolate())
  652. .ThrowTypeError(
  653. "video must be a WebFrameMain or DesktopCapturerSource");
  654. std::move(callback).Run(
  655. blink::mojom::StreamDevicesSet(),
  656. blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
  657. return;
  658. }
  659. has_video = true;
  660. }
  661. if (audio_requested && result_dict.Has("audio")) {
  662. gin_helper::Dictionary audio_dict;
  663. std::string id;
  664. std::string name;
  665. content::RenderFrameHost* rfh;
  666. // NB. this is not permitted by the documentation, but is left here as an
  667. // "escape hatch" for providing an arbitrary name/id if needed in the
  668. // future.
  669. if (result_dict.Get("audio", &audio_dict) && audio_dict.Get("id", &id) &&
  670. audio_dict.Get("name", &name)) {
  671. blink::MediaStreamDevice audio_device(request.audio_type, id, name);
  672. audio_device.display_media_info = DesktopMediaIDToDisplayMediaInformation(
  673. nullptr, url::Origin::Create(request.security_origin),
  674. GetAudioDesktopMediaId(request.requested_audio_device_ids));
  675. devices.audio_device = audio_device;
  676. } else if (result_dict.Get("audio", &rfh)) {
  677. bool enable_local_echo = false;
  678. result_dict.Get("enableLocalEcho", &enable_local_echo);
  679. bool disable_local_echo = !enable_local_echo;
  680. auto* web_contents = content::WebContents::FromRenderFrameHost(rfh);
  681. blink::MediaStreamDevice audio_device(
  682. request.audio_type,
  683. content::WebContentsMediaCaptureId(
  684. rfh->GetProcess()->GetDeprecatedID(), rfh->GetRoutingID(),
  685. disable_local_echo)
  686. .ToString(),
  687. "Tab audio");
  688. audio_device.display_media_info = DesktopMediaIDToDisplayMediaInformation(
  689. web_contents, url::Origin::Create(request.security_origin),
  690. GetAudioDesktopMediaId(request.requested_audio_device_ids));
  691. devices.audio_device = audio_device;
  692. } else if (result_dict.Get("audio", &id)) {
  693. blink::MediaStreamDevice audio_device(request.audio_type, id,
  694. "System audio");
  695. audio_device.display_media_info = DesktopMediaIDToDisplayMediaInformation(
  696. nullptr, url::Origin::Create(request.security_origin),
  697. GetAudioDesktopMediaId(request.requested_audio_device_ids));
  698. devices.audio_device = audio_device;
  699. } else {
  700. gin_helper::ErrorThrower(args->isolate())
  701. .ThrowTypeError(
  702. "audio must be a WebFrameMain, \"loopback\" or "
  703. "\"loopbackWithMute\"");
  704. std::move(callback).Run(
  705. blink::mojom::StreamDevicesSet(),
  706. blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
  707. return;
  708. }
  709. }
  710. if ((video_requested && !has_video)) {
  711. gin_helper::ErrorThrower(args->isolate())
  712. .ThrowTypeError(
  713. "Video was requested, but no video stream was provided");
  714. std::move(callback).Run(
  715. blink::mojom::StreamDevicesSet(),
  716. blink::mojom::MediaStreamRequestResult::CAPTURE_FAILURE, nullptr);
  717. return;
  718. }
  719. std::move(callback).Run(*stream_devices_set,
  720. blink::mojom::MediaStreamRequestResult::OK, nullptr);
  721. }
  722. bool ElectronBrowserContext::ChooseDisplayMediaDevice(
  723. const content::MediaStreamRequest& request,
  724. content::MediaResponseCallback callback) {
  725. if (!display_media_request_handler_)
  726. return false;
  727. DisplayMediaResponseCallbackJs callbackJs =
  728. base::BindOnce(&DisplayMediaDeviceChosen, request, std::move(callback));
  729. display_media_request_handler_.Run(request, std::move(callbackJs));
  730. return true;
  731. }
  732. void ElectronBrowserContext::GrantDevicePermission(
  733. const url::Origin& origin,
  734. const base::Value& device,
  735. blink::PermissionType permission_type) {
  736. granted_devices_[permission_type][origin].push_back(
  737. std::make_unique<base::Value>(device.Clone()));
  738. }
  739. void ElectronBrowserContext::RevokeDevicePermission(
  740. const url::Origin& origin,
  741. const base::Value& device,
  742. const blink::PermissionType permission_type) {
  743. const auto& current_devices_it = granted_devices_.find(permission_type);
  744. if (current_devices_it == granted_devices_.end())
  745. return;
  746. const auto& origin_devices_it = current_devices_it->second.find(origin);
  747. if (origin_devices_it == current_devices_it->second.end())
  748. return;
  749. std::erase_if(origin_devices_it->second,
  750. [&device, &permission_type](auto const& val) {
  751. return DoesDeviceMatch(device, *val, permission_type);
  752. });
  753. }
  754. bool ElectronBrowserContext::CheckDevicePermission(
  755. const url::Origin& origin,
  756. const base::Value& device,
  757. blink::PermissionType permission_type) {
  758. const auto& current_devices_it = granted_devices_.find(permission_type);
  759. if (current_devices_it == granted_devices_.end())
  760. return false;
  761. const auto& origin_devices_it = current_devices_it->second.find(origin);
  762. if (origin_devices_it == current_devices_it->second.end())
  763. return false;
  764. for (const auto& device_to_compare : origin_devices_it->second) {
  765. if (DoesDeviceMatch(device, *device_to_compare, permission_type))
  766. return true;
  767. }
  768. return false;
  769. }
  770. // static
  771. ElectronBrowserContext* ElectronBrowserContext::From(
  772. const std::string& partition,
  773. bool in_memory,
  774. base::Value::Dict options) {
  775. auto& context = ContextMap()[PartitionKey(partition, in_memory)];
  776. if (!context) {
  777. context.reset(new ElectronBrowserContext{std::cref(partition), in_memory,
  778. std::move(options)});
  779. }
  780. return context.get();
  781. }
  782. // static
  783. ElectronBrowserContext* ElectronBrowserContext::GetDefaultBrowserContext(
  784. base::Value::Dict options) {
  785. return ElectronBrowserContext::From("", false, std::move(options));
  786. }
  787. ElectronBrowserContext* ElectronBrowserContext::FromPath(
  788. const base::FilePath& path,
  789. base::Value::Dict options) {
  790. auto& context = ContextMap()[PartitionKey(path)];
  791. if (!context) {
  792. context.reset(
  793. new ElectronBrowserContext{std::cref(path), false, std::move(options)});
  794. }
  795. return context.get();
  796. }
  797. } // namespace electron