OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity_service.
h" |
| 6 |
| 7 #import <Foundation/Foundation.h> |
| 8 |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 11 #import "ios/public/provider/chrome/browser/signin/fake_chrome_identity.h" |
| 12 |
| 13 namespace ios { |
| 14 |
| 15 NSString* const kIdentityEmailFormat = @"%@@foo.com"; |
| 16 NSString* const kIdentityGaiaIDFormat = @"%@ID"; |
| 17 |
| 18 FakeChromeIdentityService::FakeChromeIdentityService() |
| 19 : identities_([[NSMutableArray alloc] init]) {} |
| 20 |
| 21 FakeChromeIdentityService::~FakeChromeIdentityService() {} |
| 22 |
| 23 // static |
| 24 FakeChromeIdentityService* |
| 25 FakeChromeIdentityService::GetInstanceFromChromeProvider() { |
| 26 return static_cast<ios::FakeChromeIdentityService*>( |
| 27 ios::GetChromeBrowserProvider()->GetChromeIdentityService()); |
| 28 } |
| 29 |
| 30 bool FakeChromeIdentityService::IsValidIdentity( |
| 31 ChromeIdentity* identity) const { |
| 32 return [identities_ indexOfObject:identity] != NSNotFound; |
| 33 } |
| 34 |
| 35 ChromeIdentity* FakeChromeIdentityService::GetIdentityWithGaiaID( |
| 36 const std::string& gaia_id) const { |
| 37 NSString* gaiaID = base::SysUTF8ToNSString(gaia_id); |
| 38 NSUInteger index = |
| 39 [identities_ indexOfObjectPassingTest:^BOOL(ChromeIdentity* obj, |
| 40 NSUInteger, BOOL* stop) { |
| 41 return [[obj gaiaID] isEqualToString:gaiaID]; |
| 42 }]; |
| 43 if (index == NSNotFound) { |
| 44 return nil; |
| 45 } |
| 46 return [identities_ objectAtIndex:index]; |
| 47 } |
| 48 |
| 49 bool FakeChromeIdentityService::HasIdentities() const { |
| 50 return [identities_ count] > 0; |
| 51 } |
| 52 |
| 53 NSArray* FakeChromeIdentityService::GetAllIdentities() const { |
| 54 return identities_; |
| 55 } |
| 56 |
| 57 NSArray* FakeChromeIdentityService::GetAllIdentitiesSortedForDisplay() const { |
| 58 return identities_; |
| 59 } |
| 60 |
| 61 void FakeChromeIdentityService::ForgetIdentity( |
| 62 ChromeIdentity* identity, |
| 63 ForgetIdentityCallback callback) { |
| 64 [identities_ removeObject:identity]; |
| 65 FireIdentityListChanged(); |
| 66 if (callback) { |
| 67 // Forgetting an identity is normally an asynchronous operation (that |
| 68 // require some network calls), this is replicated here by dispatching it. |
| 69 dispatch_async(dispatch_get_main_queue(), ^{ |
| 70 callback(nil); |
| 71 }); |
| 72 } |
| 73 } |
| 74 |
| 75 void FakeChromeIdentityService::AddIdentities(NSArray* identitiesNames) { |
| 76 for (NSString* name in identitiesNames) { |
| 77 NSString* email = [NSString stringWithFormat:kIdentityEmailFormat, name]; |
| 78 NSString* gaiaID = [NSString stringWithFormat:kIdentityGaiaIDFormat, name]; |
| 79 [identities_ addObject:[FakeChromeIdentity identityWithEmail:email |
| 80 gaiaID:gaiaID |
| 81 name:name]]; |
| 82 } |
| 83 } |
| 84 |
| 85 void FakeChromeIdentityService::AddIdentity(ChromeIdentity* identity) { |
| 86 [identities_ addObject:identity]; |
| 87 FireIdentityListChanged(); |
| 88 } |
| 89 |
| 90 } // namespace ios |
OLD | NEW |