Add ICU message format support
[chromium-blink-merge.git] / content / browser / download / download_net_log_parameters.cc
blob165496ca3d60cb44f1937756e15ef0d1aea63302
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 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");
42 } // namespace
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());
62 return dict.Pass();
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]);
72 return dict.Pass();
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());
84 return dict.Pass();
87 scoped_ptr<base::Value> ItemInterruptedNetLogCallback(
88 DownloadInterruptReason reason,
89 int64 bytes_so_far,
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()));
99 return dict.Pass();
102 scoped_ptr<base::Value> ItemResumingNetLogCallback(
103 bool user_initiated,
104 DownloadInterruptReason reason,
105 int64 bytes_so_far,
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()));
116 return dict.Pass();
119 scoped_ptr<base::Value> ItemCompletingNetLogCallback(
120 int64 bytes_so_far,
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()));
129 return dict.Pass();
132 scoped_ptr<base::Value> ItemFinishedNetLogCallback(
133 bool auto_opened,
134 net::NetLogCaptureMode capture_mode) {
135 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
137 dict->SetString("auto_opened", auto_opened ? "yes" : "no");
139 return dict.Pass();
142 scoped_ptr<base::Value> ItemCanceledNetLogCallback(
143 int64 bytes_so_far,
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()));
152 return dict.Pass();
155 scoped_ptr<base::Value> FileOpenedNetLogCallback(
156 const base::FilePath* file_name,
157 int64 start_offset,
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));
164 return dict.Pass();
167 scoped_ptr<base::Value> FileStreamDrainedNetLogCallback(
168 size_t stream_size,
169 size_t num_buffers,
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));
176 return dict.Pass();
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());
188 return dict.Pass();
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);
200 return dict.Pass();
203 scoped_ptr<base::Value> FileInterruptedNetLogCallback(
204 const char* operation,
205 int os_error,
206 DownloadInterruptReason reason,
207 net::NetLogCaptureMode capture_mode) {
208 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
210 dict->SetString("operation", operation);
211 if (os_error != 0)
212 dict->SetInteger("os_error", os_error);
213 dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
215 return dict.Pass();
218 } // namespace content