Commit 03bc3897 authored by Jérôme Lebel's avatar Jérôme Lebel Committed by Commit Bot

[IdentityManager] Adding observer bridge for Objective-C

Adding observer bridge for IdentityManager to Objective-C class.

Change-Id: Id17e730f968e353dc686be9b32af4c886fa9b64f
Reviewed-on: https://chromium-review.googlesource.com/1203934
Commit-Queue: Jérôme Lebel <jlebel@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#588813}
parent 1b8f9b7e
# Copyright 2018 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.
source_set("objc") {
configs += [ "//build/config/compiler:enable_arc" ]
sources = [
"identity_manager_observer_bridge.h",
"identity_manager_observer_bridge.mm",
]
public_deps = [
"//services/identity/public/cpp",
]
}
// Copyright 2018 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.
#ifndef SERVICES_IDENTITY_PUBLIC_OBJC_IDENTITY_MANAGER_OBSERVER_BRIDGE_H_
#define SERVICES_IDENTITY_PUBLIC_OBJC_IDENTITY_MANAGER_OBSERVER_BRIDGE_H_
#import <Foundation/Foundation.h>
#include <vector>
#include "services/identity/public/cpp/identity_manager.h"
// Implement this protocol and pass your implementation into an
// IdentityManagerObserverBridge object to receive IdentityManager observer
// callbacks in Objective-C.
@protocol IdentityManagerObserverBridgeDelegate<NSObject>
@optional
// These callbacks follow the semantics of the corresponding
// IdentityManager::Observer callbacks. See the comments on
// IdentityManager::Observer in identity_manager.h for the specification of
// these semantics.
- (void)onPrimaryAccountSet:(const AccountInfo&)primaryAccountInfo;
- (void)onPrimaryAccountCleared:(const AccountInfo&)previousPrimaryAccountInfo;
- (void)onRefreshTokenUpdatedForAccount:(const AccountInfo&)accountInfo
valid:(BOOL)isValid;
- (void)onRefreshTokenRemovedForAccount:(const AccountInfo&)accountInfo;
- (void)onAccountsInCookieUpdated:(const std::vector<AccountInfo>&)accounts;
@end
namespace identity {
// Bridge class that listens for |IdentityManager| notifications and
// passes them to its Objective-C delegate.
class IdentityManagerObserverBridge : public IdentityManager::Observer {
public:
IdentityManagerObserverBridge(
IdentityManager* identity_manager,
id<IdentityManagerObserverBridgeDelegate> delegate);
~IdentityManagerObserverBridge() override;
// IdentityManager::Observer.
void OnPrimaryAccountSet(const AccountInfo& primary_account_info) override;
void OnPrimaryAccountCleared(
const AccountInfo& previous_primary_account_info) override;
void OnRefreshTokenUpdatedForAccount(const AccountInfo& account_info,
bool is_valid) override;
void OnRefreshTokenRemovedForAccount(
const AccountInfo& account_info) override;
void OnAccountsInCookieUpdated(
const std::vector<AccountInfo>& accounts) override;
private:
// Identity manager to observe.
IdentityManager* identity_manager_;
// Delegate to call.
__weak id<IdentityManagerObserverBridgeDelegate> delegate_;
DISALLOW_COPY_AND_ASSIGN(IdentityManagerObserverBridge);
};
} // namespace identity
#endif // SERVICES_IDENTITY_PUBLIC_OBJC_IDENTITY_MANAGER_OBSERVER_BRIDGE_H_
// Copyright 2018 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.
#import "services/identity/public/objc/identity_manager_observer_bridge.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace identity {
IdentityManagerObserverBridge::IdentityManagerObserverBridge(
IdentityManager* identity_manager,
id<IdentityManagerObserverBridgeDelegate> delegate)
: identity_manager_(identity_manager), delegate_(delegate) {
identity_manager_->AddObserver(this);
}
IdentityManagerObserverBridge::~IdentityManagerObserverBridge() {
identity_manager_->RemoveObserver(this);
}
void IdentityManagerObserverBridge::OnPrimaryAccountSet(
const AccountInfo& primary_account_info) {
if ([delegate_ respondsToSelector:@selector(onPrimaryAccountSet:)]) {
[delegate_ onPrimaryAccountSet:primary_account_info];
}
}
void IdentityManagerObserverBridge::OnPrimaryAccountCleared(
const AccountInfo& previous_primary_account_info) {
if ([delegate_ respondsToSelector:@selector(onPrimaryAccountCleared:)]) {
[delegate_ onPrimaryAccountCleared:previous_primary_account_info];
}
}
void IdentityManagerObserverBridge::OnRefreshTokenUpdatedForAccount(
const AccountInfo& account_info,
bool is_valid) {
if ([delegate_ respondsToSelector:@selector
(onRefreshTokenUpdatedForAccount:valid:)]) {
[delegate_ onRefreshTokenUpdatedForAccount:account_info valid:is_valid];
}
}
void IdentityManagerObserverBridge::OnRefreshTokenRemovedForAccount(
const AccountInfo& account_info) {
if ([delegate_
respondsToSelector:@selector(onRefreshTokenRemovedForAccount:)]) {
[delegate_ onRefreshTokenRemovedForAccount:account_info];
}
}
void IdentityManagerObserverBridge::OnAccountsInCookieUpdated(
const std::vector<AccountInfo>& accounts) {
if ([delegate_ respondsToSelector:@selector(onAccountsInCookieUpdated:)]) {
[delegate_ onAccountsInCookieUpdated:accounts];
}
}
} // namespace identity
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment