Home | History | Annotate | Download | only in base
      1 /*
      2  *  Copyright 2015 The WebRTC Project Authors. All rights reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 #ifndef WEBRTC_BASE_NETWORKMONITOR_H_
     12 #define WEBRTC_BASE_NETWORKMONITOR_H_
     13 
     14 #include "webrtc/base/logging.h"
     15 #include "webrtc/base/scoped_ptr.h"
     16 #include "webrtc/base/sigslot.h"
     17 #include "webrtc/base/thread.h"
     18 
     19 namespace rtc {
     20 /*
     21  * Receives network-change events via |OnNetworksChanged| and signals the
     22  * networks changed event.
     23  *
     24  * Threading consideration:
     25  * It is expected that all upstream operations (from native to Java) are
     26  * performed from the worker thread. This includes creating, starting and
     27  * stopping the monitor. This avoids the potential race condition when creating
     28  * the singleton Java NetworkMonitor class. Downstream operations can be from
     29  * any thread, but this class will forward all the downstream operations onto
     30  * the worker thread.
     31  *
     32  * Memory consideration:
     33  * NetworkMonitor is owned by the caller (NetworkManager). The global network
     34  * monitor factory is owned by the factory itself but needs to be released from
     35  * the factory creator.
     36  */
     37 // Generic network monitor interface. It starts and stops monitoring network
     38 // changes, and fires the SignalNetworksChanged event when networks change.
     39 class NetworkMonitorInterface {
     40  public:
     41   NetworkMonitorInterface();
     42   virtual ~NetworkMonitorInterface();
     43 
     44   sigslot::signal0<> SignalNetworksChanged;
     45 
     46   virtual void Start() = 0;
     47   virtual void Stop() = 0;
     48 
     49   // Implementations should call this method on the base when networks change,
     50   // and the base will fire SignalNetworksChanged on the right thread.
     51   virtual void OnNetworksChanged() = 0;
     52 };
     53 
     54 class NetworkMonitorBase : public NetworkMonitorInterface,
     55                            public MessageHandler,
     56                            public sigslot::has_slots<> {
     57  public:
     58   NetworkMonitorBase();
     59   ~NetworkMonitorBase() override;
     60 
     61   void OnNetworksChanged() override;
     62 
     63   void OnMessage(Message* msg) override;
     64 
     65  private:
     66   Thread* thread_;
     67 };
     68 
     69 /*
     70  * NetworkMonitorFactory creates NetworkMonitors.
     71  */
     72 class NetworkMonitorFactory {
     73  public:
     74   // This is not thread-safe; it should be called once (or once per audio/video
     75   // call) during the call initialization.
     76   static void SetFactory(NetworkMonitorFactory* factory);
     77 
     78   static void ReleaseFactory(NetworkMonitorFactory* factory);
     79   static NetworkMonitorFactory* GetFactory();
     80 
     81   virtual NetworkMonitorInterface* CreateNetworkMonitor() = 0;
     82 
     83   virtual ~NetworkMonitorFactory();
     84 
     85  protected:
     86   NetworkMonitorFactory();
     87 };
     88 
     89 }  // namespace rtc
     90 
     91 #endif  // WEBRTC_BASE_NETWORKMONITOR_H_
     92