summaryrefslogtreecommitdiffstats
path: root/ios/web/net/cookie_notification_bridge.mm
blob: 8ebb9a7bcf78e69c4def4ff78ccb1673292bbcc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ios/web/net/cookie_notification_bridge.h"

#import <Foundation/Foundation.h>

#include "base/bind.h"
#include "base/location.h"
#include "ios/net/cookies/cookie_store_ios.h"
#include "ios/web/public/web_thread.h"

namespace web {

CookieNotificationBridge::CookieNotificationBridge() {
  observer_.reset([[NSNotificationCenter defaultCenter]
      addObserverForName:NSHTTPCookieManagerCookiesChangedNotification
                  object:[NSHTTPCookieStorage sharedHTTPCookieStorage]
                   queue:nil
              usingBlock:^(NSNotification* notification) {
                  OnNotificationReceived(notification);
              }]);
}

CookieNotificationBridge::~CookieNotificationBridge() {
  [[NSNotificationCenter defaultCenter] removeObserver:observer_];
}

void CookieNotificationBridge::OnNotificationReceived(
    NSNotification* notification) {
  DCHECK(thread_checker_.CalledOnValidThread());
  DCHECK([[notification name]
      isEqualToString:NSHTTPCookieManagerCookiesChangedNotification]);
  web::WebThread::PostTask(
      web::WebThread::IO, FROM_HERE,
      base::Bind(&net::CookieStoreIOS::NotifySystemCookiesChanged));
}

}  // namespace web