1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84

ash / assistant / assistant_setup_controller.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/assistant/assistant_setup_controller.h"

#include "ash/assistant/assistant_controller_impl.h"
#include "ash/assistant/util/deep_link_util.h"
#include "ash/assistant/util/i18n_util.h"
#include "ash/public/cpp/assistant/controller/assistant_ui_controller.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_prefs.h"
#include "chromeos/ash/services/assistant/public/cpp/features.h"

namespace {

constexpr char kGSuiteAdministratorInstructionsUrl[] =
    "https://support.google.com/a/answer/6304876";

}  // namespace

namespace ash {

AssistantSetupController::AssistantSetupController(
    AssistantControllerImpl* assistant_controller)
    : assistant_controller_(assistant_controller) {
  assistant_controller_observation_.Observe(AssistantController::Get());
}

AssistantSetupController::~AssistantSetupController() = default;

void AssistantSetupController::OnAssistantControllerConstructed() {
  assistant_controller_->view_delegate()->AddObserver(this);
}

void AssistantSetupController::OnAssistantControllerDestroying() {
  assistant_controller_->view_delegate()->RemoveObserver(this);
}

void AssistantSetupController::OnDeepLinkReceived(
    assistant::util::DeepLinkType type,
    const std::map<std::string, std::string>& params) {
  if (type != assistant::util::DeepLinkType::kOnboarding)
    return;

  std::optional<bool> relaunch = assistant::util::GetDeepLinkParamAsBool(
      params, assistant::util::DeepLinkParam::kRelaunch);

  StartOnboarding(relaunch.value_or(false));
}

void AssistantSetupController::OnOptInButtonPressed() {
  using assistant::prefs::ConsentStatus;
  if (AssistantState::Get()->consent_status().value_or(
          ConsentStatus::kUnknown) == ConsentStatus::kUnauthorized) {
    AssistantController::Get()->OpenUrl(assistant::util::CreateLocalizedGURL(
        kGSuiteAdministratorInstructionsUrl));
  } else {
    StartOnboarding(/*relaunch=*/true);
  }
}

void AssistantSetupController::StartOnboarding(bool relaunch, FlowType type) {
  auto* assistant_setup = AssistantSetup::GetInstance();
  if (!assistant_setup)
    return;

  AssistantUiController::Get()->CloseUi(assistant::AssistantExitPoint::kSetup);

  assistant_setup->StartAssistantOptInFlow(
      type, base::BindOnce(&AssistantSetupController::OnOptInFlowFinished,
                           weak_ptr_factory_.GetWeakPtr(), relaunch));
}

void AssistantSetupController::OnOptInFlowFinished(bool relaunch,
                                                   bool completed) {
  if (relaunch && completed) {
    AssistantUiController::Get()->ShowUi(
        assistant::AssistantEntryPoint::kSetup);
  }
}

}  // namespace ash