[ios][mim] Add code to open selected profile in a new window

This CL implements switchProfileButtonWasTapped by:
 - Updating the kBrowserStateLastUsed key
 - Opening a new window (creating a new scene)
 - Updating code in startUpChromeUI (in SceneController) to get the
   browserState from ChromeBrowserStateManager. Note that there is no
   change compared to old code in SceneController because the AppState
   gets the browserState in the same way.

Note that this is not a final solution, but only a temporary solution
that will be helpful to test some features until we have the correct
APIs (and with minimal code changes outside of ../multi_identity/*).

Note that:
 - It will be possible to test this code only on
   iPad since we need multi-window support.
 - This code opens a new window with a new sceneState associated to
   a browserState different from the one in the first window. It
   is not linked to user logged-in (so we can't test login/logout etc)

Bug: 333520714
Change-Id: Ia6e51ec31392de237d8a56cd20eba6cf3d5a04ab
Low-Coverage-Reason: EXPERIMENTAL_CODE
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5641336
Reviewed-by: Mark Cogan <marq@chromium.org>
Commit-Queue: Federica Germinario <fedegermi@google.com>
Cr-Commit-Position: refs/heads/main@{#1318492}
diff --git a/ios/chrome/browser/shared/coordinator/scene/scene_controller.mm b/ios/chrome/browser/shared/coordinator/scene/scene_controller.mm
index e81d0df..25868d3 100644
--- a/ios/chrome/browser/shared/coordinator/scene/scene_controller.mm
+++ b/ios/chrome/browser/shared/coordinator/scene/scene_controller.mm
@@ -93,6 +93,7 @@
 #import "ios/chrome/browser/shared/model/browser/browser_list_factory.h"
 #import "ios/chrome/browser/shared/model/browser/browser_provider_interface.h"
 #import "ios/chrome/browser/shared/model/browser_state/chrome_browser_state.h"
+#import "ios/chrome/browser/shared/model/browser_state/chrome_browser_state_manager.h"
 #import "ios/chrome/browser/shared/model/prefs/pref_names.h"
 #import "ios/chrome/browser/shared/model/url/chrome_url_constants.h"
 #import "ios/chrome/browser/shared/model/url/url_util.h"
@@ -983,7 +984,10 @@
 
   // TODO(b/326184192): startUpChromeUI should use the browser state associated
   // to the scene.
-  ChromeBrowserState* browserState = sceneState.appState.mainBrowserState;
+  ChromeBrowserState* browserState =
+      GetApplicationContext()
+          ->GetChromeBrowserStateManager()
+          ->GetLastUsedBrowserStateDeprecatedDoNotUse();
   self.browserViewWrangler = [[BrowserViewWrangler alloc]
       initWithBrowserState:browserState
                 sceneState:sceneState
diff --git a/ios/chrome/browser/ui/settings/multi_identity/BUILD.gn b/ios/chrome/browser/ui/settings/multi_identity/BUILD.gn
index 3705682..876f4e9 100644
--- a/ios/chrome/browser/ui/settings/multi_identity/BUILD.gn
+++ b/ios/chrome/browser/ui/settings/multi_identity/BUILD.gn
@@ -6,18 +6,24 @@
   sources = [
     "switch_profile_settings_coordinator.h",
     "switch_profile_settings_coordinator.mm",
+    "switch_profile_settings_mediator.h",
+    "switch_profile_settings_mediator.mm",
   ]
   deps = [
     ":multi_identity_ui",
+    "//components/prefs",
     "//ios/chrome/browser/shared/coordinator/chrome_coordinator",
+    "//ios/chrome/browser/shared/coordinator/scene:scene_state_header",
     "//ios/chrome/browser/shared/model/application_context",
     "//ios/chrome/browser/shared/model/browser",
     "//ios/chrome/browser/shared/model/browser_state",
+    "//ios/chrome/browser/shared/model/prefs:pref_names",
   ]
 }
 
 source_set("multi_identity_ui") {
   sources = [
+    "switch_profile_settings_delegate.h",
     "switch_profile_settings_view_controller.h",
     "switch_profile_settings_view_controller.mm",
   ]
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_coordinator.mm b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_coordinator.mm
index 13e0ea5..0c74429e 100644
--- a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_coordinator.mm
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_coordinator.mm
@@ -7,6 +7,7 @@
 #import "base/strings/sys_string_conversions.h"
 #import "ios/chrome/browser/shared/model/browser/browser.h"
 #import "ios/chrome/browser/shared/model/browser_state/chrome_browser_state.h"
+#import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.h"
 #import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h"
 
 @implementation SwitchProfileSettingsCoordinator {
@@ -14,6 +15,8 @@
   SwitchProfileSettingsTableViewController* _viewController;
   // The ChromeBrowserState instance passed to the initializer.
   ChromeBrowserState* _browserState;
+  // Mediator for the switch profile settings.
+  SwitchProfileSettingsMediator* _mediator;
 }
 
 @synthesize baseNavigationController = _baseNavigationController;
@@ -30,9 +33,12 @@
 }
 
 - (void)start {
+  _mediator = [[SwitchProfileSettingsMediator alloc] init];
   _viewController = [[SwitchProfileSettingsTableViewController alloc] init];
+  _viewController.delegate = _mediator;
   _viewController.activeBrowserStateName =
       base::SysUTF8ToNSString(_browserState->GetDebugName());
+
   [self.baseNavigationController pushViewController:_viewController
                                            animated:YES];
 }
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_delegate.h b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_delegate.h
new file mode 100644
index 0000000..4c6f06a
--- /dev/null
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_delegate.h
@@ -0,0 +1,16 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_DELEGATE_H_
+#define IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_DELEGATE_H_
+
+// Delegate to handle Switch Profile Settings actions.
+@protocol SwitchProfileSettingsDelegate
+
+// Opens a new window with the selected profile.
+- (void)openProfileInNewWindow:(NSString*)browserStateName;
+
+@end
+
+#endif  // IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_DELEGATE_H_
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.h b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.h
new file mode 100644
index 0000000..274e17f
--- /dev/null
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.h
@@ -0,0 +1,18 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_MEDIATOR_H_
+#define IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_MEDIATOR_H_
+
+#import <UIKit/UIKit.h>
+
+#import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_delegate.h"
+
+// Mediator for the tabs settings.
+@interface SwitchProfileSettingsMediator
+    : NSObject <SwitchProfileSettingsDelegate>
+
+@end
+
+#endif  // IOS_CHROME_BROWSER_UI_SETTINGS_MULTI_IDENTITY_SWITCH_PROFILE_SETTINGS_MEDIATOR_H_
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.mm b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.mm
new file mode 100644
index 0000000..0c6be6e
--- /dev/null
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.mm
@@ -0,0 +1,35 @@
+// Copyright 2024 The Chromium Authors
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_mediator.h"
+
+#import <Foundation/Foundation.h>
+
+#import "base/apple/foundation_util.h"
+#import "base/strings/sys_string_conversions.h"
+#import "components/prefs/pref_service.h"
+#import "ios/chrome/browser/shared/model/application_context/application_context.h"
+#import "ios/chrome/browser/shared/model/prefs/pref_names.h"
+
+@implementation SwitchProfileSettingsMediator
+
+#pragma mark - SwitchProfileSettingsDelegate
+
+- (void)openProfileInNewWindow:(NSString*)browserStateName {
+  // Update the last used browserState so that the newly created scene is linked
+  // to the selected browserState (and not to the old one).
+  GetApplicationContext()->GetLocalState()->SetString(
+      prefs::kBrowserStateLastUsed, base::SysNSStringToUTF8(browserStateName));
+
+  // Open the selected profile in a new window.
+  if (@available(iOS 17, *)) {
+    UISceneSessionActivationRequest* activationRequest =
+        [UISceneSessionActivationRequest request];
+    [UIApplication.sharedApplication
+        activateSceneSessionForRequest:activationRequest
+                          errorHandler:nil];
+  }
+}
+
+@end
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h
index d1224050..9f5d26ef 100644
--- a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h
@@ -7,14 +7,19 @@
 
 #import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"
 
+@protocol SwitchProfileSettingsDelegate;
+
 // Controller for the UI that allows the user to switch from one profile to
 // another.
 @interface SwitchProfileSettingsTableViewController
-    : SettingsRootTableViewController
+    : SettingsRootTableViewController <UITableViewDelegate>
 
 // Name of the active browser state.
 @property(nonatomic, copy) NSString* activeBrowserStateName;
 
+// InfobarTranslateModalDelegate for this ViewController.
+@property(nonatomic, strong) id<SwitchProfileSettingsDelegate> delegate;
+
 // The designated initializer.
 - (instancetype)init NS_DESIGNATED_INITIALIZER;
 - (instancetype)initWithStyle:(UITableViewStyle)style NS_UNAVAILABLE;
diff --git a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.mm b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.mm
index 3b0ddb2..a5ea93a 100644
--- a/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.mm
+++ b/ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.mm
@@ -5,6 +5,7 @@
 #import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_view_controller.h"
 
 #import "base/apple/foundation_util.h"
+#import "base/ios/ios_util.h"
 #import "base/strings/sys_string_conversions.h"
 #import "components/prefs/pref_service.h"
 #import "ios/chrome/browser/shared/model/application_context/application_context.h"
@@ -13,6 +14,7 @@
 #import "ios/chrome/browser/shared/ui/table_view/cells/table_view_text_header_footer_item.h"
 #import "ios/chrome/browser/shared/ui/table_view/table_view_utils.h"
 #import "ios/chrome/browser/ui/authentication/cells/table_view_account_item.h"
+#import "ios/chrome/browser/ui/settings/multi_identity/switch_profile_settings_delegate.h"
 #import "ios/chrome/grit/ios_strings.h"
 #import "ios/public/provider/chrome/browser/signin/signin_resources_api.h"
 #import "ui/base/l10n/l10n_util_mac.h"
@@ -35,6 +37,7 @@
 }  // namespace
 
 @implementation SwitchProfileSettingsTableViewController {
+  NSString* selectedProfile_;
 }
 
 - (instancetype)init {
@@ -114,6 +117,14 @@
       [[TableViewTextButtonItem alloc] initWithType:SwitchProfileButton];
   switchProfileButtonItem.buttonText =
       l10n_util::GetNSString(IDS_IOS_SWITCH_PROFILE_MANAGEMENT_SETTINGS);
+
+  // TODO(crbug.com/333520714): Current solution only works if multiple scenes
+  // are supported, remove this check as soon as we have the correct APIs to
+  // switch profile within the same window.
+  if (!base::ios::IsMultipleScenesSupported()) {
+    switchProfileButtonItem.enabled = NO;
+  }
+
   [model addItem:switchProfileButtonItem
       toSectionWithIdentifier:LoadedProfilesIdentifier];
 }
@@ -138,9 +149,19 @@
 }
 
 #pragma mark - Private
-
 - (void)switchProfileButtonWasTapped {
-  // TODO(crbug.com/333520714): Add logic once the API is available.
+  // TODO(crbug.com/333520714): Add logic to open the profile in the same window
+  // once the API is available.
+
+  [self.delegate openProfileInNewWindow:selectedProfile_];
+}
+
+#pragma mark - UITableViewDelegate
+
+- (void)tableView:(UITableView*)tableView
+    didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
+  UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
+  selectedProfile_ = cell.textLabel.text;
 }
 
 @end