1 // Copyright (c) 2006-2008 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 directory for changes. 6 7 #ifndef BASE_DIRECTORY_WATCHER_H_ 8 #define BASE_DIRECTORY_WATCHER_H_ 9 10 #include "base/basictypes.h" 11 #include "base/ref_counted.h" 12 13 class FilePath; 14 class MessageLoop; 15 16 // This class lets you register interest in changes on a directory. 17 // The delegate will get called whenever a file is added or changed in the 18 // directory. 19 class DirectoryWatcher { 20 public: 21 class Delegate { 22 public: ~Delegate()23 virtual ~Delegate() {} 24 virtual void OnDirectoryChanged(const FilePath& path) = 0; 25 }; 26 27 DirectoryWatcher(); ~DirectoryWatcher()28 ~DirectoryWatcher() {} 29 30 // Register interest in any changes in the directory |path|. 31 // OnDirectoryChanged will be called back for each change within the dir. 32 // Any background operations will be ran on |backend_loop|, or inside Watch 33 // if |backend_loop| is NULL. If |recursive| is true, the delegate will be 34 // notified for each change within the directory tree starting at |path|. 35 // Returns false on error. 36 // 37 // Note: on Windows you may got more notifications for non-recursive watch 38 // than you expect, especially on versions earlier than Vista. The behavior 39 // is consistent on any particular version of Windows, but not across 40 // different versions. Watch(const FilePath & path,Delegate * delegate,MessageLoop * backend_loop,bool recursive)41 bool Watch(const FilePath& path, Delegate* delegate, 42 MessageLoop* backend_loop, bool recursive) { 43 return impl_->Watch(path, delegate, backend_loop, recursive); 44 } 45 46 // Used internally to encapsulate different members on different platforms. 47 class PlatformDelegate : public base::RefCounted<PlatformDelegate> { 48 public: 49 virtual bool Watch(const FilePath& path, Delegate* delegate, 50 MessageLoop* backend_loop, bool recursive) = 0; 51 52 protected: 53 friend class base::RefCounted<PlatformDelegate>; 54 ~PlatformDelegate()55 virtual ~PlatformDelegate() {} 56 }; 57 58 private: 59 scoped_refptr<PlatformDelegate> impl_; 60 61 DISALLOW_COPY_AND_ASSIGN(DirectoryWatcher); 62 }; 63 64 #endif // BASE_DIRECTORY_WATCHER_H_ 65