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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 : m_mimeType(mimeType), m_encoding(encoding) {} | 94 : m_mimeType(mimeType), m_encoding(encoding) {} |
95 | 95 |
96 TextResourceDecoderBuilder::~TextResourceDecoderBuilder() {} | 96 TextResourceDecoderBuilder::~TextResourceDecoderBuilder() {} |
97 | 97 |
98 inline std::unique_ptr<TextResourceDecoder> | 98 inline std::unique_ptr<TextResourceDecoder> |
99 TextResourceDecoderBuilder::createDecoderInstance(Document* document) { | 99 TextResourceDecoderBuilder::createDecoderInstance(Document* document) { |
100 const WTF::TextEncoding encodingFromDomain = | 100 const WTF::TextEncoding encodingFromDomain = |
101 getEncodingFromDomain(document->url()); | 101 getEncodingFromDomain(document->url()); |
102 if (LocalFrame* frame = document->frame()) { | 102 if (LocalFrame* frame = document->frame()) { |
103 if (Settings* settings = frame->settings()) { | 103 if (Settings* settings = frame->settings()) { |
104 // Disable autodetection for XML to honor the default encoding (UTF-8) for
unlabelled documents. | 104 // Disable autodetection for XML to honor the default encoding (UTF-8) for |
| 105 // unlabelled documents. |
105 return TextResourceDecoder::create( | 106 return TextResourceDecoder::create( |
106 m_mimeType, | 107 m_mimeType, |
107 encodingFromDomain.isValid() ? encodingFromDomain | 108 encodingFromDomain.isValid() ? encodingFromDomain |
108 : settings->defaultTextEncodingName(), | 109 : settings->defaultTextEncodingName(), |
109 !DOMImplementation::isXMLMIMEType(m_mimeType)); | 110 !DOMImplementation::isXMLMIMEType(m_mimeType)); |
110 } | 111 } |
111 } | 112 } |
112 | 113 |
113 return TextResourceDecoder::create(m_mimeType, encodingFromDomain); | 114 return TextResourceDecoder::create(m_mimeType, encodingFromDomain); |
114 } | 115 } |
115 | 116 |
116 inline void TextResourceDecoderBuilder::setupEncoding( | 117 inline void TextResourceDecoderBuilder::setupEncoding( |
117 TextResourceDecoder* decoder, | 118 TextResourceDecoder* decoder, |
118 Document* document) { | 119 Document* document) { |
119 LocalFrame* frame = document->frame(); | 120 LocalFrame* frame = document->frame(); |
120 LocalFrame* parentFrame = 0; | 121 LocalFrame* parentFrame = 0; |
121 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame()) | 122 if (frame && frame->tree().parent() && frame->tree().parent()->isLocalFrame()) |
122 parentFrame = toLocalFrame(frame->tree().parent()); | 123 parentFrame = toLocalFrame(frame->tree().parent()); |
123 | 124 |
124 if (!m_encoding.isEmpty()) | 125 if (!m_encoding.isEmpty()) |
125 decoder->setEncoding(m_encoding.getString(), | 126 decoder->setEncoding(m_encoding.getString(), |
126 TextResourceDecoder::EncodingFromHTTPHeader); | 127 TextResourceDecoder::EncodingFromHTTPHeader); |
127 | 128 |
128 // Set the hint encoding to the parent frame encoding only if | 129 // Set the hint encoding to the parent frame encoding only if the parent and |
129 // the parent and the current frames share the security origin. | 130 // the current frames share the security origin. We impose this condition |
130 // We impose this condition because somebody can make a child frameg63 | 131 // because somebody can make a child frameg63 containing a carefully crafted |
131 // containing a carefully crafted html/javascript in one encoding | 132 // html/javascript in one encoding that can be mistaken for hintEncoding (or |
132 // that can be mistaken for hintEncoding (or related encoding) by | 133 // related encoding) by an auto detector. When interpreted in the latter, it |
133 // an auto detector. When interpreted in the latter, it could be | 134 // could be an attack vector. |
134 // an attack vector. | 135 // FIXME: This might be too cautious for non-7bit-encodings and we may |
135 // FIXME: This might be too cautious for non-7bit-encodings and | 136 // consider relaxing this later after testing. |
136 // we may consider relaxing this later after testing. | |
137 if (frame && canReferToParentFrameEncoding(frame, parentFrame)) { | 137 if (frame && canReferToParentFrameEncoding(frame, parentFrame)) { |
138 if (parentFrame->document()->encodingWasDetectedHeuristically()) | 138 if (parentFrame->document()->encodingWasDetectedHeuristically()) |
139 decoder->setHintEncoding(parentFrame->document()->encoding()); | 139 decoder->setHintEncoding(parentFrame->document()->encoding()); |
140 | 140 |
141 if (m_encoding.isEmpty()) | 141 if (m_encoding.isEmpty()) |
142 decoder->setEncoding(parentFrame->document()->encoding(), | 142 decoder->setEncoding(parentFrame->document()->encoding(), |
143 TextResourceDecoder::EncodingFromParentFrame); | 143 TextResourceDecoder::EncodingFromParentFrame); |
144 } | 144 } |
145 } | 145 } |
146 | 146 |
147 std::unique_ptr<TextResourceDecoder> TextResourceDecoderBuilder::buildFor( | 147 std::unique_ptr<TextResourceDecoder> TextResourceDecoderBuilder::buildFor( |
148 Document* document) { | 148 Document* document) { |
149 std::unique_ptr<TextResourceDecoder> decoder = | 149 std::unique_ptr<TextResourceDecoder> decoder = |
150 createDecoderInstance(document); | 150 createDecoderInstance(document); |
151 setupEncoding(decoder.get(), document); | 151 setupEncoding(decoder.get(), document); |
152 return decoder; | 152 return decoder; |
153 } | 153 } |
154 | 154 |
155 void TextResourceDecoderBuilder::clear() { | 155 void TextResourceDecoderBuilder::clear() { |
156 m_encoding = nullAtom; | 156 m_encoding = nullAtom; |
157 } | 157 } |
158 | 158 |
159 } // namespace blink | 159 } // namespace blink |
OLD | NEW |