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/url_request/url_fetcher_delegate.h"
25 #include "ui/app_list/speech_ui_model_observer.h"
28 struct FrameNavigateParams
;
29 struct LoadCommittedDetails
;
30 struct SpeechRecognitionSessionPreamble
;
33 namespace extensions
{
45 class SpeechAuthHelper
;
46 class SpeechRecognizer
;
47 class StartPageObserver
;
49 // StartPageService collects data to be displayed in app list's start page
50 // and hosts the start page contents.
51 class StartPageService
: public KeyedService
,
52 public content::WebContentsObserver
,
53 public net::URLFetcherDelegate
,
54 public SpeechRecognizerDelegate
{
56 typedef std::vector
<scoped_refptr
<const extensions::Extension
> >
58 // Gets the instance for the given profile.
59 static StartPageService
* Get(Profile
* profile
);
61 void AddObserver(StartPageObserver
* observer
);
62 void RemoveObserver(StartPageObserver
* observer
);
66 // Loads the start page WebContents if it hasn't already been loaded.
67 void LoadContentsIfNeeded();
71 void ToggleSpeechRecognition(
72 const scoped_refptr
<content::SpeechRecognitionSessionPreamble
>& preamble
);
74 // Called when the WebUI has finished loading.
77 // Returns true if the hotword is enabled in the app-launcher.
78 bool HotwordEnabled();
80 // They return essentially the same web contents but might return NULL when
81 // some flag disables the feature.
82 content::WebContents
* GetStartPageContents();
83 content::WebContents
* GetSpeechRecognitionContents();
85 void set_search_engine_is_google(bool search_engine_is_google
) {
86 search_engine_is_google_
= search_engine_is_google
;
88 Profile
* profile() { return profile_
; }
89 SpeechRecognitionState
state() { return state_
; }
91 // Overridden from app_list::SpeechRecognizerDelegate:
92 void OnSpeechResult(const base::string16
& query
, bool is_final
) override
;
93 void OnSpeechSoundLevelChanged(int16_t level
) override
;
94 void OnSpeechRecognitionStateChanged(
95 SpeechRecognitionState new_state
) override
;
96 void GetSpeechAuthParameters(std::string
* auth_scope
,
97 std::string
* auth_token
) override
;
100 // Protected for testing.
101 explicit StartPageService(Profile
* profile
);
102 ~StartPageService() override
;
105 friend class StartPageServiceFactory
;
107 // ProfileDestroyObserver to shutdown the service on exiting. WebContents
108 // depends on the profile and needs to be closed before the profile and its
109 // keyed service shutdown.
110 class ProfileDestroyObserver
;
112 // The WebContentsDelegate implementation for the start page. This allows
113 // getUserMedia() request from the web contents.
114 class StartPageWebContentsDelegate
;
116 #if defined(OS_CHROMEOS)
117 // This class observes the change of audio input device availability and
118 // checks if currently the system has valid audio input.
122 // This class observes network change events and disables/enables voice search
123 // based on network connectivity.
124 class NetworkChangeObserver
;
127 void UnloadContents();
129 // Loads the start page URL for |contents_|.
130 void LoadStartPageURL();
132 // Fetch the Google Doodle JSON data and update the app list start page.
133 void FetchDoodleJson();
135 // net::URLFetcherDelegate overrides:
136 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
138 // KeyedService overrides:
139 void Shutdown() override
;
141 // contents::WebContentsObserver overrides;
142 void DidNavigateMainFrame(
143 const content::LoadCommittedDetails
& details
,
144 const content::FrameNavigateParams
& params
) override
;
145 void RenderProcessGone(base::TerminationStatus status
) override
;
148 // Change the known microphone availability. |available| should be true if
149 // the microphone exists and is available for use.
150 void OnMicrophoneChanged(bool available
);
151 // Change the known network connectivity state. |available| should be true if
152 // at least one network is connected to.
153 void OnNetworkChanged(bool available
);
154 // Enables/disables voice recognition based on network and microphone state.
155 void UpdateRecognitionState();
158 scoped_ptr
<content::WebContents
> contents_
;
159 scoped_ptr
<StartPageWebContentsDelegate
> contents_delegate_
;
160 scoped_ptr
<ProfileDestroyObserver
> profile_destroy_observer_
;
161 SpeechRecognitionState state_
;
162 ObserverList
<StartPageObserver
> observers_
;
163 bool speech_button_toggled_manually_
;
164 bool speech_result_obtained_
;
166 bool webui_finished_loading_
;
167 std::vector
<base::Closure
> pending_webui_callbacks_
;
169 base::DefaultClock clock_
;
170 scoped_ptr
<SpeechRecognizer
> speech_recognizer_
;
171 scoped_ptr
<SpeechAuthHelper
> speech_auth_helper_
;
173 bool network_available_
;
174 bool microphone_available_
;
175 #if defined(OS_CHROMEOS)
176 scoped_ptr
<AudioStatus
> audio_status_
;
178 scoped_ptr
<NetworkChangeObserver
> network_change_observer_
;
180 bool search_engine_is_google_
;
181 scoped_ptr
<net::URLFetcher
> doodle_fetcher_
;
183 base::WeakPtrFactory
<StartPageService
> weak_factory_
;
185 DISALLOW_COPY_AND_ASSIGN(StartPageService
);
188 } // namespace app_list
190 #endif // CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_