cookie_change_notifier.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2018 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/browser/cookie_change_notifier.h"
  5. #include "base/functional/bind.h"
  6. #include "content/public/browser/browser_thread.h"
  7. #include "content/public/browser/storage_partition.h"
  8. #include "net/cookies/cookie_change_dispatcher.h"
  9. #include "shell/browser/electron_browser_context.h"
  10. using content::BrowserThread;
  11. namespace electron {
  12. CookieChangeNotifier::CookieChangeNotifier(
  13. ElectronBrowserContext* browser_context)
  14. : browser_context_(browser_context), receiver_(this) {
  15. StartListening();
  16. }
  17. CookieChangeNotifier::~CookieChangeNotifier() = default;
  18. base::CallbackListSubscription
  19. CookieChangeNotifier::RegisterCookieChangeCallback(
  20. const base::RepeatingCallback<void(const net::CookieChangeInfo& change)>&
  21. cb) {
  22. DCHECK_CURRENTLY_ON(BrowserThread::UI);
  23. return cookie_change_sub_list_.Add(cb);
  24. }
  25. void CookieChangeNotifier::StartListening() {
  26. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  27. DCHECK(!receiver_.is_bound());
  28. network::mojom::CookieManager* cookie_manager =
  29. browser_context_->GetDefaultStoragePartition()
  30. ->GetCookieManagerForBrowserProcess();
  31. // Cookie manager should be created whenever network context is created,
  32. // if this fails then there is something wrong with our context creation
  33. // cycle.
  34. CHECK(cookie_manager);
  35. cookie_manager->AddGlobalChangeListener(receiver_.BindNewPipeAndPassRemote());
  36. receiver_.set_disconnect_handler(base::BindOnce(
  37. &CookieChangeNotifier::OnConnectionError, base::Unretained(this)));
  38. }
  39. void CookieChangeNotifier::OnConnectionError() {
  40. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  41. receiver_.reset();
  42. StartListening();
  43. }
  44. void CookieChangeNotifier::OnCookieChange(const net::CookieChangeInfo& change) {
  45. DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  46. cookie_change_sub_list_.Notify(change);
  47. }
  48. } // namespace electron