CefExtensionHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /// Implement this interface to handle events related to browser extensions.
  10. /// The methods of this class will be called on the UI thread. See
  11. /// CefRequestContext::LoadExtension for information about extension loading.
  12. /// </summary>
  13. public abstract unsafe partial class CefExtensionHandler
  14. {
  15. private void on_extension_load_failed(cef_extension_handler_t* self, CefErrorCode result)
  16. {
  17. CheckSelf(self);
  18. OnExtensionLoadFailed(result);
  19. }
  20. /// <summary>
  21. /// Called if the CefRequestContext::LoadExtension request fails. |result| will
  22. /// be the error code.
  23. /// </summary>
  24. protected virtual void OnExtensionLoadFailed(CefErrorCode result) { }
  25. private void on_extension_loaded(cef_extension_handler_t* self, cef_extension_t* extension)
  26. {
  27. CheckSelf(self);
  28. var mExtension = CefExtension.FromNative(extension);
  29. OnExtensionLoaded(mExtension);
  30. }
  31. /// <summary>
  32. /// Called if the CefRequestContext::LoadExtension request succeeds.
  33. /// |extension| is the loaded extension.
  34. /// </summary>
  35. protected virtual void OnExtensionLoaded(CefExtension extension) { }
  36. private void on_extension_unloaded(cef_extension_handler_t* self, cef_extension_t* extension)
  37. {
  38. CheckSelf(self);
  39. var mExtension = CefExtension.FromNative(extension);
  40. OnExtensionUnloaded(mExtension);
  41. }
  42. /// <summary>
  43. /// Called after the CefExtension::Unload request has completed.
  44. /// </summary>
  45. protected virtual void OnExtensionUnloaded(CefExtension extension) { }
  46. private int on_before_background_browser(cef_extension_handler_t* self, cef_extension_t* extension, cef_string_t* url, cef_client_t** client, cef_browser_settings_t* settings)
  47. {
  48. CheckSelf(self);
  49. var m_extension = CefExtension.FromNative(extension);
  50. var m_url = cef_string_t.ToString(url);
  51. var m_client = CefClient.FromNative(*client);
  52. var m_settings = new CefBrowserSettings(settings);
  53. var o_client = m_client;
  54. var cancel = OnBeforeBackgroundBrowser(m_extension, m_url, ref m_client, m_settings);
  55. if (!cancel && ((object)o_client != m_client && m_client != null))
  56. {
  57. *client = m_client.ToNative();
  58. }
  59. m_settings.Dispose();
  60. return cancel ? 1 : 0;
  61. }
  62. /// <summary>
  63. /// Called when an extension needs a browser to host a background script
  64. /// specified via the "background" manifest key. The browser will have no
  65. /// visible window and cannot be displayed. |extension| is the extension that
  66. /// is loading the background script. |url| is an internally generated
  67. /// reference to an HTML page that will be used to load the background script
  68. /// via a &lt;script&gt; src attribute. To allow creation of the browser optionally
  69. /// modify |client| and |settings| and return false. To cancel creation of the
  70. /// browser (and consequently cancel load of the background script) return
  71. /// true. Successful creation will be indicated by a call to
  72. /// CefLifeSpanHandler::OnAfterCreated, and CefBrowserHost::IsBackgroundHost
  73. /// will return true for the resulting browser. See
  74. /// https://developer.chrome.com/extensions/event_pages for more information
  75. /// about extension background script usage.
  76. /// </summary>
  77. protected virtual bool OnBeforeBackgroundBrowser(CefExtension extension, string url, ref CefClient client, CefBrowserSettings settings)
  78. {
  79. return false;
  80. }
  81. private int on_before_browser(cef_extension_handler_t* self, cef_extension_t* extension, cef_browser_t* browser, cef_browser_t* active_browser, int index, cef_string_t* url, int active, cef_window_info_t* windowInfo, cef_client_t** client, cef_browser_settings_t* settings)
  82. {
  83. CheckSelf(self);
  84. var m_extension = CefExtension.FromNative(extension);
  85. var m_browser = CefBrowser.FromNativeOrNull(browser);
  86. var m_activeBrowser = CefBrowser.FromNativeOrNull(active_browser);
  87. var m_url = cef_string_t.ToString(url);
  88. var m_active = active != 0;
  89. var m_windowInfo = CefWindowInfo.FromNative(windowInfo);
  90. var m_client = CefClient.FromNative(*client);
  91. var m_settings = new CefBrowserSettings(settings);
  92. var o_client = m_client;
  93. var cancel = OnBeforeBrowser(m_extension, m_browser, m_activeBrowser, index, m_url, m_active, m_windowInfo, ref m_client, m_settings);
  94. if (!cancel && ((object)o_client != m_client && m_client != null))
  95. {
  96. *client = m_client.ToNative();
  97. }
  98. m_windowInfo.Dispose();
  99. m_settings.Dispose();
  100. return cancel ? 1 : 0;
  101. }
  102. /// <summary>
  103. /// Called when an extension API (e.g. chrome.tabs.create) requests creation of
  104. /// a new browser. |extension| and |browser| are the source of the API call.
  105. /// |active_browser| may optionally be specified via the windowId property or
  106. /// returned via the GetActiveBrowser() callback and provides the default
  107. /// |client| and |settings| values for the new browser. |index| is the position
  108. /// value optionally specified via the index property. |url| is the URL that
  109. /// will be loaded in the browser. |active| is true if the new browser should
  110. /// be active when opened. To allow creation of the browser optionally modify
  111. /// |windowInfo|, |client| and |settings| and return false. To cancel creation
  112. /// of the browser return true. Successful creation will be indicated by a call
  113. /// to CefLifeSpanHandler::OnAfterCreated. Any modifications to |windowInfo|
  114. /// will be ignored if |active_browser| is wrapped in a CefBrowserView.
  115. /// </summary>
  116. protected virtual bool OnBeforeBrowser(CefExtension extension, CefBrowser browser, CefBrowser activeBrowser, int index, string url, bool active, CefWindowInfo windowInfo, ref CefClient client, CefBrowserSettings settings)
  117. {
  118. return false;
  119. }
  120. private cef_browser_t* get_active_browser(cef_extension_handler_t* self, cef_extension_t* extension, cef_browser_t* browser, int include_incognito)
  121. {
  122. CheckSelf(self);
  123. var m_extension = CefExtension.FromNative(extension);
  124. var m_browser = CefBrowser.FromNative(browser);
  125. var m_includeIncognito = include_incognito != 0;
  126. var result = GetActiveBrowser(m_extension, m_browser, m_includeIncognito);
  127. return result != null ? result.ToNative() : null;
  128. }
  129. /// <summary>
  130. /// Called when no tabId is specified to an extension API call that accepts a
  131. /// tabId parameter (e.g. chrome.tabs.*). |extension| and |browser| are the
  132. /// source of the API call. Return the browser that will be acted on by the API
  133. /// call or return NULL to act on |browser|. The returned browser must share
  134. /// the same CefRequestContext as |browser|. Incognito browsers should not be
  135. /// considered unless the source extension has incognito access enabled, in
  136. /// which case |include_incognito| will be true.
  137. /// </summary>
  138. protected virtual CefBrowser GetActiveBrowser(CefExtension extension, CefBrowser browser, bool includeIncognito)
  139. {
  140. return null;
  141. }
  142. private int can_access_browser(cef_extension_handler_t* self, cef_extension_t* extension, cef_browser_t* browser, int include_incognito, cef_browser_t* target_browser)
  143. {
  144. CheckSelf(self);
  145. var m_extension = CefExtension.FromNative(extension);
  146. var m_browser = CefBrowser.FromNativeOrNull(browser);
  147. var m_includeIcognito = include_incognito != 0;
  148. var m_targetBrowser = CefBrowser.FromNativeOrNull(target_browser);
  149. var result = CanAccessBrowser(m_extension, m_browser, m_includeIcognito, m_targetBrowser);
  150. return result ? 1 : 0;
  151. }
  152. /// <summary>
  153. /// Called when the tabId associated with |target_browser| is specified to an
  154. /// extension API call that accepts a tabId parameter (e.g. chrome.tabs.*).
  155. /// |extension| and |browser| are the source of the API call. Return true
  156. /// to allow access of false to deny access. Access to incognito browsers
  157. /// should not be allowed unless the source extension has incognito access
  158. /// enabled, in which case |include_incognito| will be true.
  159. /// </summary>
  160. protected virtual bool CanAccessBrowser(CefExtension extension, CefBrowser browser, bool includeIncognito, CefBrowser targetBrowser)
  161. {
  162. return true;
  163. }
  164. private int get_extension_resource(cef_extension_handler_t* self, cef_extension_t* extension, cef_browser_t* browser, cef_string_t* file, cef_get_extension_resource_callback_t* callback)
  165. {
  166. CheckSelf(self);
  167. var m_extension = CefExtension.FromNative(extension);
  168. var m_browser = CefBrowser.FromNativeOrNull(browser);
  169. var m_file = cef_string_t.ToString(file);
  170. var m_callback = CefGetExtensionResourceCallback.FromNativeOrNull(callback);
  171. var handled = GetExtensionResource(m_extension, m_browser, m_file, m_callback);
  172. if (!handled)
  173. {
  174. m_callback.Dispose();
  175. }
  176. return handled ? 1 : 0;
  177. }
  178. /// <summary>
  179. /// Called to retrieve an extension resource that would normally be loaded from
  180. /// disk (e.g. if a file parameter is specified to chrome.tabs.executeScript).
  181. /// |extension| and |browser| are the source of the resource request. |file| is
  182. /// the requested relative file path. To handle the resource request return
  183. /// true and execute |callback| either synchronously or asynchronously. For the
  184. /// default behavior which reads the resource from the extension directory on
  185. /// disk return false. Localization substitutions will not be applied to
  186. /// resources handled via this method.
  187. /// </summary>
  188. protected virtual bool GetExtensionResource(CefExtension extension, CefBrowser browser, string file, CefGetExtensionResourceCallback callback)
  189. {
  190. return false;
  191. }
  192. }
  193. }