1// Copyright (c) 2012 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/disk_cache/net_log_parameters.h"
6
7#include "base/bind.h"
8#include "base/logging.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/values.h"
11#include "net/base/net_errors.h"
12#include "net/disk_cache/disk_cache.h"
13
14namespace {
15
16base::Value* NetLogEntryCreationCallback(
17    const disk_cache::Entry* entry,
18    bool created,
19    net::NetLog::LogLevel /* log_level */) {
20  base::DictionaryValue* dict = new base::DictionaryValue();
21  dict->SetString("key", entry->GetKey());
22  dict->SetBoolean("created", created);
23  return dict;
24}
25
26base::Value* NetLogReadWriteDataCallback(
27    int index,
28    int offset,
29    int buf_len,
30    bool truncate,
31    net::NetLog::LogLevel /* log_level */) {
32  base::DictionaryValue* dict = new base::DictionaryValue();
33  dict->SetInteger("index", index);
34  dict->SetInteger("offset", offset);
35  dict->SetInteger("buf_len", buf_len);
36  if (truncate)
37    dict->SetBoolean("truncate", truncate);
38  return dict;
39}
40
41base::Value* NetLogReadWriteCompleteCallback(
42    int bytes_copied,
43    net::NetLog::LogLevel /* log_level */) {
44  DCHECK_NE(bytes_copied, net::ERR_IO_PENDING);
45  base::DictionaryValue* dict = new base::DictionaryValue();
46  if (bytes_copied < 0) {
47    dict->SetInteger("net_error", bytes_copied);
48  } else {
49    dict->SetInteger("bytes_copied", bytes_copied);
50  }
51  return dict;
52}
53
54base::Value* NetLogSparseOperationCallback(
55    int64 offset,
56    int buff_len,
57    net::NetLog::LogLevel /* log_level */) {
58  base::DictionaryValue* dict = new base::DictionaryValue();
59  // Values can only be created with at most 32-bit integers.  Using a string
60  // instead circumvents that restriction.
61  dict->SetString("offset", base::Int64ToString(offset));
62  dict->SetInteger("buff_len", buff_len);
63  return dict;
64}
65
66base::Value* NetLogSparseReadWriteCallback(
67    const net::NetLog::Source& source,
68    int child_len,
69    net::NetLog::LogLevel /* log_level */) {
70  base::DictionaryValue* dict = new base::DictionaryValue();
71  source.AddToEventParameters(dict);
72  dict->SetInteger("child_len", child_len);
73  return dict;
74}
75
76base::Value* NetLogGetAvailableRangeResultCallback(
77    int64 start,
78    int result,
79    net::NetLog::LogLevel /* log_level */) {
80  base::DictionaryValue* dict = new base::DictionaryValue();
81  if (result > 0) {
82    dict->SetInteger("length", result);
83    dict->SetString("start",  base::Int64ToString(start));
84  } else {
85    dict->SetInteger("net_error", result);
86  }
87  return dict;
88}
89
90}  // namespace
91
92namespace disk_cache {
93
94net::NetLog::ParametersCallback CreateNetLogEntryCreationCallback(
95    const Entry* entry,
96    bool created) {
97  DCHECK(entry);
98  return base::Bind(&NetLogEntryCreationCallback, entry, created);
99}
100
101net::NetLog::ParametersCallback CreateNetLogReadWriteDataCallback(
102    int index,
103    int offset,
104    int buf_len,
105    bool truncate) {
106  return base::Bind(&NetLogReadWriteDataCallback,
107                    index, offset, buf_len, truncate);
108}
109
110net::NetLog::ParametersCallback CreateNetLogReadWriteCompleteCallback(
111    int bytes_copied) {
112  return base::Bind(&NetLogReadWriteCompleteCallback, bytes_copied);
113}
114
115net::NetLog::ParametersCallback CreateNetLogSparseOperationCallback(
116    int64 offset,
117    int buff_len) {
118  return base::Bind(&NetLogSparseOperationCallback, offset, buff_len);
119}
120
121net::NetLog::ParametersCallback CreateNetLogSparseReadWriteCallback(
122    const net::NetLog::Source& source,
123    int child_len) {
124  return base::Bind(&NetLogSparseReadWriteCallback, source, child_len);
125}
126
127net::NetLog::ParametersCallback CreateNetLogGetAvailableRangeResultCallback(
128    int64 start,
129    int result) {
130  return base::Bind(&NetLogGetAvailableRangeResultCallback, start, result);
131}
132
133}  // namespace disk_cache
134