mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 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 | |
| 5 | #include "net/log/net_log_source.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "base/bind.h" |
| 11 | #include "base/callback.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/values.h" |
| 14 | #include "net/log/net_log_capture_mode.h" |
| 15 | |
| 16 | namespace net { |
| 17 | |
| 18 | namespace { |
| 19 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 20 | base::Value SourceEventParametersCallback(const NetLogSource source) { |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 21 | if (!source.IsValid()) |
Eric Roman | b043691 | 2019-04-30 23:38:42 | [diff] [blame] | 22 | return base::Value(); |
| 23 | base::DictionaryValue event_params; |
| 24 | source.AddToEventParameters(&event_params); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 25 | return std::move(event_params); |
| 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
| 30 | // LoadTimingInfo requires this be 0. |
| 31 | const uint32_t NetLogSource::kInvalidId = 0; |
| 32 | |
| 33 | NetLogSource::NetLogSource() : type(NetLogSourceType::NONE), id(kInvalidId) {} |
| 34 | |
| 35 | NetLogSource::NetLogSource(NetLogSourceType type, uint32_t id) |
| 36 | : type(type), id(id) {} |
| 37 | |
| 38 | bool NetLogSource::IsValid() const { |
| 39 | return id != kInvalidId; |
| 40 | } |
| 41 | |
Eric Roman | 46b16b7 | 2019-05-01 01:03:01 | [diff] [blame] | 42 | void NetLogSource::AddToEventParameters(base::Value* event_params) const { |
| 43 | DCHECK(event_params->is_dict()); |
| 44 | base::Value dict(base::Value::Type::DICTIONARY); |
| 45 | dict.SetIntKey("type", static_cast<int>(type)); |
| 46 | dict.SetIntKey("id", static_cast<int>(id)); |
| 47 | event_params->SetKey("source_dependency", std::move(dict)); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 48 | } |
| 49 | |
Eric Roman | 06bd974 | 2019-07-13 15:19:13 | [diff] [blame] | 50 | base::Value NetLogSource::ToEventParameters() const { |
| 51 | return SourceEventParametersCallback(*this); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // static |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 55 | bool NetLogSource::FromEventParameters(const base::Value* event_params, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 56 | NetLogSource* source) { |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 57 | const base::DictionaryValue* dict = nullptr; |
| 58 | const base::DictionaryValue* source_dict = nullptr; |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 59 | int source_id = -1; |
| 60 | int source_type = static_cast<int>(NetLogSourceType::COUNT); |
| 61 | if (!event_params || !event_params->GetAsDictionary(&dict) || |
| 62 | !dict->GetDictionary("source_dependency", &source_dict) || |
| 63 | !source_dict->GetInteger("id", &source_id) || |
| 64 | !source_dict->GetInteger("type", &source_type)) { |
| 65 | *source = NetLogSource(); |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | DCHECK_GE(source_id, 0); |
| 70 | DCHECK_LT(source_type, static_cast<int>(NetLogSourceType::COUNT)); |
| 71 | *source = NetLogSource(static_cast<NetLogSourceType>(source_type), source_id); |
| 72 | return true; |
| 73 | } |
| 74 | |
| 75 | } // namespace net |