Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / download / download_net_log_parameters.cc
blob4c204fe21398d7f02f98fe5068771299da854e5e
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.
5 #include "content/browser/download/download_net_log_parameters.h"
7 #include "base/basictypes.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/strings/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"
16 namespace content {
18 namespace {
20 static const char* download_type_names[] = {
21 "NEW_DOWNLOAD",
22 "HISTORY_IMPORT",
23 "SAVE_PAGE_AS"
25 static 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",
36 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_type_names) == SRC_SAVE_PAGE_AS + 1,
37 download_type_enum_has_changed);
38 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_danger_names) ==
39 DOWNLOAD_DANGER_TYPE_MAX,
40 download_danger_enum_has_changed);
42 } // namespace
44 base::Value* ItemActivatedNetLogCallback(
45 const DownloadItem* download_item,
46 DownloadType download_type,
47 const std::string* file_name,
48 net::NetLog::LogLevel log_level) {
49 base::DictionaryValue* dict = new base::DictionaryValue();
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());
62 return dict;
65 base::Value* ItemCheckedNetLogCallback(
66 DownloadDangerType danger_type,
67 net::NetLog::LogLevel log_level) {
68 base::DictionaryValue* dict = new base::DictionaryValue();
70 dict->SetString("danger_type", download_danger_names[danger_type]);
72 return dict;
75 base::Value* ItemRenamedNetLogCallback(const base::FilePath* old_filename,
76 const base::FilePath* new_filename,
77 net::NetLog::LogLevel log_level) {
78 base::DictionaryValue* dict = new base::DictionaryValue();
80 dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
81 dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
83 return dict;
86 base::Value* ItemInterruptedNetLogCallback(DownloadInterruptReason reason,
87 int64 bytes_so_far,
88 const std::string* hash_state,
89 net::NetLog::LogLevel log_level) {
90 base::DictionaryValue* dict = new base::DictionaryValue();
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()));
97 return dict;
100 base::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 base::DictionaryValue* dict = new base::DictionaryValue();
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()));
113 return dict;
116 base::Value* ItemCompletingNetLogCallback(int64 bytes_so_far,
117 const std::string* final_hash,
118 net::NetLog::LogLevel log_level) {
119 base::DictionaryValue* dict = new base::DictionaryValue();
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()));
125 return dict;
128 base::Value* ItemFinishedNetLogCallback(bool auto_opened,
129 net::NetLog::LogLevel log_level) {
130 base::DictionaryValue* dict = new base::DictionaryValue();
132 dict->SetString("auto_opened", auto_opened ? "yes" : "no");
134 return dict;
137 base::Value* ItemCanceledNetLogCallback(int64 bytes_so_far,
138 const std::string* hash_state,
139 net::NetLog::LogLevel log_level) {
140 base::DictionaryValue* dict = new base::DictionaryValue();
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()));
146 return dict;
149 base::Value* FileOpenedNetLogCallback(const base::FilePath* file_name,
150 int64 start_offset,
151 net::NetLog::LogLevel log_level) {
152 base::DictionaryValue* dict = new base::DictionaryValue();
154 dict->SetString("file_name", file_name->AsUTF8Unsafe());
155 dict->SetString("start_offset", base::Int64ToString(start_offset));
157 return dict;
160 base::Value* FileStreamDrainedNetLogCallback(size_t stream_size,
161 size_t num_buffers,
162 net::NetLog::LogLevel log_level) {
163 base::DictionaryValue* dict = new base::DictionaryValue();
165 dict->SetInteger("stream_size", static_cast<int>(stream_size));
166 dict->SetInteger("num_buffers", static_cast<int>(num_buffers));
168 return dict;
171 base::Value* FileRenamedNetLogCallback(const base::FilePath* old_filename,
172 const base::FilePath* new_filename,
173 net::NetLog::LogLevel log_level) {
174 base::DictionaryValue* dict = new base::DictionaryValue();
176 dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
177 dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
179 return dict;
182 base::Value* FileErrorNetLogCallback(const char* operation,
183 net::Error net_error,
184 net::NetLog::LogLevel log_level) {
185 base::DictionaryValue* dict = new base::DictionaryValue();
187 dict->SetString("operation", operation);
188 dict->SetInteger("net_error", net_error);
190 return dict;
193 base::Value* FileInterruptedNetLogCallback(const char* operation,
194 int os_error,
195 DownloadInterruptReason reason,
196 net::NetLog::LogLevel log_level) {
197 base::DictionaryValue* dict = new base::DictionaryValue();
199 dict->SetString("operation", operation);
200 if (os_error != 0)
201 dict->SetInteger("os_error", os_error);
202 dict->SetString("interrupt_reason", InterruptReasonDebugString(reason));
204 return dict;
207 } // namespace content