remoting_service.mm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #import "remoting/ios/facade/remoting_service.h"
  8. #import <Foundation/Foundation.h>
  9. #import <Security/Security.h>
  10. #import "remoting/ios/domain/user_info.h"
  11. #import "remoting/ios/facade/ios_client_runtime_delegate.h"
  12. #import "remoting/ios/facade/remoting_authentication.h"
  13. #include "base/check.h"
  14. #include "base/strings/sys_string_conversions.h"
  15. NSString* const kUserDidUpdate = @"kUserDidUpdate";
  16. NSString* const kUserInfo = @"kUserInfo";
  17. @interface RemotingService ()<RemotingAuthenticationDelegate> {
  18. id<RemotingAuthentication> _authentication;
  19. // TODO(yuweih): It's suspicious to use a raw C++ pointer here. Investigate
  20. // its lifetime in ChromotingRuntime and change to unique_ptr if possible.
  21. remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate;
  22. }
  23. @end
  24. @implementation RemotingService
  25. // RemotingService is a singleton.
  26. + (RemotingService*)instance {
  27. static RemotingService* sharedInstance = nil;
  28. static dispatch_once_t guard;
  29. dispatch_once(&guard, ^{
  30. sharedInstance = [[RemotingService alloc] init];
  31. });
  32. return sharedInstance;
  33. }
  34. - (instancetype)init {
  35. self = [super init];
  36. if (self) {
  37. _clientRuntimeDelegate =
  38. new remoting::IosClientRuntimeDelegate();
  39. [self runtime]->Init(_clientRuntimeDelegate);
  40. }
  41. return self;
  42. }
  43. #pragma mark - RemotingAuthenticationDelegate
  44. - (void)userDidUpdate:(UserInfo*)user {
  45. NSDictionary* userInfo =
  46. user ? [NSDictionary dictionaryWithObject:user forKey:kUserInfo] : nil;
  47. [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate
  48. object:self
  49. userInfo:userInfo];
  50. }
  51. #pragma mark - Properties
  52. - (remoting::ChromotingClientRuntime*)runtime {
  53. return remoting::ChromotingClientRuntime::GetInstance();
  54. }
  55. #pragma mark - Implementation
  56. - (void)setAuthentication:(id<RemotingAuthentication>)authentication {
  57. DCHECK(_authentication == nil);
  58. authentication.delegate = self;
  59. _authentication = authentication;
  60. }
  61. - (id<RemotingAuthentication>)authentication {
  62. DCHECK(_authentication != nil);
  63. return _authentication;
  64. }
  65. @end