[M73 merge] assistant: Start OOBE for new users

If a new device receives Assistant feature after system image update,
they don't know the feature exists and we need a way to have the user
know and enable the feature.  One option is to show the Assistant OOBE
in this case.  We have a pref that marks the OOBE has shown
kVoiceInteractionActivityControlAccepted. We can check if this value is
null and show the OOBE in assistant_setup.

TBR=xiyuan@chromium.org, xiaohuic@chromium.org

(cherry picked from commit 440570664394e6675fad93fe49f7d5b7c156d9b5)

Bug: 926612, b/122851527
Test: manual
Change-Id: I38dbe44a85496d4abde8ae28ade158aa9382ad87
Reviewed-on: https://chromium-review.googlesource.com/c/1444699
Commit-Queue: Tao Wu <wutao@chromium.org>
Reviewed-by: Tao Wu <wutao@chromium.org>
Reviewed-by: Xiyuan Xia <xiyuan@chromium.org>
Reviewed-by: Xiaohui Chen <xiaohuic@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#627849}
Reviewed-on: https://chromium-review.googlesource.com/c/1450755
Cr-Commit-Position: refs/branch-heads/3683@{#136}
Cr-Branched-From: e51029943e0a38dd794b73caaf6373d5496ae783-refs/heads/master@{#625896}
diff --git a/chrome/browser/chromeos/login/session/user_session_manager.cc b/chrome/browser/chromeos/login/session/user_session_manager.cc
index 25577e2..47ca2159 100644
--- a/chrome/browser/chromeos/login/session/user_session_manager.cc
+++ b/chrome/browser/chromeos/login/session/user_session_manager.cc
@@ -1665,6 +1665,9 @@
   if (start_session_type_ == PRIMARY_USER_SESSION) {
 #if BUILDFLAG(ENABLE_CROS_ASSISTANT)
     // Initialize Assistant early to be used in post login Oobe steps.
+    // Note: AssistantClient::MaybeInit is also called in
+    // SessionControllerClient::OnSessionStateChanged, which happends after the
+    // post login Oobe steps. Therefore Assistant is initialized here.
     if (chromeos::switches::IsAssistantEnabled())
       AssistantClient::Get()->MaybeInit(profile);
 #endif
diff --git a/chrome/browser/ui/ash/assistant/assistant_client.cc b/chrome/browser/ui/ash/assistant/assistant_client.cc
index cc3f130..492d1c4 100644
--- a/chrome/browser/ui/ash/assistant/assistant_client.cc
+++ b/chrome/browser/ui/ash/assistant/assistant_client.cc
@@ -66,6 +66,10 @@
   assistant_setup_ = std::make_unique<AssistantSetup>(connector);
 }
 
+void AssistantClient::MaybeStartAssistantOptInFlow() {
+  assistant_setup_->MaybeStartAssistantOptInFlow();
+}
+
 void AssistantClient::OnAssistantStatusChanged(bool running) {
   // |running| means assistent mojom service is running. This maps to
   // |STOPPED| and |NOT_READY|. |RUNNING| maps to UI is shown and an assistant
diff --git a/chrome/browser/ui/ash/assistant/assistant_client.h b/chrome/browser/ui/ash/assistant/assistant_client.h
index a3ac07e..eef5571 100644
--- a/chrome/browser/ui/ash/assistant/assistant_client.h
+++ b/chrome/browser/ui/ash/assistant/assistant_client.h
@@ -25,6 +25,7 @@
   ~AssistantClient() override;
 
   void MaybeInit(Profile* profile);
+  void MaybeStartAssistantOptInFlow();
 
   // assistant::mojom::Client overrides:
   void OnAssistantStatusChanged(bool running) override;
diff --git a/chrome/browser/ui/ash/assistant/assistant_setup.cc b/chrome/browser/ui/ash/assistant/assistant_setup.cc
index ff0b567..68f5ed5 100644
--- a/chrome/browser/ui/ash/assistant/assistant_setup.cc
+++ b/chrome/browser/ui/ash/assistant/assistant_setup.cc
@@ -11,8 +11,11 @@
 #include "ash/public/cpp/vector_icons/vector_icons.h"
 #include "ash/public/interfaces/assistant_controller.mojom.h"
 #include "ash/public/interfaces/constants.mojom.h"
+#include "base/bind.h"
+#include "base/bind_helpers.h"
 #include "base/metrics/histogram_macros.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/threading/sequenced_task_runner_handle.h"
 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
 #include "chrome/browser/notifications/notification_display_service.h"
 #include "chrome/browser/profiles/profile.h"
@@ -81,7 +84,7 @@
 }  // namespace
 
 AssistantSetup::AssistantSetup(service_manager::Connector* connector)
-    : connector_(connector), binding_(this) {
+    : connector_(connector), binding_(this), weak_factory_(this) {
   // Bind to the AssistantSetupController in ash.
   ash::mojom::AssistantSetupControllerPtr setup_controller;
   connector_->BindInterface(ash::mojom::kServiceName, &setup_controller);
@@ -99,8 +102,10 @@
 void AssistantSetup::StartAssistantOptInFlow(
     ash::mojom::FlowType type,
     StartAssistantOptInFlowCallback callback) {
-  if (chromeos::AssistantOptInDialog::IsActive())
+  if (chromeos::AssistantOptInDialog::IsActive()) {
+    std::move(callback).Run(false);
     return;
+  }
 
   chromeos::AssistantOptInDialog::Show(type, std::move(callback));
 }
@@ -209,3 +214,16 @@
       LOG(ERROR) << "Invalid activity control consent status.";
   }
 }
+
+void AssistantSetup::MaybeStartAssistantOptInFlow() {
+  auto* pref_service = ProfileManager::GetActiveUserProfile()->GetPrefs();
+  DCHECK(pref_service);
+  if (!pref_service->GetUserPrefValue(
+          arc::prefs::kVoiceInteractionActivityControlAccepted)) {
+    base::SequencedTaskRunnerHandle::Get()->PostTask(
+        FROM_HERE, base::BindOnce(&AssistantSetup::StartAssistantOptInFlow,
+                                  weak_factory_.GetWeakPtr(),
+                                  ash::mojom::FlowType::CONSENT_FLOW,
+                                  base::DoNothing::Once<bool>()));
+  }
+}
diff --git a/chrome/browser/ui/ash/assistant/assistant_setup.h b/chrome/browser/ui/ash/assistant/assistant_setup.h
index a19653a..1a16670b 100644
--- a/chrome/browser/ui/ash/assistant/assistant_setup.h
+++ b/chrome/browser/ui/ash/assistant/assistant_setup.h
@@ -7,6 +7,7 @@
 
 #include "ash/public/interfaces/assistant_setup.mojom.h"
 #include "base/macros.h"
+#include "base/memory/weak_ptr.h"
 #include "chrome/browser/chromeos/arc/voice_interaction/voice_interaction_controller_client.h"
 #include "chromeos/services/assistant/public/mojom/settings.mojom.h"
 #include "mojo/public/cpp/bindings/binding.h"
@@ -27,6 +28,10 @@
       ash::mojom::FlowType type,
       StartAssistantOptInFlowCallback callback) override;
 
+  // If prefs::kVoiceInteractionActivityControlAccepted is nullptr, means the
+  // pref is not set by user. Therefore we need to start OOBE.
+  void MaybeStartAssistantOptInFlow();
+
  private:
   // arc::VoiceInteractionControllerClient::Observer overrides
   void OnStateChanged(ash::mojom::VoiceInteractionState state) override;
@@ -38,6 +43,8 @@
   chromeos::assistant::mojom::AssistantSettingsManagerPtr settings_manager_;
   mojo::Binding<ash::mojom::AssistantSetup> binding_;
 
+  base::WeakPtrFactory<AssistantSetup> weak_factory_;
+
   DISALLOW_COPY_AND_ASSIGN(AssistantSetup);
 };
 
diff --git a/chrome/browser/ui/ash/session_controller_client.cc b/chrome/browser/ui/ash/session_controller_client.cc
index 1afbec5..e9c866cd 100644
--- a/chrome/browser/ui/ash/session_controller_client.cc
+++ b/chrome/browser/ui/ash/session_controller_client.cc
@@ -472,9 +472,11 @@
 
 #if BUILDFLAG(ENABLE_CROS_ASSISTANT)
     // Assistant is initialized only once when primary user logs in.
+    // Initialize Assistant when browser process restarts.
     if (chromeos::switches::IsAssistantEnabled()) {
       AssistantClient::Get()->MaybeInit(
           ProfileManager::GetPrimaryUserProfile());
+      AssistantClient::Get()->MaybeStartAssistantOptInFlow();
     }
 #endif
   }