CEFInterfaceCookieManager.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Engine/Source/Runtime/WebBrowser/Private/CEF/CEFCookieManager.cpp
  2. #include "CoreTypes.h"
  3. #include "Containers/ContainersFwd.h"
  4. #if WITH_CEF3
  5. #include "IWebInterfaceBrowserCookieManager.h"
  6. #include "WebInterfaceBrowserSingleton.h"
  7. #include "CEFInterfaceLibCefIncludes.h"
  8. class FCefInterfaceCookieManager
  9. : public IWebInterfaceBrowserCookieManager
  10. {
  11. public:
  12. virtual ~FCefInterfaceCookieManager()
  13. { }
  14. virtual void SetCookie(const FString& URL, const FCookie& Cookie, TFunction<void(bool)> Completed) override
  15. {
  16. CefRefPtr<FSetCookieFunctionCallback> Callback = Completed ? new FSetCookieFunctionCallback(Completed) : nullptr;
  17. CefCookie CefCookie;
  18. CefString(&CefCookie.name) = TCHAR_TO_WCHAR(*Cookie.Name);
  19. CefString(&CefCookie.value) = TCHAR_TO_WCHAR(*Cookie.Value);
  20. CefString(&CefCookie.domain) = TCHAR_TO_WCHAR(*Cookie.Domain);
  21. CefString(&CefCookie.path) = TCHAR_TO_WCHAR(*Cookie.Path);
  22. CefCookie.secure = Cookie.bSecure;
  23. CefCookie.httponly = Cookie.bHttpOnly;
  24. CefCookie.has_expires = Cookie.bHasExpires;
  25. cef_time_t CefTime;
  26. CefTime.year = Cookie.Expires.GetYear();
  27. CefTime.month = Cookie.Expires.GetMonth();
  28. CefTime.day_of_month = Cookie.Expires.GetDay();
  29. CefTime.hour = Cookie.Expires.GetHour();
  30. CefTime.minute = Cookie.Expires.GetMinute();
  31. CefTime.second = Cookie.Expires.GetSecond();
  32. CefTime.millisecond = Cookie.Expires.GetMillisecond();
  33. const EDayOfWeek DayOfWeek = Cookie.Expires.GetDayOfWeek();
  34. if (DayOfWeek == EDayOfWeek::Sunday) // some crazy reason our date/time class think Monday is the beginning of the week
  35. {
  36. CefTime.day_of_week = 0;
  37. }
  38. else
  39. {
  40. CefTime.day_of_week = ((int32)DayOfWeek) + 1;
  41. }
  42. CefCookie.expires = CefTime;
  43. if (!CookieManager->SetCookie(TCHAR_TO_WCHAR(*URL), CefCookie, Callback))
  44. {
  45. Callback->OnComplete(false);
  46. }
  47. }
  48. virtual void DeleteCookies(const FString& URL, const FString& CookieName, TFunction<void(int)> Completed) override
  49. {
  50. CefRefPtr<FDeleteCookiesFunctionCallback> Callback = Completed ? new FDeleteCookiesFunctionCallback(Completed) : nullptr;
  51. if (!CookieManager->DeleteCookies(TCHAR_TO_WCHAR(*URL), TCHAR_TO_WCHAR(*CookieName), Callback))
  52. {
  53. Callback->OnComplete(-1);
  54. }
  55. }
  56. private:
  57. FCefInterfaceCookieManager(
  58. const CefRefPtr<CefCookieManager>& InCookieManager)
  59. : CookieManager(InCookieManager)
  60. { }
  61. // Callback that will invoke the callback with the result on the UI thread.
  62. class FDeleteCookiesFunctionCallback
  63. : public CefDeleteCookiesCallback
  64. {
  65. TFunction<void(int)> Callback;
  66. public:
  67. FDeleteCookiesFunctionCallback(const TFunction<void(int)>& InCallback)
  68. : Callback(InCallback)
  69. {}
  70. virtual void OnComplete(int NumDeleted) override
  71. {
  72. // We're on the IO thread, so we'll have to schedule the callback on the main thread
  73. CefPostTask(TID_UI, new FDeleteCookiesNotificationTask(Callback, NumDeleted));
  74. }
  75. IMPLEMENT_REFCOUNTING(FDeleteCookiesFunctionCallback);
  76. };
  77. // Callback that will invoke the callback with the result on the UI thread.
  78. class FSetCookieFunctionCallback
  79. : public CefSetCookieCallback
  80. {
  81. TFunction<void(bool)> Callback;
  82. public:
  83. FSetCookieFunctionCallback(const TFunction<void(bool)>& InCallback)
  84. : Callback(InCallback)
  85. {}
  86. virtual void OnComplete(bool bSuccess) override
  87. {
  88. // We're on the IO thread, so we'll have to schedule the callback on the main thread
  89. CefPostTask(TID_UI, new FSetCookieNotificationTask(Callback, bSuccess));
  90. }
  91. IMPLEMENT_REFCOUNTING(FSetCookieFunctionCallback);
  92. };
  93. // Task for executing a callback on a given thread.
  94. class FDeleteCookiesNotificationTask
  95. : public CefTask
  96. {
  97. TFunction<void(int)> Callback;
  98. int Value;
  99. public:
  100. FDeleteCookiesNotificationTask(const TFunction<void(int)>& InCallback, int InValue)
  101. : Callback(InCallback)
  102. , Value(InValue)
  103. {}
  104. virtual void Execute() override
  105. {
  106. Callback(Value);
  107. }
  108. IMPLEMENT_REFCOUNTING(FDeleteCookiesNotificationTask);
  109. };
  110. // Task for executing a callback on a given thread.
  111. class FSetCookieNotificationTask
  112. : public CefTask
  113. {
  114. TFunction<void(bool)> Callback;
  115. bool bSuccess;
  116. public:
  117. FSetCookieNotificationTask(const TFunction<void(bool)>& InCallback, bool InSuccess)
  118. : Callback(InCallback)
  119. , bSuccess(InSuccess)
  120. {}
  121. virtual void Execute() override
  122. {
  123. Callback(bSuccess);
  124. }
  125. IMPLEMENT_REFCOUNTING(FSetCookieNotificationTask);
  126. };
  127. private:
  128. const CefRefPtr<CefCookieManager> CookieManager;
  129. friend FCefWebInterfaceCookieManagerFactory;
  130. };
  131. TSharedRef<IWebInterfaceBrowserCookieManager> FCefWebInterfaceCookieManagerFactory::Create(
  132. const CefRefPtr<CefCookieManager>& CookieManager)
  133. {
  134. return MakeShareable(new FCefInterfaceCookieManager(
  135. CookieManager));
  136. }
  137. #endif