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_ASSISTANT_ASSISTANT_CONTROLLER_IMPL_H_
6 #define ASH_ASSISTANT_ASSISTANT_CONTROLLER_IMPL_H_
7 
8 #include <map>
9 #include <memory>
10 #include <string>
11 #include <vector>
12 
13 #include "ash/accessibility/accessibility_observer.h"
14 #include "ash/ash_export.h"
15 #include "ash/assistant/assistant_alarm_timer_controller_impl.h"
16 #include "ash/assistant/assistant_interaction_controller_impl.h"
17 #include "ash/assistant/assistant_notification_controller_impl.h"
18 #include "ash/assistant/assistant_screen_context_controller_impl.h"
19 #include "ash/assistant/assistant_setup_controller.h"
20 #include "ash/assistant/assistant_state_controller.h"
21 #include "ash/assistant/assistant_suggestions_controller_impl.h"
22 #include "ash/assistant/assistant_ui_controller_impl.h"
23 #include "ash/assistant/assistant_view_delegate_impl.h"
24 #include "ash/assistant/assistant_web_ui_controller.h"
25 #include "ash/assistant/ui/assistant_view_delegate.h"
26 #include "ash/public/cpp/assistant/assistant_interface_binder.h"
27 #include "ash/public/cpp/assistant/controller/assistant_controller.h"
28 #include "ash/public/cpp/assistant/controller/assistant_controller_observer.h"
29 #include "ash/public/cpp/image_downloader.h"
30 #include "ash/public/mojom/assistant_volume_control.mojom.h"
31 #include "base/macros.h"
32 #include "base/memory/weak_ptr.h"
33 #include "base/observer_list.h"
34 #include "chromeos/audio/cras_audio_handler.h"
35 #include "chromeos/services/assistant/public/cpp/assistant_service.h"
36 #include "components/prefs/pref_service.h"
37 #include "mojo/public/cpp/bindings/pending_receiver.h"
38 #include "mojo/public/cpp/bindings/pending_remote.h"
39 #include "mojo/public/cpp/bindings/receiver.h"
40 #include "mojo/public/cpp/bindings/receiver_set.h"
41 #include "mojo/public/cpp/bindings/remote.h"
42 #include "mojo/public/cpp/bindings/remote_set.h"
43 
44 class PrefRegistrySimple;
45 
46 namespace ash {
47 
48 class ASH_EXPORT AssistantControllerImpl
49     : public AssistantController,
50       public AssistantControllerObserver,
51       public AssistantStateObserver,
52       public mojom::AssistantVolumeControl,
53       public chromeos::CrasAudioHandler::AudioObserver,
54       public AccessibilityObserver,
55       public AssistantInterfaceBinder {
56  public:
57   AssistantControllerImpl();
58   ~AssistantControllerImpl() override;
59 
60   static void RegisterProfilePrefs(PrefRegistrySimple* registry);
61 
62   void BindReceiver(
63       mojo::PendingReceiver<mojom::AssistantVolumeControl> receiver);
64 
65   // Downloads the image found at the specified |url|. On completion, the
66   // supplied |callback| will be run with the downloaded image. If the download
67   // attempt is unsuccessful, a NULL image is returned.
68   void DownloadImage(const GURL& url,
69                      ImageDownloader::DownloadCallback callback);
70 
71   // AssistantController:
72   void AddObserver(AssistantControllerObserver* observer) override;
73   void RemoveObserver(AssistantControllerObserver* observer) override;
74   void OpenAssistantSettings() override;
75   void OpenUrl(const GURL& url, bool in_background, bool from_server) override;
76   base::WeakPtr<ash::AssistantController> GetWeakPtr() override;
77   void SetAssistant(chromeos::assistant::Assistant* assistant) override;
78   void StartSpeakerIdEnrollmentFlow() override;
79   void SendAssistantFeedback(bool assistant_debug_info_allowed,
80                              const std::string& feedback_description,
81                              const std::string& screenshot_png) override;
82 
83   // AssistantControllerObserver:
84   void OnDeepLinkReceived(
85       assistant::util::DeepLinkType type,
86       const std::map<std::string, std::string>& params) override;
87 
88   // mojom::VolumeControl:
89   void SetVolume(int volume, bool user_initiated) override;
90   void SetMuted(bool muted) override;
91   void AddVolumeObserver(
92       mojo::PendingRemote<mojom::VolumeObserver> observer) override;
93 
94   // chromeos::CrasAudioHandler::AudioObserver:
95   void OnOutputMuteChanged(bool mute_on) override;
96   void OnOutputNodeVolumeChanged(uint64_t node, int volume) override;
97 
98   // AccessibilityObserver:
99   void OnAccessibilityStatusChanged() override;
100 
alarm_timer_controller()101   AssistantAlarmTimerControllerImpl* alarm_timer_controller() {
102     return &assistant_alarm_timer_controller_;
103   }
104 
notification_controller()105   AssistantNotificationControllerImpl* notification_controller() {
106     return &assistant_notification_controller_;
107   }
108 
screen_context_controller()109   AssistantScreenContextControllerImpl* screen_context_controller() {
110     return &assistant_screen_context_controller_;
111   }
112 
setup_controller()113   AssistantSetupController* setup_controller() {
114     return &assistant_setup_controller_;
115   }
116 
web_ui_controller()117   AssistantWebUiController* web_ui_controller() {
118     return &assistant_web_ui_controller_;
119   }
120 
view_delegate()121   AssistantViewDelegate* view_delegate() { return &view_delegate_; }
122 
123   bool IsAssistantReady() const;
124 
125  private:
126   void NotifyConstructed();
127   void NotifyDestroying();
128   void NotifyDeepLinkReceived(const GURL& deep_link);
129   void NotifyOpeningUrl(const GURL& url, bool in_background, bool from_server);
130   void NotifyUrlOpened(const GURL& url, bool from_server);
131 
132   // AssistantStateObserver:
133   void OnAssistantStatusChanged(
134       chromeos::assistant::AssistantStatus status) override;
135   void OnLockedFullScreenStateChanged(bool enabled) override;
136 
137   // AssistantInterfaceBinder implementation:
138   void BindVolumeControl(
139       mojo::PendingReceiver<mojom::AssistantVolumeControl> receiver) override;
140 
141   // The observer list should be initialized early so that sub-controllers may
142   // register as observers during their construction.
143   base::ObserverList<AssistantControllerObserver> observers_;
144 
145   mojo::Receiver<mojom::AssistantVolumeControl>
146       assistant_volume_control_receiver_{this};
147   mojo::RemoteSet<mojom::VolumeObserver> volume_observers_;
148 
149   chromeos::assistant::Assistant* assistant_ = nullptr;
150 
151   // Assistant sub-controllers.
152   AssistantAlarmTimerControllerImpl assistant_alarm_timer_controller_{this};
153   AssistantInteractionControllerImpl assistant_interaction_controller_{this};
154   AssistantNotificationControllerImpl assistant_notification_controller_;
155   AssistantStateController assistant_state_controller_;
156   AssistantScreenContextControllerImpl assistant_screen_context_controller_{
157       this};
158   AssistantSetupController assistant_setup_controller_{this};
159   AssistantSuggestionsControllerImpl assistant_suggestions_controller_;
160   AssistantUiControllerImpl assistant_ui_controller_{this};
161   AssistantWebUiController assistant_web_ui_controller_;
162 
163   AssistantViewDelegateImpl view_delegate_{this};
164 
165   base::WeakPtrFactory<AssistantControllerImpl> weak_factory_{this};
166 
167   DISALLOW_COPY_AND_ASSIGN(AssistantControllerImpl);
168 };
169 
170 }  // namespace ash
171 
172 #endif  // ASH_ASSISTANT_ASSISTANT_CONTROLLER_IMPL_H_
173