1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_
6 #define ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "ash/public/cpp/ash_public_export.h"
12 #include "base/macros.h"
13 #include "base/observer_list.h"
14 #include "base/observer_list_types.h"
15 #include "base/optional.h"
16 #include "chromeos/services/assistant/public/cpp/assistant_enums.h"
17 #include "chromeos/services/assistant/public/cpp/assistant_prefs.h"
18 
19 class PrefChangeRegistrar;
20 class PrefService;
21 
22 namespace ash {
23 
24 // A checked observer which receives Assistant state change.
25 class ASH_PUBLIC_EXPORT AssistantStateObserver : public base::CheckedObserver {
26  public:
27   AssistantStateObserver() = default;
28   ~AssistantStateObserver() override = default;
29 
OnAssistantConsentStatusChanged(int consent_status)30   virtual void OnAssistantConsentStatusChanged(int consent_status) {}
OnAssistantContextEnabled(bool enabled)31   virtual void OnAssistantContextEnabled(bool enabled) {}
OnAssistantSettingsEnabled(bool enabled)32   virtual void OnAssistantSettingsEnabled(bool enabled) {}
OnAssistantHotwordAlwaysOn(bool hotword_always_on)33   virtual void OnAssistantHotwordAlwaysOn(bool hotword_always_on) {}
OnAssistantHotwordEnabled(bool enabled)34   virtual void OnAssistantHotwordEnabled(bool enabled) {}
OnAssistantLaunchWithMicOpen(bool launch_with_mic_open)35   virtual void OnAssistantLaunchWithMicOpen(bool launch_with_mic_open) {}
OnAssistantNotificationEnabled(bool notification_enabled)36   virtual void OnAssistantNotificationEnabled(bool notification_enabled) {}
OnAssistantOnboardingModeChanged(chromeos::assistant::prefs::AssistantOnboardingMode onboarding_mode)37   virtual void OnAssistantOnboardingModeChanged(
38       chromeos::assistant::prefs::AssistantOnboardingMode onboarding_mode) {}
OnAssistantStateDestroyed()39   virtual void OnAssistantStateDestroyed() {}
OnAssistantQuickAnswersEnabled(bool quick_answers_enabled)40   virtual void OnAssistantQuickAnswersEnabled(bool quick_answers_enabled) {}
OnAssistantStatusChanged(chromeos::assistant::AssistantStatus status)41   virtual void OnAssistantStatusChanged(
42       chromeos::assistant::AssistantStatus status) {}
OnAssistantFeatureAllowedChanged(chromeos::assistant::AssistantAllowedState state)43   virtual void OnAssistantFeatureAllowedChanged(
44       chromeos::assistant::AssistantAllowedState state) {}
OnArcPlayStoreEnabledChanged(bool enabled)45   virtual void OnArcPlayStoreEnabledChanged(bool enabled) {}
OnLocaleChanged(const std::string & locale)46   virtual void OnLocaleChanged(const std::string& locale) {}
OnLockedFullScreenStateChanged(bool enabled)47   virtual void OnLockedFullScreenStateChanged(bool enabled) {}
48 
49  private:
50   DISALLOW_COPY_AND_ASSIGN(AssistantStateObserver);
51 };
52 
53 // Plain data class that holds Assistant related prefs and states. This is
54 // shared by both the controller that controls these values and client proxy
55 // that caches these values locally. Please do not use this object directly.
56 // For ash/browser use |AssistantState| and for other threads use
57 // |AssistantStateProxy|.
58 class ASH_PUBLIC_EXPORT AssistantStateBase {
59  public:
60   AssistantStateBase();
61   virtual ~AssistantStateBase();
62 
assistant_status()63   chromeos::assistant::AssistantStatus assistant_status() const {
64     return assistant_status_;
65   }
66 
settings_enabled()67   const base::Optional<bool>& settings_enabled() const {
68     return settings_enabled_;
69   }
70 
consent_status()71   const base::Optional<int>& consent_status() const { return consent_status_; }
72 
context_enabled()73   const base::Optional<bool>& context_enabled() const {
74     return context_enabled_;
75   }
76 
hotword_enabled()77   const base::Optional<bool>& hotword_enabled() const {
78     return hotword_enabled_;
79   }
80 
hotword_always_on()81   const base::Optional<bool>& hotword_always_on() const {
82     return hotword_always_on_;
83   }
84 
launch_with_mic_open()85   const base::Optional<bool>& launch_with_mic_open() const {
86     return launch_with_mic_open_;
87   }
88 
notification_enabled()89   const base::Optional<bool>& notification_enabled() const {
90     return notification_enabled_;
91   }
92 
93   const base::Optional<chromeos::assistant::prefs::AssistantOnboardingMode>&
onboarding_mode()94   onboarding_mode() const {
95     return onboarding_mode_;
96   }
97 
98   const base::Optional<chromeos::assistant::AssistantAllowedState>&
allowed_state()99   allowed_state() const {
100     return allowed_state_;
101   }
102 
locale()103   const base::Optional<std::string>& locale() const { return locale_; }
104 
arc_play_store_enabled()105   const base::Optional<bool>& arc_play_store_enabled() const {
106     return arc_play_store_enabled_;
107   }
108 
locked_full_screen_enabled()109   const base::Optional<bool>& locked_full_screen_enabled() const {
110     return locked_full_screen_enabled_;
111   }
112 
113   std::string ToString() const;
114 
115   void AddObserver(AssistantStateObserver* observer);
116   void RemoveObserver(AssistantStateObserver* observer);
117 
118   void RegisterPrefChanges(PrefService* pref_service);
119 
120   bool IsScreenContextAllowed() const;
121 
122  protected:
123   void InitializeObserver(AssistantStateObserver* observer);
124 
125   // Called when the related preferences are obtained from the pref service.
126   void UpdateConsentStatus();
127   void UpdateContextEnabled();
128   void UpdateSettingsEnabled();
129   void UpdateHotwordAlwaysOn();
130   void UpdateHotwordEnabled();
131   void UpdateLaunchWithMicOpen();
132   void UpdateNotificationEnabled();
133   void UpdateOnboardingMode();
134   void UpdateQuickAnswersEnabled();
135 
136   // Called when new values of the listened states are received.
137   void UpdateAssistantStatus(chromeos::assistant::AssistantStatus status);
138   void UpdateFeatureAllowedState(
139       chromeos::assistant::AssistantAllowedState state);
140   void UpdateLocale(const std::string& locale);
141   void UpdateArcPlayStoreEnabled(bool enabled);
142   void UpdateLockedFullScreenState(bool enabled);
143 
144   chromeos::assistant::AssistantStatus assistant_status_ =
145       chromeos::assistant::AssistantStatus::NOT_READY;
146 
147   // TODO(b/138679823): Maybe remove Optional for preference values.
148   // Whether the Assistant is enabled in system settings. nullopt if the
149   // data is not available yet.
150   base::Optional<bool> settings_enabled_;
151 
152   // The status of the user's consent. nullopt if the data is not available yet.
153   base::Optional<int> consent_status_;
154 
155   // Whether screen context is enabled. nullopt if the data is not available
156   // yet.
157   base::Optional<bool> context_enabled_;
158 
159   // Whether hotword listening is enabled.
160   base::Optional<bool> hotword_enabled_;
161 
162   // Whether hotword listening is always on/only with power source. nullopt
163   // if the data is not available yet.
164   base::Optional<bool> hotword_always_on_;
165 
166   // Whether the Assistant should launch with mic open;
167   base::Optional<bool> launch_with_mic_open_;
168 
169   // Whether notification is enabled.
170   base::Optional<bool> notification_enabled_;
171 
172   // The mode for the Assistant onboarding experience.
173   base::Optional<chromeos::assistant::prefs::AssistantOnboardingMode>
174       onboarding_mode_;
175 
176   // Whether the Assistant feature is allowed or disallowed for what reason.
177   // nullopt if the data is not available yet.
178   base::Optional<chromeos::assistant::AssistantAllowedState> allowed_state_;
179 
180   base::Optional<std::string> locale_;
181 
182   // Whether play store is enabled. nullopt if the data is not available yet.
183   base::Optional<bool> arc_play_store_enabled_;
184 
185   // Whether locked full screen state is enabled. nullopt if the data is not
186   // available yet.
187   base::Optional<bool> locked_full_screen_enabled_;
188 
189   // Whether quick answers is enabled. nullopt if the data is not available yet.
190   base::Optional<bool> quick_answers_enabled_;
191 
192   // Observes user profile prefs for the Assistant.
193   std::unique_ptr<PrefChangeRegistrar> pref_change_registrar_;
194 
195   base::ObserverList<AssistantStateObserver> observers_;
196 
197  private:
198   DISALLOW_COPY_AND_ASSIGN(AssistantStateBase);
199 };
200 
201 }  // namespace ash
202 
203 #endif  // ASH_PUBLIC_CPP_ASSISTANT_ASSISTANT_STATE_BASE_H_
204