Add active user changed handling for App Service app window.

Update AppServiceProxy when the user is switched because
AppServiceProxy is based on profile.

Add MultiProfileBrowserStatusMonitor to ChromeLauncherController when
kAppServiceInstanceRegistry flag is enabled.

BUG=1011235

Change-Id: Id46f72d2334b94f601275560088112180c5bfc87
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1940314
Commit-Queue: Nancy Wang <nancylingwang@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#720789}
diff --git a/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.cc b/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.cc
index 990e5b2..85617b6 100644
--- a/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.cc
+++ b/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.cc
@@ -10,6 +10,7 @@
 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
 #include "chrome/browser/chromeos/plugin_vm/plugin_vm_util.h"
 #include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
 #include "chrome/browser/ui/ash/launcher/app_window_base.h"
 #include "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"
 #include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
@@ -41,6 +42,22 @@
   aura::Env::GetInstance()->RemoveObserver(this);
 }
 
+void AppServiceAppWindowLauncherController::ActiveUserChanged(
+    const std::string& user_email) {
+  if (proxy_)
+    Observe(nullptr);
+
+  // TODO(crbug.com/1011235): Inactive the running app windows in
+  // InstanceRegistry for the inactive user, and active the app windows for the
+  // active user.
+
+  proxy_ = apps::AppServiceProxyFactory::GetForProfile(owner()->profile());
+  DCHECK(proxy_);
+  Observe(&proxy_->InstanceRegistry());
+
+  app_service_instance_helper_->ActiveUserChanged();
+}
+
 void AppServiceAppWindowLauncherController::OnWindowInitialized(
     aura::Window* window) {
   // An app window has type WINDOW_TYPE_NORMAL, a WindowDelegate and
diff --git a/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.h b/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.h
index 93cacad..03edafb 100644
--- a/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.h
+++ b/chrome/browser/ui/ash/launcher/app_service_app_window_launcher_controller.h
@@ -40,6 +40,9 @@
       ChromeLauncherController* owner);
   ~AppServiceAppWindowLauncherController() override;
 
+  // AppWindowLauncherController:
+  void ActiveUserChanged(const std::string& user_email) override;
+
   // aura::EnvObserver:
   void OnWindowInitialized(aura::Window* window) override;
 
diff --git a/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.cc b/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.cc
index b9dd38c..1d1a131 100644
--- a/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.cc
+++ b/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.cc
@@ -11,6 +11,7 @@
 #include "chrome/browser/apps/app_service/app_service_proxy.h"
 #include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
 #include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/profiles/profile_manager.h"
 #include "chrome/common/chrome_features.h"
 #include "chrome/services/app_service/public/cpp/instance_update.h"
 #include "chrome/services/app_service/public/mojom/types.mojom.h"
@@ -24,6 +25,14 @@
 
 AppServiceInstanceRegistryHelper::~AppServiceInstanceRegistryHelper() = default;
 
+void AppServiceInstanceRegistryHelper::ActiveUserChanged() {
+  if (!base::FeatureList::IsEnabled(features::kAppServiceInstanceRegistry))
+    return;
+
+  proxy_ = apps::AppServiceProxyFactory::GetForProfile(
+      ProfileManager::GetActiveUserProfile());
+}
+
 void AppServiceInstanceRegistryHelper::OnActiveTabChanged(
     content::WebContents* old_contents,
     content::WebContents* new_contents) {
diff --git a/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.h b/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.h
index 4e30a2c..60ac1ede 100644
--- a/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.h
+++ b/chrome/browser/ui/ash/launcher/app_service_instance_registry_helper.h
@@ -30,6 +30,8 @@
   explicit AppServiceInstanceRegistryHelper(Profile* profile);
   ~AppServiceInstanceRegistryHelper();
 
+  void ActiveUserChanged();
+
   // Notifies the AppService InstanceRegistry that active tabs are changed.
   void OnActiveTabChanged(content::WebContents* old_contents,
                           content::WebContents* new_contents);
diff --git a/chrome/browser/ui/ash/launcher/browser_status_monitor.h b/chrome/browser/ui/ash/launcher/browser_status_monitor.h
index 36126b2..67edf526 100644
--- a/chrome/browser/ui/ash/launcher/browser_status_monitor.h
+++ b/chrome/browser/ui/ash/launcher/browser_status_monitor.h
@@ -80,6 +80,10 @@
   bool IsV1AppInShelf(Browser* browser);
   bool IsV1AppInShelfWithAppId(const std::string& app_id);
 
+  AppServiceInstanceRegistryHelper* app_service_instance_helper() const {
+    return app_service_instance_helper_.get();
+  }
+
  private:
   class LocalWebContentsObserver;
 
diff --git a/chrome/browser/ui/ash/launcher/chrome_launcher_controller.cc b/chrome/browser/ui/ash/launcher/chrome_launcher_controller.cc
index 3e3306e4..790df3d 100644
--- a/chrome/browser/ui/ash/launcher/chrome_launcher_controller.cc
+++ b/chrome/browser/ui/ash/launcher/chrome_launcher_controller.cc
@@ -258,8 +258,18 @@
   }
 
   if (base::FeatureList::IsEnabled(features::kAppServiceInstanceRegistry)) {
-    browser_status_monitor_ = std::make_unique<BrowserStatusMonitor>(this);
-    browser_status_monitor_->Initialize();
+    if (SessionControllerClientImpl::IsMultiProfileAvailable()) {
+      // If running in separated destkop mode, we create the multi profile
+      // version of status monitor.
+      browser_status_monitor_ =
+          std::make_unique<MultiProfileBrowserStatusMonitor>(this);
+      browser_status_monitor_->Initialize();
+    } else {
+      // Create our v1/v2 application / browser monitors which will inform the
+      // launcher of status changes.
+      browser_status_monitor_ = std::make_unique<BrowserStatusMonitor>(this);
+      browser_status_monitor_->Initialize();
+    }
     app_window_controllers_.push_back(
         std::make_unique<AppServiceAppWindowLauncherController>(this));
     return;
diff --git a/chrome/browser/ui/ash/launcher/multi_profile_browser_status_monitor.cc b/chrome/browser/ui/ash/launcher/multi_profile_browser_status_monitor.cc
index bf9aa56..086a19dc 100644
--- a/chrome/browser/ui/ash/launcher/multi_profile_browser_status_monitor.cc
+++ b/chrome/browser/ui/ash/launcher/multi_profile_browser_status_monitor.cc
@@ -27,6 +27,8 @@
 
 void MultiProfileBrowserStatusMonitor::ActiveUserChanged(
     const std::string& user_email) {
+  app_service_instance_helper()->ActiveUserChanged();
+
   // Handle windowed apps.
   for (AppList::iterator it = app_list_.begin(); it != app_list_.end(); ++it) {
     bool owned = multi_user_util::IsProfileFromActiveUser((*it)->profile());