Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / ui / app_list / start_page_service.h
blob419d59e485ea6886af737a108d529387bc74ef04
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_
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
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"
27 namespace content {
28 struct FrameNavigateParams;
29 struct LoadCommittedDetails;
30 struct SpeechRecognitionSessionPreamble;
33 namespace extensions {
34 class Extension;
37 namespace net {
38 class URLFetcher;
41 class Profile;
43 namespace app_list {
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 {
55 public:
56 typedef std::vector<scoped_refptr<const extensions::Extension> >
57 ExtensionList;
58 // Gets the instance for the given profile. May return nullptr.
59 static StartPageService* Get(Profile* profile);
61 void AddObserver(StartPageObserver* observer);
62 void RemoveObserver(StartPageObserver* observer);
64 void Init();
66 // Loads the start page WebContents if it hasn't already been loaded.
67 void LoadContentsIfNeeded();
69 void AppListShown();
70 void AppListHidden();
71 void ToggleSpeechRecognition(
72 const scoped_refptr<content::SpeechRecognitionSessionPreamble>& preamble);
74 // Called when the WebUI has finished loading.
75 void WebUILoaded();
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;
99 protected:
100 // Protected for testing.
101 explicit StartPageService(Profile* profile);
102 ~StartPageService() override;
104 private:
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.
119 class AudioStatus;
120 #endif
122 // This class observes network change events and disables/enables voice search
123 // based on network connectivity.
124 class NetworkChangeObserver;
126 void LoadContents();
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;
146 // Change the known microphone availability. |available| should be true if
147 // the microphone exists and is available for use.
148 void OnMicrophoneChanged(bool available);
149 // Change the known network connectivity state. |available| should be true if
150 // at least one network is connected to.
151 void OnNetworkChanged(bool available);
152 // Enables/disables voice recognition based on network and microphone state.
153 void UpdateRecognitionState();
154 // Determines whether speech recognition should be enabled, based on the
155 // current state of the StartPageService.
156 bool ShouldEnableSpeechRecognition() const;
158 Profile* profile_;
159 scoped_ptr<content::WebContents> contents_;
160 scoped_ptr<StartPageWebContentsDelegate> contents_delegate_;
161 scoped_ptr<ProfileDestroyObserver> profile_destroy_observer_;
162 SpeechRecognitionState state_;
163 ObserverList<StartPageObserver> observers_;
164 bool speech_button_toggled_manually_;
165 bool speech_result_obtained_;
167 bool webui_finished_loading_;
168 std::vector<base::Closure> pending_webui_callbacks_;
170 base::DefaultClock clock_;
171 scoped_ptr<SpeechRecognizer> speech_recognizer_;
172 scoped_ptr<SpeechAuthHelper> speech_auth_helper_;
174 bool network_available_;
175 bool microphone_available_;
176 #if defined(OS_CHROMEOS)
177 scoped_ptr<AudioStatus> audio_status_;
178 #endif
179 scoped_ptr<NetworkChangeObserver> network_change_observer_;
181 bool search_engine_is_google_;
182 scoped_ptr<net::URLFetcher> doodle_fetcher_;
184 base::WeakPtrFactory<StartPageService> weak_factory_;
186 DISALLOW_COPY_AND_ASSIGN(StartPageService);
189 } // namespace app_list
191 #endif // CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_