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 IOS_CHROME_BROWSER_UI_VOICE_TEXT_TO_SPEECH_PLAYBACK_CONTROLLER_H_
6 #define IOS_CHROME_BROWSER_UI_VOICE_TEXT_TO_SPEECH_PLAYBACK_CONTROLLER_H_
7 
8 #import <Foundation/Foundation.h>
9 
10 #include "base/macros.h"
11 #include "components/keyed_service/core/keyed_service.h"
12 #import "ios/chrome/browser/web_state_list/web_state_list_observer.h"
13 #include "ios/web/public/web_state_observer.h"
14 
15 @class TextToSpeechNotificationHandler;
16 class WebStateList;
17 
18 // A helper object that listens for TTS notifications and manages playback.
19 class TextToSpeechPlaybackController : public KeyedService,
20                                        public WebStateListObserver,
21                                        public web::WebStateObserver {
22  public:
23   explicit TextToSpeechPlaybackController();
24 
25   // The BrowserState's WebStateList.
26   void SetWebStateList(WebStateList* web_state_list);
27 
28   // Whether TTS playback is enabled.
29   bool IsEnabled() const;
30   void SetEnabled(bool enabled);
31 
32  private:
33   // Setter for the current WebState being observed.
34   void SetWebState(web::WebState* web_state);
35 
36   // KeyedService:
37   void Shutdown() override;
38 
39   // WebStateListObserver:
40   void WebStateInsertedAt(WebStateList* web_state_list,
41                           web::WebState* web_state,
42                           int index,
43                           bool activating) override;
44   void WebStateReplacedAt(WebStateList* web_state_list,
45                           web::WebState* old_web_state,
46                           web::WebState* new_web_state,
47                           int index) override;
48   void WebStateActivatedAt(WebStateList* web_state_list,
49                            web::WebState* old_web_state,
50                            web::WebState* new_web_state,
51                            int active_index,
52                            ActiveWebStateChangeReason reason) override;
53   void WebStateDetachedAt(WebStateList* web_state_list,
54                           web::WebState* web_state,
55                           int index) override;
56   void WillCloseWebStateAt(WebStateList* web_state_list,
57                            web::WebState* web_state,
58                            int index,
59                            bool user_action) override;
60 
61   // WebStateObserver:
62   void DidFinishNavigation(web::WebState* web_state,
63                            web::NavigationContext* navigation_context) override;
64   void WebStateDestroyed(web::WebState* web_state) override;
65 
66   // Helper object that listens for TTS notifications.
67   __strong TextToSpeechNotificationHandler* notification_helper_ = nil;
68 
69   // The WebStateList.
70   WebStateList* web_state_list_ = nullptr;
71 
72   // The WebState being observed.
73   web::WebState* web_state_ = nullptr;
74 
75   DISALLOW_COPY_AND_ASSIGN(TextToSpeechPlaybackController);
76 };
77 
78 #endif  // IOS_CHROME_BROWSER_UI_VOICE_TEXT_TO_SPEECH_PLAYBACK_CONTROLLER_H_
79