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"
20 static const char* download_type_names
[] = {
25 static const char* download_danger_names
[] = {
30 "MAYBE_DANGEROUS_CONTENT",
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
);
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());
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
]);
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());
86 base::Value
* ItemInterruptedNetLogCallback(DownloadInterruptReason reason
,
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()));
100 base::Value
* ItemResumingNetLogCallback(bool user_initiated
,
101 DownloadInterruptReason reason
,
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()));
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()));
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");
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()));
149 base::Value
* FileOpenedNetLogCallback(const base::FilePath
* file_name
,
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
));
160 base::Value
* FileStreamDrainedNetLogCallback(size_t stream_size
,
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
));
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());
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
);
193 base::Value
* FileInterruptedNetLogCallback(const char* operation
,
195 DownloadInterruptReason reason
,
196 net::NetLog::LogLevel log_level
) {
197 base::DictionaryValue
* dict
= new base::DictionaryValue();
199 dict
->SetString("operation", operation
);
201 dict
->SetInteger("os_error", os_error
);
202 dict
->SetString("interrupt_reason", InterruptReasonDebugString(reason
));
207 } // namespace content