summaryrefslogtreecommitdiffstats
path: root/net/dns/watching_file_reader.h
blob: e93004e3b3b935079194ab444935568b99ec4039 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (c) 2011 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_WATCHING_FILE_READER_H_
#define NET_DNS_WATCHING_FILE_READER_H_
#pragma once

#include <string>

#include "base/compiler_specific.h"
#include "base/files/file_path_watcher.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/net_export.h"
#include "net/dns/serial_worker.h"

// Forward declaration
namespace base {
class MessageLoopProxy;
}

namespace net {

// Allows mocking FilePathWatcher
class NET_EXPORT_PRIVATE FilePathWatcherShim
  : NON_EXPORTED_BASE(public base::NonThreadSafe) {
 public:
  FilePathWatcherShim();
  virtual ~FilePathWatcherShim();
  virtual bool Watch(
      const FilePath& path,
      base::files::FilePathWatcher::Delegate* delegate) WARN_UNUSED_RESULT;
 private:
  scoped_ptr<base::files::FilePathWatcher> watcher_;
  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherShim);
};

class NET_EXPORT_PRIVATE FilePathWatcherFactory
  : NON_EXPORTED_BASE(public base::NonThreadSafe) {
 public:
  FilePathWatcherFactory() {}
  virtual ~FilePathWatcherFactory() {}
  virtual FilePathWatcherShim* CreateFilePathWatcher();
 private:
  DISALLOW_COPY_AND_ASSIGN(FilePathWatcherFactory);
};

// Convenience wrapper which maintains a FilePathWatcher::Delegate, restarts
// Watch on failures and calls PooledReader::ReadNow when OnFilePathChanged.
// Cancels the PooledReader upon destruction.
class NET_EXPORT_PRIVATE WatchingFileReader
  : NON_EXPORTED_BASE(public base::NonThreadSafe),
    NON_EXPORTED_BASE(public base::SupportsWeakPtr<WatchingFileReader>) {
 public:
  WatchingFileReader();
  virtual ~WatchingFileReader();

  // Must be called at most once. Made virtual to allow mocking.
  virtual void StartWatch(const FilePath& path, SerialWorker* reader);

  void set_watcher_factory(FilePathWatcherFactory* factory) {
    DCHECK(!watcher_.get());
    watcher_factory_.reset(factory);
  }

  FilePath get_path() const {
    return path_;
  }

  // Delay between calls to FilePathWatcher::Watch.
  static const int kWatchRetryDelayMs = 100;

 private:
  class WatcherDelegate;
  // FilePathWatcher::Delegate interface exported via WatcherDelegate
  virtual void OnFilePathChanged(const FilePath& path);
  virtual void OnFilePathError(const FilePath& path);

  void RescheduleWatch();
  void RestartWatch();

  FilePath path_;
  scoped_ptr<FilePathWatcherFactory> watcher_factory_;
  scoped_ptr<FilePathWatcherShim> watcher_;
  scoped_refptr<WatcherDelegate> watcher_delegate_;
  scoped_refptr<SerialWorker> reader_;

  DISALLOW_COPY_AND_ASSIGN(WatchingFileReader);
};

}  // namespace net

#endif  // NET_DNS_WATCHING_FILE_READER_H_