Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(278)

Side by Side Diff: chrome/browser/chromeos/system/timezone_settings.cc

Issue 7324017: Split SystemAccess into TimezoneSettings, StatisticsProvider, and SyslogsProvider. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/system/timezone_settings.h"
6
7 #include "base/file_path.h"
8 #include "base/file_util.h"
9 #include "base/logging.h"
10 #include "base/observer_list.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/singleton.h"
13 #include "base/string_util.h"
14 #include "base/utf_string_conversions.h"
15
16 namespace chromeos {
17 namespace system {
18
19 namespace {
20
21 // The filepath to the timezone file that symlinks to the actual timezone file.
22 const char kTimezoneSymlink[] = "/var/lib/timezone/localtime";
23 const char kTimezoneSymlink2[] = "/var/lib/timezone/localtime2";
24
25 // The directory that contains all the timezone files. So for timezone
26 // "US/Pacific", the actual timezone file is: "/usr/share/zoneinfo/US/Pacific"
27 const char kTimezoneFilesDir[] = "/usr/share/zoneinfo/";
28
29 // Fallback time zone ID used in case of an unexpected error.
30 const char kFallbackTimeZoneId[] = "America/Los_Angeles";
31
32 } // namespace
33
34 class TimezoneSettingsImpl : public TimezoneSettings {
35 public:
36 // TimezoneSettings.implementation:
37 virtual const icu::TimeZone& GetTimezone();
38 virtual void SetTimezone(const icu::TimeZone& timezone);
39 virtual void AddObserver(Observer* observer);
40 virtual void RemoveObserver(Observer* observer);
41
42 static TimezoneSettingsImpl* GetInstance();
43
44 private:
45 friend struct DefaultSingletonTraits<TimezoneSettingsImpl>;
46
47 TimezoneSettingsImpl();
48
49 scoped_ptr<icu::TimeZone> timezone_;
50 ObserverList<Observer> observers_;
51
52 DISALLOW_COPY_AND_ASSIGN(TimezoneSettingsImpl);
53 };
54
55 std::string GetTimezoneIDAsString() {
56 // Look at kTimezoneSymlink, see which timezone we are symlinked to.
57 char buf[256];
58 const ssize_t len = readlink(kTimezoneSymlink, buf,
59 sizeof(buf)-1);
60 if (len == -1) {
61 LOG(ERROR) << "GetTimezoneID: Cannot read timezone symlink "
62 << kTimezoneSymlink;
63 return std::string();
64 }
65
66 std::string timezone(buf, len);
67 // Remove kTimezoneFilesDir from the beginning.
68 if (timezone.find(kTimezoneFilesDir) != 0) {
69 LOG(ERROR) << "GetTimezoneID: Timezone symlink is wrong "
70 << timezone;
71 return std::string();
72 }
73
74 return timezone.substr(strlen(kTimezoneFilesDir));
75 }
76
77 void SetTimezoneIDFromString(const std::string& id) {
78 // Change the kTimezoneSymlink symlink to the path for this timezone.
79 // We want to do this in an atomic way. So we are going to create the symlink
80 // at kTimezoneSymlink2 and then move it to kTimezoneSymlink
81
82 FilePath timezone_symlink(kTimezoneSymlink);
83 FilePath timezone_symlink2(kTimezoneSymlink2);
84 FilePath timezone_file(kTimezoneFilesDir + id);
85
86 // Make sure timezone_file exists.
87 if (!file_util::PathExists(timezone_file)) {
88 LOG(ERROR) << "SetTimezoneID: Cannot find timezone file "
89 << timezone_file.value();
90 return;
91 }
92
93 // Delete old symlink2 if it exists.
94 file_util::Delete(timezone_symlink2, false);
95
96 // Create new symlink2.
97 if (symlink(timezone_file.value().c_str(),
98 timezone_symlink2.value().c_str()) == -1) {
99 LOG(ERROR) << "SetTimezoneID: Unable to create symlink "
100 << timezone_symlink2.value() << " to " << timezone_file.value();
101 return;
102 }
103
104 // Move symlink2 to symlink.
105 if (!file_util::ReplaceFile(timezone_symlink2, timezone_symlink)) {
106 LOG(ERROR) << "SetTimezoneID: Unable to move symlink "
107 << timezone_symlink2.value() << " to "
108 << timezone_symlink.value();
109 }
110 }
111
112 const icu::TimeZone& TimezoneSettingsImpl::GetTimezone() {
113 return *timezone_.get();
114 }
115
116 void TimezoneSettingsImpl::SetTimezone(const icu::TimeZone& timezone) {
117 timezone_.reset(timezone.clone());
118 icu::UnicodeString unicode;
119 timezone.getID(unicode);
120 std::string id;
121 UTF16ToUTF8(unicode.getBuffer(), unicode.length(), &id);
122 VLOG(1) << "Setting timezone to " << id;
123 SetTimezoneIDFromString(id);
124 icu::TimeZone::setDefault(timezone);
125 FOR_EACH_OBSERVER(Observer, observers_, TimezoneChanged(timezone));
126 }
127
128 void TimezoneSettingsImpl::AddObserver(Observer* observer) {
129 observers_.AddObserver(observer);
130 }
131
132 void TimezoneSettingsImpl::RemoveObserver(Observer* observer) {
133 observers_.RemoveObserver(observer);
134 }
135
136 TimezoneSettingsImpl::TimezoneSettingsImpl() {
137 // Get Timezone
138 std::string id = GetTimezoneIDAsString();
139 if (id.empty()) {
140 id = kFallbackTimeZoneId;
141 LOG(ERROR) << "Got an empty string for timezone, default to " << id;
142 }
143 icu::TimeZone* timezone =
144 icu::TimeZone::createTimeZone(icu::UnicodeString::fromUTF8(id));
145 timezone_.reset(timezone);
146 icu::TimeZone::setDefault(*timezone);
147 VLOG(1) << "Timezone is " << id;
148 }
149
150 TimezoneSettingsImpl* TimezoneSettingsImpl::GetInstance() {
151 return Singleton<TimezoneSettingsImpl,
152 DefaultSingletonTraits<TimezoneSettingsImpl> >::get();
153 }
154
155 TimezoneSettings* TimezoneSettings::GetInstance() {
156 return TimezoneSettingsImpl::GetInstance();
157 }
158
159 } // namespace system
160 } // namespace chromeos
161
162 // Allows InvokeLater without adding refcounting. TimezoneSettingsImpl is a
163 // Singleton and won't be deleted until it's last InvokeLater is run.
164 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::system::TimezoneSettingsImpl);
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/system/timezone_settings.h ('k') | chrome/browser/chromeos/system_access.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698