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 "services/device/time_zone_monitor/time_zone_monitor.h" | 5 #include "services/device/time_zone_monitor/time_zone_monitor.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 class TimeZoneMonitorLinuxImpl | 52 class TimeZoneMonitorLinuxImpl |
53 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { | 53 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { |
54 public: | 54 public: |
55 explicit TimeZoneMonitorLinuxImpl( | 55 explicit TimeZoneMonitorLinuxImpl( |
56 TimeZoneMonitorLinux* owner, | 56 TimeZoneMonitorLinux* owner, |
57 scoped_refptr<base::SequencedTaskRunner> file_task_runner) | 57 scoped_refptr<base::SequencedTaskRunner> file_task_runner) |
58 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), | 58 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), |
59 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 59 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
60 file_task_runner_(file_task_runner), | 60 file_task_runner_(file_task_runner), |
61 owner_(owner) { | 61 owner_(owner) { |
62 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 62 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
63 file_task_runner_->PostTask( | 63 file_task_runner_->PostTask( |
64 FROM_HERE, | 64 FROM_HERE, |
65 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); | 65 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); |
66 } | 66 } |
67 | 67 |
68 void StopWatching() { | 68 void StopWatching() { |
69 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 69 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
70 owner_ = NULL; | 70 owner_ = NULL; |
71 file_task_runner_->PostTask( | 71 file_task_runner_->PostTask( |
72 FROM_HERE, | 72 FROM_HERE, |
73 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); | 73 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); |
74 } | 74 } |
75 | 75 |
76 private: | 76 private: |
77 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; | 77 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; |
78 | 78 |
79 ~TimeZoneMonitorLinuxImpl() { DCHECK(!owner_); } | 79 ~TimeZoneMonitorLinuxImpl() { DCHECK(!owner_); } |
80 | 80 |
81 void StartWatchingOnFileThread() { | 81 void StartWatchingOnFileThread() { |
82 base::ThreadRestrictions::AssertIOAllowed(); | 82 base::ThreadRestrictions::AssertIOAllowed(); |
83 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 83 DCHECK(file_task_runner_->RunsTasksInCurrentSequence()); |
84 | 84 |
85 // There is no true standard for where time zone information is actually | 85 // There is no true standard for where time zone information is actually |
86 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older | 86 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older |
87 // systems store the name of the time zone file within /usr/share/zoneinfo | 87 // systems store the name of the time zone file within /usr/share/zoneinfo |
88 // in /etc/timezone. Different libraries and custom builds may mean that | 88 // in /etc/timezone. Different libraries and custom builds may mean that |
89 // still more paths are used. Just watch all three of these paths, because | 89 // still more paths are used. Just watch all three of these paths, because |
90 // false positives are harmless, assuming the false positive rate is | 90 // false positives are harmless, assuming the false positive rate is |
91 // reasonable. | 91 // reasonable. |
92 const char* const kFilesToWatch[] = { | 92 const char* const kFilesToWatch[] = { |
93 "/etc/localtime", "/etc/timezone", "/etc/TZ", | 93 "/etc/localtime", "/etc/timezone", "/etc/TZ", |
94 }; | 94 }; |
95 | 95 |
96 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { | 96 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { |
97 file_path_watchers_.push_back(base::MakeUnique<base::FilePathWatcher>()); | 97 file_path_watchers_.push_back(base::MakeUnique<base::FilePathWatcher>()); |
98 file_path_watchers_.back()->Watch( | 98 file_path_watchers_.back()->Watch( |
99 base::FilePath(kFilesToWatch[index]), false, | 99 base::FilePath(kFilesToWatch[index]), false, |
100 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); | 100 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); |
101 } | 101 } |
102 } | 102 } |
103 | 103 |
104 void StopWatchingOnFileThread() { | 104 void StopWatchingOnFileThread() { |
105 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 105 DCHECK(file_task_runner_->RunsTasksInCurrentSequence()); |
106 file_path_watchers_.clear(); | 106 file_path_watchers_.clear(); |
107 } | 107 } |
108 | 108 |
109 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { | 109 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { |
110 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 110 DCHECK(file_task_runner_->RunsTasksInCurrentSequence()); |
111 main_task_runner_->PostTask( | 111 main_task_runner_->PostTask( |
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(main_task_runner_->RunsTasksOnCurrentThread()); | 118 DCHECK(main_task_runner_->RunsTasksInCurrentSequence()); |
119 if (owner_) { | 119 if (owner_) { |
120 owner_->NotifyClientsFromImpl(); | 120 owner_->NotifyClientsFromImpl(); |
121 } | 121 } |
122 } | 122 } |
123 | 123 |
124 std::vector<std::unique_ptr<base::FilePathWatcher>> file_path_watchers_; | 124 std::vector<std::unique_ptr<base::FilePathWatcher>> file_path_watchers_; |
125 | 125 |
126 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | 126 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; |
127 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 127 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
128 TimeZoneMonitorLinux* owner_; | 128 TimeZoneMonitorLinux* owner_; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 // static | 162 // static |
163 std::unique_ptr<TimeZoneMonitor> TimeZoneMonitor::Create( | 163 std::unique_ptr<TimeZoneMonitor> TimeZoneMonitor::Create( |
164 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { | 164 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { |
165 return std::unique_ptr<TimeZoneMonitor>( | 165 return std::unique_ptr<TimeZoneMonitor>( |
166 new TimeZoneMonitorLinux(file_task_runner)); | 166 new TimeZoneMonitorLinux(file_task_runner)); |
167 } | 167 } |
168 | 168 |
169 } // namespace device | 169 } // namespace device |
170 | 170 |
171 #endif // !OS_CHROMEOS | 171 #endif // !OS_CHROMEOS |
OLD | NEW |