| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ |
| 6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ | 6 #define CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | |
| 10 #include <string> | 9 #include <string> |
| 11 #include <vector> | |
| 12 | 10 |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/memory/singleton.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "unicode/timezone.h" | 11 #include "unicode/timezone.h" |
| 17 | 12 |
| 18 namespace chromeos { | 13 namespace chromeos { |
| 19 | 14 |
| 20 // Machine information is represented as a key-value map. | 15 // This interface provides access to Chrome OS system APIs such as the |
| 21 typedef std::map<std::string, std::string> MachineInfo; | |
| 22 | |
| 23 // This class provides access to Chrome OS system APIs such as the | |
| 24 // timezone setting. | 16 // timezone setting. |
| 25 class SystemAccess { | 17 class SystemAccess { |
| 26 public: | 18 public: |
| 27 class Observer { | 19 class Observer { |
| 28 public: | 20 public: |
| 29 // Called when the timezone has changed. |timezone| is non-null. | 21 // Called when the timezone has changed. |timezone| is non-null. |
| 30 virtual void TimezoneChanged(const icu::TimeZone& timezone) = 0; | 22 virtual void TimezoneChanged(const icu::TimeZone& timezone) = 0; |
| 31 }; | 23 }; |
| 32 | 24 |
| 33 static SystemAccess* GetInstance(); | 25 static SystemAccess* GetInstance(); |
| 34 | 26 |
| 35 // Returns the current timezone as an icu::Timezone object. | 27 // Returns the current timezone as an icu::Timezone object. |
| 36 const icu::TimeZone& GetTimezone(); | 28 virtual const icu::TimeZone& GetTimezone() = 0; |
| 37 | 29 |
| 38 // Sets the current timezone. |timezone| must be non-null. | 30 // Sets the current timezone. |timezone| must be non-null. |
| 39 void SetTimezone(const icu::TimeZone& timezone); | 31 virtual void SetTimezone(const icu::TimeZone& timezone) = 0; |
| 40 | 32 |
| 41 // Retrieve the named machine statistic (e.g. "hardware_class"). | 33 // Retrieve the named machine statistic (e.g. "hardware_class"). |
| 42 // This does not update the statistcs. | 34 // This does not update the statistcs. If the |name| is not set, |result| |
| 43 bool GetMachineStatistic(const std::string& name, std::string* result); | 35 // preserves old value. |
| 36 virtual bool GetMachineStatistic(const std::string& name, |
| 37 std::string* result) = 0; |
| 44 | 38 |
| 45 void AddObserver(Observer* observer); | 39 virtual void AddObserver(Observer* observer) = 0; |
| 46 void RemoveObserver(Observer* observer); | 40 virtual void RemoveObserver(Observer* observer) = 0; |
| 47 | 41 |
| 48 private: | 42 protected: |
| 49 friend struct DefaultSingletonTraits<SystemAccess>; | 43 virtual ~SystemAccess() {} |
| 50 | |
| 51 SystemAccess(); | |
| 52 // Updates the machine statistcs by examining the system. | |
| 53 void UpdateMachineStatistics(); | |
| 54 | |
| 55 scoped_ptr<icu::TimeZone> timezone_; | |
| 56 ObserverList<Observer> observers_; | |
| 57 MachineInfo machine_info_; | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(SystemAccess); | |
| 60 }; | |
| 61 | |
| 62 // The parser is used to get machine info as name-value pairs. Defined | |
| 63 // here to be accessable by tests. | |
| 64 class NameValuePairsParser { | |
| 65 public: | |
| 66 typedef std::vector<std::pair<std::string, std::string> > NameValuePairs; | |
| 67 | |
| 68 // The obtained machine info will be written into machine_info. | |
| 69 explicit NameValuePairsParser(MachineInfo* machine_info); | |
| 70 | |
| 71 void AddNameValuePair(const std::string& key, const std::string& value); | |
| 72 | |
| 73 // Executes tool and inserts (key, <output>) into name_value_pairs_. | |
| 74 bool GetSingleValueFromTool(int argc, const char* argv[], | |
| 75 const std::string& key); | |
| 76 // Executes tool, parses the output using ParseNameValuePairs, | |
| 77 // and inserts the results into name_value_pairs_. | |
| 78 bool ParseNameValuePairsFromTool(int argc, const char* argv[], | |
| 79 const std::string& eq, | |
| 80 const std::string& delim); | |
| 81 | |
| 82 private: | |
| 83 // This will parse strings with output in the format: | |
| 84 // <key><EQ><value><DELIM>[<key><EQ><value>][...] | |
| 85 // e.g. ParseNameValuePairs("key1=value1 key2=value2", "=", " ") | |
| 86 bool ParseNameValuePairs(const std::string& in_string, | |
| 87 const std::string& eq, | |
| 88 const std::string& delim); | |
| 89 | |
| 90 MachineInfo* machine_info_; | |
| 91 | |
| 92 DISALLOW_COPY_AND_ASSIGN(NameValuePairsParser); | |
| 93 }; | 44 }; |
| 94 | 45 |
| 95 } // namespace chromeos | 46 } // namespace chromeos |
| 96 | 47 |
| 97 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ | 48 #endif // CHROME_BROWSER_CHROMEOS_SYSTEM_ACCESS_H_ |
| OLD | NEW |