summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/sdk/objc/native/src/objc_network_monitor.mm
blob: 535548c64cdcdcca465bdedb7d744b06ea0fee92 (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
/*
 *  Copyright 2020 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "sdk/objc/native/src/objc_network_monitor.h"
#include "absl/strings/string_view.h"

#include <algorithm>

#include "rtc_base/logging.h"
#include "rtc_base/string_utils.h"

namespace webrtc {

rtc::NetworkMonitorInterface* ObjCNetworkMonitorFactory::CreateNetworkMonitor(
    const FieldTrialsView& field_trials) {
  return new ObjCNetworkMonitor();
}

ObjCNetworkMonitor::ObjCNetworkMonitor() {
  safety_flag_ = PendingTaskSafetyFlag::Create();
}

ObjCNetworkMonitor::~ObjCNetworkMonitor() {
  [network_monitor_ stop];
  network_monitor_ = nil;
}

void ObjCNetworkMonitor::Start() {
  if (started_) {
    return;
  }
  thread_ = rtc::Thread::Current();
  RTC_DCHECK_RUN_ON(thread_);
  safety_flag_->SetAlive();
  network_monitor_ = [[RTCNetworkMonitor alloc] initWithObserver:this];
  if (network_monitor_ == nil) {
    RTC_LOG(LS_WARNING) << "Failed to create RTCNetworkMonitor; not available on this OS?";
  }
  started_ = true;
}

void ObjCNetworkMonitor::Stop() {
  RTC_DCHECK_RUN_ON(thread_);
  if (!started_) {
    return;
  }
  safety_flag_->SetNotAlive();
  [network_monitor_ stop];
  network_monitor_ = nil;
  started_ = false;
}

rtc::NetworkMonitorInterface::InterfaceInfo ObjCNetworkMonitor::GetInterfaceInfo(
    absl::string_view interface_name) {
  RTC_DCHECK_RUN_ON(thread_);
  if (adapter_type_by_name_.empty()) {
    // If we have no path update, assume everything's available, because it's
    // preferable for WebRTC to try all interfaces rather than none at all.
    return {
        .adapter_type = rtc::ADAPTER_TYPE_UNKNOWN,
        .available = true,
    };
  }
  auto iter = adapter_type_by_name_.find(interface_name);
  if (iter == adapter_type_by_name_.end()) {
    return {
        .adapter_type = rtc::ADAPTER_TYPE_UNKNOWN,
        .available = false,
    };
  }

  return {
      .adapter_type = iter->second,
      .available = true,
  };
}

void ObjCNetworkMonitor::OnPathUpdate(
    std::map<std::string, rtc::AdapterType, rtc::AbslStringViewCmp> adapter_type_by_name) {
  RTC_DCHECK(network_monitor_ != nil);
  thread_->PostTask(SafeTask(safety_flag_, [this, adapter_type_by_name] {
    RTC_DCHECK_RUN_ON(thread_);
    adapter_type_by_name_ = adapter_type_by_name;
    InvokeNetworksChangedCallback();
  }));
}

}  // namespace webrtc