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 scoped_ptr
<base::Value
> ItemActivatedNetLogCallback(
45 const DownloadItem
* download_item
,
46 DownloadType download_type
,
47 const std::string
* file_name
,
48 net::NetLogCaptureMode capture_mode
) {
49 scoped_ptr
<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 scoped_ptr
<base::Value
> ItemCheckedNetLogCallback(
66 DownloadDangerType danger_type
,
67 net::NetLogCaptureMode capture_mode
) {
68 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
70 dict
->SetString("danger_type", download_danger_names
[danger_type
]);
75 scoped_ptr
<base::Value
> ItemRenamedNetLogCallback(
76 const base::FilePath
* old_filename
,
77 const base::FilePath
* new_filename
,
78 net::NetLogCaptureMode capture_mode
) {
79 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
81 dict
->SetString("old_filename", old_filename
->AsUTF8Unsafe());
82 dict
->SetString("new_filename", new_filename
->AsUTF8Unsafe());
87 scoped_ptr
<base::Value
> ItemInterruptedNetLogCallback(
88 DownloadInterruptReason reason
,
90 const std::string
* hash_state
,
91 net::NetLogCaptureMode capture_mode
) {
92 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
94 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
95 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
96 dict
->SetString("hash_state",
97 base::HexEncode(hash_state
->data(), hash_state
->size()));
102 scoped_ptr
<base::Value
> ItemResumingNetLogCallback(
104 DownloadInterruptReason reason
,
106 const std::string
* hash_state
,
107 net::NetLogCaptureMode capture_mode
) {
108 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
110 dict
->SetString("user_initiated", user_initiated
? "true" : "false");
111 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
112 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
113 dict
->SetString("hash_state",
114 base::HexEncode(hash_state
->data(), hash_state
->size()));
119 scoped_ptr
<base::Value
> ItemCompletingNetLogCallback(
121 const std::string
* final_hash
,
122 net::NetLogCaptureMode capture_mode
) {
123 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
125 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
126 dict
->SetString("final_hash",
127 base::HexEncode(final_hash
->data(), final_hash
->size()));
132 scoped_ptr
<base::Value
> ItemFinishedNetLogCallback(
134 net::NetLogCaptureMode capture_mode
) {
135 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
137 dict
->SetString("auto_opened", auto_opened
? "yes" : "no");
142 scoped_ptr
<base::Value
> ItemCanceledNetLogCallback(
144 const std::string
* hash_state
,
145 net::NetLogCaptureMode capture_mode
) {
146 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
148 dict
->SetString("bytes_so_far", base::Int64ToString(bytes_so_far
));
149 dict
->SetString("hash_state",
150 base::HexEncode(hash_state
->data(), hash_state
->size()));
155 scoped_ptr
<base::Value
> FileOpenedNetLogCallback(
156 const base::FilePath
* file_name
,
158 net::NetLogCaptureMode capture_mode
) {
159 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
161 dict
->SetString("file_name", file_name
->AsUTF8Unsafe());
162 dict
->SetString("start_offset", base::Int64ToString(start_offset
));
167 scoped_ptr
<base::Value
> FileStreamDrainedNetLogCallback(
170 net::NetLogCaptureMode capture_mode
) {
171 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
173 dict
->SetInteger("stream_size", static_cast<int>(stream_size
));
174 dict
->SetInteger("num_buffers", static_cast<int>(num_buffers
));
179 scoped_ptr
<base::Value
> FileRenamedNetLogCallback(
180 const base::FilePath
* old_filename
,
181 const base::FilePath
* new_filename
,
182 net::NetLogCaptureMode capture_mode
) {
183 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
185 dict
->SetString("old_filename", old_filename
->AsUTF8Unsafe());
186 dict
->SetString("new_filename", new_filename
->AsUTF8Unsafe());
191 scoped_ptr
<base::Value
> FileErrorNetLogCallback(
192 const char* operation
,
193 net::Error net_error
,
194 net::NetLogCaptureMode capture_mode
) {
195 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
197 dict
->SetString("operation", operation
);
198 dict
->SetInteger("net_error", net_error
);
203 scoped_ptr
<base::Value
> FileInterruptedNetLogCallback(
204 const char* operation
,
206 DownloadInterruptReason reason
,
207 net::NetLogCaptureMode capture_mode
) {
208 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
210 dict
->SetString("operation", operation
);
212 dict
->SetInteger("os_error", os_error
);
213 dict
->SetString("interrupt_reason", DownloadInterruptReasonToString(reason
));
218 } // namespace content