remoting_menu_view_controller.mm 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #import "remoting/ios/app/remoting_menu_view_controller.h"
  5. #import <MaterialComponents/MaterialAppBar.h>
  6. #import <MaterialComponents/MaterialButtons.h>
  7. #include "base/strings/escape.h"
  8. #include "base/strings/stringprintf.h"
  9. #include "base/strings/sys_string_conversions.h"
  10. #include "google_apis/google_api_keys.h"
  11. #include "remoting/base/string_resources.h"
  12. #import "remoting/ios/app/remoting_theme.h"
  13. #import "remoting/ios/app/side_menu_items.h"
  14. #import "remoting/ios/facade/remoting_authentication.h"
  15. #import "remoting/ios/facade/remoting_service.h"
  16. #include "ui/base/l10n/l10n_util.h"
  17. #if !defined(__has_feature) || !__has_feature(objc_arc)
  18. #error "This file requires ARC support."
  19. #endif
  20. // TODO(nicholss): This should be generated from a remoting/base class:
  21. static NSString* const kReusableIdentifierItem =
  22. @"remotingMenuViewControllerItem";
  23. static UIColor* kBackgroundColor =
  24. [UIColor colorWithRed:0.f green:0.67f blue:0.55f alpha:1.f];
  25. namespace {
  26. // To use these scopes in a debug build, your development account will need to
  27. // be allowlisted.
  28. const char kChromotingAuthScopeValues[] =
  29. "https://www.googleapis.com/auth/chromoting.directory "
  30. "https://www.googleapis.com/auth/tachyon "
  31. "https://www.googleapis.com/auth/userinfo.email";
  32. std::string GetAuthorizationCodeUri() {
  33. // Replace space characters with a '+' sign when formatting.
  34. bool use_plus = true;
  35. return base::StringPrintf(
  36. "https://accounts.google.com/o/oauth2/auth"
  37. "?scope=%s"
  38. "&redirect_uri=https://remotedesktop.google.com/_/oauthredirect"
  39. "&response_type=code"
  40. "&client_id=%s"
  41. "&access_type=offline"
  42. "&approval_prompt=force",
  43. base::EscapeUrlEncodedData(kChromotingAuthScopeValues, use_plus).c_str(),
  44. base::EscapeUrlEncodedData(
  45. google_apis::GetOAuth2ClientID(google_apis::CLIENT_REMOTING),
  46. use_plus)
  47. .c_str());
  48. }
  49. } // namespace
  50. @interface RemotingMenuViewController () {
  51. MDCAppBarViewController* _appBarViewController;
  52. NSArray<NSArray<SideMenuItem*>*>* _content;
  53. }
  54. @end
  55. // This is the chromium version of the menu view controller. This will
  56. // launch a web view to login and collect an oauth token to be able to login to
  57. // the app without the standard google login flow. It also loads and shows all
  58. // other menu items from SideMenuItemsProvider.
  59. //
  60. // The official app's implementation is in //ios_internal.
  61. //
  62. // Note: this class is not localized, it will not be shipped to production.
  63. @implementation RemotingMenuViewController
  64. - (id)init {
  65. self = [super init];
  66. if (self) {
  67. self.title = l10n_util::GetNSString(IDS_SETTINGS_BUTTON);
  68. _appBarViewController = [[MDCAppBarViewController alloc] init];
  69. [self addChildViewController:_appBarViewController];
  70. _appBarViewController.headerView.backgroundColor = kBackgroundColor;
  71. _appBarViewController.navigationBar.tintColor = [UIColor whiteColor];
  72. _appBarViewController.navigationBar.titleTextAttributes =
  73. @{NSForegroundColorAttributeName : [UIColor whiteColor]};
  74. }
  75. return self;
  76. }
  77. #pragma mark - UIViewController
  78. - (void)viewDidLoad {
  79. [super viewDidLoad];
  80. _appBarViewController.headerView.trackingScrollView = self.collectionView;
  81. [self.view addSubview:_appBarViewController.view];
  82. [_appBarViewController didMoveToParentViewController:self];
  83. UIBarButtonItem* backButton =
  84. [[UIBarButtonItem alloc] initWithImage:RemotingTheme.backIcon
  85. style:UIBarButtonItemStyleDone
  86. target:self
  87. action:@selector(didTapBack:)];
  88. self.navigationItem.leftBarButtonItem = backButton;
  89. self.navigationItem.rightBarButtonItem = nil;
  90. [self.collectionView registerClass:[MDCCollectionViewTextCell class]
  91. forCellWithReuseIdentifier:kReusableIdentifierItem];
  92. [self.collectionView registerClass:[MDCCollectionViewTextCell class]
  93. forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
  94. withReuseIdentifier:UICollectionElementKindSectionHeader];
  95. self.styler.cellStyle = MDCCollectionViewCellStyleCard;
  96. // Add the account management section to the beginning of the content.
  97. __weak __typeof(self) weakSelf = self;
  98. NSArray<SideMenuItem*>* accountManagementSection = @[
  99. [[SideMenuItem alloc]
  100. initWithTitle:l10n_util::GetNSString(IDS_SIGN_IN_BUTTON)
  101. icon:nil
  102. action:^{
  103. [weakSelf didTapGetAccessCode];
  104. }],
  105. [[SideMenuItem alloc]
  106. initWithTitle:l10n_util::GetNSString(IDS_SIGN_OUT_BUTTON)
  107. icon:nil
  108. action:^{
  109. [weakSelf didTapLogout];
  110. }]
  111. ];
  112. NSMutableArray<NSArray<SideMenuItem*>*>* mutableContent =
  113. [NSMutableArray arrayWithArray:SideMenuItemsProvider.sideMenuItems];
  114. [mutableContent insertObject:accountManagementSection atIndex:0];
  115. _content = mutableContent;
  116. }
  117. #pragma mark - UICollectionViewDataSource
  118. - (NSInteger)numberOfSectionsInCollectionView:
  119. (UICollectionView*)collectionView {
  120. return _content.count;
  121. }
  122. - (NSInteger)collectionView:(UICollectionView*)collectionView
  123. numberOfItemsInSection:(NSInteger)section {
  124. return _content[section].count;
  125. }
  126. - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
  127. cellForItemAtIndexPath:(NSIndexPath*)indexPath {
  128. MDCCollectionViewTextCell* cell = [collectionView
  129. dequeueReusableCellWithReuseIdentifier:kReusableIdentifierItem
  130. forIndexPath:indexPath];
  131. SideMenuItem* item = _content[indexPath.section][indexPath.item];
  132. cell.textLabel.text = item.title;
  133. cell.imageView.image = item.icon;
  134. return cell;
  135. }
  136. - (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView
  137. viewForSupplementaryElementOfKind:(NSString*)kind
  138. atIndexPath:(NSIndexPath*)indexPath {
  139. MDCCollectionViewTextCell* supplementaryView =
  140. [collectionView dequeueReusableSupplementaryViewOfKind:kind
  141. withReuseIdentifier:kind
  142. forIndexPath:indexPath];
  143. if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
  144. if (indexPath.section == 0) {
  145. supplementaryView.textLabel.text = @"Account";
  146. }
  147. supplementaryView.textLabel.textColor = kBackgroundColor;
  148. }
  149. return supplementaryView;
  150. }
  151. #pragma mark - UICollectionViewDelegate
  152. - (void)collectionView:(UICollectionView*)collectionView
  153. didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
  154. [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
  155. _content[indexPath.section][indexPath.item].action();
  156. }
  157. #pragma mark - <UICollectionViewDelegateFlowLayout>
  158. - (CGSize)collectionView:(UICollectionView*)collectionView
  159. layout:
  160. (UICollectionViewLayout*)collectionViewLayout
  161. referenceSizeForHeaderInSection:(NSInteger)section {
  162. // Only show the title if it's the account management section.
  163. if (section != 0) {
  164. return CGSizeZero;
  165. }
  166. return CGSizeMake(collectionView.bounds.size.width,
  167. MDCCellDefaultOneLineHeight);
  168. }
  169. #pragma mark - Private
  170. - (void)didTapBack:(id)button {
  171. [self dismissViewControllerAnimated:YES completion:nil];
  172. }
  173. - (void)didTapGetAccessCode {
  174. NSString* authUri = base::SysUTF8ToNSString(GetAuthorizationCodeUri());
  175. if (@available(iOS 10, *)) {
  176. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:authUri]
  177. options:@{}
  178. completionHandler:nil];
  179. }
  180. #if !defined(__IPHONE_10_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0
  181. else {
  182. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:authUri]];
  183. }
  184. #endif
  185. }
  186. - (void)didTapLogout {
  187. [RemotingService.instance.authentication logout];
  188. }
  189. @end