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"
20 static const char* download_type_names
[] = {
25 static const char* download_danger_names
[] = {
30 "MAYBE_DANGEROUS_CONTENT",
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
);
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());
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
]);
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());
87 base::Value
* ItemInterruptedNetLogCallback(DownloadInterruptReason reason
,
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()));
101 base::Value
* ItemResumingNetLogCallback(bool user_initiated
,
102 DownloadInterruptReason reason
,
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()));
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()));
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");
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()));
150 base::Value
* FileOpenedNetLogCallback(const base::FilePath
* file_name
,
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
));
161 base::Value
* FileStreamDrainedNetLogCallback(size_t stream_size
,
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
));
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());
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
);
194 base::Value
* FileInterruptedNetLogCallback(const char* operation
,
196 DownloadInterruptReason reason
,
197 net::NetLog::LogLevel log_level
) {
198 base::DictionaryValue
* dict
= new base::DictionaryValue();
200 dict
->SetString("operation", operation
);
202 dict
->SetInteger("os_error", os_error
);
203 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
208 } // namespace content