2018-10-04 18:08:56 +00:00
|
|
|
// Copyright (c) 2018 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "shell/browser/cookie_change_notifier.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
#include "content/public/browser/storage_partition.h"
|
|
|
|
#include "net/cookies/canonical_cookie.h"
|
2020-02-04 20:19:40 +00:00
|
|
|
#include "shell/browser/electron_browser_context.h"
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
using content::BrowserThread;
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2018-10-04 18:08:56 +00:00
|
|
|
|
2020-02-04 20:19:40 +00:00
|
|
|
CookieChangeNotifier::CookieChangeNotifier(
|
|
|
|
ElectronBrowserContext* browser_context)
|
2019-08-24 01:14:23 +00:00
|
|
|
: browser_context_(browser_context), receiver_(this) {
|
2018-10-04 18:08:56 +00:00
|
|
|
StartListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
CookieChangeNotifier::~CookieChangeNotifier() = default;
|
|
|
|
|
2020-12-14 18:57:36 +00:00
|
|
|
base::CallbackListSubscription
|
2018-10-04 18:08:56 +00:00
|
|
|
CookieChangeNotifier::RegisterCookieChangeCallback(
|
2019-10-28 22:12:35 +00:00
|
|
|
const base::Callback<void(const net::CookieChangeInfo& change)>& cb) {
|
2018-10-04 18:08:56 +00:00
|
|
|
DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
|
|
|
|
|
|
|
return cookie_change_sub_list_.Add(cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieChangeNotifier::StartListening() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
2019-08-24 01:14:23 +00:00
|
|
|
DCHECK(!receiver_.is_bound());
|
2018-10-04 18:08:56 +00:00
|
|
|
|
|
|
|
network::mojom::CookieManager* cookie_manager =
|
2021-05-04 03:13:46 +00:00
|
|
|
browser_context_->GetDefaultStoragePartition()
|
2018-10-04 18:08:56 +00:00
|
|
|
->GetCookieManagerForBrowserProcess();
|
2019-08-24 01:14:23 +00:00
|
|
|
|
2018-10-04 18:08:56 +00:00
|
|
|
// Cookie manager should be created whenever network context is created,
|
|
|
|
// if this fails then there is something wrong with our context creation
|
|
|
|
// cycle.
|
|
|
|
CHECK(cookie_manager);
|
|
|
|
|
2019-08-24 01:14:23 +00:00
|
|
|
cookie_manager->AddGlobalChangeListener(receiver_.BindNewPipeAndPassRemote());
|
|
|
|
receiver_.set_disconnect_handler(base::BindOnce(
|
2018-10-04 18:08:56 +00:00
|
|
|
&CookieChangeNotifier::OnConnectionError, base::Unretained(this)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void CookieChangeNotifier::OnConnectionError() {
|
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
|
2019-08-24 01:14:23 +00:00
|
|
|
receiver_.reset();
|
2018-10-04 18:08:56 +00:00
|
|
|
StartListening();
|
|
|
|
}
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
void CookieChangeNotifier::OnCookieChange(const net::CookieChangeInfo& change) {
|
2018-10-04 18:08:56 +00:00
|
|
|
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
|
|
|
|
|
2019-10-28 22:12:35 +00:00
|
|
|
cookie_change_sub_list_.Notify(change);
|
2018-10-04 18:08:56 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|