cros_settings.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
5#ifndef CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
6#define CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/callback_forward.h"
12#include "base/hash_tables.h"
13#include "base/observer_list.h"
14#include "base/threading/non_thread_safe.h"
15#include "chrome/browser/chromeos/settings/cros_settings_names.h"
16#include "chrome/browser/chromeos/settings/cros_settings_provider.h"
17#include "content/public/browser/notification_observer.h"
18
19namespace base {
20class DictionaryValue;
21class ListValue;
22class Value;
23}
24
25namespace chromeos {
26
27// This class manages per-device/global settings.
28class CrosSettings : public base::NonThreadSafe {
29 public:
30  // Manage singleton instance.
31  static void Initialize();
32  static bool IsInitialized();
33  static void Shutdown();
34  static CrosSettings* Get();
35
36  // Helper function to test if the given |path| is a valid cros setting.
37  static bool IsCrosSettings(const std::string& path);
38
39  // Sets |in_value| to given |path| in cros settings.
40  void Set(const std::string& path, const base::Value& in_value);
41
42  // Returns setting value for the given |path|.
43  const base::Value* GetPref(const std::string& path) const;
44
45  // Requests all providers to fetch their values from a trusted store, if they
46  // haven't done so yet. Returns true if the cros settings returned by |this|
47  // are trusted during the current loop cycle; otherwise returns false, and
48  // |callback| will be invoked later when trusted values become available.
49  // PrepareTrustedValues() should be tried again in that case.
50  virtual CrosSettingsProvider::TrustedStatus PrepareTrustedValues(
51      const base::Closure& callback) const;
52
53  // Convenience forms of Set().  These methods will replace any existing
54  // value at that |path|, even if it has a different type.
55  void SetBoolean(const std::string& path, bool in_value);
56  void SetInteger(const std::string& path, int in_value);
57  void SetDouble(const std::string& path, double in_value);
58  void SetString(const std::string& path, const std::string& in_value);
59
60  // Convenience functions for manipulating lists. Note that the following
61  // functions employs a read, modify and write pattern. If underlying settings
62  // provider updates its value asynchronously such as DeviceSettingsProvider,
63  // value cache they read from might not be fresh and multiple calls to those
64  // function would lose data. See http://crbug.com/127215
65  void AppendToList(const std::string& path, const base::Value* value);
66  void RemoveFromList(const std::string& path, const base::Value* value);
67
68  // These are convenience forms of Get().  The value will be retrieved
69  // and the return value will be true if the |path| is valid and the value at
70  // the end of the path can be returned in the form specified.
71  bool GetBoolean(const std::string& path, bool* out_value) const;
72  bool GetInteger(const std::string& path, int* out_value) const;
73  bool GetDouble(const std::string& path, double* out_value) const;
74  bool GetString(const std::string& path, std::string* out_value) const;
75  bool GetList(const std::string& path,
76               const base::ListValue** out_value) const;
77  bool GetDictionary(const std::string& path,
78                     const base::DictionaryValue** out_value) const;
79
80  // Helper function for the whitelist op. Implemented here because we will need
81  // this in a few places. The functions searches for |email| in the pref |path|
82  // It respects whitelists so foo@bar.baz will match *@bar.baz too.
83  bool FindEmailInList(const std::string& path, const std::string& email) const;
84
85  // Adding/removing of providers.
86  bool AddSettingsProvider(CrosSettingsProvider* provider);
87  bool RemoveSettingsProvider(CrosSettingsProvider* provider);
88
89  // If the pref at the given |path| changes, we call the observer's Observe
90  // method with NOTIFICATION_SYSTEM_SETTING_CHANGED.
91  void AddSettingsObserver(const char* path,
92                           content::NotificationObserver* obs);
93  void RemoveSettingsObserver(const char* path,
94                              content::NotificationObserver* obs);
95
96  // Returns the provider that handles settings with the |path| or prefix.
97  CrosSettingsProvider* GetProvider(const std::string& path) const;
98
99 private:
100  friend class CrosSettingsTest;
101
102  CrosSettings();
103  virtual ~CrosSettings();
104
105  // Fires system setting change notification.
106  void FireObservers(const std::string& path);
107
108  // List of ChromeOS system settings providers.
109  std::vector<CrosSettingsProvider*> providers_;
110
111  // A map from settings names to a list of observers. Observers get fired in
112  // the order they are added.
113  typedef ObserverList<content::NotificationObserver, true>
114      NotificationObserverList;
115  typedef base::hash_map<std::string, NotificationObserverList*>
116      SettingsObserverMap;
117  SettingsObserverMap settings_observers_;
118
119  DISALLOW_COPY_AND_ASSIGN(CrosSettings);
120};
121
122// Helper class for tests. Initializes the CrosSettings singleton on
123// construction and tears it down again on destruction.
124class ScopedTestCrosSettings {
125 public:
126  ScopedTestCrosSettings();
127  ~ScopedTestCrosSettings();
128
129 private:
130  DISALLOW_COPY_AND_ASSIGN(ScopedTestCrosSettings);
131};
132
133}  // namespace chromeos
134
135#endif  // CHROME_BROWSER_CHROMEOS_SETTINGS_CROS_SETTINGS_H_
136