1// Copyright (c) 2011 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/logging.h"
8#include "base/string_number_conversions.h"
9#include "base/values.h"
10#include "net/base/net_errors.h"
11
12namespace disk_cache {
13
14EntryCreationParameters::EntryCreationParameters(
15    const std::string& key, bool created)
16    : key_(key), created_(created) {
17}
18
19Value* EntryCreationParameters::ToValue() const {
20  DictionaryValue* dict = new DictionaryValue();
21  dict->SetString("key", key_);
22  dict->SetBoolean("created", created_);
23  return dict;
24}
25
26ReadWriteDataParameters::ReadWriteDataParameters(
27    int index, int offset, int buf_len, bool truncate)
28    : index_(index), offset_(offset), buf_len_(buf_len), truncate_(truncate) {
29}
30
31Value* ReadWriteDataParameters::ToValue() const {
32  DictionaryValue* dict = new 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
41
42// NetLog parameters logged when non-sparse reads and writes complete.
43ReadWriteCompleteParameters::ReadWriteCompleteParameters(int bytes_copied)
44    : bytes_copied_(bytes_copied) {
45}
46
47Value* ReadWriteCompleteParameters::ToValue() const {
48  DCHECK_NE(bytes_copied_, net::ERR_IO_PENDING);
49  DictionaryValue* dict = new DictionaryValue();
50  if (bytes_copied_ < 0) {
51    dict->SetInteger("net_error", bytes_copied_);
52  } else {
53    dict->SetInteger("bytes_copied", bytes_copied_);
54  }
55  return dict;
56}
57
58SparseOperationParameters::SparseOperationParameters(
59    int64 offset, int buff_len)
60    : offset_(offset), buff_len_(buff_len) {
61}
62
63Value* SparseOperationParameters::ToValue() const {
64  DictionaryValue* dict = new DictionaryValue();
65  // Values can only be created with at most 32-bit integers.  Using a string
66  // instead circumvents that restriction.
67  dict->SetString("offset", base::Int64ToString(offset_));
68  dict->SetInteger("buff_len", buff_len_);
69  return dict;
70}
71
72SparseReadWriteParameters::SparseReadWriteParameters(
73    const net::NetLog::Source& source, int child_len)
74    : source_(source), child_len_(child_len) {
75}
76
77Value* SparseReadWriteParameters::ToValue() const {
78  DictionaryValue* dict = new DictionaryValue();
79  dict->Set("source_dependency", source_.ToValue());
80  dict->SetInteger("child_len", child_len_);
81  return dict;
82}
83
84GetAvailableRangeResultParameters::GetAvailableRangeResultParameters(
85    int64 start, int result)
86    : start_(start), result_(result) {
87}
88
89Value* GetAvailableRangeResultParameters::ToValue() const {
90  DictionaryValue* dict = new DictionaryValue();
91  if (result_ > 0) {
92    dict->SetInteger("length", result_);
93    dict->SetString("start",  base::Int64ToString(start_));
94  } else {
95    dict->SetInteger("net_error", result_);
96  }
97  return dict;
98}
99
100}  // namespace disk_cache
101