Skip to content

Commit

Permalink
UI: Use shared cookie manager for YT Control Panel
Browse files Browse the repository at this point in the history
Use the cookie manager shared by service integration browser docks for
YouTube Control Panel.

This will enable users of the YouTube Chat panel have a better (creator
facing) experience for observed chat message latency, for those users
who sign-in to the YouTube Control Panel or YouTube Live Chat.

NOTE: This commit does not have any migration logic for existing
logged-in users of YTCP, they will need to sign-in again. Based on usage
stats, this is not going to affect a large fraction of OBS users.
  • Loading branch information
msuman-google committed Sep 16, 2024
1 parent 61d74fb commit 335b102
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
19 changes: 18 additions & 1 deletion UI/auth-youtube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ static inline void OpenBrowser(const QString auth_uri)
QDesktopServices::openUrl(url);
}

static void DeleteCookies()
{
if (panel_cookies) {
panel_cookies->DeleteCookies("youtube.com", "");
panel_cookies->DeleteCookies("google.com", "");
}
}

void RegisterYoutubeAuth()
{
for (auto &service : youtubeServices) {
Expand All @@ -64,7 +72,7 @@ void RegisterYoutubeAuth()
return std::make_shared<YoutubeApiWrappers>(
service);
},
YoutubeAuth::Login, []() { return; });
YoutubeAuth::Login, DeleteCookies);
}
}

Expand Down Expand Up @@ -216,6 +224,15 @@ void YoutubeAuth::ResetChat()
#endif
}

void YoutubeAuth::ReloadChat()
{
#ifdef BROWSER_AVAILABLE
if (chat && chat->cefWidget) {
chat->cefWidget->reloadPage();
}
#endif
}

QString YoutubeAuth::GenerateState()
{
char state[YOUTUBE_API_STATE_LENGTH + 1];
Expand Down
1 change: 1 addition & 0 deletions UI/auth-youtube.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class YoutubeAuth : public OAuthStreamKey {

void SetChatId(const QString &chat_id, const std::string &api_chat_id);
void ResetChat();
void ReloadChat();

static std::shared_ptr<Auth> Login(QWidget *parent,
const std::string &service);
Expand Down
41 changes: 21 additions & 20 deletions UI/window-dock-youtube-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,13 @@ static constexpr const char *INGESTION_STOPPED = "INGESTION_STOPPED";

YouTubeAppDock::YouTubeAppDock(const QString &title)
: BrowserDock(title),
dockBrowser(nullptr),
cookieManager(nullptr)
dockBrowser(nullptr)
{
cef->init_browser();
OBSBasic::InitBrowserPanelSafeBlock();
AddYouTubeAppDock();
}

YouTubeAppDock::~YouTubeAppDock()
{
if (cookieManager) {
cookieManager->FlushStore();
delete cookieManager;
}
}

bool YouTubeAppDock::IsYTServiceSelected()
{
if (!cef_js_avail)
Expand Down Expand Up @@ -78,9 +69,12 @@ void YouTubeAppDock::SettingsUpdated(bool cleanup)

// definitely cleanup if YT switched off
if (!ytservice || cleanup) {
if (cookieManager)
cookieManager->DeleteCookies("", "");
if (panel_cookies) {
panel_cookies->DeleteCookies("youtube.com", "");
panel_cookies->DeleteCookies("google.com", "");
}
}

if (ytservice)
Update();
}
Expand Down Expand Up @@ -135,23 +129,20 @@ void YouTubeAppDock::AddYouTubeAppDock()

void YouTubeAppDock::CreateBrowserWidget(const std::string &url)
{
std::string dir_name = std::string("obs_profile_cookies_youtube/") +
config_get_string(OBSBasic::Get()->Config(),
"Panels", "CookieId");
if (cookieManager)
delete cookieManager;
cookieManager = cef->create_cookie_manager(dir_name, true);

if (dockBrowser)
delete dockBrowser;
dockBrowser = cef->create_widget(this, url, cookieManager);
dockBrowser = cef->create_widget(this, url, panel_cookies);
if (!dockBrowser)
return;

if (obs_browser_qcef_version() >= 1)
dockBrowser->allowAllPopups(true);

this->SetWidget(dockBrowser);

QWidget::connect(dockBrowser.get(), &QCefWidget::urlChanged, this,
&YouTubeAppDock::ReloadChatDock);

Update();
}

Expand Down Expand Up @@ -348,6 +339,16 @@ void YouTubeAppDock::UpdateChannelId()
}
}

void YouTubeAppDock::ReloadChatDock()
{
if (IsUserSignedIntoYT()) {
YoutubeApiWrappers *apiYouTube = GetYTApi();
if (apiYouTube) {
apiYouTube->ReloadChat();
}
}
}

void YouTubeAppDock::SetInitEvent(streaming_mode_t mode, const char *event,
const char *video_id, const char *channelId)
{
Expand Down
3 changes: 1 addition & 2 deletions UI/window-dock-youtube-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class YouTubeAppDock : public BrowserDock {

public:
YouTubeAppDock(const QString &title);
~YouTubeAppDock();

enum streaming_mode_t { YTSM_ACCOUNT, YTSM_STREAM_KEY };

Expand Down Expand Up @@ -43,11 +42,11 @@ class YouTubeAppDock : public BrowserDock {
void DispatchYTEvent(const char *event, const char *video_id,
streaming_mode_t mode);
void UpdateChannelId();
void ReloadChatDock();
void SetInitEvent(streaming_mode_t mode, const char *event = nullptr,
const char *video_id = nullptr,
const char *channelId = nullptr);

QString channelId;
QPointer<QCefWidget> dockBrowser;
QCefCookieManager *cookieManager; // is not a Qt object
};

0 comments on commit 335b102

Please sign in to comment.