OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This module provides a way to monitor a file or directory for changes. | |
6 | |
7 #ifndef CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
8 #define CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
9 #pragma once | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/file_path.h" | |
13 #include "base/ref_counted.h" | |
14 #include "content/browser/browser_thread.h" | |
15 | |
16 // This class lets you register interest in changes on a FilePath. | |
17 // The delegate will get called whenever the file or directory referenced by the | |
18 // FilePath is changed, including created or deleted. Due to limitations in the | |
19 // underlying OS APIs, spurious notifications might occur that don't relate to | |
20 // an actual change to the watch target. | |
21 class FilePathWatcher { | |
22 public: | |
23 // Declares the callback client code implements to receive notifications. Note | |
24 // that implementations of this interface should not keep a reference to the | |
25 // corresponding FileWatcher object to prevent a reference cycle. | |
26 class Delegate : public base::RefCountedThreadSafe<Delegate> { | |
27 public: | |
28 virtual ~Delegate() {} | |
29 virtual void OnFilePathChanged(const FilePath& path) = 0; | |
30 // Called when platform specific code detected an error. The watcher will | |
31 // not call OnFilePathChanged for future changes. | |
32 virtual void OnError() {} | |
33 }; | |
34 | |
35 FilePathWatcher(); | |
36 ~FilePathWatcher(); | |
37 | |
38 // Register interest in any changes on |path|. OnPathChanged will be called | |
39 // back for each change. Returns true on success. | |
40 bool Watch(const FilePath& path, Delegate* delegate) WARN_UNUSED_RESULT; | |
41 | |
42 // Used internally to encapsulate different members on different platforms. | |
43 class PlatformDelegate | |
44 : public base::RefCountedThreadSafe<PlatformDelegate, | |
45 BrowserThread::DeleteOnFileThread> { | |
46 public: | |
47 // Start watching for the given |path| and notify |delegate| about changes. | |
48 virtual bool Watch(const FilePath& path, Delegate* delegate) | |
49 WARN_UNUSED_RESULT = 0; | |
50 | |
51 // Stop watching. This is called from FilePathWatcher's dtor in order to | |
52 // allow to shut down properly while the object is still alive. | |
53 virtual void Cancel() {} | |
54 | |
55 protected: | |
56 friend struct BrowserThread::DeleteOnThread<BrowserThread::FILE>; | |
57 friend class DeleteTask<PlatformDelegate>; | |
58 | |
59 virtual ~PlatformDelegate() {} | |
60 }; | |
61 | |
62 private: | |
63 scoped_refptr<PlatformDelegate> impl_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(FilePathWatcher); | |
66 }; | |
67 | |
68 #endif // CHROME_BROWSER_FILE_PATH_WATCHER_FILE_PATH_WATCHER_H_ | |
OLD | NEW |