/CefSharp.Core/RequestContext.h

https://github.com/cefsharp/CefSharp · C Header · 222 lines · 124 code · 33 blank · 65 comment · 4 complexity · c5e05c8fbea7c73b6e3a82a2fb476745 MD5 · raw file

  1. // Copyright Š 2010-2016 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. #include "include\cef_request_context.h"
  7. #include "RequestContextSettings.h"
  8. #include "SchemeHandlerFactoryWrapper.h"
  9. #include "RequestContextHandler.h"
  10. #include "Internals\CefCompletionCallbackAdapter.h"
  11. #include "Internals\CookieManager.h"
  12. using namespace System::Runtime::InteropServices;
  13. namespace CefSharp
  14. {
  15. /// <summary>
  16. /// A request context provides request handling for a set of related browser objects.
  17. /// A request context is specified when creating a new browser object via the CefBrowserHost
  18. /// static factory methods. Browser objects with different request contexts will never be
  19. /// hosted in the same render process. Browser objects with the same request context may or
  20. /// may not be hosted in the same render process depending on the process model.
  21. /// Browser objects created indirectly via the JavaScript window.open function or targeted
  22. /// links will share the same render process and the same request context as the source browser.
  23. /// When running in single-process mode there is only a single render process (the main process)
  24. /// and so all browsers created in single-process mode will share the same request context.
  25. /// This will be the first request context passed into a CefBrowserHost static factory method
  26. /// and all other request context objects will be ignored.
  27. /// </summary>
  28. public ref class RequestContext : public IRequestContext
  29. {
  30. private:
  31. MCefRefPtr<CefRequestContext> _requestContext;
  32. RequestContextSettings^ _settings;
  33. internal:
  34. RequestContext(CefRefPtr<CefRequestContext>& context)
  35. {
  36. _requestContext = context;
  37. }
  38. public:
  39. RequestContext()
  40. {
  41. CefRequestContextSettings settings;
  42. _requestContext = CefRequestContext::CreateContext(settings, NULL);
  43. }
  44. RequestContext(RequestContextSettings^ settings) : _settings(settings)
  45. {
  46. _requestContext = CefRequestContext::CreateContext(settings, NULL);
  47. }
  48. RequestContext(IPluginHandler^ pluginHandler)
  49. {
  50. CefRequestContextSettings settings;
  51. _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(pluginHandler));
  52. }
  53. RequestContext(RequestContextSettings^ settings, IPluginHandler^ pluginHandler) : _settings(settings)
  54. {
  55. _requestContext = CefRequestContext::CreateContext(settings, new RequestContextHandler(pluginHandler));
  56. }
  57. !RequestContext()
  58. {
  59. _requestContext = NULL;
  60. }
  61. ~RequestContext()
  62. {
  63. this->!RequestContext();
  64. delete _settings;
  65. }
  66. virtual bool IsSame(IRequestContext^ context)
  67. {
  68. auto requestContext = (RequestContext^)context;
  69. return _requestContext->IsSame(requestContext);
  70. }
  71. virtual bool IsSharingWith(IRequestContext^ context)
  72. {
  73. auto requestContext = (RequestContext^)context;
  74. return _requestContext->IsSharingWith(requestContext);
  75. }
  76. virtual ICookieManager^ GetDefaultCookieManager(ICompletionCallback^ callback)
  77. {
  78. CefRefPtr<CefCompletionCallback> wrapper = callback == nullptr ? NULL : new CefCompletionCallbackAdapter(callback);
  79. auto cookieManager = _requestContext->GetDefaultCookieManager(wrapper);
  80. if (cookieManager.get())
  81. {
  82. return gcnew CookieManager(cookieManager);
  83. }
  84. return nullptr;
  85. }
  86. virtual property bool IsGlobal
  87. {
  88. bool get() { return _requestContext->IsGlobal(); }
  89. }
  90. virtual bool RegisterSchemeHandlerFactory(String^ schemeName, String^ domainName, ISchemeHandlerFactory^ factory)
  91. {
  92. auto wrapper = new SchemeHandlerFactoryWrapper(factory);
  93. return _requestContext->RegisterSchemeHandlerFactory(StringUtils::ToNative(schemeName), StringUtils::ToNative(domainName), wrapper);
  94. }
  95. virtual bool ClearSchemeHandlerFactories()
  96. {
  97. return _requestContext->ClearSchemeHandlerFactories();
  98. }
  99. ///
  100. // Returns the cache path for this object. If empty an "incognito mode"
  101. // in-memory cache is being used.
  102. ///
  103. /*--cef()--*/
  104. virtual property String^ CachePath
  105. {
  106. String^ get() { return StringUtils::ToClr(_requestContext->GetCachePath()); }
  107. }
  108. ///
  109. // Tells all renderer processes associated with this context to throw away
  110. // their plugin list cache. If |reload_pages| is true they will also reload
  111. // all pages with plugins. CefRequestContextHandler::OnBeforePluginLoad may
  112. // be called to rebuild the plugin list cache.
  113. ///
  114. /*--cef()--*/
  115. virtual void PurgePluginListCache(bool reloadPages)
  116. {
  117. _requestContext->PurgePluginListCache(reloadPages);
  118. }
  119. ///
  120. // Returns true if a preference with the specified |name| exists. This method
  121. // must be called on the browser process UI thread.
  122. ///
  123. /*--cef()--*/
  124. virtual bool HasPreference(String^ name)
  125. {
  126. return _requestContext->HasPreference(StringUtils::ToNative(name));
  127. }
  128. ///
  129. // Returns the value for the preference with the specified |name|. Returns
  130. // NULL if the preference does not exist. The returned object contains a copy
  131. // of the underlying preference value and modifications to the returned object
  132. // will not modify the underlying preference value. This method must be called
  133. // on the browser process UI thread.
  134. ///
  135. /*--cef()--*/
  136. virtual Object^ GetPreference(String^ name)
  137. {
  138. return TypeConversion::FromNative(_requestContext->GetPreference(StringUtils::ToNative(name)));
  139. }
  140. ///
  141. // Returns all preferences as a dictionary. If |include_defaults| is true then
  142. // preferences currently at their default value will be included. The returned
  143. // object contains a copy of the underlying preference values and
  144. // modifications to the returned object will not modify the underlying
  145. // preference values. This method must be called on the browser process UI
  146. // thread.
  147. ///
  148. /*--cef()--*/
  149. virtual IDictionary<String^, Object^>^ GetAllPreferences(bool includeDefaults)
  150. {
  151. auto preferences = _requestContext->GetAllPreferences(includeDefaults);
  152. return TypeConversion::FromNative(preferences);
  153. }
  154. ///
  155. // Returns true if the preference with the specified |name| can be modified
  156. // using SetPreference. As one example preferences set via the command-line
  157. // usually cannot be modified. This method must be called on the browser
  158. // process UI thread.
  159. ///
  160. /*--cef()--*/
  161. virtual bool CanSetPreference(String^ name)
  162. {
  163. return _requestContext->CanSetPreference(StringUtils::ToNative(name));
  164. }
  165. ///
  166. // Set the |value| associated with preference |name|. Returns true if the
  167. // value is set successfully and false otherwise. If |value| is NULL the
  168. // preference will be restored to its default value. If setting the preference
  169. // fails then |error| will be populated with a detailed description of the
  170. // problem. This method must be called on the browser process UI thread.
  171. ///
  172. /*--cef(optional_param=value)--*/
  173. virtual bool SetPreference(String^ name, Object^ value, [Out] String^ %error)
  174. {
  175. CefString cefError;
  176. auto success = _requestContext->SetPreference(StringUtils::ToNative(name), TypeConversion::ToNative(value), cefError);
  177. error = StringUtils::ToClr(cefError);
  178. return success;
  179. }
  180. operator CefRefPtr<CefRequestContext>()
  181. {
  182. if(this == nullptr)
  183. {
  184. return NULL;
  185. }
  186. return _requestContext.get();
  187. }
  188. };
  189. }