Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: third_party/WebKit/Source/core/loader/TextResourceDecoderBuilder.cpp

Issue 1725283002: Top-level domain-based default encoding (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 24 matching lines...) Expand all
35 #include "core/frame/Settings.h" 35 #include "core/frame/Settings.h"
36 #include "platform/weborigin/SecurityOrigin.h" 36 #include "platform/weborigin/SecurityOrigin.h"
37 37
38 namespace blink { 38 namespace blink {
39 39
40 static inline bool canReferToParentFrameEncoding(const LocalFrame* frame, const LocalFrame* parentFrame) 40 static inline bool canReferToParentFrameEncoding(const LocalFrame* frame, const LocalFrame* parentFrame)
41 { 41 {
42 return parentFrame && parentFrame->document()->getSecurityOrigin()->canAcces s(frame->document()->getSecurityOrigin()); 42 return parentFrame && parentFrame->document()->getSecurityOrigin()->canAcces s(frame->document()->getSecurityOrigin());
43 } 43 }
44 44
45 namespace {
46
47 struct LegacyEncoding {
48 const char* domain;
49 const char* encoding;
50 };
51
52 static const LegacyEncoding encodings[] = {
53 { "au", "windows-1252" },
54 { "az", "ISO-8859-9" },
55 { "bd", "windows-1252" },
56 { "bg", "windows-1251" },
57 { "br", "windows-1252" },
58 { "ca", "windows-1252" },
59 { "ch", "windows-1252" },
60 { "cn", "GBK" },
61 { "cz", "windows-1250" },
62 { "de", "windows-1252" },
63 { "dk", "windows-1252" },
64 { "ee", "windows-1256" },
65 { "eg", "windows-1257" },
66 { "et", "windows-1252" },
67 { "fi", "windows-1252" },
68 { "fr", "windows-1252" },
69 { "gb", "windows-1252" },
70 { "gr", "ISO-8859-7" },
71 { "hk", "Big5" },
72 { "hr", "windows-1250" },
73 { "hu", "ISO-8859-2" },
74 { "il", "windows-1255" },
75 { "ir", "windows-1257" },
76 { "is", "windows-1252" },
77 { "it", "windows-1252" },
78 { "jp", "Shift_JIS" },
79 { "kr", "windows-949" },
80 { "lt", "windows-1256" },
81 { "lv", "windows-1256" },
82 { "mk", "windows-1251" },
83 { "nl", "windows-1252" },
84 { "no", "windows-1252" },
85 { "pl", "ISO-8859-2" },
86 { "pt", "windows-1252" },
87 { "ro", "ISO-8859-2" },
88 { "rs", "windows-1251" },
89 { "ru", "windows-1251" },
90 { "se", "windows-1252" },
91 { "si", "ISO-8859-2" },
92 { "sk", "windows-1250" },
93 { "th", "windows-874" },
94 { "tr", "ISO-8859-9" },
95 { "tw", "Big5" },
96 { "tz", "windows-1252" },
97 { "ua", "windows-1251" },
98 { "us", "windows-1252" },
99 { "vn", "windows-1258" },
100 { "xa", "windows-1252" },
101 { "xb", "windows-1257" }
102 };
103
104 static const WTF::TextEncoding getEncodingFromDomain(const KURL& url)
105 {
106 Vector<String> tokens;
107 url.host().split(".", tokens);
108 if (!tokens.isEmpty()) {
109 auto tld = tokens.last();
110 for (size_t i = 0; i < WTF_ARRAY_LENGTH(encodings); i++) {
111 if (tld == encodings[i].domain)
112 return WTF::TextEncoding(encodings[i].encoding);
113 }
114 }
115 return WTF::TextEncoding();
116 }
117
118 } // namespace
45 119
46 TextResourceDecoderBuilder::TextResourceDecoderBuilder(const AtomicString& mimeT ype, const AtomicString& encoding) 120 TextResourceDecoderBuilder::TextResourceDecoderBuilder(const AtomicString& mimeT ype, const AtomicString& encoding)
47 : m_mimeType(mimeType) 121 : m_mimeType(mimeType)
48 , m_encoding(encoding) 122 , m_encoding(encoding)
49 { 123 {
50 } 124 }
51 125
52 TextResourceDecoderBuilder::~TextResourceDecoderBuilder() 126 TextResourceDecoderBuilder::~TextResourceDecoderBuilder()
53 { 127 {
54 } 128 }
55 129
56 130
57 inline PassOwnPtr<TextResourceDecoder> TextResourceDecoderBuilder::createDecoder Instance(Document* document) 131 inline PassOwnPtr<TextResourceDecoder> TextResourceDecoderBuilder::createDecoder Instance(Document* document)
58 { 132 {
133 const WTF::TextEncoding encodingFromDomain = getEncodingFromDomain(document- >url());
59 if (LocalFrame* frame = document->frame()) { 134 if (LocalFrame* frame = document->frame()) {
60 if (Settings* settings = frame->settings()) 135 if (Settings* settings = frame->settings())
61 return TextResourceDecoder::create(m_mimeType, settings->defaultText EncodingName(), settings->usesEncodingDetector()); 136 return TextResourceDecoder::create(m_mimeType, encodingFromDomain.is Valid() ? encodingFromDomain : settings->defaultTextEncodingName(), settings->us esEncodingDetector());
62 } 137 }
63 138
64 return TextResourceDecoder::create(m_mimeType, String()); 139 return TextResourceDecoder::create(m_mimeType, encodingFromDomain);
65 } 140 }
66 141
67 inline void TextResourceDecoderBuilder::setupEncoding(TextResourceDecoder* decod er, Document* document) 142 inline void TextResourceDecoderBuilder::setupEncoding(TextResourceDecoder* decod er, Document* document)
68 { 143 {
69 LocalFrame* frame = document->frame(); 144 LocalFrame* frame = document->frame();
70 LocalFrame* parentFrame = 0; 145 LocalFrame* parentFrame = 0;
71 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame( )) 146 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame( ))
72 parentFrame = toLocalFrame(frame->tree().parent()); 147 parentFrame = toLocalFrame(frame->tree().parent());
73 148
74 if (!m_encoding.isEmpty()) 149 if (!m_encoding.isEmpty())
(...skipping 23 matching lines...) Expand all
98 setupEncoding(decoder.get(), document); 173 setupEncoding(decoder.get(), document);
99 return decoder.release(); 174 return decoder.release();
100 } 175 }
101 176
102 void TextResourceDecoderBuilder::clear() 177 void TextResourceDecoderBuilder::clear()
103 { 178 {
104 m_encoding = nullAtom; 179 m_encoding = nullAtom;
105 } 180 }
106 181
107 } // namespace blink 182 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698