Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / download / download_net_log_parameters.cc
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.
4
5 #include "content/browser/download/download_net_log_parameters.h"
6
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"
15
16 namespace content {
17
18 namespace {
19
20 static const char* download_type_names[] = {
21   "NEW_DOWNLOAD",
22   "HISTORY_IMPORT",
23   "SAVE_PAGE_AS"
24 };
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"
35 };
36
37 COMPILE_ASSERT(arraysize(download_type_names) == SRC_SAVE_PAGE_AS + 1,
38                download_type_enum_has_changed);
39 COMPILE_ASSERT(arraysize(download_danger_names) == DOWNLOAD_DANGER_TYPE_MAX,
40                download_danger_enum_has_changed);
41
42 }  // namespace
43
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();
50
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());
61
62   return dict;
63 }
64
65 base::Value* ItemCheckedNetLogCallback(
66     DownloadDangerType danger_type,
67     net::NetLog::LogLevel log_level) {
68   base::DictionaryValue* dict = new base::DictionaryValue();
69
70   dict->SetString("danger_type", download_danger_names[danger_type]);
71
72   return dict;
73 }
74
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();
79
80   dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
81   dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
82
83   return dict;
84 }
85
86 base::Value* ItemInterruptedNetLogCallback(DownloadInterruptReason reason,
87                                            int64 bytes_so_far,
88                                            const std::string* hash_state,
89                                            net::NetLog::LogLevel log_level) {
90   base::DictionaryValue* dict = new base::DictionaryValue();
91
92   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(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()));
96
97   return dict;
98 }
99
100 base::Value* ItemResumingNetLogCallback(bool user_initiated,
101                                         DownloadInterruptReason reason,
102                                         int64 bytes_so_far,
103                                         const std::string* hash_state,
104                                         net::NetLog::LogLevel log_level) {
105   base::DictionaryValue* dict = new base::DictionaryValue();
106
107   dict->SetString("user_initiated", user_initiated ? "true" : "false");
108   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(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()));
112
113   return dict;
114 }
115
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();
120
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()));
124
125   return dict;
126 }
127
128 base::Value* ItemFinishedNetLogCallback(bool auto_opened,
129                                         net::NetLog::LogLevel log_level) {
130   base::DictionaryValue* dict = new base::DictionaryValue();
131
132   dict->SetString("auto_opened", auto_opened ? "yes" : "no");
133
134   return dict;
135 }
136
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();
141
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()));
145
146   return dict;
147 }
148
149 base::Value* FileOpenedNetLogCallback(const base::FilePath* file_name,
150                                       int64 start_offset,
151                                       net::NetLog::LogLevel log_level) {
152   base::DictionaryValue* dict = new base::DictionaryValue();
153
154   dict->SetString("file_name", file_name->AsUTF8Unsafe());
155   dict->SetString("start_offset", base::Int64ToString(start_offset));
156
157   return dict;
158 }
159
160 base::Value* FileStreamDrainedNetLogCallback(size_t stream_size,
161                                              size_t num_buffers,
162                                              net::NetLog::LogLevel log_level) {
163   base::DictionaryValue* dict = new base::DictionaryValue();
164
165   dict->SetInteger("stream_size", static_cast<int>(stream_size));
166   dict->SetInteger("num_buffers", static_cast<int>(num_buffers));
167
168   return dict;
169 }
170
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();
175
176   dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
177   dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
178
179   return dict;
180 }
181
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();
186
187   dict->SetString("operation", operation);
188   dict->SetInteger("net_error", net_error);
189
190   return dict;
191 }
192
193 base::Value* FileInterruptedNetLogCallback(const char* operation,
194                                            int os_error,
195                                            DownloadInterruptReason reason,
196                                            net::NetLog::LogLevel log_level) {
197   base::DictionaryValue* dict = new base::DictionaryValue();
198
199   dict->SetString("operation", operation);
200   if (os_error != 0)
201     dict->SetInteger("os_error", os_error);
202   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
203
204   return dict;
205 }
206
207 }  // namespace content