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_interrupt_reasons_impl.h"
6
7#include "base/logging.h"
8
9namespace content {
10
11DownloadInterruptReason ConvertFileErrorToInterruptReason(
12    base::File::Error file_error) {
13  switch (file_error) {
14    case base::File::FILE_OK:
15      return DOWNLOAD_INTERRUPT_REASON_NONE;
16
17    case base::File::FILE_ERROR_IN_USE:
18      return DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR;
19
20    case base::File::FILE_ERROR_ACCESS_DENIED:
21      return DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
22
23    case base::File::FILE_ERROR_TOO_MANY_OPENED:
24      return DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR;
25
26    case base::File::FILE_ERROR_NO_MEMORY:
27      return DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR;
28
29    case base::File::FILE_ERROR_NO_SPACE:
30      return DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE;
31
32    case base::File::FILE_ERROR_SECURITY:
33      return DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
34
35    default:
36      return DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
37  }
38}
39
40DownloadInterruptReason ConvertNetErrorToInterruptReason(
41    net::Error net_error, DownloadInterruptSource source) {
42  switch (net_error) {
43    case net::OK:
44      return DOWNLOAD_INTERRUPT_REASON_NONE;
45
46    // File errors.
47
48    // The file is too large.
49    case net::ERR_FILE_TOO_BIG:
50      return DOWNLOAD_INTERRUPT_REASON_FILE_TOO_LARGE;
51
52    // Permission to access a resource, other than the network, was denied.
53    case net::ERR_ACCESS_DENIED:
54      return DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED;
55
56    // There were not enough resources to complete the operation.
57    case net::ERR_INSUFFICIENT_RESOURCES:
58      return DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR;
59
60    // Memory allocation failed.
61    case net::ERR_OUT_OF_MEMORY:
62      return DOWNLOAD_INTERRUPT_REASON_FILE_TRANSIENT_ERROR;
63
64    // The path or file name is too long.
65    case net::ERR_FILE_PATH_TOO_LONG:
66      return DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG;
67
68    // Not enough room left on the disk.
69    case net::ERR_FILE_NO_SPACE:
70      return DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE;
71
72    // The file has a virus.
73    case net::ERR_FILE_VIRUS_INFECTED:
74      return DOWNLOAD_INTERRUPT_REASON_FILE_VIRUS_INFECTED;
75
76    // The file was blocked by local policy.
77    case net::ERR_BLOCKED_BY_CLIENT:
78      return DOWNLOAD_INTERRUPT_REASON_FILE_BLOCKED;
79
80    // Network errors.
81
82    // The network operation timed out.
83    case net::ERR_TIMED_OUT:
84      return DOWNLOAD_INTERRUPT_REASON_NETWORK_TIMEOUT;
85
86    // The network connection has been lost.
87    case net::ERR_INTERNET_DISCONNECTED:
88      return DOWNLOAD_INTERRUPT_REASON_NETWORK_DISCONNECTED;
89
90    // The server has gone down.
91    case net::ERR_CONNECTION_FAILED:
92      return DOWNLOAD_INTERRUPT_REASON_NETWORK_SERVER_DOWN;
93
94    // Server responses.
95
96    // The server does not support range requests.
97    case net::ERR_REQUEST_RANGE_NOT_SATISFIABLE:
98      return DOWNLOAD_INTERRUPT_REASON_SERVER_NO_RANGE;
99
100    default: break;
101  }
102
103  // Handle errors that don't have mappings, depending on the source.
104  switch (source) {
105    case DOWNLOAD_INTERRUPT_FROM_DISK:
106      return DOWNLOAD_INTERRUPT_REASON_FILE_FAILED;
107    case DOWNLOAD_INTERRUPT_FROM_NETWORK:
108      return DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED;
109    case DOWNLOAD_INTERRUPT_FROM_SERVER:
110      return DOWNLOAD_INTERRUPT_REASON_SERVER_FAILED;
111    default:
112      break;
113  }
114
115  NOTREACHED();
116
117  return DOWNLOAD_INTERRUPT_REASON_NONE;
118}
119
120std::string DownloadInterruptReasonToString(DownloadInterruptReason error) {
121
122#define INTERRUPT_REASON(name, value)  \
123    case DOWNLOAD_INTERRUPT_REASON_##name: return #name;
124
125  switch (error) {
126    INTERRUPT_REASON(NONE, 0)
127
128#include "content/public/browser/download_interrupt_reason_values.h"
129
130    default:
131      break;
132  }
133
134#undef INTERRUPT_REASON
135
136  return "Unknown error";
137}
138
139}  // namespace content
140