Home | History | Annotate | Download | only in download
      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_UNSAFE(download_type_names) == SRC_SAVE_PAGE_AS + 1,
     38                download_type_enum_has_changed);
     39 COMPILE_ASSERT(ARRAYSIZE_UNSAFE(download_danger_names) ==
     40                   DOWNLOAD_DANGER_TYPE_MAX,
     41                download_danger_enum_has_changed);
     42 
     43 }  // namespace
     44 
     45 base::Value* ItemActivatedNetLogCallback(
     46     const DownloadItem* download_item,
     47     DownloadType download_type,
     48     const std::string* file_name,
     49     net::NetLog::LogLevel log_level) {
     50   base::DictionaryValue* dict = new base::DictionaryValue();
     51 
     52   dict->SetString("type", download_type_names[download_type]);
     53   dict->SetString("id", base::Int64ToString(download_item->GetId()));
     54   dict->SetString("original_url", download_item->GetOriginalUrl().spec());
     55   dict->SetString("final_url", download_item->GetURL().spec());
     56   dict->SetString("file_name", *file_name);
     57   dict->SetString("danger_type",
     58                   download_danger_names[download_item->GetDangerType()]);
     59   dict->SetString("start_offset",
     60                   base::Int64ToString(download_item->GetReceivedBytes()));
     61   dict->SetBoolean("has_user_gesture", download_item->HasUserGesture());
     62 
     63   return dict;
     64 }
     65 
     66 base::Value* ItemCheckedNetLogCallback(
     67     DownloadDangerType danger_type,
     68     net::NetLog::LogLevel log_level) {
     69   base::DictionaryValue* dict = new base::DictionaryValue();
     70 
     71   dict->SetString("danger_type", download_danger_names[danger_type]);
     72 
     73   return dict;
     74 }
     75 
     76 base::Value* ItemRenamedNetLogCallback(const base::FilePath* old_filename,
     77                                        const base::FilePath* new_filename,
     78                                        net::NetLog::LogLevel log_level) {
     79   base::DictionaryValue* dict = new base::DictionaryValue();
     80 
     81   dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
     82   dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
     83 
     84   return dict;
     85 }
     86 
     87 base::Value* ItemInterruptedNetLogCallback(DownloadInterruptReason reason,
     88                                            int64 bytes_so_far,
     89                                            const std::string* hash_state,
     90                                            net::NetLog::LogLevel log_level) {
     91   base::DictionaryValue* dict = new base::DictionaryValue();
     92 
     93   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
     94   dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
     95   dict->SetString("hash_state",
     96                   base::HexEncode(hash_state->data(), hash_state->size()));
     97 
     98   return dict;
     99 }
    100 
    101 base::Value* ItemResumingNetLogCallback(bool user_initiated,
    102                                         DownloadInterruptReason reason,
    103                                         int64 bytes_so_far,
    104                                         const std::string* hash_state,
    105                                         net::NetLog::LogLevel log_level) {
    106   base::DictionaryValue* dict = new base::DictionaryValue();
    107 
    108   dict->SetString("user_initiated", user_initiated ? "true" : "false");
    109   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
    110   dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
    111   dict->SetString("hash_state",
    112                   base::HexEncode(hash_state->data(), hash_state->size()));
    113 
    114   return dict;
    115 }
    116 
    117 base::Value* ItemCompletingNetLogCallback(int64 bytes_so_far,
    118                                           const std::string* final_hash,
    119                                           net::NetLog::LogLevel log_level) {
    120   base::DictionaryValue* dict = new base::DictionaryValue();
    121 
    122   dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
    123   dict->SetString("final_hash",
    124                   base::HexEncode(final_hash->data(), final_hash->size()));
    125 
    126   return dict;
    127 }
    128 
    129 base::Value* ItemFinishedNetLogCallback(bool auto_opened,
    130                                         net::NetLog::LogLevel log_level) {
    131   base::DictionaryValue* dict = new base::DictionaryValue();
    132 
    133   dict->SetString("auto_opened", auto_opened ? "yes" : "no");
    134 
    135   return dict;
    136 }
    137 
    138 base::Value* ItemCanceledNetLogCallback(int64 bytes_so_far,
    139                                         const std::string* hash_state,
    140                                         net::NetLog::LogLevel log_level) {
    141   base::DictionaryValue* dict = new base::DictionaryValue();
    142 
    143   dict->SetString("bytes_so_far", base::Int64ToString(bytes_so_far));
    144   dict->SetString("hash_state",
    145                   base::HexEncode(hash_state->data(), hash_state->size()));
    146 
    147   return dict;
    148 }
    149 
    150 base::Value* FileOpenedNetLogCallback(const base::FilePath* file_name,
    151                                       int64 start_offset,
    152                                       net::NetLog::LogLevel log_level) {
    153   base::DictionaryValue* dict = new base::DictionaryValue();
    154 
    155   dict->SetString("file_name", file_name->AsUTF8Unsafe());
    156   dict->SetString("start_offset", base::Int64ToString(start_offset));
    157 
    158   return dict;
    159 }
    160 
    161 base::Value* FileStreamDrainedNetLogCallback(size_t stream_size,
    162                                              size_t num_buffers,
    163                                              net::NetLog::LogLevel log_level) {
    164   base::DictionaryValue* dict = new base::DictionaryValue();
    165 
    166   dict->SetInteger("stream_size", static_cast<int>(stream_size));
    167   dict->SetInteger("num_buffers", static_cast<int>(num_buffers));
    168 
    169   return dict;
    170 }
    171 
    172 base::Value* FileRenamedNetLogCallback(const base::FilePath* old_filename,
    173                                        const base::FilePath* new_filename,
    174                                        net::NetLog::LogLevel log_level) {
    175   base::DictionaryValue* dict = new base::DictionaryValue();
    176 
    177   dict->SetString("old_filename", old_filename->AsUTF8Unsafe());
    178   dict->SetString("new_filename", new_filename->AsUTF8Unsafe());
    179 
    180   return dict;
    181 }
    182 
    183 base::Value* FileErrorNetLogCallback(const char* operation,
    184                                      net::Error net_error,
    185                                      net::NetLog::LogLevel log_level) {
    186   base::DictionaryValue* dict = new base::DictionaryValue();
    187 
    188   dict->SetString("operation", operation);
    189   dict->SetInteger("net_error", net_error);
    190 
    191   return dict;
    192 }
    193 
    194 base::Value* FileInterruptedNetLogCallback(const char* operation,
    195                                            int os_error,
    196                                            DownloadInterruptReason reason,
    197                                            net::NetLog::LogLevel log_level) {
    198   base::DictionaryValue* dict = new base::DictionaryValue();
    199 
    200   dict->SetString("operation", operation);
    201   if (os_error != 0)
    202     dict->SetInteger("os_error", os_error);
    203   dict->SetString("interrupt_reason", DownloadInterruptReasonToString(reason));
    204 
    205   return dict;
    206 }
    207 
    208 }  // namespace content
    209