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/ip_endpoint.h" 13#include "net/base/network_change_notifier.h" 14 15namespace net { 16class DatagramServerSocket; 17class IOBufferWithSize; 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 void Start(); 35 36 private: 37 friend struct content::BrowserThread::DeleteOnThread< 38 content::BrowserThread::IO>; 39 friend class base::DeleteHelper<PrivetTrafficDetector>; 40 virtual ~PrivetTrafficDetector(); 41 42 // net::NetworkChangeNotifier::NetworkChangeObserver implementation. 43 virtual void OnNetworkChanged( 44 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; 45 46 void StartOnIOThread(); 47 void ScheduleRestart(); 48 void Restart(const net::NetworkInterfaceList& networks); 49 int Bind(); 50 bool IsSourceAcceptable() const; 51 bool IsPrivetPacket(int rv) const; 52 int DoLoop(int rv); 53 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_; 63 64 base::WeakPtrFactory<PrivetTrafficDetector> weak_ptr_factory_; 65 66 DISALLOW_COPY_AND_ASSIGN(PrivetTrafficDetector); 67}; 68 69} // namespace local_discovery 70 71#endif // CHROME_BROWSER_LOCAL_DISCOVERY_PRIVET_TRAFFIC_DETECTOR_H_ 72