blob: e9ffa467997d46734593a723f02ed79ba8757c5b [file] [log] [blame]
mikecironef22f9812016-10-04 03:40:191// 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
16namespace net {
17
18namespace {
19
Eric Roman06bd9742019-07-13 15:19:1320base::Value SourceEventParametersCallback(const NetLogSource source) {
mikecironef22f9812016-10-04 03:40:1921 if (!source.IsValid())
Eric Romanb0436912019-04-30 23:38:4222 return base::Value();
23 base::DictionaryValue event_params;
24 source.AddToEventParameters(&event_params);
mikecironef22f9812016-10-04 03:40:1925 return std::move(event_params);
26}
27
28} // namespace
29
30// LoadTimingInfo requires this be 0.
31const uint32_t NetLogSource::kInvalidId = 0;
32
33NetLogSource::NetLogSource() : type(NetLogSourceType::NONE), id(kInvalidId) {}
34
35NetLogSource::NetLogSource(NetLogSourceType type, uint32_t id)
36 : type(type), id(id) {}
37
38bool NetLogSource::IsValid() const {
39 return id != kInvalidId;
40}
41
Eric Roman46b16b72019-05-01 01:03:0142void 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));
mikecironef22f9812016-10-04 03:40:1948}
49
Eric Roman06bd9742019-07-13 15:19:1350base::Value NetLogSource::ToEventParameters() const {
51 return SourceEventParametersCallback(*this);
mikecironef22f9812016-10-04 03:40:1952}
53
54// static
Eric Roman79cc7552019-07-19 02:17:5455bool NetLogSource::FromEventParameters(const base::Value* event_params,
mikecironef22f9812016-10-04 03:40:1956 NetLogSource* source) {
Eric Roman79cc7552019-07-19 02:17:5457 const base::DictionaryValue* dict = nullptr;
58 const base::DictionaryValue* source_dict = nullptr;
mikecironef22f9812016-10-04 03:40:1959 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