CefRequestContext.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. namespace Xilium.CefGlue
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Runtime.InteropServices;
  7. using Xilium.CefGlue.Interop;
  8. /// <summary>
  9. /// A request context provides request handling for a set of related browser
  10. /// or URL request objects. A request context can be specified when creating a
  11. /// new browser via the CefBrowserHost static factory methods or when creating a
  12. /// new URL request via the CefURLRequest static factory methods. Browser objects
  13. /// with different request contexts will never be hosted in the same render
  14. /// process. Browser objects with the same request context may or may not be
  15. /// hosted in the same render process depending on the process model. Browser
  16. /// objects created indirectly via the JavaScript window.open function or
  17. /// targeted links will share the same render process and the same request
  18. /// context as the source browser. When running in single-process mode there is
  19. /// only a single render process (the main process) and so all browsers created
  20. /// in single-process mode will share the same request context. This will be the
  21. /// first request context passed into a CefBrowserHost static factory method and
  22. /// all other request context objects will be ignored.
  23. /// </summary>
  24. public sealed unsafe partial class CefRequestContext
  25. {
  26. /// <summary>
  27. /// Returns the global context object.
  28. /// </summary>
  29. public static CefRequestContext GetGlobalContext()
  30. {
  31. return CefRequestContext.FromNative(
  32. cef_request_context_t.get_global_context()
  33. );
  34. }
  35. /// <summary>
  36. /// Creates a new context object with the specified |settings| and optional
  37. /// |handler|.
  38. /// </summary>
  39. public static CefRequestContext CreateContext(CefRequestContextSettings settings, CefRequestContextHandler handler)
  40. {
  41. var n_settings = settings.ToNative();
  42. var result = CefRequestContext.FromNative(
  43. cef_request_context_t.create_context(
  44. n_settings,
  45. handler != null ? handler.ToNative() : null
  46. )
  47. );
  48. CefRequestContextSettings.Free(n_settings);
  49. return result;
  50. }
  51. /// <summary>
  52. /// Creates a new context object that shares storage with |other| and uses an
  53. /// optional |handler|.
  54. /// </summary>
  55. public static CefRequestContext CreateContext(CefRequestContext other, CefRequestContextHandler handler)
  56. {
  57. return CefRequestContext.FromNative(
  58. cef_request_context_t.create_context(
  59. other.ToNative(),
  60. handler != null ? handler.ToNative() : null
  61. )
  62. );
  63. }
  64. /// <summary>
  65. /// Returns true if this object is pointing to the same context as |that|
  66. /// object.
  67. /// </summary>
  68. public bool IsSame(CefRequestContext other)
  69. {
  70. if (other == null) return false;
  71. return cef_request_context_t.is_same(_self, other.ToNative()) != 0;
  72. }
  73. /// <summary>
  74. /// Returns true if this object is sharing the same storage as |that| object.
  75. /// </summary>
  76. public bool IsSharingWith(CefRequestContext other)
  77. {
  78. return cef_request_context_t.is_sharing_with(_self, other.ToNative()) != 0;
  79. }
  80. /// <summary>
  81. /// Returns true if this object is the global context. The global context is
  82. /// used by default when creating a browser or URL request with a NULL context
  83. /// argument.
  84. /// </summary>
  85. public bool IsGlobal
  86. {
  87. get
  88. {
  89. return cef_request_context_t.is_global(_self) != 0;
  90. }
  91. }
  92. /// <summary>
  93. /// Returns the handler for this context if any.
  94. /// </summary>
  95. public CefRequestContextHandler GetHandler()
  96. {
  97. return CefRequestContextHandler.FromNativeOrNull(
  98. cef_request_context_t.get_handler(_self)
  99. );
  100. }
  101. /// <summary>
  102. /// Returns the cache path for this object. If empty an "incognito mode"
  103. /// in-memory cache is being used.
  104. /// </summary>
  105. public string CachePath
  106. {
  107. get
  108. {
  109. var n_result = cef_request_context_t.get_cache_path(_self);
  110. return cef_string_userfree.ToString(n_result);
  111. }
  112. }
  113. /// <summary>
  114. /// Returns the cookie manager for this object. If |callback| is non-NULL it
  115. /// will be executed asnychronously on the UI thread after the manager's
  116. /// storage has been initialized.
  117. /// </summary>
  118. public CefCookieManager GetCookieManager(CefCompletionCallback callback)
  119. {
  120. var n_callback = callback != null ? callback.ToNative() : null;
  121. return CefCookieManager.FromNativeOrNull(
  122. cef_request_context_t.get_cookie_manager(_self, n_callback)
  123. );
  124. }
  125. /// <summary>
  126. /// Register a scheme handler factory for the specified |scheme_name| and
  127. /// optional |domain_name|. An empty |domain_name| value for a standard scheme
  128. /// will cause the factory to match all domain names. The |domain_name| value
  129. /// will be ignored for non-standard schemes. If |scheme_name| is a built-in
  130. /// scheme and no handler is returned by |factory| then the built-in scheme
  131. /// handler factory will be called. If |scheme_name| is a custom scheme then
  132. /// you must also implement the CefApp::OnRegisterCustomSchemes() method in all
  133. /// processes. This function may be called multiple times to change or remove
  134. /// the factory that matches the specified |scheme_name| and optional
  135. /// |domain_name|. Returns false if an error occurs. This function may be
  136. /// called on any thread in the browser process.
  137. /// </summary>
  138. public bool RegisterSchemeHandlerFactory(string schemeName, string domainName, CefSchemeHandlerFactory factory)
  139. {
  140. if (string.IsNullOrEmpty(schemeName)) throw new ArgumentNullException("schemeName");
  141. if (factory == null) throw new ArgumentNullException("factory");
  142. fixed (char* schemeName_str = schemeName)
  143. fixed (char* domainName_str = domainName)
  144. {
  145. var n_schemeName = new cef_string_t(schemeName_str, schemeName.Length);
  146. var n_domainName = new cef_string_t(domainName_str, domainName != null ? domainName.Length : 0);
  147. return cef_request_context_t.register_scheme_handler_factory(_self, &n_schemeName, &n_domainName, factory.ToNative()) != 0;
  148. }
  149. }
  150. /// <summary>
  151. /// Clear all registered scheme handler factories. Returns false on error. This
  152. /// function may be called on any thread in the browser process.
  153. /// </summary>
  154. public bool ClearSchemeHandlerFactories()
  155. {
  156. return cef_request_context_t.clear_scheme_handler_factories(_self) != 0;
  157. }
  158. /// <summary>
  159. /// Tells all renderer processes associated with this context to throw away
  160. /// their plugin list cache. If |reload_pages| is true they will also reload
  161. /// all pages with plugins. CefRequestContextHandler::OnBeforePluginLoad may
  162. /// be called to rebuild the plugin list cache.
  163. /// </summary>
  164. public void PurgePluginListCache(bool reloadPages)
  165. {
  166. cef_request_context_t.purge_plugin_list_cache(_self, reloadPages ? 1 : 0);
  167. }
  168. /// <summary>
  169. /// Returns true if a preference with the specified |name| exists. This method
  170. /// must be called on the browser process UI thread.
  171. /// </summary>
  172. public bool HasPreference(string name)
  173. {
  174. fixed (char* name_str = name)
  175. {
  176. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  177. return cef_request_context_t.has_preference(_self, &n_name) != 0;
  178. }
  179. }
  180. /// <summary>
  181. /// Returns the value for the preference with the specified |name|. Returns
  182. /// NULL if the preference does not exist. The returned object contains a copy
  183. /// of the underlying preference value and modifications to the returned object
  184. /// will not modify the underlying preference value. This method must be called
  185. /// on the browser process UI thread.
  186. /// </summary>
  187. public CefValue GetPreference(string name)
  188. {
  189. fixed (char* name_str = name)
  190. {
  191. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  192. var n_value = cef_request_context_t.get_preference(_self, &n_name);
  193. return CefValue.FromNativeOrNull(n_value);
  194. }
  195. }
  196. /// <summary>
  197. /// Returns all preferences as a dictionary. If |include_defaults| is true then
  198. /// preferences currently at their default value will be included. The returned
  199. /// object contains a copy of the underlying preference values and
  200. /// modifications to the returned object will not modify the underlying
  201. /// preference values. This method must be called on the browser process UI
  202. /// thread.
  203. /// </summary>
  204. public CefDictionaryValue GetAllPreferences(bool includeDefaults)
  205. {
  206. var n_result = cef_request_context_t.get_all_preferences(_self, includeDefaults ? 1 : 0);
  207. return CefDictionaryValue.FromNative(n_result);
  208. }
  209. /// <summary>
  210. /// Returns true if the preference with the specified |name| can be modified
  211. /// using SetPreference. As one example preferences set via the command-line
  212. /// usually cannot be modified. This method must be called on the browser
  213. /// process UI thread.
  214. /// </summary>
  215. public bool CanSetPreference(string name)
  216. {
  217. fixed (char* name_str = name)
  218. {
  219. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  220. return cef_request_context_t.can_set_preference(_self, &n_name) != 0;
  221. }
  222. }
  223. /// <summary>
  224. /// Set the |value| associated with preference |name|. Returns true if the
  225. /// value is set successfully and false otherwise. If |value| is NULL the
  226. /// preference will be restored to its default value. If setting the preference
  227. /// fails then |error| will be populated with a detailed description of the
  228. /// problem. This method must be called on the browser process UI thread.
  229. /// </summary>
  230. public bool SetPreference(string name, CefValue value, out string error)
  231. {
  232. fixed (char* name_str = name)
  233. {
  234. var n_name = new cef_string_t(name_str, name != null ? name.Length : 0);
  235. var n_value = value != null ? value.ToNative() : null;
  236. cef_string_t n_error;
  237. var n_result = cef_request_context_t.set_preference(_self, &n_name, n_value, &n_error);
  238. error = cef_string_t.ToString(&n_error);
  239. return n_result != 0;
  240. }
  241. }
  242. /// <summary>
  243. /// Clears all certificate exceptions that were added as part of handling
  244. /// CefRequestHandler::OnCertificateError(). If you call this it is
  245. /// recommended that you also call CloseAllConnections() or you risk not
  246. /// being prompted again for server certificates if you reconnect quickly.
  247. /// If |callback| is non-NULL it will be executed on the UI thread after
  248. /// completion.
  249. /// </summary>
  250. public void ClearCertificateExceptions(CefCompletionCallback callback)
  251. {
  252. var n_callback = callback != null ? callback.ToNative() : null;
  253. cef_request_context_t.clear_certificate_exceptions(_self, n_callback);
  254. }
  255. /// <summary>
  256. /// Clears all HTTP authentication credentials that were added as part of
  257. /// handling GetAuthCredentials. If |callback| is non-NULL it will be executed
  258. /// on the UI thread after completion.
  259. /// </summary>
  260. public void ClearHttpAuthCredentials(CefCompletionCallback callback)
  261. {
  262. var n_callback = callback != null ? callback.ToNative() : null;
  263. cef_request_context_t.clear_http_auth_credentials(_self, n_callback);
  264. }
  265. /// <summary>
  266. /// Clears all active and idle connections that Chromium currently has.
  267. /// This is only recommended if you have released all other CEF objects but
  268. /// don't yet want to call CefShutdown(). If |callback| is non-NULL it will be
  269. /// executed on the UI thread after completion.
  270. /// </summary>
  271. public void CloseAllConnections(CefCompletionCallback callback)
  272. {
  273. var n_callback = callback != null ? callback.ToNative() : null;
  274. cef_request_context_t.close_all_connections(_self, n_callback);
  275. }
  276. /// <summary>
  277. /// Attempts to resolve |origin| to a list of associated IP addresses.
  278. /// |callback| will be executed on the UI thread after completion.
  279. /// </summary>
  280. public void ResolveHost(string origin, CefResolveCallback callback)
  281. {
  282. if (string.IsNullOrEmpty(origin)) throw new ArgumentNullException("origin");
  283. if (callback == null) throw new ArgumentNullException("callback");
  284. fixed (char* origin_str = origin)
  285. {
  286. var n_origin = new cef_string_t(origin_str, origin != null ? origin.Length : 0);
  287. var n_callback = callback.ToNative();
  288. cef_request_context_t.resolve_host(_self, &n_origin, n_callback);
  289. }
  290. }
  291. /// <summary>
  292. /// Load an extension.
  293. /// If extension resources will be read from disk using the default load
  294. /// implementation then |root_directory| should be the absolute path to the
  295. /// extension resources directory and |manifest| should be NULL. If extension
  296. /// resources will be provided by the client (e.g. via CefRequestHandler and/or
  297. /// CefExtensionHandler) then |root_directory| should be a path component
  298. /// unique to the extension (if not absolute this will be internally prefixed
  299. /// with the PK_DIR_RESOURCES path) and |manifest| should contain the contents
  300. /// that would otherwise be read from the "manifest.json" file on disk.
  301. /// The loaded extension will be accessible in all contexts sharing the same
  302. /// storage (HasExtension returns true). However, only the context on which
  303. /// this method was called is considered the loader (DidLoadExtension returns
  304. /// true) and only the loader will receive CefRequestContextHandler callbacks
  305. /// for the extension.
  306. /// CefExtensionHandler::OnExtensionLoaded will be called on load success or
  307. /// CefExtensionHandler::OnExtensionLoadFailed will be called on load failure.
  308. /// If the extension specifies a background script via the "background"
  309. /// manifest key then CefExtensionHandler::OnBeforeBackgroundBrowser will be
  310. /// called to create the background browser. See that method for additional
  311. /// information about background scripts.
  312. /// For visible extension views the client application should evaluate the
  313. /// manifest to determine the correct extension URL to load and then pass that
  314. /// URL to the CefBrowserHost::CreateBrowser* function after the extension has
  315. /// loaded. For example, the client can look for the "browser_action" manifest
  316. /// key as documented at https://developer.chrome.com/extensions/browserAction.
  317. /// Extension URLs take the form "chrome-extension://&lt;extension_id&gt;/&lt;path&gt;".
  318. /// Browsers that host extensions differ from normal browsers as follows:
  319. /// - Can access chrome.* JavaScript APIs if allowed by the manifest. Visit
  320. /// chrome://extensions-support for the list of extension APIs currently
  321. /// supported by CEF.
  322. /// - Main frame navigation to non-extension content is blocked.
  323. /// - Pinch-zooming is disabled.
  324. /// - CefBrowserHost::GetExtension returns the hosted extension.
  325. /// - CefBrowserHost::IsBackgroundHost returns true for background hosts.
  326. /// See https://developer.chrome.com/extensions for extension implementation
  327. /// and usage documentation.
  328. /// </summary>
  329. public void LoadExtension(string rootDirectory, CefDictionaryValue manifest, CefExtensionHandler handler)
  330. {
  331. fixed(char* rootDirectory_str = rootDirectory)
  332. {
  333. var n_rootDirectory = new cef_string_t(rootDirectory_str, rootDirectory != null ? rootDirectory.Length : 0);
  334. var n_manifest = manifest != null ? manifest.ToNative() : null;
  335. var n_handler = handler != null ? handler.ToNative() : null;
  336. cef_request_context_t.load_extension(_self, &n_rootDirectory, n_manifest, n_handler);
  337. }
  338. }
  339. /// <summary>
  340. /// Returns true if this context was used to load the extension identified by
  341. /// |extension_id|. Other contexts sharing the same storage will also have
  342. /// access to the extension (see HasExtension). This method must be called on
  343. /// the browser process UI thread.
  344. /// </summary>
  345. public bool DidLoadExtension(string extensionId)
  346. {
  347. fixed(char* extensionId_str = extensionId)
  348. {
  349. var n_extensionId = new cef_string_t(extensionId_str, extensionId != null ? extensionId.Length : 0);
  350. return cef_request_context_t.did_load_extension(_self, &n_extensionId) != 0;
  351. }
  352. }
  353. /// <summary>
  354. /// Returns true if this context has access to the extension identified by
  355. /// |extension_id|. This may not be the context that was used to load the
  356. /// extension (see DidLoadExtension). This method must be called on the browser
  357. /// process UI thread.
  358. /// </summary>
  359. public bool HasExtension(string extensionId)
  360. {
  361. fixed (char* extensionId_str = extensionId)
  362. {
  363. var n_extensionId = new cef_string_t(extensionId_str, extensionId != null ? extensionId.Length : 0);
  364. return cef_request_context_t.has_extension(_self, &n_extensionId) != 0;
  365. }
  366. }
  367. /// <summary>
  368. /// Retrieve the list of all extensions that this context has access to (see
  369. /// HasExtension). |extension_ids| will be populated with the list of extension
  370. /// ID values. Returns true on success. This method must be called on the
  371. /// browser process UI thread.
  372. /// </summary>
  373. public bool GetExtensions(out string[] extensionIds)
  374. {
  375. var n_extensionIds = libcef.string_list_alloc();
  376. var result = cef_request_context_t.get_extensions(_self, n_extensionIds) != 0;
  377. if (result)
  378. {
  379. extensionIds = cef_string_list.ToArray(n_extensionIds);
  380. }
  381. else
  382. {
  383. extensionIds = null;
  384. }
  385. libcef.string_list_free(n_extensionIds);
  386. return result;
  387. }
  388. /// <summary>
  389. /// Returns the extension matching |extension_id| or NULL if no matching
  390. /// extension is accessible in this context (see HasExtension). This method
  391. /// must be called on the browser process UI thread.
  392. /// </summary>
  393. public CefExtension GetExtension(string extensionId)
  394. {
  395. fixed (char* extensionId_str = extensionId)
  396. {
  397. var n_extensionId = new cef_string_t(extensionId_str, extensionId != null ? extensionId.Length : 0);
  398. var n_result = cef_request_context_t.get_extension(_self, &n_extensionId);
  399. return CefExtension.FromNativeOrNull(n_result);
  400. }
  401. }
  402. /// <summary>
  403. /// Returns the MediaRouter object associated with this context. If |callback|
  404. /// is non-NULL it will be executed asnychronously on the UI thread after the
  405. /// manager's context has been initialized.
  406. /// </summary>
  407. public CefMediaRouter GetMediaRouter(CefCompletionCallback? callback)
  408. {
  409. var nCallback = callback != null ? callback.ToNative() : null;
  410. var nResult = cef_request_context_t.get_media_router(_self, nCallback);
  411. return CefMediaRouter.FromNative(nResult);
  412. }
  413. }
  414. }