cookie_notification_bridge.mm 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/web/net/cookie_notification_bridge.h"
  5. #import <Foundation/Foundation.h>
  6. #include "base/bind.h"
  7. #include "base/location.h"
  8. #import "ios/net/cookies/cookie_store_ios.h"
  9. #include "ios/web/public/thread/web_task_traits.h"
  10. #include "ios/web/public/thread/web_thread.h"
  11. #if !defined(__has_feature) || !__has_feature(objc_arc)
  12. #error "This file requires ARC support."
  13. #endif
  14. namespace web {
  15. CookieNotificationBridge::CookieNotificationBridge() {
  16. id<NSObject> registration = [[NSNotificationCenter defaultCenter]
  17. addObserverForName:NSHTTPCookieManagerCookiesChangedNotification
  18. object:[NSHTTPCookieStorage sharedHTTPCookieStorage]
  19. queue:nil
  20. usingBlock:^(NSNotification* notification) {
  21. OnNotificationReceived(notification);
  22. }];
  23. registration_ = registration;
  24. }
  25. CookieNotificationBridge::~CookieNotificationBridge() {
  26. [[NSNotificationCenter defaultCenter] removeObserver:registration_];
  27. }
  28. void CookieNotificationBridge::OnNotificationReceived(
  29. NSNotification* notification) {
  30. DCHECK([[notification name]
  31. isEqualToString:NSHTTPCookieManagerCookiesChangedNotification]);
  32. web::GetIOThreadTaskRunner({})->PostTask(
  33. FROM_HERE,
  34. base::BindOnce(&net::CookieStoreIOS::NotifySystemCookiesChanged));
  35. }
  36. } // namespace web