OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/files/file_path_watcher.h" | 5 #include "base/files/file_path_watcher.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 void Reset() { | 59 void Reset() { |
60 signaled_.clear(); | 60 signaled_.clear(); |
61 } | 61 } |
62 | 62 |
63 bool Success() { | 63 bool Success() { |
64 return signaled_ == delegates_; | 64 return signaled_ == delegates_; |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
| 68 friend class base::RefCountedThreadSafe<NotificationCollector>; |
| 69 ~NotificationCollector() {} |
| 70 |
68 void RecordChange(TestDelegate* delegate) { | 71 void RecordChange(TestDelegate* delegate) { |
69 ASSERT_TRUE(loop_->BelongsToCurrentThread()); | 72 ASSERT_TRUE(loop_->BelongsToCurrentThread()); |
70 ASSERT_TRUE(delegates_.count(delegate)); | 73 ASSERT_TRUE(delegates_.count(delegate)); |
71 signaled_.insert(delegate); | 74 signaled_.insert(delegate); |
72 | 75 |
73 // Check whether all delegates have been signaled. | 76 // Check whether all delegates have been signaled. |
74 if (signaled_ == delegates_) | 77 if (signaled_ == delegates_) |
75 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 78 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
76 } | 79 } |
77 | 80 |
(...skipping 22 matching lines...) Expand all Loading... |
100 } | 103 } |
101 | 104 |
102 virtual void OnFilePathChanged(const FilePath&) { | 105 virtual void OnFilePathChanged(const FilePath&) { |
103 collector_->OnChange(this); | 106 collector_->OnChange(this); |
104 } | 107 } |
105 | 108 |
106 virtual void OnFilePathError(const FilePath& path) { | 109 virtual void OnFilePathError(const FilePath& path) { |
107 ADD_FAILURE() << "Error " << path.value(); | 110 ADD_FAILURE() << "Error " << path.value(); |
108 } | 111 } |
109 | 112 |
| 113 protected: |
| 114 virtual ~TestDelegate() {} |
| 115 |
110 private: | 116 private: |
111 scoped_refptr<NotificationCollector> collector_; | 117 scoped_refptr<NotificationCollector> collector_; |
112 | 118 |
113 DISALLOW_COPY_AND_ASSIGN(TestDelegate); | 119 DISALLOW_COPY_AND_ASSIGN(TestDelegate); |
114 }; | 120 }; |
115 | 121 |
116 void SetupWatchCallback(const FilePath& target, | 122 void SetupWatchCallback(const FilePath& target, |
117 FilePathWatcher* watcher, | 123 FilePathWatcher* watcher, |
118 FilePathWatcher::Delegate* delegate, | 124 FilePathWatcher::Delegate* delegate, |
119 bool* result, | 125 bool* result, |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 loop_(loop) { | 248 loop_(loop) { |
243 } | 249 } |
244 | 250 |
245 virtual void OnFilePathChanged(const FilePath& path) { | 251 virtual void OnFilePathChanged(const FilePath& path) { |
246 watcher_.reset(); | 252 watcher_.reset(); |
247 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 253 loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
248 } | 254 } |
249 | 255 |
250 scoped_ptr<FilePathWatcher> watcher_; | 256 scoped_ptr<FilePathWatcher> watcher_; |
251 MessageLoop* loop_; | 257 MessageLoop* loop_; |
| 258 |
| 259 private: |
| 260 virtual ~Deleter() {} |
252 }; | 261 }; |
253 | 262 |
254 // Verify that deleting a watcher during the callback doesn't crash. | 263 // Verify that deleting a watcher during the callback doesn't crash. |
255 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { | 264 TEST_F(FilePathWatcherTest, DeleteDuringNotify) { |
256 FilePathWatcher* watcher = new FilePathWatcher; | 265 FilePathWatcher* watcher = new FilePathWatcher; |
257 // Takes ownership of watcher. | 266 // Takes ownership of watcher. |
258 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); | 267 scoped_refptr<Deleter> deleter(new Deleter(watcher, &loop_)); |
259 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); | 268 ASSERT_TRUE(SetupWatch(test_file(), watcher, deleter.get())); |
260 | 269 |
261 ASSERT_TRUE(WriteFile(test_file(), "content")); | 270 ASSERT_TRUE(WriteFile(test_file(), "content")); |
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); | 775 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); |
767 ASSERT_TRUE(WaitForEvents()); | 776 ASSERT_TRUE(WaitForEvents()); |
768 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); | 777 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); |
769 } | 778 } |
770 | 779 |
771 #endif // OS_MACOSX | 780 #endif // OS_MACOSX |
772 } // namespace | 781 } // namespace |
773 | 782 |
774 } // namespace files | 783 } // namespace files |
775 } // namespace base | 784 } // namespace base |
OLD | NEW |