OBSBasic_Browser.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "OBSBasic.hpp"
  15. #ifdef BROWSER_AVAILABLE
  16. #include <dialogs/OBSExtraBrowsers.hpp>
  17. #include <docks/BrowserDock.hpp>
  18. #include <json11.hpp>
  19. #include <qt-wrappers.hpp>
  20. #include <QDir>
  21. using namespace json11;
  22. #endif
  23. #include <random>
  24. struct QCef;
  25. struct QCefCookieManager;
  26. QCef *cef = nullptr;
  27. QCefCookieManager *panel_cookies = nullptr;
  28. bool cef_js_avail = false;
  29. static std::string GenId()
  30. {
  31. std::random_device rd;
  32. std::mt19937_64 e2(rd());
  33. std::uniform_int_distribution<uint64_t> dist(0, 0xFFFFFFFFFFFFFFFF);
  34. uint64_t id = dist(e2);
  35. char id_str[20];
  36. snprintf(id_str, sizeof(id_str), "%16llX", (unsigned long long)id);
  37. return std::string(id_str);
  38. }
  39. void CheckExistingCookieId()
  40. {
  41. OBSBasic *main = OBSBasic::Get();
  42. if (config_has_user_value(main->Config(), "Panels", "CookieId"))
  43. return;
  44. config_set_string(main->Config(), "Panels", "CookieId", GenId().c_str());
  45. }
  46. #ifdef BROWSER_AVAILABLE
  47. static void InitPanelCookieManager()
  48. {
  49. if (!cef)
  50. return;
  51. if (panel_cookies)
  52. return;
  53. CheckExistingCookieId();
  54. OBSBasic *main = OBSBasic::Get();
  55. const char *cookie_id = config_get_string(main->Config(), "Panels", "CookieId");
  56. std::string sub_path;
  57. sub_path += "obs_profile_cookies/";
  58. sub_path += cookie_id;
  59. panel_cookies = cef->create_cookie_manager(sub_path);
  60. }
  61. #endif
  62. void DestroyPanelCookieManager()
  63. {
  64. #ifdef BROWSER_AVAILABLE
  65. if (panel_cookies) {
  66. panel_cookies->FlushStore();
  67. delete panel_cookies;
  68. panel_cookies = nullptr;
  69. }
  70. #endif
  71. }
  72. void DeleteCookies()
  73. {
  74. #ifdef BROWSER_AVAILABLE
  75. if (panel_cookies) {
  76. panel_cookies->DeleteCookies("", "");
  77. }
  78. #endif
  79. }
  80. void DuplicateCurrentCookieProfile(ConfigFile &config)
  81. {
  82. #ifdef BROWSER_AVAILABLE
  83. if (cef) {
  84. OBSBasic *main = OBSBasic::Get();
  85. std::string cookie_id = config_get_string(main->Config(), "Panels", "CookieId");
  86. std::string src_path;
  87. src_path += "obs_profile_cookies/";
  88. src_path += cookie_id;
  89. std::string new_id = GenId();
  90. std::string dst_path;
  91. dst_path += "obs_profile_cookies/";
  92. dst_path += new_id;
  93. BPtr<char> src_path_full = cef->get_cookie_path(src_path);
  94. BPtr<char> dst_path_full = cef->get_cookie_path(dst_path);
  95. QDir srcDir(src_path_full.Get());
  96. QDir dstDir(dst_path_full.Get());
  97. if (srcDir.exists()) {
  98. if (!dstDir.exists())
  99. dstDir.mkdir(dst_path_full.Get());
  100. QStringList files = srcDir.entryList(QDir::Files);
  101. for (const QString &file : files) {
  102. QString src = QString(src_path_full);
  103. QString dst = QString(dst_path_full);
  104. src += QDir::separator() + file;
  105. dst += QDir::separator() + file;
  106. QFile::copy(src, dst);
  107. }
  108. }
  109. config_set_string(config, "Panels", "CookieId", cookie_id.c_str());
  110. config_set_string(main->Config(), "Panels", "CookieId", new_id.c_str());
  111. }
  112. #else
  113. UNUSED_PARAMETER(config);
  114. #endif
  115. }
  116. void OBSBasic::InitBrowserPanelSafeBlock()
  117. {
  118. #ifdef BROWSER_AVAILABLE
  119. if (!cef)
  120. return;
  121. if (cef->init_browser()) {
  122. InitPanelCookieManager();
  123. return;
  124. }
  125. ExecThreadedWithoutBlocking([] { cef->wait_for_browser_init(); }, QTStr("BrowserPanelInit.Title"),
  126. QTStr("BrowserPanelInit.Text"));
  127. InitPanelCookieManager();
  128. #endif
  129. }