Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / common / localized_error.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 "chrome/common/localized_error.h"
6
7 #include "base/i18n/rtl.h"
8 #include "base/logging.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/common/extensions/extension_constants.h"
15 #include "chrome/common/extensions/extension_icon_set.h"
16 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
17 #include "chrome/common/net/net_error_info.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "net/base/escape.h"
21 #include "net/base/net_errors.h"
22 #include "net/base/net_util.h"
23 #include "third_party/WebKit/public/platform/WebURLError.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/webui/web_ui_util.h"
26 #include "url/gurl.h"
27
28 #if defined(OS_WIN)
29 #include "base/win/windows_version.h"
30 #endif
31
32 using blink::WebURLError;
33
34 // Some error pages have no details.
35 const unsigned int kErrorPagesNoDetails = 0;
36
37 namespace {
38
39 static const char kRedirectLoopLearnMoreUrl[] =
40     "https://www.google.com/support/chrome/bin/answer.py?answer=95626";
41 static const char kWeakDHKeyLearnMoreUrl[] =
42     "http://sites.google.com/a/chromium.org/dev/"
43     "err_ssl_weak_server_ephemeral_dh_key";
44 #if defined(OS_CHROMEOS)
45 static const char kAppWarningLearnMoreUrl[] =
46     "chrome-extension://honijodknafkokifofgiaalefdiedpko/main.html"
47     "?answer=1721911";
48 #endif  // defined(OS_CHROMEOS)
49
50 enum NAV_SUGGESTIONS {
51   SUGGEST_NONE                  = 0,
52   SUGGEST_RELOAD                = 1 << 0,
53   SUGGEST_CHECK_CONNECTION      = 1 << 1,
54   SUGGEST_DNS_CONFIG            = 1 << 2,
55   SUGGEST_FIREWALL_CONFIG       = 1 << 3,
56   SUGGEST_PROXY_CONFIG          = 1 << 4,
57   SUGGEST_DISABLE_EXTENSION     = 1 << 5,
58   SUGGEST_LEARNMORE             = 1 << 6,
59   SUGGEST_VIEW_POLICIES         = 1 << 7,
60   SUGGEST_CONTACT_ADMINISTRATOR = 1 << 8,
61 };
62
63 struct LocalizedErrorMap {
64   int error_code;
65   unsigned int title_resource_id;
66   unsigned int heading_resource_id;
67   // Detailed summary used when the error is in the main frame.
68   unsigned int summary_resource_id;
69   // Short one sentence description shown on mouse over when the error is in
70   // a frame.
71   unsigned int details_resource_id;
72   int suggestions;  // Bitmap of SUGGEST_* values.
73 };
74
75 const LocalizedErrorMap net_error_options[] = {
76   {net::ERR_TIMED_OUT,
77    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
78    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
79    IDS_ERRORPAGES_SUMMARY_TIMED_OUT,
80    IDS_ERRORPAGES_DETAILS_TIMED_OUT,
81    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_FIREWALL_CONFIG |
82        SUGGEST_PROXY_CONFIG,
83   },
84   {net::ERR_CONNECTION_TIMED_OUT,
85    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
86    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
87    IDS_ERRORPAGES_SUMMARY_TIMED_OUT,
88    IDS_ERRORPAGES_DETAILS_TIMED_OUT,
89    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_FIREWALL_CONFIG |
90        SUGGEST_PROXY_CONFIG,
91   },
92   {net::ERR_CONNECTION_CLOSED,
93    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
94    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
95    IDS_ERRORPAGES_SUMMARY_NOT_AVAILABLE,
96    IDS_ERRORPAGES_DETAILS_CONNECTION_CLOSED,
97    SUGGEST_RELOAD,
98   },
99   {net::ERR_CONNECTION_RESET,
100    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
101    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
102    IDS_ERRORPAGES_SUMMARY_CONNECTION_RESET,
103    IDS_ERRORPAGES_DETAILS_CONNECTION_RESET,
104    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_FIREWALL_CONFIG |
105        SUGGEST_PROXY_CONFIG,
106   },
107   {net::ERR_CONNECTION_REFUSED,
108    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
109    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
110    IDS_ERRORPAGES_SUMMARY_CONNECTION_REFUSED,
111    IDS_ERRORPAGES_DETAILS_CONNECTION_REFUSED,
112    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_FIREWALL_CONFIG |
113        SUGGEST_PROXY_CONFIG,
114   },
115   {net::ERR_CONNECTION_FAILED,
116    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
117    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
118    IDS_ERRORPAGES_SUMMARY_NOT_AVAILABLE,
119    IDS_ERRORPAGES_DETAILS_CONNECTION_FAILED,
120    SUGGEST_RELOAD,
121   },
122   {net::ERR_NAME_NOT_RESOLVED,
123    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
124    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
125    IDS_ERRORPAGES_SUMMARY_NAME_NOT_RESOLVED,
126    IDS_ERRORPAGES_DETAILS_NAME_NOT_RESOLVED,
127    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_DNS_CONFIG |
128        SUGGEST_FIREWALL_CONFIG | SUGGEST_PROXY_CONFIG,
129   },
130   {net::ERR_ADDRESS_UNREACHABLE,
131    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
132    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
133    IDS_ERRORPAGES_SUMMARY_ADDRESS_UNREACHABLE,
134    IDS_ERRORPAGES_DETAILS_ADDRESS_UNREACHABLE,
135    SUGGEST_RELOAD | SUGGEST_FIREWALL_CONFIG | SUGGEST_PROXY_CONFIG,
136   },
137   {net::ERR_NETWORK_ACCESS_DENIED,
138    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
139    IDS_ERRORPAGES_HEADING_NETWORK_ACCESS_DENIED,
140    IDS_ERRORPAGES_SUMMARY_NETWORK_ACCESS_DENIED,
141    IDS_ERRORPAGES_DETAILS_NETWORK_ACCESS_DENIED,
142    SUGGEST_FIREWALL_CONFIG,
143   },
144   {net::ERR_PROXY_CONNECTION_FAILED,
145    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
146    IDS_ERRORPAGES_HEADING_PROXY_CONNECTION_FAILED,
147    IDS_ERRORPAGES_SUMMARY_PROXY_CONNECTION_FAILED,
148    IDS_ERRORPAGES_DETAILS_PROXY_CONNECTION_FAILED,
149    SUGGEST_PROXY_CONFIG,
150   },
151   {net::ERR_INTERNET_DISCONNECTED,
152    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
153    IDS_ERRORPAGES_HEADING_INTERNET_DISCONNECTED,
154    IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED,
155    IDS_ERRORPAGES_DETAILS_INTERNET_DISCONNECTED,
156    SUGGEST_NONE,
157   },
158   {net::ERR_FILE_NOT_FOUND,
159    IDS_ERRORPAGES_TITLE_NOT_FOUND,
160    IDS_ERRORPAGES_HEADING_NOT_FOUND,
161    IDS_ERRORPAGES_SUMMARY_NOT_FOUND,
162    IDS_ERRORPAGES_DETAILS_FILE_NOT_FOUND,
163    SUGGEST_NONE,
164   },
165   {net::ERR_CACHE_MISS,
166    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
167    IDS_ERRORPAGES_HEADING_CACHE_MISS,
168    IDS_ERRORPAGES_SUMMARY_CACHE_MISS,
169    IDS_ERRORPAGES_DETAILS_CACHE_MISS,
170    SUGGEST_RELOAD,
171   },
172   {net::ERR_CACHE_READ_FAILURE,
173    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
174    IDS_ERRORPAGES_HEADING_CACHE_READ_FAILURE,
175    IDS_ERRORPAGES_SUMMARY_CACHE_READ_FAILURE,
176    IDS_ERRORPAGES_DETAILS_CACHE_READ_FAILURE,
177    SUGGEST_RELOAD,
178   },
179   {net::ERR_NETWORK_IO_SUSPENDED,
180    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
181    IDS_ERRORPAGES_HEADING_NETWORK_IO_SUSPENDED,
182    IDS_ERRORPAGES_SUMMARY_NETWORK_IO_SUSPENDED,
183    IDS_ERRORPAGES_DETAILS_NETWORK_IO_SUSPENDED,
184    SUGGEST_RELOAD,
185   },
186   {net::ERR_TOO_MANY_REDIRECTS,
187    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
188    IDS_ERRORPAGES_HEADING_TOO_MANY_REDIRECTS,
189    IDS_ERRORPAGES_SUMMARY_TOO_MANY_REDIRECTS,
190    IDS_ERRORPAGES_DETAILS_TOO_MANY_REDIRECTS,
191    SUGGEST_RELOAD | SUGGEST_LEARNMORE,
192   },
193   {net::ERR_EMPTY_RESPONSE,
194    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
195    IDS_ERRORPAGES_HEADING_EMPTY_RESPONSE,
196    IDS_ERRORPAGES_SUMMARY_EMPTY_RESPONSE,
197    IDS_ERRORPAGES_DETAILS_EMPTY_RESPONSE,
198    SUGGEST_RELOAD,
199   },
200   {net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH,
201    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
202    IDS_ERRORPAGES_HEADING_DUPLICATE_HEADERS,
203    IDS_ERRORPAGES_SUMMARY_DUPLICATE_HEADERS,
204    IDS_ERRORPAGES_DETAILS_RESPONSE_HEADERS_MULTIPLE_CONTENT_LENGTH,
205    SUGGEST_NONE,
206   },
207   {net::ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,
208    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
209    IDS_ERRORPAGES_HEADING_DUPLICATE_HEADERS,
210    IDS_ERRORPAGES_SUMMARY_DUPLICATE_HEADERS,
211    IDS_ERRORPAGES_DETAILS_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION,
212    SUGGEST_NONE,
213   },
214   {net::ERR_RESPONSE_HEADERS_MULTIPLE_LOCATION,
215    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
216    IDS_ERRORPAGES_HEADING_DUPLICATE_HEADERS,
217    IDS_ERRORPAGES_SUMMARY_DUPLICATE_HEADERS,
218    IDS_ERRORPAGES_DETAILS_RESPONSE_HEADERS_MULTIPLE_LOCATION,
219    SUGGEST_NONE,
220   },
221   {net::ERR_CONTENT_LENGTH_MISMATCH,
222    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
223    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
224    IDS_ERRORPAGES_SUMMARY_NOT_AVAILABLE,
225    IDS_ERRORPAGES_DETAILS_CONNECTION_CLOSED,
226    SUGGEST_RELOAD,
227   },
228   {net::ERR_INCOMPLETE_CHUNKED_ENCODING,
229    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
230    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
231    IDS_ERRORPAGES_SUMMARY_NOT_AVAILABLE,
232    IDS_ERRORPAGES_DETAILS_CONNECTION_CLOSED,
233    SUGGEST_RELOAD,
234   },
235   {net::ERR_SSL_PROTOCOL_ERROR,
236    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
237    IDS_ERRORPAGES_HEADING_SSL_PROTOCOL_ERROR,
238    IDS_ERRORPAGES_SUMMARY_SSL_PROTOCOL_ERROR,
239    IDS_ERRORPAGES_DETAILS_SSL_PROTOCOL_ERROR,
240    SUGGEST_NONE,
241   },
242   {net::ERR_SSL_UNSAFE_NEGOTIATION,
243    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
244    IDS_ERRORPAGES_HEADING_SSL_PROTOCOL_ERROR,
245    IDS_ERRORPAGES_SUMMARY_SSL_PROTOCOL_ERROR,
246    IDS_ERRORPAGES_DETAILS_SSL_UNSAFE_NEGOTIATION,
247    SUGGEST_NONE,
248   },
249   {net::ERR_BAD_SSL_CLIENT_AUTH_CERT,
250    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
251    IDS_ERRORPAGES_HEADING_BAD_SSL_CLIENT_AUTH_CERT,
252    IDS_ERRORPAGES_SUMMARY_BAD_SSL_CLIENT_AUTH_CERT,
253    IDS_ERRORPAGES_DETAILS_BAD_SSL_CLIENT_AUTH_CERT,
254    SUGGEST_NONE,
255   },
256   {net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY,
257    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
258    IDS_ERRORPAGES_HEADING_WEAK_SERVER_EPHEMERAL_DH_KEY,
259    IDS_ERRORPAGES_SUMMARY_WEAK_SERVER_EPHEMERAL_DH_KEY,
260    IDS_ERRORPAGES_DETAILS_SSL_PROTOCOL_ERROR,
261    SUGGEST_LEARNMORE,
262   },
263   {net::ERR_SSL_PINNED_KEY_NOT_IN_CERT_CHAIN,
264    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
265    IDS_ERRORPAGES_HEADING_PINNING_FAILURE,
266    IDS_ERRORPAGES_SUMMARY_PINNING_FAILURE,
267    IDS_ERRORPAGES_DETAILS_PINNING_FAILURE,
268    SUGGEST_NONE,
269   },
270   {net::ERR_TEMPORARILY_THROTTLED,
271    IDS_ERRORPAGES_TITLE_ACCESS_DENIED,
272    IDS_ERRORPAGES_HEADING_ACCESS_DENIED,
273    IDS_ERRORPAGES_SUMMARY_TEMPORARILY_THROTTLED,
274    IDS_ERRORPAGES_DETAILS_TEMPORARILY_THROTTLED,
275    SUGGEST_NONE,
276   },
277   {net::ERR_BLOCKED_BY_CLIENT,
278    IDS_ERRORPAGES_TITLE_BLOCKED,
279    IDS_ERRORPAGES_HEADING_BLOCKED,
280    IDS_ERRORPAGES_SUMMARY_BLOCKED,
281    IDS_ERRORPAGES_DETAILS_BLOCKED,
282    SUGGEST_RELOAD | SUGGEST_DISABLE_EXTENSION,
283   },
284   {net::ERR_NETWORK_CHANGED,
285    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
286    IDS_ERRORPAGES_HEADING_NETWORK_ACCESS_DENIED,
287    IDS_ERRORPAGES_SUMMARY_NETWORK_CHANGED,
288    IDS_ERRORPAGES_DETAILS_NETWORK_CHANGED,
289    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION,
290   },
291   {net::ERR_BLOCKED_BY_ADMINISTRATOR,
292    IDS_ERRORPAGES_TITLE_BLOCKED,
293    IDS_ERRORPAGES_HEADING_BLOCKED_BY_ADMINISTRATOR,
294    IDS_ERRORPAGES_SUMMARY_BLOCKED_BY_ADMINISTRATOR,
295    IDS_ERRORPAGES_DETAILS_BLOCKED_BY_ADMINISTRATOR,
296    SUGGEST_VIEW_POLICIES | SUGGEST_CONTACT_ADMINISTRATOR,
297   },
298 };
299
300 // Special error page to be used in the case of navigating back to a page
301 // generated by a POST.  LocalizedError::HasStrings expects this net error code
302 // to also appear in the array above.
303 const LocalizedErrorMap repost_error = {
304   net::ERR_CACHE_MISS,
305   IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
306   IDS_HTTP_POST_WARNING_TITLE,
307   IDS_ERRORPAGES_HTTP_POST_WARNING,
308   IDS_ERRORPAGES_DETAILS_CACHE_MISS,
309   SUGGEST_RELOAD,
310 };
311
312 const LocalizedErrorMap http_error_options[] = {
313   {403,
314    IDS_ERRORPAGES_TITLE_ACCESS_DENIED,
315    IDS_ERRORPAGES_HEADING_ACCESS_DENIED,
316    IDS_ERRORPAGES_SUMMARY_FORBIDDEN,
317    IDS_ERRORPAGES_DETAILS_FORBIDDEN,
318    SUGGEST_NONE,
319   },
320   {410,
321    IDS_ERRORPAGES_TITLE_NOT_FOUND,
322    IDS_ERRORPAGES_HEADING_NOT_FOUND,
323    IDS_ERRORPAGES_SUMMARY_GONE,
324    IDS_ERRORPAGES_DETAILS_GONE,
325    SUGGEST_NONE,
326   },
327
328   {500,
329    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
330    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
331    IDS_ERRORPAGES_SUMMARY_INTERNAL_SERVER_ERROR,
332    IDS_ERRORPAGES_DETAILS_INTERNAL_SERVER_ERROR,
333    SUGGEST_RELOAD,
334   },
335   {501,
336    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
337    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
338    IDS_ERRORPAGES_SUMMARY_WEBSITE_CANNOT_HANDLE,
339    IDS_ERRORPAGES_DETAILS_NOT_IMPLEMENTED,
340    SUGGEST_NONE,
341   },
342   {502,
343    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
344    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
345    IDS_ERRORPAGES_SUMMARY_BAD_GATEWAY,
346    IDS_ERRORPAGES_DETAILS_BAD_GATEWAY,
347    SUGGEST_RELOAD,
348   },
349   {503,
350    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
351    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
352    IDS_ERRORPAGES_SUMMARY_SERVICE_UNAVAILABLE,
353    IDS_ERRORPAGES_DETAILS_SERVICE_UNAVAILABLE,
354    SUGGEST_RELOAD,
355   },
356   {504,
357    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
358    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
359    IDS_ERRORPAGES_SUMMARY_GATEWAY_TIMEOUT,
360    IDS_ERRORPAGES_DETAILS_GATEWAY_TIMEOUT,
361    SUGGEST_RELOAD,
362   },
363   {505,
364    IDS_ERRORPAGES_TITLE_LOAD_FAILED,
365    IDS_ERRORPAGES_HEADING_HTTP_SERVER_ERROR,
366    IDS_ERRORPAGES_SUMMARY_WEBSITE_CANNOT_HANDLE,
367    IDS_ERRORPAGES_DETAILS_HTTP_VERSION_NOT_SUPPORTED,
368    SUGGEST_NONE,
369   },
370 };
371
372 const LocalizedErrorMap dns_probe_error_options[] = {
373   {chrome_common_net::DNS_PROBE_POSSIBLE,
374    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
375    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
376    IDS_ERRORPAGES_SUMMARY_DNS_PROBE_RUNNING,
377    IDS_ERRORPAGES_DETAILS_DNS_PROBE_RUNNING,
378    SUGGEST_RELOAD,
379   },
380
381   // DNS_PROBE_NOT_RUN is not here; NetErrorHelper will restore the original
382   // error, which might be one of several DNS-related errors.
383
384   {chrome_common_net::DNS_PROBE_STARTED,
385    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
386    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
387    IDS_ERRORPAGES_SUMMARY_DNS_PROBE_RUNNING,
388    IDS_ERRORPAGES_DETAILS_DNS_PROBE_RUNNING,
389    // Include SUGGEST_RELOAD so the More button doesn't jump when we update.
390    SUGGEST_RELOAD,
391   },
392
393   // DNS_PROBE_FINISHED_UNKNOWN is not here; NetErrorHelper will restore the
394   // original error, which might be one of several DNS-related errors.
395
396   {chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET,
397    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
398    IDS_ERRORPAGES_HEADING_INTERNET_DISCONNECTED,
399    IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED,
400    IDS_ERRORPAGES_DETAILS_INTERNET_DISCONNECTED,
401    SUGGEST_RELOAD | SUGGEST_CHECK_CONNECTION | SUGGEST_FIREWALL_CONFIG,
402   },
403   {chrome_common_net::DNS_PROBE_FINISHED_BAD_CONFIG,
404    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
405    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
406    IDS_ERRORPAGES_SUMMARY_NAME_NOT_RESOLVED,
407    IDS_ERRORPAGES_DETAILS_NAME_NOT_RESOLVED,
408    SUGGEST_RELOAD | SUGGEST_DNS_CONFIG | SUGGEST_FIREWALL_CONFIG,
409   },
410   {chrome_common_net::DNS_PROBE_FINISHED_NXDOMAIN,
411    IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
412    IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
413    IDS_ERRORPAGES_SUMMARY_NAME_NOT_RESOLVED,
414    IDS_ERRORPAGES_DETAILS_NAME_NOT_RESOLVED,
415    SUGGEST_RELOAD,
416   },
417 };
418
419 const LocalizedErrorMap* FindErrorMapInArray(const LocalizedErrorMap* maps,
420                                                    size_t num_maps,
421                                                    int error_code) {
422   for (size_t i = 0; i < num_maps; ++i) {
423     if (maps[i].error_code == error_code)
424       return &maps[i];
425   }
426   return NULL;
427 }
428
429 const LocalizedErrorMap* LookupErrorMap(const std::string& error_domain,
430                                         int error_code, bool is_post) {
431   if (error_domain == net::kErrorDomain) {
432     // Display a different page in the special case of navigating through the
433     // history to an uncached page created by a POST.
434     if (is_post && error_code == net::ERR_CACHE_MISS)
435       return &repost_error;
436     return FindErrorMapInArray(net_error_options,
437                                arraysize(net_error_options),
438                                error_code);
439   } else if (error_domain == LocalizedError::kHttpErrorDomain) {
440     return FindErrorMapInArray(http_error_options,
441                                arraysize(http_error_options),
442                                error_code);
443   } else if (error_domain == chrome_common_net::kDnsProbeErrorDomain) {
444     const LocalizedErrorMap* map =
445         FindErrorMapInArray(dns_probe_error_options,
446                             arraysize(dns_probe_error_options),
447                             error_code);
448     DCHECK(map);
449     return map;
450   } else {
451     NOTREACHED();
452     return NULL;
453   }
454 }
455
456 bool LocaleIsRTL() {
457 #if defined(TOOLKIT_GTK)
458   // base::i18n::IsRTL() uses the GTK text direction, which doesn't work within
459   // the renderer sandbox.
460   return base::i18n::ICUIsRTL();
461 #else
462   return base::i18n::IsRTL();
463 #endif
464 }
465
466 // Returns a dictionary containing the strings for the settings menu under the
467 // wrench, and the advanced settings button.
468 base::DictionaryValue* GetStandardMenuItemsText() {
469   base::DictionaryValue* standard_menu_items_text = new base::DictionaryValue();
470   standard_menu_items_text->SetString("settingsTitle",
471       l10n_util::GetStringUTF16(IDS_SETTINGS_TITLE));
472   standard_menu_items_text->SetString("advancedTitle",
473       l10n_util::GetStringUTF16(IDS_SETTINGS_SHOW_ADVANCED_SETTINGS));
474   return standard_menu_items_text;
475 }
476
477 // Gets the icon class for a given |error_domain| and |error_code|.
478 const char* GetIconClassForError(const std::string& error_domain,
479                                  int error_code) {
480   if ((error_code == net::ERR_INTERNET_DISCONNECTED &&
481        error_domain == net::kErrorDomain) ||
482       (error_code == chrome_common_net::DNS_PROBE_FINISHED_NO_INTERNET &&
483        error_domain == chrome_common_net::kDnsProbeErrorDomain))
484     return "icon-offline";
485
486   return "icon-generic";
487 }
488
489 }  // namespace
490
491 const char LocalizedError::kHttpErrorDomain[] = "http";
492
493 void LocalizedError::GetStrings(int error_code,
494                                 const std::string& error_domain,
495                                 const GURL& failed_url,
496                                 bool is_post,
497                                 bool stale_copy_in_cache,
498                                 const std::string& locale,
499                                 const std::string& accept_languages,
500                                 base::DictionaryValue* error_strings) {
501   bool rtl = LocaleIsRTL();
502   error_strings->SetString("textdirection", rtl ? "rtl" : "ltr");
503
504   // Grab the strings and settings that depend on the error type.  Init
505   // options with default values.
506   LocalizedErrorMap options = {
507     0,
508     IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
509     IDS_ERRORPAGES_HEADING_NOT_AVAILABLE,
510     IDS_ERRORPAGES_SUMMARY_NOT_AVAILABLE,
511     kErrorPagesNoDetails,
512     SUGGEST_NONE,
513   };
514
515   const LocalizedErrorMap* error_map = LookupErrorMap(error_domain, error_code,
516                                                       is_post);
517   if (error_map)
518     options = *error_map;
519
520   // If we got "access denied" but the url was a file URL, then we say it was a
521   // file instead of just using the "not available" default message. Just adding
522   // ERR_ACCESS_DENIED to the map isn't sufficient, since that message may be
523   // generated by some OSs when the operation doesn't involve a file URL.
524   if (error_domain == net::kErrorDomain &&
525       error_code == net::ERR_ACCESS_DENIED &&
526       failed_url.scheme() == "file") {
527     options.title_resource_id = IDS_ERRORPAGES_TITLE_ACCESS_DENIED;
528     options.heading_resource_id = IDS_ERRORPAGES_HEADING_FILE_ACCESS_DENIED;
529     options.summary_resource_id = IDS_ERRORPAGES_SUMMARY_FILE_ACCESS_DENIED;
530     options.details_resource_id = IDS_ERRORPAGES_DETAILS_FILE_ACCESS_DENIED;
531     options.suggestions = SUGGEST_NONE;
532   }
533
534   base::string16 failed_url_string(net::FormatUrl(
535       failed_url, accept_languages, net::kFormatUrlOmitNothing,
536       net::UnescapeRule::NORMAL, NULL, NULL, NULL));
537   // URLs are always LTR.
538   if (rtl)
539     base::i18n::WrapStringWithLTRFormatting(&failed_url_string);
540   error_strings->SetString("title",
541       l10n_util::GetStringFUTF16(options.title_resource_id, failed_url_string));
542   error_strings->SetString("heading",
543       l10n_util::GetStringUTF16(options.heading_resource_id));
544
545   std::string icon_class = GetIconClassForError(error_domain, error_code);
546   error_strings->SetString("iconClass", icon_class);
547
548   base::DictionaryValue* summary = new base::DictionaryValue;
549   summary->SetString("msg",
550       l10n_util::GetStringUTF16(options.summary_resource_id));
551   summary->SetString("failedUrl", failed_url_string);
552   summary->SetString("hostName", net::IDNToUnicode(failed_url.host(),
553                                                    accept_languages));
554   summary->SetString("productName",
555                      l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
556
557   error_strings->SetString(
558       "more", l10n_util::GetStringUTF16(IDS_ERRORPAGES_BUTTON_MORE));
559   error_strings->SetString(
560       "less", l10n_util::GetStringUTF16(IDS_ERRORPAGES_BUTTON_LESS));
561   error_strings->Set("summary", summary);
562   error_strings->SetBoolean("staleCopyInCache", stale_copy_in_cache);
563
564 #if defined(OS_CHROMEOS)
565   error_strings->SetString(
566       "diagnose", l10n_util::GetStringUTF16(IDS_ERRORPAGES_BUTTON_DIAGNOSE));
567 #endif  // defined(OS_CHROMEOS)
568
569   if (options.details_resource_id != kErrorPagesNoDetails) {
570     error_strings->SetString(
571         "errorDetails", l10n_util::GetStringUTF16(options.details_resource_id));
572   }
573
574   base::string16 error_string;
575   if (error_domain == net::kErrorDomain) {
576     // Non-internationalized error string, for debugging Chrome itself.
577     std::string ascii_error_string = net::ErrorToString(error_code);
578     // Remove the leading "net::" from the returned string.
579     base::RemoveChars(ascii_error_string, "net:", &ascii_error_string);
580     error_string = base::ASCIIToUTF16(ascii_error_string);
581   } else if (error_domain == chrome_common_net::kDnsProbeErrorDomain) {
582     std::string ascii_error_string =
583         chrome_common_net::DnsProbeStatusToString(error_code);
584     error_string = base::ASCIIToUTF16(ascii_error_string);
585   } else {
586     DCHECK_EQ(LocalizedError::kHttpErrorDomain, error_domain);
587     error_string = base::IntToString16(error_code);
588   }
589   error_strings->SetString("errorCode",
590       l10n_util::GetStringFUTF16(IDS_ERRORPAGES_ERROR_CODE, error_string));
591
592   base::ListValue* suggestions = new base::ListValue();
593
594   // Platform specific instructions for diagnosing network issues on OSX and
595   // Windows.
596 #if defined(OS_MACOSX) || defined(OS_WIN)
597   if (error_domain == net::kErrorDomain &&
598       error_code == net::ERR_INTERNET_DISCONNECTED) {
599     int platform_string_id =
600         IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED_PLATFORM;
601 #if defined(OS_WIN)
602     // Different versions of Windows have different instructions.
603     base::win::Version windows_version = base::win::GetVersion();
604     if (windows_version < base::win::VERSION_VISTA) {
605       // XP, XP64, and Server 2003.
606       platform_string_id =
607           IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED_PLATFORM_XP;
608     } else if (windows_version == base::win::VERSION_VISTA) {
609       // Vista
610       platform_string_id =
611           IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED_PLATFORM_VISTA;
612     }
613 #endif  // defined(OS_WIN)
614     // Lead with the general error description, and suffix with the platform
615     // dependent portion of the summary section.
616     summary->SetString("msg",
617         l10n_util::GetStringFUTF16(
618             IDS_ERRORPAGES_SUMMARY_INTERNET_DISCONNECTED_INSTRUCTIONS_TEMPLATE,
619             l10n_util::GetStringUTF16(options.summary_resource_id),
620             l10n_util::GetStringUTF16(platform_string_id)));
621   }
622 #endif  // defined(OS_MACOSX) || defined(OS_WIN)
623
624   if (options.suggestions & SUGGEST_RELOAD) {
625     if (!is_post) {
626       base::DictionaryValue* reload_button = new base::DictionaryValue;
627       reload_button->SetString("msg",
628           l10n_util::GetStringUTF16(IDS_ERRORPAGES_BUTTON_RELOAD));
629       reload_button->SetString("reloadUrl", failed_url.spec());
630       error_strings->Set("reload", reload_button);
631     } else {
632       // If the page was created by a post, it can't be reloaded in the same
633       // way, so just add a suggestion instead.
634       // TODO(mmenke):  Make the reload button bring up the repost confirmation
635       //                dialog for pages resulting from posts.
636       base::DictionaryValue* suggest_reload_repost = new base::DictionaryValue;
637       suggest_reload_repost->SetString("header",
638           l10n_util::GetStringUTF16(
639               IDS_ERRORPAGES_SUGGESTION_RELOAD_REPOST_HEADER));
640       suggest_reload_repost->SetString("body",
641           l10n_util::GetStringUTF16(
642               IDS_ERRORPAGES_SUGGESTION_RELOAD_REPOST_BODY));
643       suggestions->Append(suggest_reload_repost);
644     }
645   }
646
647   if (options.suggestions & SUGGEST_CHECK_CONNECTION) {
648     base::DictionaryValue* suggest_check_connection = new base::DictionaryValue;
649     suggest_check_connection->SetString("header",
650         l10n_util::GetStringUTF16(
651             IDS_ERRORPAGES_SUGGESTION_CHECK_CONNECTION_HEADER));
652     suggest_check_connection->SetString("body",
653         l10n_util::GetStringUTF16(
654             IDS_ERRORPAGES_SUGGESTION_CHECK_CONNECTION_BODY));
655     suggestions->Append(suggest_check_connection);
656   }
657
658   if (options.suggestions & SUGGEST_DNS_CONFIG) {
659     base::DictionaryValue* suggest_dns_config = new base::DictionaryValue;
660     suggest_dns_config->SetString("header",
661         l10n_util::GetStringUTF16(
662             IDS_ERRORPAGES_SUGGESTION_DNS_CONFIG_HEADER));
663     suggest_dns_config->SetString("body",
664         l10n_util::GetStringUTF16(
665             IDS_ERRORPAGES_SUGGESTION_DNS_CONFIG_BODY));
666     suggestions->Append(suggest_dns_config);
667
668     base::DictionaryValue* suggest_network_prediction =
669         GetStandardMenuItemsText();
670     suggest_network_prediction->SetString("header",
671         l10n_util::GetStringUTF16(
672             IDS_ERRORPAGES_SUGGESTION_NETWORK_PREDICTION_HEADER));
673     suggest_network_prediction->SetString("body",
674         l10n_util::GetStringUTF16(
675             IDS_ERRORPAGES_SUGGESTION_NETWORK_PREDICTION_BODY));
676     suggest_network_prediction->SetString(
677         "noNetworkPredictionTitle",
678         l10n_util::GetStringUTF16(
679             IDS_NETWORK_PREDICTION_ENABLED_DESCRIPTION));
680     suggestions->Append(suggest_network_prediction);
681   }
682
683   if (options.suggestions & SUGGEST_FIREWALL_CONFIG) {
684     base::DictionaryValue* suggest_firewall_config = new base::DictionaryValue;
685     suggest_firewall_config->SetString("header",
686         l10n_util::GetStringUTF16(
687             IDS_ERRORPAGES_SUGGESTION_FIREWALL_CONFIG_HEADER));
688     suggest_firewall_config->SetString("body",
689         l10n_util::GetStringUTF16(
690             IDS_ERRORPAGES_SUGGESTION_FIREWALL_CONFIG_BODY));
691     suggestions->Append(suggest_firewall_config);
692   }
693
694   if (options.suggestions & SUGGEST_PROXY_CONFIG) {
695     base::DictionaryValue* suggest_proxy_config = GetStandardMenuItemsText();
696     suggest_proxy_config->SetString("header",
697         l10n_util::GetStringUTF16(
698             IDS_ERRORPAGES_SUGGESTION_PROXY_CONFIG_HEADER));
699     suggest_proxy_config->SetString("body",
700         l10n_util::GetStringFUTF16(IDS_ERRORPAGES_SUGGESTION_PROXY_CONFIG_BODY,
701             l10n_util::GetStringUTF16(
702                 IDS_ERRORPAGES_SUGGESTION_PROXY_DISABLE_PLATFORM)));
703     suggest_proxy_config->SetString("proxyTitle",
704         l10n_util::GetStringUTF16(IDS_OPTIONS_PROXIES_CONFIGURE_BUTTON));
705
706     suggestions->Append(suggest_proxy_config);
707   }
708
709   if (options.suggestions & SUGGEST_DISABLE_EXTENSION) {
710     base::DictionaryValue* suggest_disable_extension =
711         new base::DictionaryValue;
712     // There's only a header for this suggestion.
713     suggest_disable_extension->SetString("header",
714         l10n_util::GetStringUTF16(
715             IDS_ERRORPAGES_SUGGESTION_DISABLE_EXTENSION_HEADER));
716     suggestions->Append(suggest_disable_extension);
717   }
718
719   if (options.suggestions & SUGGEST_VIEW_POLICIES) {
720     base::DictionaryValue* suggest_view_policies = new base::DictionaryValue;
721     suggest_view_policies->SetString(
722         "header",
723         l10n_util::GetStringUTF16(
724             IDS_ERRORPAGES_SUGGESTION_VIEW_POLICIES_HEADER));
725     suggest_view_policies->SetString(
726         "body",
727         l10n_util::GetStringUTF16(
728             IDS_ERRORPAGES_SUGGESTION_VIEW_POLICIES_BODY));
729     suggestions->Append(suggest_view_policies);
730   }
731
732   if (options.suggestions & SUGGEST_CONTACT_ADMINISTRATOR) {
733     base::DictionaryValue* suggest_contant_administrator =
734         new base::DictionaryValue;
735     suggest_contant_administrator->SetString(
736         "body",
737         l10n_util::GetStringUTF16(
738             IDS_ERRORPAGES_SUGGESTION_CONTACT_ADMINISTRATOR_BODY));
739     suggestions->Append(suggest_contant_administrator);
740   }
741
742   if (options.suggestions & SUGGEST_LEARNMORE) {
743     GURL learn_more_url;
744     switch (options.error_code) {
745       case net::ERR_TOO_MANY_REDIRECTS:
746         learn_more_url = GURL(kRedirectLoopLearnMoreUrl);
747         break;
748       case net::ERR_SSL_WEAK_SERVER_EPHEMERAL_DH_KEY:
749         learn_more_url = GURL(kWeakDHKeyLearnMoreUrl);
750         break;
751       default:
752         break;
753     }
754
755     if (learn_more_url.is_valid()) {
756       // Add the language parameter to the URL.
757       std::string query = learn_more_url.query() + "&hl=" + locale;
758       GURL::Replacements repl;
759       repl.SetQueryStr(query);
760       learn_more_url = learn_more_url.ReplaceComponents(repl);
761
762       base::DictionaryValue* suggest_learn_more = new base::DictionaryValue;
763       // There's only a body for this suggestion.
764       suggest_learn_more->SetString("body",
765           l10n_util::GetStringUTF16(IDS_ERRORPAGES_SUGGESTION_LEARNMORE_BODY));
766       suggest_learn_more->SetString("learnMoreUrl", learn_more_url.spec());
767       suggestions->Append(suggest_learn_more);
768     }
769   }
770
771   error_strings->Set("suggestions", suggestions);
772 }
773
774 base::string16 LocalizedError::GetErrorDetails(const blink::WebURLError& error,
775                                                bool is_post) {
776   const LocalizedErrorMap* error_map =
777       LookupErrorMap(error.domain.utf8(), error.reason, is_post);
778   if (error_map)
779     return l10n_util::GetStringUTF16(error_map->details_resource_id);
780   else
781     return l10n_util::GetStringUTF16(IDS_ERRORPAGES_DETAILS_UNKNOWN);
782 }
783
784 bool LocalizedError::HasStrings(const std::string& error_domain,
785                                 int error_code) {
786   // Whether or not the there are strings for an error does not depend on
787   // whether or not the page was be generated by a POST, so just claim it was
788   // not.
789   return LookupErrorMap(error_domain, error_code, /*is_post=*/false) != NULL;
790 }
791
792 void LocalizedError::GetAppErrorStrings(
793     const GURL& display_url,
794     const extensions::Extension* app,
795     base::DictionaryValue* error_strings) {
796   DCHECK(app);
797
798   bool rtl = LocaleIsRTL();
799   error_strings->SetString("textdirection", rtl ? "rtl" : "ltr");
800
801   base::string16 failed_url(base::ASCIIToUTF16(display_url.spec()));
802   // URLs are always LTR.
803   if (rtl)
804     base::i18n::WrapStringWithLTRFormatting(&failed_url);
805   error_strings->SetString(
806      "url", l10n_util::GetStringFUTF16(IDS_ERRORPAGES_TITLE_NOT_AVAILABLE,
807                                        failed_url.c_str()));
808
809   error_strings->SetString("title", app->name());
810   error_strings->SetString(
811       "icon",
812       extensions::IconsInfo::GetIconURL(
813           app,
814           extension_misc::EXTENSION_ICON_GIGANTOR,
815           ExtensionIconSet::MATCH_SMALLER).spec());
816   error_strings->SetString("name", app->name());
817   error_strings->SetString(
818       "msg",
819       l10n_util::GetStringUTF16(IDS_ERRORPAGES_APP_WARNING));
820
821 #if defined(OS_CHROMEOS)
822   GURL learn_more_url(kAppWarningLearnMoreUrl);
823   base::DictionaryValue* suggest_learn_more = new base::DictionaryValue();
824   suggest_learn_more->SetString("msg",
825                                 l10n_util::GetStringUTF16(
826                                     IDS_ERRORPAGES_SUGGESTION_LEARNMORE_BODY));
827   suggest_learn_more->SetString("learnMoreUrl", learn_more_url.spec());
828   error_strings->Set("suggestionsLearnMore", suggest_learn_more);
829 #endif  // defined(OS_CHROMEOS)
830 }