OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 #include "content/browser/time_zone_monitor.h" | 5 #include "content/browser/time_zone_monitor.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 33 matching lines...) Loading... |
44 // FilePathWatcher needs to run on the FILE thread, but TimeZoneMonitor runs | 44 // FilePathWatcher needs to run on the FILE thread, but TimeZoneMonitor runs |
45 // on the UI thread. TimeZoneMonitorLinuxImpl is the bridge between these | 45 // on the UI thread. TimeZoneMonitorLinuxImpl is the bridge between these |
46 // threads. | 46 // threads. |
47 class TimeZoneMonitorLinuxImpl | 47 class TimeZoneMonitorLinuxImpl |
48 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { | 48 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { |
49 public: | 49 public: |
50 explicit TimeZoneMonitorLinuxImpl(TimeZoneMonitorLinux* owner) | 50 explicit TimeZoneMonitorLinuxImpl(TimeZoneMonitorLinux* owner) |
51 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), | 51 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), |
52 file_path_watchers_(), | 52 file_path_watchers_(), |
53 owner_(owner) { | 53 owner_(owner) { |
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
55 BrowserThread::PostTask( | 55 BrowserThread::PostTask( |
56 BrowserThread::FILE, | 56 BrowserThread::FILE, |
57 FROM_HERE, | 57 FROM_HERE, |
58 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); | 58 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); |
59 } | 59 } |
60 | 60 |
61 void StopWatching() { | 61 void StopWatching() { |
62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 62 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
63 owner_ = NULL; | 63 owner_ = NULL; |
64 BrowserThread::PostTask( | 64 BrowserThread::PostTask( |
65 BrowserThread::FILE, | 65 BrowserThread::FILE, |
66 FROM_HERE, | 66 FROM_HERE, |
67 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); | 67 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); |
68 } | 68 } |
69 | 69 |
70 private: | 70 private: |
71 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; | 71 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; |
72 | 72 |
73 ~TimeZoneMonitorLinuxImpl() { | 73 ~TimeZoneMonitorLinuxImpl() { |
74 DCHECK(!owner_); | 74 DCHECK(!owner_); |
75 STLDeleteElements(&file_path_watchers_); | 75 STLDeleteElements(&file_path_watchers_); |
76 } | 76 } |
77 | 77 |
78 void StartWatchingOnFileThread() { | 78 void StartWatchingOnFileThread() { |
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 79 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
80 | 80 |
81 // There is no true standard for where time zone information is actually | 81 // There is no true standard for where time zone information is actually |
82 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older | 82 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older |
83 // systems store the name of the time zone file within /usr/share/zoneinfo | 83 // systems store the name of the time zone file within /usr/share/zoneinfo |
84 // in /etc/timezone. Different libraries and custom builds may mean that | 84 // in /etc/timezone. Different libraries and custom builds may mean that |
85 // still more paths are used. Just watch all three of these paths, because | 85 // still more paths are used. Just watch all three of these paths, because |
86 // false positives are harmless, assuming the false positive rate is | 86 // false positives are harmless, assuming the false positive rate is |
87 // reasonable. | 87 // reasonable. |
88 const char* kFilesToWatch[] = { | 88 const char* kFilesToWatch[] = { |
89 "/etc/localtime", | 89 "/etc/localtime", |
90 "/etc/timezone", | 90 "/etc/timezone", |
91 "/etc/TZ", | 91 "/etc/TZ", |
92 }; | 92 }; |
93 | 93 |
94 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { | 94 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { |
95 file_path_watchers_.push_back(new base::FilePathWatcher()); | 95 file_path_watchers_.push_back(new base::FilePathWatcher()); |
96 file_path_watchers_.back()->Watch( | 96 file_path_watchers_.back()->Watch( |
97 base::FilePath(kFilesToWatch[index]), | 97 base::FilePath(kFilesToWatch[index]), |
98 false, | 98 false, |
99 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); | 99 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); |
100 } | 100 } |
101 } | 101 } |
102 | 102 |
103 void StopWatchingOnFileThread() { | 103 void StopWatchingOnFileThread() { |
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 104 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
105 STLDeleteElements(&file_path_watchers_); | 105 STLDeleteElements(&file_path_watchers_); |
106 } | 106 } |
107 | 107 |
108 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { | 108 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 109 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
110 BrowserThread::PostTask( | 110 BrowserThread::PostTask( |
111 BrowserThread::UI, | 111 BrowserThread::UI, |
112 FROM_HERE, | 112 FROM_HERE, |
113 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChangedOnUIThread, | 113 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChangedOnUIThread, |
114 this)); | 114 this)); |
115 } | 115 } |
116 | 116 |
117 void OnTimeZoneFileChangedOnUIThread() { | 117 void OnTimeZoneFileChangedOnUIThread() { |
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 118 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
119 if (owner_) { | 119 if (owner_) { |
120 owner_->NotifyRenderersFromImpl(); | 120 owner_->NotifyRenderersFromImpl(); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 std::vector<base::FilePathWatcher*> file_path_watchers_; | 124 std::vector<base::FilePathWatcher*> file_path_watchers_; |
125 TimeZoneMonitorLinux* owner_; | 125 TimeZoneMonitorLinux* owner_; |
126 | 126 |
127 DISALLOW_COPY_AND_ASSIGN(TimeZoneMonitorLinuxImpl); | 127 DISALLOW_COPY_AND_ASSIGN(TimeZoneMonitorLinuxImpl); |
128 }; | 128 }; |
(...skipping 28 matching lines...) Loading... |
157 } | 157 } |
158 | 158 |
159 // static | 159 // static |
160 scoped_ptr<TimeZoneMonitor> TimeZoneMonitor::Create() { | 160 scoped_ptr<TimeZoneMonitor> TimeZoneMonitor::Create() { |
161 return scoped_ptr<TimeZoneMonitor>(new TimeZoneMonitorLinux()); | 161 return scoped_ptr<TimeZoneMonitor>(new TimeZoneMonitorLinux()); |
162 } | 162 } |
163 | 163 |
164 } // namespace content | 164 } // namespace content |
165 | 165 |
166 #endif // !OS_CHROMEOS | 166 #endif // !OS_CHROMEOS |
OLD | NEW |