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 static_assert(arraysize(download_type_names
) == SRC_SAVE_PAGE_AS
+ 1,
38 "download type enum has changed");
39 static_assert(arraysize(download_danger_names
) == DOWNLOAD_DANGER_TYPE_MAX
,
40 "download danger enum has changed");
44 base::Value
* ItemActivatedNetLogCallback(const DownloadItem
* download_item
,
45 DownloadType download_type
,
46 const std::string
* file_name
,
47 net::NetLogCaptureMode capture_mode
) {
48 base::DictionaryValue
* dict
= new base::DictionaryValue();
50 dict
->SetString("type", download_type_names
[download_type
]);
51 dict
->SetString("id", base::Int64ToString(download_item
->GetId()));
52 dict
->SetString("original_url", download_item
->GetOriginalUrl().spec());
53 dict
->SetString("final_url", download_item
->GetURL().spec());
54 dict
->SetString("file_name", *file_name
);
55 dict
->SetString("danger_type",
56 download_danger_names
[download_item
->GetDangerType()]);
57 dict
->SetString("start_offset",
58 base::Int64ToString(download_item
->GetReceivedBytes()));
59 dict
->SetBoolean("has_user_gesture", download_item
->HasUserGesture());
64 base::Value
* ItemCheckedNetLogCallback(DownloadDangerType danger_type
,
65 net::NetLogCaptureMode capture_mode
) {
66 base::DictionaryValue
* dict
= new base::DictionaryValue();
68 dict
->SetString("danger_type", download_danger_names
[danger_type
]);
73 base::Value
* ItemRenamedNetLogCallback(const base::FilePath
* old_filename
,
74 const base::FilePath
* new_filename
,
75 net::NetLogCaptureMode capture_mode
) {
76 base::DictionaryValue
* dict
= new base::DictionaryValue();
78 dict
->SetString("old_filename", old_filename
->AsUTF8Unsafe());
79 dict
->SetString("new_filename", new_filename
->AsUTF8Unsafe());
84 base::Value
* ItemInterruptedNetLogCallback(
85 DownloadInterruptReason reason
,
87 const std::string
* hash_state
,
88 net::NetLogCaptureMode capture_mode
) {
89 base::DictionaryValue
* dict
= new base::DictionaryValue();
91 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
92 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
93 dict
->SetString("hash_state",
94 base::HexEncode(hash_state
->data(), hash_state
->size()));
99 base::Value
* ItemResumingNetLogCallback(bool user_initiated
,
100 DownloadInterruptReason reason
,
102 const std::string
* hash_state
,
103 net::NetLogCaptureMode capture_mode
) {
104 base::DictionaryValue
* dict
= new base::DictionaryValue();
106 dict
->SetString("user_initiated", user_initiated
? "true" : "false");
107 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
108 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
109 dict
->SetString("hash_state",
110 base::HexEncode(hash_state
->data(), hash_state
->size()));
115 base::Value
* ItemCompletingNetLogCallback(int64 bytes_so_far
,
116 const std::string
* final_hash
,
117 net::NetLogCaptureMode capture_mode
) {
118 base::DictionaryValue
* dict
= new base::DictionaryValue();
120 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
121 dict
->SetString("final_hash",
122 base::HexEncode(final_hash
->data(), final_hash
->size()));
127 base::Value
* ItemFinishedNetLogCallback(bool auto_opened
,
128 net::NetLogCaptureMode capture_mode
) {
129 base::DictionaryValue
* dict
= new base::DictionaryValue();
131 dict
->SetString("auto_opened", auto_opened
? "yes" : "no");
136 base::Value
* ItemCanceledNetLogCallback(int64 bytes_so_far
,
137 const std::string
* hash_state
,
138 net::NetLogCaptureMode capture_mode
) {
139 base::DictionaryValue
* dict
= new base::DictionaryValue();
141 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
142 dict
->SetString("hash_state",
143 base::HexEncode(hash_state
->data(), hash_state
->size()));
148 base::Value
* FileOpenedNetLogCallback(const base::FilePath
* file_name
,
150 net::NetLogCaptureMode capture_mode
) {
151 base::DictionaryValue
* dict
= new base::DictionaryValue();
153 dict
->SetString("file_name", file_name
->AsUTF8Unsafe());
154 dict
->SetString("start_offset", base::Int64ToString(start_offset
));
159 base::Value
* FileStreamDrainedNetLogCallback(
162 net::NetLogCaptureMode capture_mode
) {
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::NetLogCaptureMode capture_mode
) {
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::NetLogCaptureMode capture_mode
) {
185 base::DictionaryValue
* dict
= new base::DictionaryValue();
187 dict
->SetString("operation", operation
);
188 dict
->SetInteger("net_error", net_error
);
193 base::Value
* FileInterruptedNetLogCallback(
194 const char* operation
,
196 DownloadInterruptReason reason
,
197 net::NetLogCaptureMode capture_mode
) {
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