ios_oauth_token_getter.mm 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2017 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. #if !defined(__has_feature) || !__has_feature(objc_arc)
  5. #error "This file requires ARC support."
  6. #endif
  7. #include "remoting/ios/facade/ios_oauth_token_getter.h"
  8. #include "base/strings/sys_string_conversions.h"
  9. #include "remoting/ios/facade/remoting_authentication.h"
  10. #include "remoting/ios/facade/remoting_service.h"
  11. namespace remoting {
  12. IosOauthTokenGetter::IosOauthTokenGetter() : weak_factory_(this) {
  13. weak_ptr_ = weak_factory_.GetWeakPtr();
  14. }
  15. IosOauthTokenGetter::~IosOauthTokenGetter() {}
  16. void IosOauthTokenGetter::CallWithToken(TokenCallback on_access_token) {
  17. __block TokenCallback block_callback = std::move(on_access_token);
  18. [RemotingService.instance.authentication
  19. callbackWithAccessToken:^(RemotingAuthenticationStatus status,
  20. NSString* userEmail, NSString* accessToken) {
  21. Status oauth_status;
  22. switch (status) {
  23. case RemotingAuthenticationStatusSuccess:
  24. oauth_status = Status::SUCCESS;
  25. break;
  26. case RemotingAuthenticationStatusAuthError:
  27. oauth_status = Status::AUTH_ERROR;
  28. break;
  29. case RemotingAuthenticationStatusNetworkError:
  30. oauth_status = Status::NETWORK_ERROR;
  31. break;
  32. default:
  33. NOTREACHED();
  34. }
  35. std::move(block_callback)
  36. .Run(oauth_status, base::SysNSStringToUTF8(userEmail),
  37. base::SysNSStringToUTF8(accessToken));
  38. }];
  39. }
  40. void IosOauthTokenGetter::InvalidateCache() {
  41. [RemotingService.instance.authentication invalidateCache];
  42. }
  43. base::WeakPtr<IosOauthTokenGetter> IosOauthTokenGetter::GetWeakPtr() {
  44. return weak_ptr_;
  45. }
  46. } // namespace remoting