Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / download / download_net_log_parameters.cc
blob4532136227300dfca6ea7b3f311fa73a80ecf30e
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 "net/base/net_errors.h"
14 #include "url/gurl.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",
34 "POTENTIALLY_UNWANTED"
37 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_type_names) == SRC_SAVE_PAGE_AS + 1,
38 download_type_enum_has_changed);
39 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_danger_names) ==
40 DOWNLOAD_DANGER_TYPE_MAX,
41 download_danger_enum_has_changed);
43 } // namespace
45 base::Value* ItemActivatedNetLogCallback(
46 const DownloadItem* download_item,
47 DownloadType download_type,
48 const std::string* file_name,
49 net::NetLog::LogLevel log_level) {
50 base::DictionaryValue* dict = new base::DictionaryValue();
52 dict->SetString("type", download_type_names[download_type]);
53 dict->SetString("id", base::Int64ToString(download_item->GetId()));
54 dict->SetString("original_url", download_item->GetOriginalUrl().spec());
55 dict->SetString("final_url", download_item->GetURL().spec());
56 dict->SetString("file_name", *file_name);
57 dict->SetString("danger_type",
58 download_danger_names[download_item->GetDangerType()]);
59 dict->SetString("start_offset",
60 base::Int64ToString(download_item->GetReceivedBytes()));
61 dict->SetBoolean("has_user_gesture", download_item->HasUserGesture());
63 return dict;
66 base::Value* ItemCheckedNetLogCallback(
67 DownloadDangerType danger_type,
68 net::NetLog::LogLevel log_level) {
69 base::DictionaryValue* dict = new base::DictionaryValue();
71 dict->SetString("danger_type", download_danger_names[danger_type]);
73 return dict;
76 base::Value* ItemRenamedNetLogCallback(const base::FilePath* old_filename,
77 const base::FilePath* new_filename,
78 net::NetLog::LogLevel log_level) {
79 base::DictionaryValue* dict = new base::DictionaryValue();
81 dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
82 dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
84 return dict;
87 base::Value* ItemInterruptedNetLogCallback(DownloadInterruptReason reason,
88 int64 bytes_so_far,
89 const std::string* hash_state,
90 net::NetLog::LogLevel log_level) {
91 base::DictionaryValue* dict = new base::DictionaryValue();
93 dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
94 dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
95 dict->SetString("hash_state",
96 base::HexEncode(hash_state->data(), hash_state->size()));
98 return dict;
101 base::Value* ItemResumingNetLogCallback(bool user_initiated,
102 DownloadInterruptReason reason,
103 int64 bytes_so_far,
104 const std::string* hash_state,
105 net::NetLog::LogLevel log_level) {
106 base::DictionaryValue* dict = new base::DictionaryValue();
108 dict->SetString("user_initiated", user_initiated ? "true" : "false");
109 dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
110 dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
111 dict->SetString("hash_state",
112 base::HexEncode(hash_state->data(), hash_state->size()));
114 return dict;
117 base::Value* ItemCompletingNetLogCallback(int64 bytes_so_far,
118 const std::string* final_hash,
119 net::NetLog::LogLevel log_level) {
120 base::DictionaryValue* dict = new base::DictionaryValue();
122 dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
123 dict->SetString("final_hash",
124 base::HexEncode(final_hash->data(), final_hash->size()));
126 return dict;
129 base::Value* ItemFinishedNetLogCallback(bool auto_opened,
130 net::NetLog::LogLevel log_level) {
131 base::DictionaryValue* dict = new base::DictionaryValue();
133 dict->SetString("auto_opened", auto_opened ? "yes" : "no");
135 return dict;
138 base::Value* ItemCanceledNetLogCallback(int64 bytes_so_far,
139 const std::string* hash_state,
140 net::NetLog::LogLevel log_level) {
141 base::DictionaryValue* dict = new base::DictionaryValue();
143 dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
144 dict->SetString("hash_state",
145 base::HexEncode(hash_state->data(), hash_state->size()));
147 return dict;
150 base::Value* FileOpenedNetLogCallback(const base::FilePath* file_name,
151 int64 start_offset,
152 net::NetLog::LogLevel log_level) {
153 base::DictionaryValue* dict = new base::DictionaryValue();
155 dict->SetString("file_name", file_name->AsUTF8Unsafe());
156 dict->SetString("start_offset", base::Int64ToString(start_offset));
158 return dict;
161 base::Value* FileStreamDrainedNetLogCallback(size_t stream_size,
162 size_t num_buffers,
163 net::NetLog::LogLevel log_level) {
164 base::DictionaryValue* dict = new base::DictionaryValue();
166 dict->SetInteger("stream_size", static_cast<int>(stream_size));
167 dict->SetInteger("num_buffers", static_cast<int>(num_buffers));
169 return dict;
172 base::Value* FileRenamedNetLogCallback(const base::FilePath* old_filename,
173 const base::FilePath* new_filename,
174 net::NetLog::LogLevel log_level) {
175 base::DictionaryValue* dict = new base::DictionaryValue();
177 dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
178 dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
180 return dict;
183 base::Value* FileErrorNetLogCallback(const char* operation,
184 net::Error net_error,
185 net::NetLog::LogLevel log_level) {
186 base::DictionaryValue* dict = new base::DictionaryValue();
188 dict->SetString("operation", operation);
189 dict->SetInteger("net_error", net_error);
191 return dict;
194 base::Value* FileInterruptedNetLogCallback(const char* operation,
195 int os_error,
196 DownloadInterruptReason reason,
197 net::NetLog::LogLevel log_level) {
198 base::DictionaryValue* dict = new base::DictionaryValue();
200 dict->SetString("operation", operation);
201 if (os_error != 0)
202 dict->SetInteger("os_error", os_error);
203 dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
205 return dict;
208 } // namespace content