window-basic-main-browser.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************
  2. Copyright (C) 2018 by Hugh 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 "window-basic-main.hpp"
  18. #include "qt-wrappers.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",
  43. GenId().c_str());
  44. }
  45. #ifdef BROWSER_AVAILABLE
  46. static void InitPanelCookieManager()
  47. {
  48. if (!cef)
  49. return;
  50. if (panel_cookies)
  51. return;
  52. CheckExistingCookieId();
  53. OBSBasic *main = OBSBasic::Get();
  54. const char *cookie_id =
  55. 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 =
  86. config_get_string(main->Config(), "Panels", "CookieId");
  87. std::string src_path;
  88. src_path += "obs_profile_cookies/";
  89. src_path += cookie_id;
  90. std::string new_id = GenId();
  91. std::string dst_path;
  92. dst_path += "obs_profile_cookies/";
  93. dst_path += new_id;
  94. BPtr<char> src_path_full = cef->get_cookie_path(src_path);
  95. BPtr<char> dst_path_full = cef->get_cookie_path(dst_path);
  96. QDir srcDir(src_path_full.Get());
  97. QDir dstDir(dst_path_full.Get());
  98. if (srcDir.exists()) {
  99. if (!dstDir.exists())
  100. dstDir.mkdir(dst_path_full.Get());
  101. QStringList files = srcDir.entryList(QDir::Files);
  102. for (const QString &file : files) {
  103. QString src = QString(src_path_full);
  104. QString dst = QString(dst_path_full);
  105. src += QDir::separator() + file;
  106. dst += QDir::separator() + file;
  107. QFile::copy(src, dst);
  108. }
  109. }
  110. config_set_string(config, "Panels", "CookieId",
  111. cookie_id.c_str());
  112. config_set_string(main->Config(), "Panels", "CookieId",
  113. new_id.c_str());
  114. }
  115. #else
  116. UNUSED_PARAMETER(config);
  117. #endif
  118. }
  119. void OBSBasic::InitBrowserPanelSafeBlock()
  120. {
  121. #ifdef BROWSER_AVAILABLE
  122. if (!cef)
  123. return;
  124. if (cef->init_browser()) {
  125. InitPanelCookieManager();
  126. return;
  127. }
  128. ExecThreadedWithoutBlocking([] { cef->wait_for_browser_init(); },
  129. QTStr("BrowserPanelInit.Title"),
  130. QTStr("BrowserPanelInit.Text"));
  131. InitPanelCookieManager();
  132. #endif
  133. }