Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / speech / chrome_speech_recognition_preferences.h
blob6bab29933aedc95682da331db7ef488d66f6dd28
1 // Copyright (c) 2012 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_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_
6 #define CHROME_BROWSER_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h"
12 #include "base/prefs/public/pref_change_registrar.h"
13 #include "base/prefs/public/pref_observer.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "chrome/browser/profiles/profile_keyed_service.h"
17 #include "chrome/browser/profiles/profile_keyed_service_factory.h"
18 #include "content/public/browser/speech_recognition_preferences.h"
20 class PrefService;
22 namespace base {
23 class ListValue;
26 // Utility class that handles persistent storage of speech recognition
27 // preferences. Instances of this class must be obtained exclusively through the
28 // method ChromeSpeechRecognitionPreferences::GetForProfile(...), so that
29 // preferences are handled within the Profile (if not NULL). The internal
30 // factory class also handles ownership and lifetime of the
31 // ChromeSpeechRecognitionPreferences instances (through the inner Service
32 // class), allowing them to be used in a detached mode (no persistent storage)
33 // even when a Profile is not available (or has been shutdown).
35 class ChromeSpeechRecognitionPreferences
36 : public content::SpeechRecognitionPreferences,
37 public PrefObserver {
38 public:
39 static void InitializeFactory();
40 static scoped_refptr<ChromeSpeechRecognitionPreferences> GetForProfile(
41 Profile* profile);
43 // PrefObserver implementation.
44 virtual void OnPreferenceChanged(PrefServiceBase* service,
45 const std::string& pref_name) OVERRIDE;
48 // content::SpeechRecognitionPreferences implementation.
49 // Called by both Content (on IO thread) and Chrome (on UI thread).
50 virtual bool FilterProfanities() const OVERRIDE;
52 // Called only by Chrome (on UI thread).
53 void SetFilterProfanities(bool filter_profanities);
54 void ToggleFilterProfanities();
55 bool ShouldShowSecurityNotification(const std::string& context_name) const;
56 void SetHasShownSecurityNotification(const std::string& context_name);
58 private:
59 // The two classes below are needed to handle storage of speech recognition
60 // preferences in profile preferences, according to the Chromium Profile
61 // Architecture document entitled "The New Way: ProfileKeyedServiceFactory".
63 // Singleton that manages instantiation of ChromeSpeechRecognitionPreferences
64 // handling its association with Profiles.
65 class Factory : public ProfileKeyedServiceFactory {
66 public:
67 static Factory* GetInstance();
68 scoped_refptr<ChromeSpeechRecognitionPreferences> GetForProfile(
69 Profile* profile);
71 private:
72 friend struct DefaultSingletonTraits<Factory>;
74 Factory();
75 virtual ~Factory();
77 // ProfileKeyedServiceFactory methods:
78 virtual ProfileKeyedService* BuildServiceInstanceFor(Profile* profile)
79 const OVERRIDE;
80 virtual void RegisterUserPrefs(PrefService* prefs) OVERRIDE;
81 virtual bool ServiceRedirectedInIncognito() const OVERRIDE;
82 virtual bool ServiceIsNULLWhileTesting() const OVERRIDE;
83 virtual bool ServiceIsCreatedWithProfile() const OVERRIDE;
85 DISALLOW_COPY_AND_ASSIGN(Factory);
88 // This wrapper handles the binding between ChromeSpeechRecognitionPreferences
89 // instances (which can have a longer lifetime, since they are refcounted) and
90 // ProfileKeyedService (which lifetime depends on the Profile service). Upon
91 // profile shutdown, the ChromeSpeechRecognitionPreferences instance is
92 // detached from profile, meaning that after that point its clients can still
93 // use it, but preferences will no longer be kept in sync with the profile.
94 class Service : public ProfileKeyedService {
95 public:
96 explicit Service(Profile* profile);
97 virtual ~Service();
99 // ProfileKeyedService implementation.
100 virtual void Shutdown() OVERRIDE;
102 scoped_refptr<ChromeSpeechRecognitionPreferences> GetPreferences() const;
104 private:
105 scoped_refptr<ChromeSpeechRecognitionPreferences> preferences_;
107 DISALLOW_COPY_AND_ASSIGN(Service);
110 explicit ChromeSpeechRecognitionPreferences(Profile* profile);
111 virtual ~ChromeSpeechRecognitionPreferences();
113 void DetachFromProfile();
114 void ReloadPreference(const std::string& key);
116 Profile* profile_; // NULL when detached.
117 scoped_ptr<PrefChangeRegistrar> pref_change_registrar_;
118 bool filter_profanities_;
119 scoped_ptr<base::ListValue> notifications_shown_;
121 // Lock used to ensure exclusive access to preference variables that are
122 // accessed by both threads (note: mutable is required to keep getters const).
123 mutable base::Lock preferences_lock_;
125 DISALLOW_COPY_AND_ASSIGN(ChromeSpeechRecognitionPreferences);
128 #endif // CHROME_BROWSER_SPEECH_CHROME_SPEECH_RECOGNITION_PREFERENCES_H_