xref: /aosp_15_r20/external/cronet/net/log/net_log_source.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/log/net_log_source.h"
6 
7 #include <memory>
8 #include <utility>
9 
10 #include "base/values.h"
11 
12 namespace net {
13 
14 // LoadTimingInfo requires this be 0.
15 const uint32_t NetLogSource::kInvalidId = 0;
16 
NetLogSource()17 NetLogSource::NetLogSource()
18     : NetLogSource(NetLogSourceType::NONE, kInvalidId) {}
19 
NetLogSource(NetLogSourceType type,uint32_t id)20 NetLogSource::NetLogSource(NetLogSourceType type, uint32_t id)
21     : NetLogSource(type, id, base::TimeTicks::Now()) {}
22 
NetLogSource(NetLogSourceType type,uint32_t id,base::TimeTicks start_time)23 NetLogSource::NetLogSource(NetLogSourceType type,
24                            uint32_t id,
25                            base::TimeTicks start_time)
26     : type(type), id(id), start_time(start_time) {}
27 
operator ==(const NetLogSource & rhs) const28 bool NetLogSource::operator==(const NetLogSource& rhs) const {
29   return type == rhs.type && id == rhs.id && start_time == rhs.start_time;
30 }
31 
IsValid() const32 bool NetLogSource::IsValid() const {
33   return id != kInvalidId;
34 }
35 
AddToEventParameters(base::Value::Dict & event_params) const36 void NetLogSource::AddToEventParameters(base::Value::Dict& event_params) const {
37   base::Value::Dict dict;
38   dict.Set("type", static_cast<int>(type));
39   dict.Set("id", static_cast<int>(id));
40   event_params.Set("source_dependency", std::move(dict));
41 }
42 
ToEventParameters() const43 base::Value::Dict NetLogSource::ToEventParameters() const {
44   if (!IsValid())
45     return base::Value::Dict();
46   base::Value::Dict event_params;
47   AddToEventParameters(event_params);
48   return event_params;
49 }
50 
51 }  // namespace net
52