window-basic-main-browser.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <QDir>
  15. #include <QThread>
  16. #include <QMessageBox>
  17. #include <qt-wrappers.hpp>
  18. #include "window-basic-main.hpp"
  19. #include <random>
  20. #ifdef BROWSER_AVAILABLE
  21. #include <browser-panel.hpp>
  22. #endif
  23. struct QCef;
  24. struct QCefCookieManager;
  25. extern QCef *cef;
  26. extern QCefCookieManager *panel_cookies;
  27. static std::string GenId()
  28. {
  29. std::random_device rd;
  30. std::mt19937_64 e2(rd());
  31. std::uniform_int_distribution<uint64_t> dist(0, 0xFFFFFFFFFFFFFFFF);
  32. uint64_t id = dist(e2);
  33. char id_str[20];
  34. snprintf(id_str, sizeof(id_str), "%16llX", (unsigned long long)id);
  35. return std::string(id_str);
  36. }
  37. void CheckExistingCookieId()
  38. {
  39. OBSBasic *main = OBSBasic::Get();
  40. if (config_has_user_value(main->Config(), "Panels", "CookieId"))
  41. return;
  42. config_set_string(main->Config(), "Panels", "CookieId", GenId().c_str());
  43. }
  44. #ifdef BROWSER_AVAILABLE
  45. static void InitPanelCookieManager()
  46. {
  47. if (!cef)
  48. return;
  49. if (panel_cookies)
  50. return;
  51. CheckExistingCookieId();
  52. OBSBasic *main = OBSBasic::Get();
  53. const char *cookie_id = config_get_string(main->Config(), "Panels", "CookieId");
  54. std::string sub_path;
  55. sub_path += "obs_profile_cookies/";
  56. sub_path += cookie_id;
  57. panel_cookies = cef->create_cookie_manager(sub_path);
  58. }
  59. #endif
  60. void DestroyPanelCookieManager()
  61. {
  62. #ifdef BROWSER_AVAILABLE
  63. if (panel_cookies) {
  64. panel_cookies->FlushStore();
  65. delete panel_cookies;
  66. panel_cookies = nullptr;
  67. }
  68. #endif
  69. }
  70. void DeleteCookies()
  71. {
  72. #ifdef BROWSER_AVAILABLE
  73. if (panel_cookies) {
  74. panel_cookies->DeleteCookies("", "");
  75. }
  76. #endif
  77. }
  78. void DuplicateCurrentCookieProfile(ConfigFile &config)
  79. {
  80. #ifdef BROWSER_AVAILABLE
  81. if (cef) {
  82. OBSBasic *main = OBSBasic::Get();
  83. std::string cookie_id = config_get_string(main->Config(), "Panels", "CookieId");
  84. std::string src_path;
  85. src_path += "obs_profile_cookies/";
  86. src_path += cookie_id;
  87. std::string new_id = GenId();
  88. std::string dst_path;
  89. dst_path += "obs_profile_cookies/";
  90. dst_path += new_id;
  91. BPtr<char> src_path_full = cef->get_cookie_path(src_path);
  92. BPtr<char> dst_path_full = cef->get_cookie_path(dst_path);
  93. QDir srcDir(src_path_full.Get());
  94. QDir dstDir(dst_path_full.Get());
  95. if (srcDir.exists()) {
  96. if (!dstDir.exists())
  97. dstDir.mkdir(dst_path_full.Get());
  98. QStringList files = srcDir.entryList(QDir::Files);
  99. for (const QString &file : files) {
  100. QString src = QString(src_path_full);
  101. QString dst = QString(dst_path_full);
  102. src += QDir::separator() + file;
  103. dst += QDir::separator() + file;
  104. QFile::copy(src, dst);
  105. }
  106. }
  107. config_set_string(config, "Panels", "CookieId", cookie_id.c_str());
  108. config_set_string(main->Config(), "Panels", "CookieId", new_id.c_str());
  109. }
  110. #else
  111. UNUSED_PARAMETER(config);
  112. #endif
  113. }
  114. void OBSBasic::InitBrowserPanelSafeBlock()
  115. {
  116. #ifdef BROWSER_AVAILABLE
  117. if (!cef)
  118. return;
  119. if (cef->init_browser()) {
  120. InitPanelCookieManager();
  121. return;
  122. }
  123. ExecThreadedWithoutBlocking([] { cef->wait_for_browser_init(); }, QTStr("BrowserPanelInit.Title"),
  124. QTStr("BrowserPanelInit.Text"));
  125. InitPanelCookieManager();
  126. #endif
  127. }