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
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197

ash / public / cpp / assistant / assistant_state_base.h [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.

#ifndef ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_
#define ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_

#include <memory>
#include <optional>
#include <string>

#include "ash/public/cpp/ash_public_export.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_enums.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_prefs.h"

class PrefChangeRegistrar;
class PrefService;

namespace ash {

// A checked observer which receives Assistant state change.
class ASH_PUBLIC_EXPORT AssistantStateObserver : public base::CheckedObserver {
 public:
  AssistantStateObserver() = default;

  AssistantStateObserver(const AssistantStateObserver&) = delete;
  AssistantStateObserver& operator=(const AssistantStateObserver&) = delete;

  ~AssistantStateObserver() override = default;

  virtual void OnAssistantConsentStatusChanged(int consent_status) {}
  virtual void OnAssistantContextEnabled(bool enabled) {}
  virtual void OnAssistantSettingsEnabled(bool enabled) {}
  virtual void OnAssistantHotwordAlwaysOn(bool hotword_always_on) {}
  virtual void OnAssistantHotwordEnabled(bool enabled) {}
  virtual void OnAssistantLaunchWithMicOpen(bool launch_with_mic_open) {}
  virtual void OnAssistantNotificationEnabled(bool notification_enabled) {}
  virtual void OnAssistantOnboardingModeChanged(
      assistant::prefs::AssistantOnboardingMode onboarding_mode) {}
  virtual void OnAssistantStateDestroyed() {}
  virtual void OnAssistantStatusChanged(assistant::AssistantStatus status) {}
  virtual void OnAssistantFeatureAllowedChanged(
      assistant::AssistantAllowedState state) {}
  virtual void OnArcPlayStoreEnabledChanged(bool enabled) {}
  virtual void OnLocaleChanged(const std::string& locale) {}
  virtual void OnLockedFullScreenStateChanged(bool enabled) {}
};

// Plain data class that holds Assistant related prefs and states. This is
// shared by both the controller that controls these values and client proxy
// that caches these values locally. Please do not use this object directly.
// For ash/browser use |AssistantState| and for other threads use
// |AssistantStateProxy|.
class ASH_PUBLIC_EXPORT AssistantStateBase {
 public:
  AssistantStateBase();

  AssistantStateBase(const AssistantStateBase&) = delete;
  AssistantStateBase& operator=(const AssistantStateBase&) = delete;

  virtual ~AssistantStateBase();

  assistant::AssistantStatus assistant_status() const {
    return assistant_status_;
  }

  const std::optional<bool>& settings_enabled() const {
    return settings_enabled_;
  }

  const std::optional<int>& consent_status() const { return consent_status_; }

  const std::optional<bool>& context_enabled() const {
    return context_enabled_;
  }

  const std::optional<bool>& hotword_enabled() const {
    return hotword_enabled_;
  }

  const std::optional<bool>& hotword_always_on() const {
    return hotword_always_on_;
  }

  const std::optional<bool>& launch_with_mic_open() const {
    return launch_with_mic_open_;
  }

  const std::optional<bool>& notification_enabled() const {
    return notification_enabled_;
  }

  const std::optional<assistant::prefs::AssistantOnboardingMode>&
  onboarding_mode() const {
    return onboarding_mode_;
  }

  const std::optional<assistant::AssistantAllowedState>& allowed_state() const {
    return allowed_state_;
  }

  const std::optional<std::string>& locale() const { return locale_; }

  const std::optional<bool>& arc_play_store_enabled() const {
    return arc_play_store_enabled_;
  }

  const std::optional<bool>& locked_full_screen_enabled() const {
    return locked_full_screen_enabled_;
  }

  std::string ToString() const;

  void AddObserver(AssistantStateObserver* observer);
  void RemoveObserver(AssistantStateObserver* observer);

  void RegisterPrefChanges(PrefService* pref_service);

  bool IsScreenContextAllowed() const;

  bool HasAudioInputDevice() const;

 protected:
  void InitializeObserver(AssistantStateObserver* observer);

  // Called when the related preferences are obtained from the pref service.
  void UpdateConsentStatus();
  void UpdateContextEnabled();
  void UpdateSettingsEnabled();
  void UpdateHotwordAlwaysOn();
  void UpdateHotwordEnabled();
  void UpdateLaunchWithMicOpen();
  void UpdateNotificationEnabled();
  void UpdateOnboardingMode();

  // Called when new values of the listened states are received.
  void UpdateAssistantStatus(assistant::AssistantStatus status);
  void UpdateFeatureAllowedState(assistant::AssistantAllowedState state);
  void UpdateLocale(const std::string& locale);
  void UpdateArcPlayStoreEnabled(bool enabled);
  void UpdateLockedFullScreenState(bool enabled);

  assistant::AssistantStatus assistant_status_ =
      assistant::AssistantStatus::NOT_READY;

  // TODO(b/138679823): Maybe remove Optional for preference values.
  // Whether the Assistant is enabled in system settings. nullopt if the
  // data is not available yet.
  std::optional<bool> settings_enabled_;

  // The status of the user's consent. nullopt if the data is not available yet.
  std::optional<int> consent_status_;

  // Whether screen context is enabled. nullopt if the data is not available
  // yet.
  std::optional<bool> context_enabled_;

  // Whether hotword listening is enabled.
  std::optional<bool> hotword_enabled_;

  // Whether hotword listening is always on/only with power source. nullopt
  // if the data is not available yet.
  std::optional<bool> hotword_always_on_;

  // Whether the Assistant should launch with mic open;
  std::optional<bool> launch_with_mic_open_;

  // Whether notification is enabled.
  std::optional<bool> notification_enabled_;

  // The mode for the Assistant onboarding experience.
  std::optional<assistant::prefs::AssistantOnboardingMode> onboarding_mode_;

  // Whether the Assistant feature is allowed or disallowed for what reason.
  // nullopt if the data is not available yet.
  std::optional<assistant::AssistantAllowedState> allowed_state_;

  std::optional<std::string> locale_;

  // Whether play store is enabled. nullopt if the data is not available yet.
  std::optional<bool> arc_play_store_enabled_;

  // Whether locked full screen state is enabled. nullopt if the data is not
  // available yet.
  std::optional<bool> locked_full_screen_enabled_;

  // Observes user profile prefs for the Assistant.
  std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;

  base::ObserverList<AssistantStateObserver> observers_;
};

}  // namespace ash

#endif  // ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_