privet_traffic_detector.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
1// Copyright 2013 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#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_
6#define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_
7
8#include "base/callback.h"
9#include "base/cancelable_callback.h"
10#include "content/public/browser/browser_thread.h"
11#include "net/base/address_family.h"
12#include "net/base/network_change_notifier.h"
13
14namespace net {
15class DatagramServerSocket;
16class IOBufferWithSize;
17class IPEndPoint;
18}
19
20namespace local_discovery {
21
22// Detects mDns traffic that looks like "Privet" protocol.
23// Can produce false positives results, but main task of the class is to avoid
24// running full mDns listener if user doesn't have devices.
25// When traffic is detected, class fires callback and shutdowns itself.
26class PrivetTrafficDetector
27    : public base::RefCountedThreadSafe<
28          PrivetTrafficDetector, content::BrowserThread::DeleteOnIOThread>,
29      private net::NetworkChangeNotifier::NetworkChangeObserver {
30 public:
31  PrivetTrafficDetector(net::AddressFamily address_family,
32                        const base::Closure& on_traffic_detected);
33
34 private:
35  friend struct content::BrowserThread::DeleteOnThread<
36      content::BrowserThread::IO>;
37  friend class base::DeleteHelper<PrivetTrafficDetector>;
38  virtual ~PrivetTrafficDetector();
39
40    // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
41  virtual void OnNetworkChanged(
42      net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
43
44  void Start();
45  void ScheduleRestart();
46  void Restart();
47  int Bind();
48  int DoLoop(int rv);
49
50  base::Closure on_traffic_detected_;
51  scoped_refptr<base::TaskRunner> callback_runner_;
52  net::AddressFamily address_family_;
53  scoped_ptr<net::IPEndPoint> recv_addr_;
54  scoped_ptr<net::DatagramServerSocket> socket_;
55  scoped_refptr<net::IOBufferWithSize> io_buffer_;
56  base::CancelableClosure restart_callback_;
57
58  DISALLOW_COPY_AND_ASSIGN(PrivetTrafficDetector);
59};
60
61}  // namespace local_discovery
62
63#endif  // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_
64