18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#ifndef CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#define CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/basictypes.h"
96d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/memory/weak_ptr.h"
1023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)#include "base/prefs/pref_change_registrar.h"
116d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "base/scoped_observer.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/browser/notification_observer.h"
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/browser/notification_registrar.h"
156d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "extensions/browser/extension_registry_observer.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)class ExtensionService;
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class HotwordClient;
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)class Profile;
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)namespace extensions {
236d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class Extension;
246d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)class WebstoreStandaloneInstaller;
256d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)}  // namespace extensions
266d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace hotword_internal {
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Constants for the hotword field trial.
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern const char kHotwordFieldTrialName[];
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)extern const char kHotwordFieldTrialDisabledGroupName[];
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace hotword_internal
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Provides an interface for the Hotword component that does voice triggered
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// search.
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochclass HotwordService : public content::NotificationObserver,
366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                       public extensions::ExtensionRegistryObserver,
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       public KeyedService {
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) public:
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns true if the hotword supports the current system language.
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static bool DoesHotwordSupportLanguage(Profile* profile);
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // Returns true if the "enable-experimental-hotwording" flag is set.
4303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  static bool IsExperimentalHotwordingEnabled();
4403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  explicit HotwordService(Profile* profile);
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  virtual ~HotwordService();
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Overridden from content::NotificationObserver:
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual void Observe(int type,
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       const content::NotificationSource& source,
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                       const content::NotificationDetails& details) OVERRIDE;
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Overridden from ExtensionRegisterObserver:
546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void OnExtensionInstalled(
556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      content::BrowserContext* browser_context,
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const extensions::Extension* extension,
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool is_update) OVERRIDE;
586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void OnExtensionUninstalled(
596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      content::BrowserContext* browser_context,
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      const extensions::Extension* extension,
615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      extensions::UninstallReason reason) OVERRIDE;
626d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Checks for whether all the necessary files have downloaded to allow for
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // using the extension.
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsServiceAvailable();
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Determine if hotwording is allowed in this profile based on field trials
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and language.
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsHotwordAllowed();
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Checks if the user has opted into audio logging. Returns true if the user
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // is opted in, false otherwise..
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  bool IsOptedIntoAudioLogging();
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
7523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Control the state of the hotword extension.
7623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void EnableHotwordExtension(ExtensionService* extension_service);
7723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void DisableHotwordExtension(ExtensionService* extension_service);
7823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
7923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // Handles enabling/disabling the hotword extension when the user
8023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  // turns it off via the settings menu.
8123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  void OnHotwordSearchEnabledChanged(const std::string& pref_name);
8223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
83010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Called to handle the hotword session from |client|.
84010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void RequestHotwordSession(HotwordClient* client);
85010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  void StopHotwordSession(HotwordClient* client);
86010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  HotwordClient* client() { return client_; }
87010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Checks if the current version of the hotword extension should be
896d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // uninstalled in order to update to a different language version.
906d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Returns true if the extension was uninstalled.
916d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool MaybeReinstallHotwordExtension();
926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Checks based on locale if the current version should be uninstalled so that
946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // a version with a different language can be installed.
956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool ShouldReinstallHotwordExtension();
966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Helper functions pulled out for testing purposes.
986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // UninstallHotwordExtension returns true if the extension was uninstalled.
996d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual bool UninstallHotwordExtension(ExtensionService* extension_service);
1006d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  virtual void InstallHotwordExtensionFromWebstore();
1016d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1026d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Sets the pref value of the previous language.
1036d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void SetPreviousLanguagePref();
1046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns the current error message id. A value of 0 indicates
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // no error.
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int error_message() { return error_message_; }
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // These methods are for launching, and getting and setting the launch mode of
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // the Hotword Audio Verification App.
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  //
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(kcarattini): Remove this when
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // https://code.google.com/p/chromium/issues/detail?id=165573 is fixed,
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // at which time we can simply launch the app in the given mode instead of
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // having to check for it here.
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  enum LaunchMode {
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    AUDIO_HISTORY_ONLY,
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    HOTWORD_ONLY,
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    HOTWORD_AND_AUDIO_HISTORY,
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    SPEECH_TRAINING
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  };
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void LaunchHotwordAudioVerificationApp(const LaunchMode& launch_mode);
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual LaunchMode GetHotwordAudioVerificationLaunchMode();
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) private:
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Profile* profile_;
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
12823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  PrefChangeRegistrar pref_registrar_;
12923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
130effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  content::NotificationRegistrar registrar_;
131effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
1326d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // For observing the ExtensionRegistry.
1336d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  ScopedObserver<extensions::ExtensionRegistry,
1346d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                 extensions::ExtensionRegistryObserver>
1356d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      extension_registry_observer_;
1366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  scoped_refptr<extensions::WebstoreStandaloneInstaller> installer_;
1386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
139010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  HotwordClient* client_;
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int error_message_;
1416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool reinstall_pending_;
1426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  base::WeakPtrFactory<HotwordService> weak_factory_;
144010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Stores the launch mode for the Hotword Audio Verification App.
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  LaunchMode hotword_audio_verification_launch_mode_;
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HotwordService);
1498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
1508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif  // CHROME_BROWSER_SEARCH_HOTWORD_SERVICE_H_
152