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 "device/time_zone_monitor/time_zone_monitor.h" | 5 #include "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 <vector> | 11 #include <vector> |
11 | 12 |
12 #include "base/bind.h" | 13 #include "base/bind.h" |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/files/file_path_watcher.h" | 15 #include "base/files/file_path_watcher.h" |
15 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
16 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
17 #include "base/sequenced_task_runner.h" | 19 #include "base/sequenced_task_runner.h" |
18 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
19 #include "base/threading/thread_restrictions.h" | 21 #include "base/threading/thread_restrictions.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 22 #include "base/threading/thread_task_runner_handle.h" |
21 #include "build/build_config.h" | 23 #include "build/build_config.h" |
22 | 24 |
23 #if !defined(OS_CHROMEOS) | 25 #if !defined(OS_CHROMEOS) |
24 | 26 |
25 namespace device { | 27 namespace device { |
(...skipping 21 matching lines...) Expand all Loading... |
47 // FilePathWatcher needs to run on the FILE thread, but TimeZoneMonitor runs | 49 // FilePathWatcher needs to run on the FILE thread, but TimeZoneMonitor runs |
48 // on the UI thread. TimeZoneMonitorLinuxImpl is the bridge between these | 50 // on the UI thread. TimeZoneMonitorLinuxImpl is the bridge between these |
49 // threads. | 51 // threads. |
50 class TimeZoneMonitorLinuxImpl | 52 class TimeZoneMonitorLinuxImpl |
51 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { | 53 : public base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl> { |
52 public: | 54 public: |
53 explicit TimeZoneMonitorLinuxImpl( | 55 explicit TimeZoneMonitorLinuxImpl( |
54 TimeZoneMonitorLinux* owner, | 56 TimeZoneMonitorLinux* owner, |
55 scoped_refptr<base::SequencedTaskRunner> file_task_runner) | 57 scoped_refptr<base::SequencedTaskRunner> file_task_runner) |
56 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), | 58 : base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>(), |
57 file_path_watchers_(), | |
58 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 59 main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
59 file_task_runner_(file_task_runner), | 60 file_task_runner_(file_task_runner), |
60 owner_(owner) { | 61 owner_(owner) { |
61 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 62 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
62 file_task_runner_->PostTask( | 63 file_task_runner_->PostTask( |
63 FROM_HERE, | 64 FROM_HERE, |
64 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); | 65 base::Bind(&TimeZoneMonitorLinuxImpl::StartWatchingOnFileThread, this)); |
65 } | 66 } |
66 | 67 |
67 void StopWatching() { | 68 void StopWatching() { |
68 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 69 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
69 owner_ = NULL; | 70 owner_ = NULL; |
70 file_task_runner_->PostTask( | 71 file_task_runner_->PostTask( |
71 FROM_HERE, | 72 FROM_HERE, |
72 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); | 73 base::Bind(&TimeZoneMonitorLinuxImpl::StopWatchingOnFileThread, this)); |
73 } | 74 } |
74 | 75 |
75 private: | 76 private: |
76 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; | 77 friend class base::RefCountedThreadSafe<TimeZoneMonitorLinuxImpl>; |
77 | 78 |
78 ~TimeZoneMonitorLinuxImpl() { | 79 ~TimeZoneMonitorLinuxImpl() { |
79 DCHECK(!owner_); | 80 DCHECK(!owner_); |
80 base::STLDeleteElements(&file_path_watchers_); | |
81 } | 81 } |
82 | 82 |
83 void StartWatchingOnFileThread() { | 83 void StartWatchingOnFileThread() { |
84 base::ThreadRestrictions::AssertIOAllowed(); | 84 base::ThreadRestrictions::AssertIOAllowed(); |
85 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 85 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
86 | 86 |
87 // There is no true standard for where time zone information is actually | 87 // There is no true standard for where time zone information is actually |
88 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older | 88 // stored. glibc uses /etc/localtime, uClibc uses /etc/TZ, and some older |
89 // systems store the name of the time zone file within /usr/share/zoneinfo | 89 // systems store the name of the time zone file within /usr/share/zoneinfo |
90 // in /etc/timezone. Different libraries and custom builds may mean that | 90 // in /etc/timezone. Different libraries and custom builds may mean that |
91 // still more paths are used. Just watch all three of these paths, because | 91 // still more paths are used. Just watch all three of these paths, because |
92 // false positives are harmless, assuming the false positive rate is | 92 // false positives are harmless, assuming the false positive rate is |
93 // reasonable. | 93 // reasonable. |
94 const char* const kFilesToWatch[] = { | 94 const char* const kFilesToWatch[] = { |
95 "/etc/localtime", "/etc/timezone", "/etc/TZ", | 95 "/etc/localtime", "/etc/timezone", "/etc/TZ", |
96 }; | 96 }; |
97 | 97 |
98 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { | 98 for (size_t index = 0; index < arraysize(kFilesToWatch); ++index) { |
99 file_path_watchers_.push_back(new base::FilePathWatcher()); | 99 file_path_watchers_.push_back(base::MakeUnique<base::FilePathWatcher>()); |
100 file_path_watchers_.back()->Watch( | 100 file_path_watchers_.back()->Watch( |
101 base::FilePath(kFilesToWatch[index]), false, | 101 base::FilePath(kFilesToWatch[index]), false, |
102 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); | 102 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChanged, this)); |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 void StopWatchingOnFileThread() { | 106 void StopWatchingOnFileThread() { |
107 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 107 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
108 base::STLDeleteElements(&file_path_watchers_); | 108 file_path_watchers_.clear(); |
109 } | 109 } |
110 | 110 |
111 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { | 111 void OnTimeZoneFileChanged(const base::FilePath& path, bool error) { |
112 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); | 112 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); |
113 main_task_runner_->PostTask( | 113 main_task_runner_->PostTask( |
114 FROM_HERE, | 114 FROM_HERE, |
115 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChangedOnUIThread, | 115 base::Bind(&TimeZoneMonitorLinuxImpl::OnTimeZoneFileChangedOnUIThread, |
116 this)); | 116 this)); |
117 } | 117 } |
118 | 118 |
119 void OnTimeZoneFileChangedOnUIThread() { | 119 void OnTimeZoneFileChangedOnUIThread() { |
120 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 120 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
121 if (owner_) { | 121 if (owner_) { |
122 owner_->NotifyClientsFromImpl(); | 122 owner_->NotifyClientsFromImpl(); |
123 } | 123 } |
124 } | 124 } |
125 | 125 |
126 std::vector<base::FilePathWatcher*> file_path_watchers_; | 126 std::vector<std::unique_ptr<base::FilePathWatcher>> file_path_watchers_; |
127 | 127 |
128 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | 128 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; |
129 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; | 129 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; |
130 TimeZoneMonitorLinux* owner_; | 130 TimeZoneMonitorLinux* owner_; |
131 | 131 |
132 DISALLOW_COPY_AND_ASSIGN(TimeZoneMonitorLinuxImpl); | 132 DISALLOW_COPY_AND_ASSIGN(TimeZoneMonitorLinuxImpl); |
133 }; | 133 }; |
134 | 134 |
135 } // namespace | 135 } // namespace |
136 | 136 |
(...skipping 27 matching lines...) Expand all Loading... |
164 // static | 164 // static |
165 std::unique_ptr<TimeZoneMonitor> TimeZoneMonitor::Create( | 165 std::unique_ptr<TimeZoneMonitor> TimeZoneMonitor::Create( |
166 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { | 166 scoped_refptr<base::SequencedTaskRunner> file_task_runner) { |
167 return std::unique_ptr<TimeZoneMonitor>( | 167 return std::unique_ptr<TimeZoneMonitor>( |
168 new TimeZoneMonitorLinux(file_task_runner)); | 168 new TimeZoneMonitorLinux(file_task_runner)); |
169 } | 169 } |
170 | 170 |
171 } // namespace device | 171 } // namespace device |
172 | 172 |
173 #endif // !OS_CHROMEOS | 173 #endif // !OS_CHROMEOS |
OLD | NEW |