download_net_log_parameters.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 "content/browser/download/download_net_log_parameters.h"
6
7#include "base/basictypes.h"
8#include "base/files/file_path.h"
9#include "base/logging.h"
10#include "base/string_number_conversions.h"
11#include "base/values.h"
12#include "content/public/browser/download_interrupt_reasons.h"
13#include "googleurl/src/gurl.h"
14#include "net/base/net_errors.h"
15
16namespace content {
17
18namespace {
19
20static const char* download_type_names[] = {
21  "NEW_DOWNLOAD",
22  "HISTORY_IMPORT",
23  "SAVE_PAGE_AS"
24};
25static const char* download_danger_names[] = {
26  "NOT_DANGEROUS",
27  "DANGEROUS_FILE",
28  "DANGEROUS_URL",
29  "DANGEROUS_CONTENT",
30  "MAYBE_DANGEROUS_CONTENT",
31  "UNCOMMON_CONTENT",
32  "USER_VALIDATED",
33  "DANGEROUS_HOST",
34};
35
36COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_type_names) == SRC_SAVE_PAGE_AS + 1,
37               download_type_enum_has_changed);
38COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_danger_names) ==
39                  DOWNLOAD_DANGER_TYPE_MAX,
40               download_danger_enum_has_changed);
41
42}  // namespace
43
44base::Value* ItemActivatedNetLogCallback(
45    const DownloadItem* download_item,
46    DownloadType download_type,
47    const std::string* file_name,
48    net::NetLog::LogLevel log_level) {
49  DictionaryValue* dict = new DictionaryValue();
50
51  dict->SetString("type", download_type_names[download_type]);
52  dict->SetString("id", base::Int64ToString(download_item->GetId()));
53  dict->SetString("original_url", download_item->GetOriginalUrl().spec());
54  dict->SetString("final_url", download_item->GetURL().spec());
55  dict->SetString("file_name", *file_name);
56  dict->SetString("danger_type",
57                  download_danger_names[download_item->GetDangerType()]);
58  dict->SetString("start_offset",
59                  base::Int64ToString(download_item->GetReceivedBytes()));
60  dict->SetBoolean("has_user_gesture", download_item->HasUserGesture());
61
62  return dict;
63}
64
65base::Value* ItemCheckedNetLogCallback(
66    DownloadDangerType danger_type,
67    net::NetLog::LogLevel log_level) {
68  DictionaryValue* dict = new DictionaryValue();
69
70  dict->SetString("danger_type", download_danger_names[danger_type]);
71
72  return dict;
73}
74
75base::Value* ItemRenamedNetLogCallback(const base::FilePath* old_filename,
76                                       const base::FilePath* new_filename,
77                                       net::NetLog::LogLevel log_level) {
78  DictionaryValue* dict = new DictionaryValue();
79
80  dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
81  dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
82
83  return dict;
84}
85
86base::Value* ItemInterruptedNetLogCallback(DownloadInterruptReason reason,
87                                           int64 bytes_so_far,
88                                           const std::string* hash_state,
89                                           net::NetLog::LogLevel log_level) {
90  DictionaryValue* dict = new DictionaryValue();
91
92  dict->SetString("interrupt_reason", InterruptReasonDebugString(reason));
93  dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
94  dict->SetString("hash_state",
95                  base::HexEncode(hash_state->data(), hash_state->size()));
96
97  return dict;
98}
99
100base::Value* ItemResumingNetLogCallback(bool user_initiated,
101                                        DownloadInterruptReason reason,
102                                        int64 bytes_so_far,
103                                        const std::string* hash_state,
104                                        net::NetLog::LogLevel log_level) {
105  DictionaryValue* dict = new DictionaryValue();
106
107  dict->SetString("user_initiated", user_initiated ? "true" : "false");
108  dict->SetString("interrupt_reason", InterruptReasonDebugString(reason));
109  dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
110  dict->SetString("hash_state",
111                  base::HexEncode(hash_state->data(), hash_state->size()));
112
113  return dict;
114}
115
116base::Value* ItemCompletingNetLogCallback(int64 bytes_so_far,
117                                          const std::string* final_hash,
118                                          net::NetLog::LogLevel log_level) {
119  DictionaryValue* dict = new DictionaryValue();
120
121  dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
122  dict->SetString("final_hash",
123                  base::HexEncode(final_hash->data(), final_hash->size()));
124
125  return dict;
126}
127
128base::Value* ItemFinishedNetLogCallback(bool auto_opened,
129                                        net::NetLog::LogLevel log_level) {
130  DictionaryValue* dict = new DictionaryValue();
131
132  dict->SetString("auto_opened", auto_opened ? "yes" : "no");
133
134  return dict;
135}
136
137base::Value* ItemCanceledNetLogCallback(int64 bytes_so_far,
138                                        const std::string* hash_state,
139                                        net::NetLog::LogLevel log_level) {
140  DictionaryValue* dict = new DictionaryValue();
141
142  dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
143  dict->SetString("hash_state",
144                  base::HexEncode(hash_state->data(), hash_state->size()));
145
146  return dict;
147}
148
149base::Value* FileOpenedNetLogCallback(const base::FilePath* file_name,
150                                      int64 start_offset,
151                                      net::NetLog::LogLevel log_level) {
152  DictionaryValue* dict = new DictionaryValue();
153
154  dict->SetString("file_name", file_name->AsUTF8Unsafe());
155  dict->SetString("start_offset", base::Int64ToString(start_offset));
156
157  return dict;
158}
159
160base::Value* FileStreamDrainedNetLogCallback(size_t stream_size,
161                                             size_t num_buffers,
162                                             net::NetLog::LogLevel log_level) {
163  DictionaryValue* dict = new DictionaryValue();
164
165  dict->SetInteger("stream_size", static_cast<int>(stream_size));
166  dict->SetInteger("num_buffers", static_cast<int>(num_buffers));
167
168  return dict;
169}
170
171base::Value* FileRenamedNetLogCallback(const base::FilePath* old_filename,
172                                       const base::FilePath* new_filename,
173                                       net::NetLog::LogLevel log_level) {
174  DictionaryValue* dict = new DictionaryValue();
175
176  dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
177  dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
178
179  return dict;
180}
181
182base::Value* FileErrorNetLogCallback(const char* operation,
183                                     net::Error net_error,
184                                     net::NetLog::LogLevel log_level) {
185  DictionaryValue* dict = new DictionaryValue();
186
187  dict->SetString("operation", operation);
188  dict->SetInteger("net_error", net_error);
189
190  return dict;
191}
192
193base::Value* FileInterruptedNetLogCallback(const char* operation,
194                                           int os_error,
195                                           DownloadInterruptReason reason,
196                                           net::NetLog::LogLevel log_level) {
197  DictionaryValue* dict = new DictionaryValue();
198
199  dict->SetString("operation", operation);
200  if (os_error != 0)
201    dict->SetInteger("os_error", os_error);
202  dict->SetString("interrupt_reason", InterruptReasonDebugString(reason));
203
204  return dict;
205}
206
207
208}  // namespace content
209