• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 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/disk_cache/net_log_parameters.h"
6 
7 #include <utility>
8 
9 #include "base/check_op.h"
10 #include "base/values.h"
11 #include "net/base/net_errors.h"
12 #include "net/disk_cache/disk_cache.h"
13 #include "net/log/net_log_source.h"
14 #include "net/log/net_log_values.h"
15 
16 namespace {
17 
NetLogReadWriteDataParams(int index,int offset,int buf_len,bool truncate)18 base::Value::Dict NetLogReadWriteDataParams(int index,
19                                             int offset,
20                                             int buf_len,
21                                             bool truncate) {
22   base::Value::Dict dict;
23   dict.Set("index", index);
24   dict.Set("offset", offset);
25   dict.Set("buf_len", buf_len);
26   if (truncate)
27     dict.Set("truncate", truncate);
28   return dict;
29 }
30 
NetLogReadWriteCompleteParams(int bytes_copied)31 base::Value::Dict NetLogReadWriteCompleteParams(int bytes_copied) {
32   DCHECK_NE(bytes_copied, net::ERR_IO_PENDING);
33   base::Value::Dict dict;
34   if (bytes_copied < 0) {
35     dict.Set("net_error", bytes_copied);
36   } else {
37     dict.Set("bytes_copied", bytes_copied);
38   }
39   return dict;
40 }
41 
NetLogSparseOperationParams(int64_t offset,int buf_len)42 base::Value::Dict NetLogSparseOperationParams(int64_t offset, int buf_len) {
43   base::Value::Dict dict;
44   dict.Set("offset", net::NetLogNumberValue(offset));
45   dict.Set("buf_len", buf_len);
46   return dict;
47 }
48 
NetLogSparseReadWriteParams(const net::NetLogSource & source,int child_len)49 base::Value::Dict NetLogSparseReadWriteParams(const net::NetLogSource& source,
50                                               int child_len) {
51   base::Value::Dict dict;
52   source.AddToEventParameters(dict);
53   dict.Set("child_len", child_len);
54   return dict;
55 }
56 
57 }  // namespace
58 
59 namespace disk_cache {
60 
CreateNetLogParametersEntryCreationParams(const Entry * entry,bool created)61 base::Value::Dict CreateNetLogParametersEntryCreationParams(const Entry* entry,
62                                                             bool created) {
63   DCHECK(entry);
64   base::Value::Dict dict;
65   dict.Set("key", entry->GetKey());
66   dict.Set("created", created);
67   return dict;
68 }
69 
NetLogReadWriteData(const net::NetLogWithSource & net_log,net::NetLogEventType type,net::NetLogEventPhase phase,int index,int offset,int buf_len,bool truncate)70 void NetLogReadWriteData(const net::NetLogWithSource& net_log,
71                          net::NetLogEventType type,
72                          net::NetLogEventPhase phase,
73                          int index,
74                          int offset,
75                          int buf_len,
76                          bool truncate) {
77   net_log.AddEntry(type, phase, [&] {
78     return NetLogReadWriteDataParams(index, offset, buf_len, truncate);
79   });
80 }
81 
NetLogReadWriteComplete(const net::NetLogWithSource & net_log,net::NetLogEventType type,net::NetLogEventPhase phase,int bytes_copied)82 void NetLogReadWriteComplete(const net::NetLogWithSource& net_log,
83                              net::NetLogEventType type,
84                              net::NetLogEventPhase phase,
85                              int bytes_copied) {
86   net_log.AddEntry(type, phase,
87                    [&] { return NetLogReadWriteCompleteParams(bytes_copied); });
88 }
89 
NetLogSparseOperation(const net::NetLogWithSource & net_log,net::NetLogEventType type,net::NetLogEventPhase phase,int64_t offset,int buf_len)90 void NetLogSparseOperation(const net::NetLogWithSource& net_log,
91                            net::NetLogEventType type,
92                            net::NetLogEventPhase phase,
93                            int64_t offset,
94                            int buf_len) {
95   net_log.AddEntry(type, phase, [&] {
96     return NetLogSparseOperationParams(offset, buf_len);
97   });
98 }
99 
NetLogSparseReadWrite(const net::NetLogWithSource & net_log,net::NetLogEventType type,net::NetLogEventPhase phase,const net::NetLogSource & source,int child_len)100 void NetLogSparseReadWrite(const net::NetLogWithSource& net_log,
101                            net::NetLogEventType type,
102                            net::NetLogEventPhase phase,
103                            const net::NetLogSource& source,
104                            int child_len) {
105   net_log.AddEntry(type, phase, [&] {
106     return NetLogSparseReadWriteParams(source, child_len);
107   });
108 }
109 
CreateNetLogGetAvailableRangeResultParams(disk_cache::RangeResult result)110 base::Value::Dict CreateNetLogGetAvailableRangeResultParams(
111     disk_cache::RangeResult result) {
112   base::Value::Dict dict;
113   if (result.net_error == net::OK) {
114     dict.Set("length", result.available_len);
115     dict.Set("start", net::NetLogNumberValue(result.start));
116   } else {
117     dict.Set("net_error", result.net_error);
118   }
119   return dict;
120 }
121 
122 }  // namespace disk_cache
123