summaryrefslogtreecommitdiffstats
path: root/net/dns/file_path_watcher_wrapper.h
blob: b960c453ab897e388131527beaf0758defd72734 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_
#define NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/callback.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/net_export.h"

namespace base {
namespace files {
class FilePathWatcher;
}
}

namespace net {

// Convenience wrapper which holds a FilePathWatcher and a
// FilePathWatcher::Delegate which forwards its calls to a Callback.
class NET_EXPORT FilePathWatcherWrapper
    : NON_EXPORTED_BASE(public base::NonThreadSafe) {
 public:
  FilePathWatcherWrapper();
  // When deleted, automatically cancels the FilePathWatcher.
  virtual ~FilePathWatcherWrapper();

  // Starts watching the file at |path|. Returns true if Watch succeeds.
  // If so, |callback| will be called with true on change and false on error.
  // After failure the watch is cancelled and will have to be restarted.
  bool Watch(const FilePath& path,
             const base::Callback<void(bool succeeded)>& callback);

  bool IsWatching() const;

  // Cancels the current watch.
  void Cancel();

 protected:
  // Creates a FilePathWatcher. Override to provide a different implementation.
  virtual scoped_ptr<base::files::FilePathWatcher> CreateFilePathWatcher();

 private:
  class WatcherDelegate;

  void OnFilePathChanged();
  void OnFilePathError();

  base::Callback<void(bool succeeded)> callback_;
  scoped_ptr<base::files::FilePathWatcher> watcher_;
  scoped_refptr<WatcherDelegate> delegate_;
  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherWrapper);
};

}  // namespace net

#endif  // NET_DNS_FILE_PATH_WATCHER_WRAPPER_H_