net_log_source.cc 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2016 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/log/net_log_source.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/check_op.h"
  10. #include "base/values.h"
  11. #include "net/log/net_log_capture_mode.h"
  12. namespace net {
  13. namespace {
  14. base::Value SourceEventParametersCallback(const NetLogSource source) {
  15. if (!source.IsValid())
  16. return base::Value();
  17. base::Value::Dict event_params;
  18. source.AddToEventParameters(event_params);
  19. return base::Value(std::move(event_params));
  20. }
  21. } // namespace
  22. // LoadTimingInfo requires this be 0.
  23. const uint32_t NetLogSource::kInvalidId = 0;
  24. NetLogSource::NetLogSource()
  25. : NetLogSource(NetLogSourceType::NONE, kInvalidId) {}
  26. NetLogSource::NetLogSource(NetLogSourceType type, uint32_t id)
  27. : NetLogSource(type, id, base::TimeTicks::Now()) {}
  28. NetLogSource::NetLogSource(NetLogSourceType type,
  29. uint32_t id,
  30. base::TimeTicks start_time)
  31. : type(type), id(id), start_time(start_time) {}
  32. bool NetLogSource::operator==(const NetLogSource& rhs) const {
  33. return type == rhs.type && id == rhs.id && start_time == rhs.start_time;
  34. }
  35. bool NetLogSource::IsValid() const {
  36. return id != kInvalidId;
  37. }
  38. void NetLogSource::AddToEventParameters(base::Value::Dict& event_params) const {
  39. base::Value::Dict dict;
  40. dict.Set("type", static_cast<int>(type));
  41. dict.Set("id", static_cast<int>(id));
  42. event_params.Set("source_dependency", std::move(dict));
  43. }
  44. base::Value NetLogSource::ToEventParameters() const {
  45. return SourceEventParametersCallback(*this);
  46. }
  47. } // namespace net