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.
5 #ifndef CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_
6 #define CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_
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/ip_endpoint.h"
13 #include "net/base/network_change_notifier.h"
16 class DatagramServerSocket
;
17 class IOBufferWithSize
;
20 namespace local_discovery
{
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.
26 class PrivetTrafficDetector
27 : public base::RefCountedThreadSafe
<
28 PrivetTrafficDetector
, content::BrowserThread::DeleteOnIOThread
>,
29 private net::NetworkChangeNotifier::NetworkChangeObserver
{
31 PrivetTrafficDetector(net::AddressFamily address_family
,
32 const base::Closure
& on_traffic_detected
);
37 friend struct content::BrowserThread::DeleteOnThread
<
38 content::BrowserThread::IO
>;
39 friend class base::DeleteHelper
<PrivetTrafficDetector
>;
40 virtual ~PrivetTrafficDetector();
42 // net::NetworkChangeNotifier::NetworkChangeObserver implementation.
43 virtual void OnNetworkChanged(
44 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
46 void StartOnIOThread();
47 void ScheduleRestart();
48 void Restart(const net::NetworkInterfaceList
& networks
);
50 bool IsSourceAcceptable() const;
51 bool IsPrivetPacket(int rv
) const;
54 base::Closure on_traffic_detected_
;
55 scoped_refptr
<base::TaskRunner
> callback_runner_
;
56 net::NetworkInterfaceList networks_
;
57 net::AddressFamily address_family_
;
58 net::IPEndPoint recv_addr_
;
59 scoped_ptr
<net::DatagramServerSocket
> socket_
;
60 scoped_refptr
<net::IOBufferWithSize
> io_buffer_
;
61 base::Time start_time_
;
62 int restart_attempts_
;
64 base::WeakPtrFactory
<PrivetTrafficDetector
> weak_ptr_factory_
;
66 DISALLOW_COPY_AND_ASSIGN(PrivetTrafficDetector
);
69 } // namespace local_discovery
71 #endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_