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.
4
5#ifndef CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_
6#define CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/observer_list.h"
14#include "base/strings/string16.h"
15#include "components/keyed_service/core/keyed_service.h"
16#include "content/public/browser/web_contents.h"
17#include "ui/app_list/speech_ui_model_observer.h"
18
19namespace extensions {
20class Extension;
21}
22
23class Profile;
24
25namespace app_list {
26
27class RecommendedApps;
28class StartPageObserver;
29
30// StartPageService collects data to be displayed in app list's start page
31// and hosts the start page contents.
32class StartPageService : public KeyedService {
33 public:
34  typedef std::vector<scoped_refptr<const extensions::Extension> >
35      ExtensionList;
36  // Gets the instance for the given profile.
37  static StartPageService* Get(Profile* profile);
38
39  void AddObserver(StartPageObserver* observer);
40  void RemoveObserver(StartPageObserver* observer);
41
42  void AppListShown();
43  void AppListHidden();
44  void ToggleSpeechRecognition();
45
46  // Returns true if the hotword is enabled in the app-launcher.
47  bool HotwordEnabled();
48
49  // They return essentially the same web contents but might return NULL when
50  // some flag disables the feature.
51  content::WebContents* GetStartPageContents();
52  content::WebContents* GetSpeechRecognitionContents();
53
54  RecommendedApps* recommended_apps() { return recommended_apps_.get(); }
55  Profile* profile() { return profile_; }
56  SpeechRecognitionState state() { return state_; }
57  void OnSpeechResult(const base::string16& query, bool is_final);
58  void OnSpeechSoundLevelChanged(int16 level);
59  void OnSpeechRecognitionStateChanged(SpeechRecognitionState new_state);
60
61 private:
62  friend class StartPageServiceFactory;
63
64  // ProfileDestroyObserver to shutdown the service on exiting. WebContents
65  // depends on the profile and needs to be closed before the profile and its
66  // keyed service shutdown.
67  class ProfileDestroyObserver;
68
69  // The WebContentsDelegate implementation for the start page. This allows
70  // getUserMedia() request from the web contents.
71  class StartPageWebContentsDelegate;
72
73  explicit StartPageService(Profile* profile);
74  virtual ~StartPageService();
75
76  void LoadContents();
77  void UnloadContents();
78
79  // KeyedService overrides:
80  virtual void Shutdown() OVERRIDE;
81
82  Profile* profile_;
83  scoped_ptr<content::WebContents> contents_;
84  scoped_ptr<StartPageWebContentsDelegate> contents_delegate_;
85  scoped_ptr<ProfileDestroyObserver> profile_destroy_observer_;
86  scoped_ptr<RecommendedApps> recommended_apps_;
87  SpeechRecognitionState state_;
88  ObserverList<StartPageObserver> observers_;
89  bool speech_button_toggled_manually_;
90  bool speech_result_obtained_;
91
92  DISALLOW_COPY_AND_ASSIGN(StartPageService);
93};
94
95}  // namespace app_list
96
97#endif  // CHROME_BROWSER_UI_APP_LIST_START_PAGE_SERVICE_H_
98