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

Side by Side Diff: base/files/file_path_watcher_browsertest.cc

Issue 369703003: Reduce usage of MessageLoopProxy in base/ (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased Created 6 years, 2 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
« no previous file with comments | « base/files/file_path_watcher.cc ('k') | base/files/file_path_watcher_fsevents.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <aclapi.h> 9 #include <aclapi.h>
10 #elif defined(OS_POSIX) 10 #elif defined(OS_POSIX)
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #endif 12 #endif
13 13
14 #include <set> 14 #include <set>
15 15
16 #include "base/basictypes.h" 16 #include "base/basictypes.h"
17 #include "base/bind.h" 17 #include "base/bind.h"
18 #include "base/bind_helpers.h" 18 #include "base/bind_helpers.h"
19 #include "base/compiler_specific.h" 19 #include "base/compiler_specific.h"
20 #include "base/files/file_path.h" 20 #include "base/files/file_path.h"
21 #include "base/files/file_util.h" 21 #include "base/files/file_util.h"
22 #include "base/files/scoped_temp_dir.h" 22 #include "base/files/scoped_temp_dir.h"
23 #include "base/message_loop/message_loop.h" 23 #include "base/message_loop/message_loop.h"
24 #include "base/message_loop/message_loop_proxy.h"
25 #include "base/run_loop.h" 24 #include "base/run_loop.h"
25 #include "base/sequenced_task_runner.h"
26 #include "base/stl_util.h" 26 #include "base/stl_util.h"
27 #include "base/strings/stringprintf.h" 27 #include "base/strings/stringprintf.h"
28 #include "base/synchronization/waitable_event.h" 28 #include "base/synchronization/waitable_event.h"
29 #include "base/test/test_file_util.h" 29 #include "base/test/test_file_util.h"
30 #include "base/test/test_timeouts.h" 30 #include "base/test/test_timeouts.h"
31 #include "base/thread_task_runner_handle.h"
31 #include "base/threading/thread.h" 32 #include "base/threading/thread.h"
32 #include "testing/gtest/include/gtest/gtest.h" 33 #include "testing/gtest/include/gtest/gtest.h"
33 34
34 namespace base { 35 namespace base {
35 36
36 namespace { 37 namespace {
37 38
38 class TestDelegate; 39 class TestDelegate;
39 40
40 // Aggregates notifications from the test delegates and breaks the message loop 41 // Aggregates notifications from the test delegates and breaks the message loop
41 // the test thread is waiting on once they all came in. 42 // the test thread is waiting on once they all came in.
42 class NotificationCollector 43 class NotificationCollector
43 : public base::RefCountedThreadSafe<NotificationCollector> { 44 : public base::RefCountedThreadSafe<NotificationCollector> {
44 public: 45 public:
45 NotificationCollector() 46 NotificationCollector() : task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
46 : loop_(base::MessageLoopProxy::current()) {}
47 47
48 // Called from the file thread by the delegates. 48 // Called from the file thread by the delegates.
49 void OnChange(TestDelegate* delegate) { 49 void OnChange(TestDelegate* delegate) {
50 loop_->PostTask(FROM_HERE, 50 task_runner_->PostTask(FROM_HERE,
51 base::Bind(&NotificationCollector::RecordChange, this, 51 base::Bind(&NotificationCollector::RecordChange,
52 base::Unretained(delegate))); 52 this,
53 base::Unretained(delegate)));
53 } 54 }
54 55
55 void Register(TestDelegate* delegate) { 56 void Register(TestDelegate* delegate) {
56 delegates_.insert(delegate); 57 delegates_.insert(delegate);
57 } 58 }
58 59
59 void Reset() { 60 void Reset() {
60 signaled_.clear(); 61 signaled_.clear();
61 } 62 }
62 63
63 bool Success() { 64 bool Success() {
64 return signaled_ == delegates_; 65 return signaled_ == delegates_;
65 } 66 }
66 67
67 private: 68 private:
68 friend class base::RefCountedThreadSafe<NotificationCollector>; 69 friend class base::RefCountedThreadSafe<NotificationCollector>;
69 ~NotificationCollector() {} 70 ~NotificationCollector() {}
70 71
71 void RecordChange(TestDelegate* delegate) { 72 void RecordChange(TestDelegate* delegate) {
72 // Warning: |delegate| is Unretained. Do not dereference. 73 // Warning: |delegate| is Unretained. Do not dereference.
73 ASSERT_TRUE(loop_->BelongsToCurrentThread()); 74 ASSERT_TRUE(task_runner_->RunsTasksOnCurrentThread());
74 ASSERT_TRUE(delegates_.count(delegate)); 75 ASSERT_TRUE(delegates_.count(delegate));
75 signaled_.insert(delegate); 76 signaled_.insert(delegate);
76 77
77 // Check whether all delegates have been signaled. 78 // Check whether all delegates have been signaled.
78 if (signaled_ == delegates_) 79 if (signaled_ == delegates_)
79 loop_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure()); 80 task_runner_->PostTask(FROM_HERE, MessageLoop::QuitWhenIdleClosure());
80 } 81 }
81 82
82 // Set of registered delegates. 83 // Set of registered delegates.
83 std::set<TestDelegate*> delegates_; 84 std::set<TestDelegate*> delegates_;
84 85
85 // Set of signaled delegates. 86 // Set of signaled delegates.
86 std::set<TestDelegate*> signaled_; 87 std::set<TestDelegate*> signaled_;
87 88
88 // The loop we should break after all delegates signaled. 89 // The loop we should break after all delegates signaled.
89 scoped_refptr<base::MessageLoopProxy> loop_; 90 scoped_refptr<base::SequencedTaskRunner> task_runner_;
90 }; 91 };
91 92
92 class TestDelegateBase : public SupportsWeakPtr<TestDelegateBase> { 93 class TestDelegateBase : public SupportsWeakPtr<TestDelegateBase> {
93 public: 94 public:
94 TestDelegateBase() {} 95 TestDelegateBase() {}
95 virtual ~TestDelegateBase() {} 96 virtual ~TestDelegateBase() {}
96 97
97 virtual void OnFileChanged(const FilePath& path, bool error) = 0; 98 virtual void OnFileChanged(const FilePath& path, bool error) = 0;
98 99
99 private: 100 private:
(...skipping 763 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false)); 864 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, false));
864 ASSERT_TRUE(WaitForEvents()); 865 ASSERT_TRUE(WaitForEvents());
865 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true)); 866 ASSERT_TRUE(ChangeFilePermissions(test_dir1, Execute, true));
866 DeleteDelegateOnFileThread(delegate.release()); 867 DeleteDelegateOnFileThread(delegate.release());
867 } 868 }
868 869
869 #endif // OS_MACOSX 870 #endif // OS_MACOSX
870 } // namespace 871 } // namespace
871 872
872 } // namespace base 873 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_path_watcher.cc ('k') | base/files/file_path_watcher_fsevents.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698