1
0

window-basic-main-browser.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 DuplicateCurrentCookieProfile(ConfigFile &config)
  72. {
  73. #ifdef BROWSER_AVAILABLE
  74. if (cef) {
  75. OBSBasic *main = OBSBasic::Get();
  76. const char *cookie_id = config_get_string(main->Config(),
  77. "Panels", "CookieId");
  78. std::string src_path;
  79. src_path += "obs_profile_cookies/";
  80. src_path += cookie_id;
  81. std::string new_id = GenId();
  82. std::string dst_path;
  83. dst_path += "obs_profile_cookies/";
  84. dst_path += new_id;
  85. BPtr<char> src_path_full = cef->get_cookie_path(src_path);
  86. BPtr<char> dst_path_full = cef->get_cookie_path(dst_path);
  87. QDir srcDir(src_path_full.Get());
  88. QDir dstDir(dst_path_full.Get());
  89. if (srcDir.exists()) {
  90. if (!dstDir.exists())
  91. dstDir.mkdir(dst_path_full.Get());
  92. QStringList files = srcDir.entryList(QDir::Files);
  93. for (const QString &file : files) {
  94. QString src = QString(src_path_full);
  95. QString dst = QString(dst_path_full);
  96. src += QDir::separator() + file;
  97. dst += QDir::separator() + file;
  98. QFile::copy(src, dst);
  99. }
  100. }
  101. config_set_string(config, "Panels", "CookieId", new_id.c_str());
  102. }
  103. #else
  104. UNUSED_PARAMETER(config);
  105. #endif
  106. }
  107. void OBSBasic::InitBrowserPanelSafeBlock(bool showDialog)
  108. {
  109. #ifdef BROWSER_AVAILABLE
  110. if (!cef)
  111. return;
  112. if (cef->init_browser()) {
  113. InitPanelCookieManager();
  114. return;
  115. }
  116. if (showDialog)
  117. ExecuteFuncSafeBlockMsgBox(
  118. [] {cef->wait_for_browser_init();},
  119. QTStr("BrowserPanelInit.Title"),
  120. QTStr("BrowserPanelInit.Text"));
  121. else
  122. ExecuteFuncSafeBlock(
  123. [] {cef->wait_for_browser_init();});
  124. InitPanelCookieManager();
  125. #else
  126. UNUSED_PARAMETER(showDialog);
  127. #endif
  128. }