socket_tag.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2017 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. #include "net/socket/socket_tag.h"
  5. #include <tuple>
  6. #include "base/check.h"
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_ANDROID)
  9. #include "net/android/network_library.h"
  10. #endif // BUILDFLAG(IS_ANDROID)
  11. namespace net {
  12. #if BUILDFLAG(IS_ANDROID)
  13. // Expose UNSET_UID to Java.
  14. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
  15. enum TrafficStatsUid {
  16. UNSET = -1,
  17. };
  18. // Java generator needs explicit integer, verify equality here.
  19. static_assert(UNSET == SocketTag::UNSET_UID,
  20. "TrafficStatsUid does not match SocketTag::UNSET_UID");
  21. #endif // BUILDFLAG(IS_ANDROID)
  22. bool SocketTag::operator<(const SocketTag& other) const {
  23. #if BUILDFLAG(IS_ANDROID)
  24. return std::tie(uid_, traffic_stats_tag_) <
  25. std::tie(other.uid_, other.traffic_stats_tag_);
  26. #else
  27. return false;
  28. #endif // BUILDFLAG(IS_ANDROID)
  29. }
  30. bool SocketTag::operator==(const SocketTag& other) const {
  31. #if BUILDFLAG(IS_ANDROID)
  32. return std::tie(uid_, traffic_stats_tag_) ==
  33. std::tie(other.uid_, other.traffic_stats_tag_);
  34. #else
  35. return true;
  36. #endif // BUILDFLAG(IS_ANDROID)
  37. }
  38. void SocketTag::Apply(SocketDescriptor socket) const {
  39. #if BUILDFLAG(IS_ANDROID)
  40. net::android::TagSocket(socket, uid_, traffic_stats_tag_);
  41. #else
  42. CHECK(false);
  43. #endif // BUILDFLAG(IS_ANDROID)
  44. }
  45. } // namespace net