local_network_access_permission.h (3484B)
1 /* 2 * Copyright 2025 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 API_LOCAL_NETWORK_ACCESS_PERMISSION_H_ 12 #define API_LOCAL_NETWORK_ACCESS_PERMISSION_H_ 13 14 #include <memory> 15 16 #include "absl/functional/any_invocable.h" 17 #include "rtc_base/socket_address.h" 18 19 namespace webrtc { 20 21 // This interface defines methods to request a Local Network Access permission 22 // asynchronously. The LocalNetworkAccessPermissionInterface class encapsulates 23 // a single permission request. 24 // 25 // Usage: 26 // // An implementation of the factory should be passed in by the embedder. 27 // std::unique_ptr<LocalNetworkAccessPermissionFactoryInterface> factory = 28 // embedder_factory; 29 // 30 // Stores pending permission requests. 31 // std::vector<std::unique_ptr<LocalNetworkAccessPermissionInterface>> 32 // permission_list; 33 // 34 // std::unique_ptr<LocalNetworkAccessPermissionInterface> permission = 35 // factory->Create(); 36 // permission->RequestPermission( 37 // target_address, 38 // [&, r = permission.get()](LocalNetworkAccessPermissionStatus status) { 39 // std::erase_if(permission_list, 40 // [&](const auto& refptr) { return refptr.get() == r; }); 41 // 42 // if (status == LocalNetworkAccessPermissionStatus::kGranted) { 43 // // Permission was granted. 44 // } else { 45 // // Permission was denied. 46 // } 47 // }); 48 // permission_list.push_back(std::move(permission)); 49 50 enum class LocalNetworkAccessPermissionStatus { 51 kGranted, 52 kDenied, 53 }; 54 55 // The API for a single permission query. 56 // The constructor, destructor and all functions must be called from 57 // the same sequence, and the callback will also be called on that sequence. 58 // The class guarantees that the callback will not be called if the 59 // permission's destructor has been called. 60 class LocalNetworkAccessPermissionInterface { 61 public: 62 virtual ~LocalNetworkAccessPermissionInterface() = default; 63 64 // Returns whether or not the caller should request permission. Depending 65 // on the originator's address space, sometimes it's not necessary to request 66 // permission. 67 // TODO(crbug.com/421223919): Make this method pure virtual once all 68 // implementations implement it. 69 virtual bool ShouldRequestPermission(const SocketAddress& addr) { 70 return addr.IsPrivateIP() || addr.IsLoopbackIP(); 71 } 72 73 // The callback will be called when the permission is granted or denied. The 74 // callback will be called on the sequence that the caller runs on. 75 virtual void RequestPermission( 76 const SocketAddress& addr, 77 absl::AnyInvocable<void(LocalNetworkAccessPermissionStatus)> 78 callback) = 0; 79 }; 80 81 // An abstract factory for creating LocalNetworkPermissionInterfaces. This 82 // allows client applications to provide WebRTC with their own mechanism for 83 // checking and requesting Local Network Access permission. 84 class LocalNetworkAccessPermissionFactoryInterface { 85 public: 86 virtual ~LocalNetworkAccessPermissionFactoryInterface() = default; 87 88 // Creates a LocalNetworkAccessPermission. 89 virtual std::unique_ptr<LocalNetworkAccessPermissionInterface> Create() = 0; 90 }; 91 92 } // namespace webrtc 93 94 #endif // API_LOCAL_NETWORK_ACCESS_PERMISSION_H_