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

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

Issue 1113953002: Revert of base: Remove use of MessageLoopProxy (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
« no previous file with comments | « base/files/file_path_watcher_unittest.cc ('k') | base/files/file_proxy.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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/files/file.h" 8 #include "base/files/file.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/thread_task_runner_handle.h" 13 #include "base/message_loop/message_loop_proxy.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "base/win/object_watcher.h" 15 #include "base/win/object_watcher.h"
16 16
17 namespace base { 17 namespace base {
18 18
19 namespace { 19 namespace {
20 20
21 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate, 21 class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
22 public base::win::ObjectWatcher::Delegate, 22 public base::win::ObjectWatcher::Delegate,
23 public MessageLoop::DestructionObserver { 23 public MessageLoop::DestructionObserver {
(...skipping 26 matching lines...) Expand all
50 static bool SetupWatchHandle(const FilePath& dir, 50 static bool SetupWatchHandle(const FilePath& dir,
51 bool recursive, 51 bool recursive,
52 HANDLE* handle) WARN_UNUSED_RESULT; 52 HANDLE* handle) WARN_UNUSED_RESULT;
53 53
54 // (Re-)Initialize the watch handle. 54 // (Re-)Initialize the watch handle.
55 bool UpdateWatch() WARN_UNUSED_RESULT; 55 bool UpdateWatch() WARN_UNUSED_RESULT;
56 56
57 // Destroy the watch handle. 57 // Destroy the watch handle.
58 void DestroyWatch(); 58 void DestroyWatch();
59 59
60 // Cleans up and stops observing the |task_runner_| thread. 60 // Cleans up and stops observing the |message_loop_| thread.
61 void CancelOnMessageLoopThread() override; 61 void CancelOnMessageLoopThread() override;
62 62
63 // Callback to notify upon changes. 63 // Callback to notify upon changes.
64 FilePathWatcher::Callback callback_; 64 FilePathWatcher::Callback callback_;
65 65
66 // Path we're supposed to watch (passed to callback). 66 // Path we're supposed to watch (passed to callback).
67 FilePath target_; 67 FilePath target_;
68 68
69 // Handle for FindFirstChangeNotification. 69 // Handle for FindFirstChangeNotification.
70 HANDLE handle_; 70 HANDLE handle_;
(...skipping 13 matching lines...) Expand all
84 Time first_notification_; 84 Time first_notification_;
85 85
86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl); 86 DISALLOW_COPY_AND_ASSIGN(FilePathWatcherImpl);
87 }; 87 };
88 88
89 bool FilePathWatcherImpl::Watch(const FilePath& path, 89 bool FilePathWatcherImpl::Watch(const FilePath& path,
90 bool recursive, 90 bool recursive,
91 const FilePathWatcher::Callback& callback) { 91 const FilePathWatcher::Callback& callback) {
92 DCHECK(target_.value().empty()); // Can only watch one path. 92 DCHECK(target_.value().empty()); // Can only watch one path.
93 93
94 set_task_runner(ThreadTaskRunnerHandle::Get()); 94 set_message_loop(MessageLoopProxy::current());
95 callback_ = callback; 95 callback_ = callback;
96 target_ = path; 96 target_ = path;
97 recursive_watch_ = recursive; 97 recursive_watch_ = recursive;
98 MessageLoop::current()->AddDestructionObserver(this); 98 MessageLoop::current()->AddDestructionObserver(this);
99 99
100 File::Info file_info; 100 File::Info file_info;
101 if (GetFileInfo(target_, &file_info)) { 101 if (GetFileInfo(target_, &file_info)) {
102 last_modified_ = file_info.last_modified; 102 last_modified_ = file_info.last_modified;
103 first_notification_ = Time::Now(); 103 first_notification_ = Time::Now();
104 } 104 }
105 105
106 if (!UpdateWatch()) 106 if (!UpdateWatch())
107 return false; 107 return false;
108 108
109 watcher_.StartWatching(handle_, this); 109 watcher_.StartWatching(handle_, this);
110 110
111 return true; 111 return true;
112 } 112 }
113 113
114 void FilePathWatcherImpl::Cancel() { 114 void FilePathWatcherImpl::Cancel() {
115 if (callback_.is_null()) { 115 if (callback_.is_null()) {
116 // Watch was never called, or the |task_runner_| has already quit. 116 // Watch was never called, or the |message_loop_| has already quit.
117 set_cancelled(); 117 set_cancelled();
118 return; 118 return;
119 } 119 }
120 120
121 // Switch to the file thread if necessary so we can stop |watcher_|. 121 // Switch to the file thread if necessary so we can stop |watcher_|.
122 if (!task_runner()->BelongsToCurrentThread()) { 122 if (!message_loop()->BelongsToCurrentThread()) {
123 task_runner()->PostTask(FROM_HERE, Bind(&FilePathWatcher::CancelWatch, 123 message_loop()->PostTask(FROM_HERE,
124 make_scoped_refptr(this))); 124 Bind(&FilePathWatcher::CancelWatch,
125 make_scoped_refptr(this)));
125 } else { 126 } else {
126 CancelOnMessageLoopThread(); 127 CancelOnMessageLoopThread();
127 } 128 }
128 } 129 }
129 130
130 void FilePathWatcherImpl::CancelOnMessageLoopThread() { 131 void FilePathWatcherImpl::CancelOnMessageLoopThread() {
131 DCHECK(task_runner()->BelongsToCurrentThread()); 132 DCHECK(message_loop()->BelongsToCurrentThread());
132 set_cancelled(); 133 set_cancelled();
133 134
134 if (handle_ != INVALID_HANDLE_VALUE) 135 if (handle_ != INVALID_HANDLE_VALUE)
135 DestroyWatch(); 136 DestroyWatch();
136 137
137 if (!callback_.is_null()) { 138 if (!callback_.is_null()) {
138 MessageLoop::current()->RemoveDestructionObserver(this); 139 MessageLoop::current()->RemoveDestructionObserver(this);
139 callback_.Reset(); 140 callback_.Reset();
140 } 141 }
141 } 142 }
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 handle_ = INVALID_HANDLE_VALUE; 292 handle_ = INVALID_HANDLE_VALUE;
292 } 293 }
293 294
294 } // namespace 295 } // namespace
295 296
296 FilePathWatcher::FilePathWatcher() { 297 FilePathWatcher::FilePathWatcher() {
297 impl_ = new FilePathWatcherImpl(); 298 impl_ = new FilePathWatcherImpl();
298 } 299 }
299 300
300 } // namespace base 301 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_path_watcher_unittest.cc ('k') | base/files/file_proxy.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698