CefSettings.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  4. #pragma once
  5. #include "Stdafx.h"
  6. using namespace System::Collections::Generic;
  7. namespace CefSharp
  8. {
  9. /// <summary>
  10. /// Initialization settings. Many of these and other settings can also configured
  11. /// using command-line switches.
  12. /// </summary>
  13. public ref class CefSettings
  14. {
  15. private:
  16. List<CefExtension^>^ _cefExtensions;
  17. IDictionary<String^, String^>^ _cefCommandLineArgs;
  18. internal:
  19. ::CefSettings* _cefSettings;
  20. List<CefCustomScheme^>^ _cefCustomSchemes;
  21. bool _focusedNodeChangedEnabled;
  22. public:
  23. /// <summary>
  24. /// Default Constructor
  25. /// </summary>
  26. CefSettings() : _cefSettings(new ::CefSettings())
  27. {
  28. _cefSettings->multi_threaded_message_loop = true;
  29. _cefSettings->no_sandbox = true;
  30. BrowserSubprocessPath = "CefSharp.BrowserSubprocess.exe";
  31. _cefCustomSchemes = gcnew List<CefCustomScheme^>();
  32. _cefExtensions = gcnew List<CefExtension^>();
  33. _cefCommandLineArgs = gcnew Dictionary<String^, String^>();
  34. //Automatically discovered and load a system-wide installation of Pepper Flash.
  35. _cefCommandLineArgs->Add("enable-system-flash", "1");
  36. _focusedNodeChangedEnabled = false;
  37. }
  38. !CefSettings()
  39. {
  40. delete _cefSettings;
  41. }
  42. ~CefSettings()
  43. {
  44. this->!CefSettings();
  45. }
  46. /// <summary>
  47. /// Add Customs schemes to this collection
  48. /// </summary>
  49. property IEnumerable<CefCustomScheme^>^ CefCustomSchemes
  50. {
  51. IEnumerable<CefCustomScheme^>^ get() { return _cefCustomSchemes; }
  52. }
  53. /// <summary>
  54. /// Add CefExtensions to be registered
  55. /// </summary>
  56. virtual property IEnumerable<CefExtension^>^ Extensions
  57. {
  58. IEnumerable<CefExtension^>^ get() { return _cefExtensions; }
  59. }
  60. /// <summary>
  61. /// Add custom command line argumens to this collection, they will be
  62. /// added in OnBeforeCommandLineProcessing.
  63. // The CefSettings.command_line_args_disabled value can be used to start with an empty command-line object. Any values specified in CefSettings that equate to command-line arguments will be set before this method is called.
  64. /// </summary>
  65. virtual property IDictionary<String^, String^>^ CefCommandLineArgs
  66. {
  67. IDictionary<String^, String^>^ get() { return _cefCommandLineArgs; }
  68. }
  69. /// <summary>
  70. /// Set to true to disable configuration of browser process features using
  71. /// standard CEF and Chromium command-line arguments. Configuration can still
  72. /// be specified using CEF data structures or by adding to CefCommandLineArgs
  73. /// </summary>
  74. property bool CommandLineArgsDisabled
  75. {
  76. bool get() { return _cefSettings->command_line_args_disabled == 1; }
  77. void set(bool value) { _cefSettings->command_line_args_disabled = value; }
  78. }
  79. /// <summary>
  80. /// Set to true to control browser process main (UI) thread message pump
  81. /// scheduling via the IBrowserProcessHandler.OnScheduleMessagePumpWork
  82. /// callback. This option is recommended for use in combination with the
  83. /// Cef.DoMessageLoopWork() function in cases where the CEF message loop must be
  84. /// integrated into an existing application message loop (see additional
  85. /// comments and warnings on Cef.DoMessageLoopWork). Enabling this option is not
  86. /// recommended for most users; leave this option disabled and use either
  87. /// MultiThreadedMessageLoop (the default) if possible.
  88. /// </summary>
  89. property bool ExternalMessagePump
  90. {
  91. bool get() { return _cefSettings->external_message_pump == 1; }
  92. void set(bool value) { _cefSettings->external_message_pump = value; }
  93. }
  94. /// <summary>
  95. //// Set to true to have the browser process message loop run in a separate
  96. /// thread. If false than the CefDoMessageLoopWork() function must be
  97. /// called from your application message loop. This option is only supported on
  98. /// Windows. The default value is true
  99. /// </summary>
  100. property bool MultiThreadedMessageLoop
  101. {
  102. bool get() { return _cefSettings->multi_threaded_message_loop == 1; }
  103. void set(bool value) { _cefSettings->multi_threaded_message_loop = value; }
  104. }
  105. /// <summary>
  106. /// The path to a separate executable that will be launched for sub-processes.
  107. /// By default the browser process executable is used. See the comments on
  108. /// Cef.ExecuteProcess() for details. Also configurable using the
  109. /// "browser-subprocess-path" command-line switch. Default is CefSharp.BrowserSubprocess.exe
  110. /// </summary>
  111. property String^ BrowserSubprocessPath
  112. {
  113. String^ get() { return StringUtils::ToClr(_cefSettings->browser_subprocess_path); }
  114. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->browser_subprocess_path, value); }
  115. }
  116. /// <summary>
  117. /// The location where cache data will be stored on disk. If empty then
  118. /// browsers will be created in "incognito mode" where in-memory caches are
  119. /// used for storage and no data is persisted to disk. HTML5 databases such as
  120. /// localStorage will only persist across sessions if a cache path is
  121. /// specified. Can be overridden for individual CefRequestContext instances via
  122. /// the RequestContextSettings.CachePath value.
  123. /// </summary>
  124. property String^ CachePath
  125. {
  126. String^ get() { return StringUtils::ToClr(_cefSettings->cache_path); }
  127. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->cache_path, value); }
  128. }
  129. /// <summary>
  130. /// The location where user data such as spell checking dictionary files will
  131. /// be stored on disk. If empty then the default platform-specific user data
  132. /// directory will be used ("~/.cef_user_data" directory on Linux,
  133. /// "~/Library/Application Support/CEF/User Data" directory on Mac OS X,
  134. /// "Local Settings\Application Data\CEF\User Data" directory under the user
  135. /// profile directory on Windows).
  136. /// </summary>
  137. property String^ UserDataPath
  138. {
  139. String^ get() { return StringUtils::ToClr(_cefSettings->user_data_path); }
  140. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->user_data_path, value); }
  141. }
  142. /// <summary>
  143. /// Set to true in order to completely ignore SSL certificate errors.
  144. /// This is NOT recommended.
  145. /// </summary>
  146. property bool IgnoreCertificateErrors
  147. {
  148. bool get() { return _cefSettings->ignore_certificate_errors == 1; }
  149. void set(bool value) { _cefSettings->ignore_certificate_errors = value; }
  150. }
  151. /// <summary>
  152. /// The locale string that will be passed to WebKit. If empty the default
  153. /// locale of "en-US" will be used. Also configurable using the "lang"
  154. /// command-line switch.
  155. /// </summary>
  156. property String^ Locale
  157. {
  158. String^ get() { return StringUtils::ToClr(_cefSettings->locale); }
  159. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->locale, value); }
  160. }
  161. /// <summary>
  162. /// The fully qualified path for the locales directory. If this value is empty
  163. /// the locales directory must be located in the module directory.
  164. /// Also configurable using the "locales-dir-path" command-line switch.
  165. /// </summary>
  166. property String^ LocalesDirPath
  167. {
  168. String^ get() { return StringUtils::ToClr(_cefSettings->locales_dir_path); }
  169. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->locales_dir_path, value); }
  170. }
  171. /// <summary>
  172. /// The fully qualified path for the resources directory. If this value is
  173. /// empty the cef.pak and/or devtools_resources.pak files must be located in
  174. /// the module directory. Also configurable using the "resources-dir-path" command-line
  175. /// switch.
  176. /// </summary>
  177. property String^ ResourcesDirPath
  178. {
  179. String^ get() { return StringUtils::ToClr(_cefSettings->resources_dir_path); }
  180. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->resources_dir_path, value); }
  181. }
  182. /// <summary>
  183. /// The directory and file name to use for the debug log. If empty a default
  184. /// log file name and location will be used. On Windows and Linux a "debug.log"
  185. /// file will be written in the main executable directory.
  186. /// Also configurable using the"log-file" command-line switch.
  187. /// </summary>
  188. property String^ LogFile
  189. {
  190. String^ get() { return StringUtils::ToClr(_cefSettings->log_file); }
  191. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->log_file, value); }
  192. }
  193. /// <summary>
  194. /// The log severity. Only messages of this severity level or higher will be
  195. /// logged. Also configurable using the "log-severity" command-line switch with
  196. /// a value of "verbose", "info", "warning", "error", "error-report" or
  197. /// "disable".
  198. /// </summary>
  199. property CefSharp::LogSeverity LogSeverity
  200. {
  201. CefSharp::LogSeverity get() { return (CefSharp::LogSeverity)_cefSettings->log_severity; }
  202. void set(CefSharp::LogSeverity value) { _cefSettings->log_severity = (cef_log_severity_t)value; }
  203. }
  204. /// <summary>
  205. /// Custom flags that will be used when initializing the V8 JavaScript engine.
  206. /// The consequences of using custom flags may not be well tested. Also
  207. /// configurable using the "js-flags" command-line switch.
  208. /// </summary>
  209. property String^ JavascriptFlags
  210. {
  211. String^ get() { return StringUtils::ToClr(_cefSettings->javascript_flags); }
  212. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->javascript_flags, value); }
  213. }
  214. /// <summary>
  215. /// Set to true to disable loading of pack files for resources and locales.
  216. /// A resource bundle handler must be provided for the browser and render
  217. /// processes via CefApp::GetResourceBundleHandler() if loading of pack files
  218. /// is disabled. Also configurable using the "disable-pack-loading" command-
  219. /// line switch.
  220. /// </summary>
  221. property bool PackLoadingDisabled
  222. {
  223. bool get() { return _cefSettings->pack_loading_disabled == 1; }
  224. void set(bool value) { _cefSettings->pack_loading_disabled = value; }
  225. }
  226. /// <summary>
  227. /// Value that will be inserted as the product portion of the default
  228. /// User-Agent string. If empty the Chromium product version will be used. If
  229. /// |userAgent| is specified this value will be ignored. Also configurable
  230. /// using the "product-version" command-line switch.
  231. /// </summary>
  232. property String^ ProductVersion
  233. {
  234. String^ get() { return StringUtils::ToClr(_cefSettings->product_version); }
  235. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->product_version, value); }
  236. }
  237. /// <summary>
  238. /// Set to a value between 1024 and 65535 to enable remote debugging on the
  239. /// specified port. For example, if 8080 is specified the remote debugging URL
  240. /// will be http://localhost:8080. CEF can be remotely debugged from any CEF or
  241. /// Chrome browser window. Also configurable using the "remote-debugging-port"
  242. /// command-line switch.
  243. /// </summary>
  244. property int RemoteDebuggingPort
  245. {
  246. int get() { return _cefSettings->remote_debugging_port; }
  247. void set(int value) { _cefSettings->remote_debugging_port = value; }
  248. }
  249. /// <summary>
  250. /// The number of stack trace frames to capture for uncaught exceptions.
  251. /// Specify a positive value to enable the CefRenderProcessHandler::
  252. /// OnUncaughtException() callback. Specify 0 (default value) and
  253. /// OnUncaughtException() will not be called. Also configurable using the
  254. /// "uncaught-exception-stack-size" command-line switch.
  255. /// </summary>
  256. property int UncaughtExceptionStackSize
  257. {
  258. int get() { return _cefSettings->uncaught_exception_stack_size; }
  259. void set(int value) { _cefSettings->uncaught_exception_stack_size = value; }
  260. }
  261. /// <summary>
  262. /// Value that will be returned as the User-Agent HTTP header. If empty the
  263. /// default User-Agent string will be used. Also configurable using the
  264. /// "user-agent" command-line switch.
  265. /// </summary>
  266. property String^ UserAgent
  267. {
  268. String^ get() { return StringUtils::ToClr(_cefSettings->user_agent); }
  269. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->user_agent, value); }
  270. }
  271. /// <summary>
  272. /// Set to true (1) to enable windowless (off-screen) rendering support. Do not
  273. /// enable this value if the application does not use windowless rendering as
  274. /// it may reduce rendering performance on some systems.
  275. /// </summary>
  276. property bool WindowlessRenderingEnabled
  277. {
  278. bool get() { return _cefSettings->windowless_rendering_enabled == 1; }
  279. void set(bool value) { _cefSettings->windowless_rendering_enabled = value; }
  280. }
  281. /// <summary>
  282. /// To persist session cookies (cookies without an expiry date or validity
  283. /// interval) by default when using the global cookie manager set this value to
  284. /// true. Session cookies are generally intended to be transient and most
  285. /// Web browsers do not persist them. A CachePath value must also be
  286. /// specified to enable this feature. Also configurable using the
  287. /// "persist-session-cookies" command-line switch. Can be overridden for
  288. /// individual RequestContext instances via the
  289. /// RequestContextSettings.PersistSessionCookies value.
  290. /// </summary>
  291. property bool PersistSessionCookies
  292. {
  293. bool get() { return _cefSettings->persist_session_cookies == 1; }
  294. void set(bool value) { _cefSettings->persist_session_cookies = value; }
  295. }
  296. /// <summary>
  297. /// To persist user preferences as a JSON file in the cache path directory set
  298. /// this value to true. A CachePath value must also be specified
  299. /// to enable this feature. Also configurable using the
  300. /// "persist-user-preferences" command-line switch. Can be overridden for
  301. /// individual RequestContext instances via the
  302. /// RequestContextSettings.PersistUserPreferences value.
  303. /// </summary>
  304. property bool PersistUserPreferences
  305. {
  306. bool get() { return _cefSettings->persist_user_preferences == 1; }
  307. void set(bool value) { _cefSettings->persist_user_preferences = value; }
  308. }
  309. /// <summary>
  310. /// Comma delimited ordered list of language codes without any whitespace that
  311. /// will be used in the "Accept-Language" HTTP header. May be set globally
  312. /// using the CefSettings.AcceptLanguageList value. If both values are
  313. /// empty then "en-US,en" will be used.
  314. /// </summary>
  315. property String^ AcceptLanguageList
  316. {
  317. String^ get() { return StringUtils::ToClr(_cefSettings->accept_language_list); }
  318. void set(String^ value) { StringUtils::AssignNativeFromClr(_cefSettings->accept_language_list, value); }
  319. }
  320. /// <summary>
  321. /// If true a message will be sent from the render subprocess to the
  322. /// browser when a DOM node (or no node) gets focus. The default is
  323. /// false.
  324. /// </summary>
  325. property bool FocusedNodeChangedEnabled
  326. {
  327. bool get() { return _focusedNodeChangedEnabled; }
  328. void set(bool value) { _focusedNodeChangedEnabled = value; }
  329. }
  330. /// <summary>
  331. /// Registers a custom scheme using the provided settings.
  332. /// </summary>
  333. /// <param name="cefCustomScheme">The CefCustomScheme which provides the details about the scheme.</param>
  334. void RegisterScheme(CefCustomScheme^ cefCustomScheme)
  335. {
  336. _cefCustomSchemes->Add(cefCustomScheme);
  337. }
  338. /// <summary>
  339. /// Registers an extension with the provided settings.
  340. /// </summary>
  341. /// <param name="extension">The CefExtension that contains the extension code.</param>
  342. void RegisterExtension(CefExtension^ extension)
  343. {
  344. if (_cefExtensions->Contains(extension))
  345. {
  346. throw gcnew ArgumentException("An extension with the same name is already registered.", "extension");
  347. }
  348. _cefExtensions->Add(extension);
  349. }
  350. /// <summary>
  351. /// Set command line argument to disable GPU Acceleration, this will disable WebGL.
  352. /// </summary>
  353. void DisableGpuAcceleration()
  354. {
  355. if (!_cefCommandLineArgs->ContainsKey("disable-gpu"))
  356. {
  357. _cefCommandLineArgs->Add("disable-gpu", "1");
  358. }
  359. }
  360. /// <summary>
  361. /// Set command line arguments for best OSR (Offscreen and WPF) Rendering performance
  362. /// This will disable WebGL, look at the source to determine which flags best suite
  363. /// your requirements.
  364. /// </summary>
  365. void SetOffScreenRenderingBestPerformanceArgs()
  366. {
  367. // Use software rendering and compositing (disable GPU) for increased FPS
  368. // and decreased CPU usage. This will also disable WebGL so remove these
  369. // switches if you need that capability.
  370. // See https://bitbucket.org/chromiumembedded/cef/issues/1257 for details.
  371. if (!_cefCommandLineArgs->ContainsKey("disable-gpu"))
  372. {
  373. _cefCommandLineArgs->Add("disable-gpu", "1");
  374. }
  375. if (!_cefCommandLineArgs->ContainsKey("disable-gpu-compositing"))
  376. {
  377. _cefCommandLineArgs->Add("disable-gpu-compositing", "1");
  378. }
  379. // Synchronize the frame rate between all processes. This results in
  380. // decreased CPU usage by avoiding the generation of extra frames that
  381. // would otherwise be discarded. The frame rate can be set at browser
  382. // creation time via CefBrowserSettings.windowless_frame_rate or changed
  383. // dynamically using CefBrowserHost::SetWindowlessFrameRate. In cefclient
  384. // it can be set via the command-line using `--off-screen-frame-rate=XX`.
  385. // See https://bitbucket.org/chromiumembedded/cef/issues/1368 for details.
  386. if (!_cefCommandLineArgs->ContainsKey("enable-begin-frame-scheduling"))
  387. {
  388. _cefCommandLineArgs->Add("enable-begin-frame-scheduling", "1");
  389. }
  390. }
  391. };
  392. }