1// Copyright 2014 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// Chromecast-specific configurations retrieved from and stored into a given
6// configuration file.
7
8#ifndef CHROMECAST_COMMON_CHROMECAST_CONFIG_H_
9#define CHROMECAST_COMMON_CHROMECAST_CONFIG_H_
10
11#include <string>
12
13#include "base/files/file_path.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/scoped_ptr.h"
16#include "base/prefs/pref_service.h"
17#include "base/threading/sequenced_worker_pool.h"
18#include "base/threading/thread_checker.h"
19
20class PrefRegistrySimple;
21
22namespace chromecast {
23
24// Manages configuration for Chromecast via PrefService.
25// It uses JsonPrefStore internally and/so the format of config file is same to
26// that of JsonPrefStore.
27// It is NOT thread-safe; all functions must be run on the same thread as
28// the object is created.
29class ChromecastConfig {
30 public:
31  // Creates new singleton instance of ChromecastConfig.
32  static void Create(PrefRegistrySimple* registry);
33
34  // Returns the singleton instance of ChromecastConfig.
35  static ChromecastConfig* GetInstance();
36
37  // Saves configs into configuration file.
38  void Save() const;
39
40  // Returns string value for |key|, if present.
41  const std::string GetValue(const std::string& key) const;
42
43  // Returns integer value for |key|, if present.
44  const int GetIntValue(const std::string& key) const;
45
46  // Sets new string value for |key|.
47  void SetValue(const std::string& key, const std::string& value) const;
48
49  // Sets new int value for |key|.
50  void SetIntValue(const std::string& key, int value) const;
51
52  // Whether or not a value has been set for |key|.
53  bool HasValue(const std::string& key) const;
54
55  scoped_refptr<base::SequencedWorkerPool> worker_pool() const {
56    return worker_pool_;
57  }
58
59  PrefService* pref_service() const { return pref_service_.get(); }
60
61 private:
62  ChromecastConfig();
63  ~ChromecastConfig();
64
65  // Loads configs from config file. Returns true if successful.
66  bool Load(PrefRegistrySimple* registry);
67
68  // Registers any needed preferences for the current platform.
69  void RegisterPlatformPrefs(PrefRegistrySimple* registry);
70
71  // Global singleton instance.
72  static ChromecastConfig* g_instance_;
73
74  const base::FilePath config_path_;
75  const scoped_refptr<base::SequencedWorkerPool> worker_pool_;
76  scoped_ptr<PrefService> pref_service_;
77
78  base::ThreadChecker thread_checker_;
79
80  DISALLOW_COPY_AND_ASSIGN(ChromecastConfig);
81};
82
83}  // namespace chromecast
84
85#endif  // CHROMECAST_COMMON_CHROMECAST_CONFIG_H_
86