window-basic-main-browser.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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", 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(),
  54. "Panels", "CookieId");
  55. std::string sub_path;
  56. sub_path += "obs_profile_cookies/";
  57. sub_path += cookie_id;
  58. panel_cookies = cef->create_cookie_manager(sub_path);
  59. }
  60. #endif
  61. void DestroyPanelCookieManager()
  62. {
  63. #ifdef BROWSER_AVAILABLE
  64. if (panel_cookies) {
  65. panel_cookies->FlushStore();
  66. delete panel_cookies;
  67. panel_cookies = nullptr;
  68. }
  69. #endif
  70. }
  71. void DeleteCookies()
  72. {
  73. #ifdef BROWSER_AVAILABLE
  74. if (panel_cookies) {
  75. panel_cookies->DeleteCookies("", "");
  76. }
  77. #endif
  78. }
  79. void DuplicateCurrentCookieProfile(ConfigFile &config)
  80. {
  81. #ifdef BROWSER_AVAILABLE
  82. if (cef) {
  83. OBSBasic *main = OBSBasic::Get();
  84. std::string cookie_id = config_get_string(main->Config(),
  85. "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",
  110. cookie_id.c_str());
  111. config_set_string(main->Config(), "Panels", "CookieId",
  112. new_id.c_str());
  113. }
  114. #else
  115. UNUSED_PARAMETER(config);
  116. #endif
  117. }
  118. void OBSBasic::InitBrowserPanelSafeBlock()
  119. {
  120. #ifdef BROWSER_AVAILABLE
  121. if (!cef)
  122. return;
  123. if (cef->init_browser()) {
  124. InitPanelCookieManager();
  125. return;
  126. }
  127. ExecThreadedWithoutBlocking(
  128. [] {cef->wait_for_browser_init();},
  129. QTStr("BrowserPanelInit.Title"),
  130. QTStr("BrowserPanelInit.Text"));
  131. InitPanelCookieManager();
  132. #else
  133. UNUSED_PARAMETER(showDialog);
  134. #endif
  135. }