Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 16 matching lines...) Expand all Loading... | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/loader/TextResourceDecoderBuilder.h" | 31 #include "core/loader/TextResourceDecoderBuilder.h" |
| 32 | 32 |
| 33 #include "core/dom/Document.h" | 33 #include "core/dom/Document.h" |
| 34 #include "core/frame/LocalFrame.h" | 34 #include "core/frame/LocalFrame.h" |
| 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 #include "wtf/HashMap.h" | |
| 37 | 38 |
| 38 namespace blink { | 39 namespace blink { |
| 39 | 40 |
| 40 static inline bool canReferToParentFrameEncoding(const LocalFrame* frame, const LocalFrame* parentFrame) | 41 static inline bool canReferToParentFrameEncoding(const LocalFrame* frame, const LocalFrame* parentFrame) |
| 41 { | 42 { |
| 42 return parentFrame && parentFrame->document()->securityOrigin()->canAccess(f rame->document()->securityOrigin()); | 43 return parentFrame && parentFrame->document()->securityOrigin()->canAccess(f rame->document()->securityOrigin()); |
| 43 } | 44 } |
| 44 | 45 |
| 46 static WTF::HashMap<String, WTF::TextEncoding>* getEncodingMap() | |
|
esprehn
2016/02/26 09:43:13
if you use a Map you should return by const ref he
Jinsuk Kim
2016/03/02 03:46:08
I chose to use a simple array. Please see if it lo
| |
| 47 { | |
| 48 static WTF::HashMap<String, WTF::TextEncoding>* map {}; | |
| 49 if (!map) { | |
| 50 map = new WTF::HashMap<String, WTF::TextEncoding>(); | |
|
aelias_OOO_until_Jul13
2016/02/26 09:07:24
Hmm, in Chromium we would use something like LAZY_
esprehn
2016/02/26 09:43:13
This should use DEFINE_STATIC_LOCAL and map.isEmpt
Jinsuk Kim
2016/03/02 03:46:08
Please see the reply above.
Jinsuk Kim
2016/03/02 03:46:08
I agree. It got complicated that I had wished. Rew
| |
| 51 const WTF::TextEncoding windows1250("windows-1250"); | |
| 52 const WTF::TextEncoding windows1251("windows-1251"); | |
| 53 const WTF::TextEncoding windows1252("windows-1252"); | |
| 54 const WTF::TextEncoding windows1255("windows-1255"); | |
| 55 const WTF::TextEncoding windows1256("windows-1256"); | |
| 56 const WTF::TextEncoding windows1257("windows-1257"); | |
| 57 const WTF::TextEncoding windows1258("windows-1258"); | |
| 58 const WTF::TextEncoding windows874("windows-874"); | |
| 59 const WTF::TextEncoding windows949("windows-949"); | |
| 60 const WTF::TextEncoding iso88592("ISO-8859-2"); | |
| 61 const WTF::TextEncoding iso88597("ISO-8859-7"); | |
| 62 const WTF::TextEncoding iso88599("ISO-8859-9"); | |
| 63 const WTF::TextEncoding big5("Big5"); | |
| 64 const WTF::TextEncoding gbk("GBK"); | |
| 65 const WTF::TextEncoding shiftJis("Shift_JIS"); | |
| 66 | |
| 67 struct { | |
| 68 const char* domain; | |
| 69 const WTF::TextEncoding& encoding; | |
| 70 } encodingMap[] = { | |
| 71 { "au", windows1252 }, | |
| 72 { "az", iso88599 }, | |
| 73 { "bd", windows1252 }, | |
| 74 { "bg", windows1251 }, | |
| 75 { "br", windows1252 }, | |
| 76 { "ca", windows1252 }, | |
| 77 { "ch", windows1252 }, | |
| 78 { "cn", gbk }, | |
| 79 { "cz", windows1250 }, | |
| 80 { "de", windows1252 }, | |
| 81 { "dk", windows1252 }, | |
| 82 { "ee", windows1257 }, | |
| 83 { "eg", windows1256 }, | |
| 84 { "et", windows1252 }, | |
| 85 { "fi", windows1252 }, | |
| 86 { "fr", windows1252 }, | |
| 87 { "gb", windows1252 }, | |
| 88 { "gr", iso88597 }, | |
| 89 { "hk", big5 }, | |
| 90 { "hr", windows1250 }, | |
| 91 { "hu", iso88592 }, | |
| 92 { "il", windows1255 }, | |
| 93 { "ir", windows1256 }, | |
| 94 { "is", windows1252 }, | |
| 95 { "it", windows1252 }, | |
| 96 { "jp", shiftJis }, | |
| 97 { "kr", windows949 }, | |
| 98 { "lt", windows1257 }, | |
| 99 { "lv", windows1257 }, | |
| 100 { "mk", windows1251 }, | |
| 101 { "nl", windows1252 }, | |
| 102 { "no", windows1252 }, | |
| 103 { "pl", iso88592 }, | |
| 104 { "pt", windows1252 }, | |
| 105 { "ro", iso88592 }, | |
| 106 { "rs", windows1251 }, | |
| 107 { "ru", windows1251 }, | |
| 108 { "se", windows1252 }, | |
| 109 { "si", iso88592 }, | |
| 110 { "sk", windows1250 }, | |
| 111 { "th", windows874 }, | |
| 112 { "tr", iso88599 }, | |
| 113 { "tw", big5 }, | |
| 114 { "tz", windows1252 }, | |
| 115 { "ua", windows1251 }, | |
| 116 { "us", windows1252 }, | |
| 117 { "vn", windows1258 }, | |
| 118 { "xa", windows1252 }, | |
| 119 { "xb", windows1256 } | |
| 120 }; | |
| 121 for (size_t i = 0; i < WTF_ARRAY_LENGTH(encodingMap); ++i) | |
| 122 map->add(String(encodingMap[i].domain), encodingMap[i].encoding); | |
|
esprehn
2016/02/26 09:43:13
lets not do this, make this method into something
Jinsuk Kim
2016/03/02 03:46:08
Rewrote it with an array.
| |
| 123 } | |
| 124 return map; | |
| 125 } | |
| 126 | |
| 127 static const WTF::TextEncoding getEncodingFromDomain(const KURL& url) | |
| 128 { | |
| 129 Vector<String> tokens; | |
| 130 url.host().split(String(".", 1), tokens); | |
|
esprehn
2016/02/26 09:43:13
you don't need to do String(".", 1), just pass "."
Jinsuk Kim
2016/03/02 03:46:08
Better. Thanks. Done.
| |
| 131 if (tokens.size() > 0) { | |
| 132 String tld = tokens[tokens.size()-1]; | |
| 133 auto map = getEncodingMap(); | |
| 134 if (map->find(tld) != map->end()) | |
| 135 return map->get(tld); | |
|
esprehn
2016/02/26 09:43:13
lets use std::lower_bound on a static list instead
Jinsuk Kim
2016/03/02 03:46:08
Rewrote it with an array.
| |
| 136 } | |
| 137 return WTF::TextEncoding(); | |
| 138 } | |
| 45 | 139 |
| 46 TextResourceDecoderBuilder::TextResourceDecoderBuilder(const AtomicString& mimeT ype, const AtomicString& encoding) | 140 TextResourceDecoderBuilder::TextResourceDecoderBuilder(const AtomicString& mimeT ype, const AtomicString& encoding) |
| 47 : m_mimeType(mimeType) | 141 : m_mimeType(mimeType) |
| 48 , m_encoding(encoding) | 142 , m_encoding(encoding) |
| 49 { | 143 { |
| 50 } | 144 } |
| 51 | 145 |
| 52 TextResourceDecoderBuilder::~TextResourceDecoderBuilder() | 146 TextResourceDecoderBuilder::~TextResourceDecoderBuilder() |
| 53 { | 147 { |
| 54 } | 148 } |
| 55 | 149 |
| 56 | 150 |
| 57 inline PassOwnPtr<TextResourceDecoder> TextResourceDecoderBuilder::createDecoder Instance(Document* document) | 151 inline PassOwnPtr<TextResourceDecoder> TextResourceDecoderBuilder::createDecoder Instance(Document* document) |
| 58 { | 152 { |
| 153 const WTF::TextEncoding encodingFromDomain = getEncodingFromDomain(document- >url()); | |
| 59 if (LocalFrame* frame = document->frame()) { | 154 if (LocalFrame* frame = document->frame()) { |
| 60 if (Settings* settings = frame->settings()) | 155 if (Settings* settings = frame->settings()) |
| 61 return TextResourceDecoder::create(m_mimeType, settings->defaultText EncodingName(), settings->usesEncodingDetector()); | 156 return TextResourceDecoder::create(m_mimeType, encodingFromDomain.is Valid() ? encodingFromDomain : settings->defaultTextEncodingName(), settings->us esEncodingDetector()); |
|
aelias_OOO_until_Jul13
2016/02/26 09:07:24
Looks OK, can you add a test verifying (for one or
Jinsuk Kim
2016/03/02 03:46:08
Added tests.
| |
| 62 } | 157 } |
| 63 | 158 |
| 64 return TextResourceDecoder::create(m_mimeType, String()); | 159 return TextResourceDecoder::create(m_mimeType, encodingFromDomain); |
| 65 } | 160 } |
| 66 | 161 |
| 67 inline void TextResourceDecoderBuilder::setupEncoding(TextResourceDecoder* decod er, Document* document) | 162 inline void TextResourceDecoderBuilder::setupEncoding(TextResourceDecoder* decod er, Document* document) |
| 68 { | 163 { |
| 69 LocalFrame* frame = document->frame(); | 164 LocalFrame* frame = document->frame(); |
| 70 LocalFrame* parentFrame = 0; | 165 LocalFrame* parentFrame = 0; |
| 71 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame( )) | 166 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame( )) |
| 72 parentFrame = toLocalFrame(frame->tree().parent()); | 167 parentFrame = toLocalFrame(frame->tree().parent()); |
| 73 | 168 |
| 74 if (!m_encoding.isEmpty()) | 169 if (!m_encoding.isEmpty()) |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 98 setupEncoding(decoder.get(), document); | 193 setupEncoding(decoder.get(), document); |
| 99 return decoder.release(); | 194 return decoder.release(); |
| 100 } | 195 } |
| 101 | 196 |
| 102 void TextResourceDecoderBuilder::clear() | 197 void TextResourceDecoderBuilder::clear() |
| 103 { | 198 { |
| 104 m_encoding = nullAtom; | 199 m_encoding = nullAtom; |
| 105 } | 200 } |
| 106 | 201 |
| 107 } // namespace blink | 202 } // namespace blink |
| OLD | NEW |