1 // Copyright 2013 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.
5 #ifndef CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_
6 #define CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/strings/string16.h"
19 #include "base/time/default_clock.h"
20 #include "chrome/browser/ui/app_list/speech_recognizer_delegate.h"
21 #include "components/keyed_service/core/keyed_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_contents_observer.h"
24 #include "net/base/backoff_entry.h"
25 #include "net/url_request/url_fetcher_delegate.h"
26 #include "ui/app_list/speech_ui_model_observer.h"
29 struct FrameNavigateParams
;
30 struct LoadCommittedDetails
;
31 struct SpeechRecognitionSessionPreamble
;
34 namespace extensions
{
46 class SpeechAuthHelper
;
47 class SpeechRecognizer
;
48 class StartPageObserver
;
50 // StartPageService collects data to be displayed in app list's start page
51 // and hosts the start page contents.
52 class StartPageService
: public KeyedService
,
53 public content::WebContentsObserver
,
54 public net::URLFetcherDelegate
,
55 public SpeechRecognizerDelegate
{
57 typedef std::vector
<scoped_refptr
<const extensions::Extension
> >
59 // Gets the instance for the given profile. May return nullptr.
60 static StartPageService
* Get(Profile
* profile
);
62 void AddObserver(StartPageObserver
* observer
);
63 void RemoveObserver(StartPageObserver
* observer
);
67 // Loads the start page WebContents if it hasn't already been loaded.
68 void LoadContentsIfNeeded();
72 void ToggleSpeechRecognition(
73 const scoped_refptr
<content::SpeechRecognitionSessionPreamble
>& preamble
);
75 // Called when the WebUI has finished loading.
78 // Returns true if the hotword is enabled in the app-launcher.
79 bool HotwordEnabled();
81 // They return essentially the same web contents but might return NULL when
82 // some flag disables the feature.
83 content::WebContents
* GetStartPageContents();
84 content::WebContents
* GetSpeechRecognitionContents();
86 void set_search_engine_is_google(bool search_engine_is_google
) {
87 search_engine_is_google_
= search_engine_is_google
;
89 Profile
* profile() { return profile_
; }
90 SpeechRecognitionState
state() { return state_
; }
92 // Overridden from app_list::SpeechRecognizerDelegate:
93 void OnSpeechResult(const base::string16
& query
, bool is_final
) override
;
94 void OnSpeechSoundLevelChanged(int16_t level
) override
;
95 void OnSpeechRecognitionStateChanged(
96 SpeechRecognitionState new_state
) override
;
97 void GetSpeechAuthParameters(std::string
* auth_scope
,
98 std::string
* auth_token
) override
;
101 // Protected for testing.
102 explicit StartPageService(Profile
* profile
);
103 ~StartPageService() override
;
106 friend class StartPageServiceFactory
;
108 // ProfileDestroyObserver to shutdown the service on exiting. WebContents
109 // depends on the profile and needs to be closed before the profile and its
110 // keyed service shutdown.
111 class ProfileDestroyObserver
;
113 // The WebContentsDelegate implementation for the start page. This allows
114 // getUserMedia() request from the web contents.
115 class StartPageWebContentsDelegate
;
117 #if defined(OS_CHROMEOS)
118 // This class observes the change of audio input device availability and
119 // checks if currently the system has valid audio input.
123 // This class observes network change events and disables/enables voice search
124 // based on network connectivity.
125 class NetworkChangeObserver
;
128 void UnloadContents();
130 // Loads the start page URL for |contents_|.
131 void LoadStartPageURL();
133 // Fetch the Google Doodle JSON data and update the app list start page.
134 void FetchDoodleJson();
136 // net::URLFetcherDelegate overrides:
137 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
139 // KeyedService overrides:
140 void Shutdown() override
;
142 // contents::WebContentsObserver overrides;
143 void DidNavigateMainFrame(
144 const content::LoadCommittedDetails
& details
,
145 const content::FrameNavigateParams
& params
) override
;
147 // Change the known microphone availability. |available| should be true if
148 // the microphone exists and is available for use.
149 void OnMicrophoneChanged(bool available
);
150 // Change the known network connectivity state. |available| should be true if
151 // at least one network is connected to.
152 void OnNetworkChanged(bool available
);
153 // Enables/disables voice recognition based on network and microphone state.
154 void UpdateRecognitionState();
155 // Determines whether speech recognition should be enabled, based on the
156 // current state of the StartPageService.
157 bool ShouldEnableSpeechRecognition() const;
160 scoped_ptr
<content::WebContents
> contents_
;
161 scoped_ptr
<StartPageWebContentsDelegate
> contents_delegate_
;
162 scoped_ptr
<ProfileDestroyObserver
> profile_destroy_observer_
;
163 SpeechRecognitionState state_
;
164 base::ObserverList
<StartPageObserver
> observers_
;
165 bool speech_button_toggled_manually_
;
166 bool speech_result_obtained_
;
168 bool webui_finished_loading_
;
169 std::vector
<base::Closure
> pending_webui_callbacks_
;
171 base::DefaultClock clock_
;
172 scoped_ptr
<SpeechRecognizer
> speech_recognizer_
;
173 scoped_ptr
<SpeechAuthHelper
> speech_auth_helper_
;
175 bool network_available_
;
176 bool microphone_available_
;
177 #if defined(OS_CHROMEOS)
178 scoped_ptr
<AudioStatus
> audio_status_
;
180 scoped_ptr
<NetworkChangeObserver
> network_change_observer_
;
182 bool search_engine_is_google_
;
183 scoped_ptr
<net::URLFetcher
> doodle_fetcher_
;
184 net::BackoffEntry backoff_entry_
;
186 base::WeakPtrFactory
<StartPageService
> weak_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(StartPageService
);
191 } // namespace app_list
193 #endif // CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_