Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 #include "core/frame/LocalFrame.h" | 52 #include "core/frame/LocalFrame.h" |
| 53 #include "core/html/HTMLFrameElementBase.h" | 53 #include "core/html/HTMLFrameElementBase.h" |
| 54 #include "core/html/HTMLImageElement.h" | 54 #include "core/html/HTMLImageElement.h" |
| 55 #include "core/html/HTMLInputElement.h" | 55 #include "core/html/HTMLInputElement.h" |
| 56 #include "core/html/HTMLLinkElement.h" | 56 #include "core/html/HTMLLinkElement.h" |
| 57 #include "core/html/HTMLMetaElement.h" | 57 #include "core/html/HTMLMetaElement.h" |
| 58 #include "core/html/HTMLStyleElement.h" | 58 #include "core/html/HTMLStyleElement.h" |
| 59 #include "core/html/ImageDocument.h" | 59 #include "core/html/ImageDocument.h" |
| 60 #include "core/style/StyleFetchedImage.h" | 60 #include "core/style/StyleFetchedImage.h" |
| 61 #include "core/style/StyleImage.h" | 61 #include "core/style/StyleImage.h" |
| 62 #include "platform/Histogram.h" | |
| 62 #include "platform/SerializedResource.h" | 63 #include "platform/SerializedResource.h" |
| 63 #include "platform/TraceEvent.h" | 64 #include "platform/TraceEvent.h" |
| 64 #include "platform/graphics/Image.h" | 65 #include "platform/graphics/Image.h" |
| 65 #include "platform/heap/Handle.h" | 66 #include "platform/heap/Handle.h" |
| 66 #include "wtf/HashSet.h" | 67 #include "wtf/HashSet.h" |
| 67 #include "wtf/text/CString.h" | 68 #include "wtf/text/CString.h" |
| 68 #include "wtf/text/StringBuilder.h" | 69 #include "wtf/text/StringBuilder.h" |
| 69 #include "wtf/text/TextEncoding.h" | 70 #include "wtf/text/TextEncoding.h" |
| 70 #include "wtf/text/WTFString.h" | 71 #include "wtf/text/WTFString.h" |
| 71 | 72 |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 // documents which leads to bugs like <https://crbug.com/251898>. Not being | 237 // documents which leads to bugs like <https://crbug.com/251898>. Not being |
| 237 // able to rewrite URLs inside CSS documents means that resources imported from | 238 // able to rewrite URLs inside CSS documents means that resources imported from |
| 238 // url(...) statements in CSS might not work when rewriting links for the | 239 // url(...) statements in CSS might not work when rewriting links for the |
| 239 // "Webpage, Complete" method of saving a page. It will take some work but it | 240 // "Webpage, Complete" method of saving a page. It will take some work but it |
| 240 // needs to be done if we want to continue to support non-MHTML saved pages. | 241 // needs to be done if we want to continue to support non-MHTML saved pages. |
| 241 | 242 |
| 242 FrameSerializer::FrameSerializer( | 243 FrameSerializer::FrameSerializer( |
| 243 Vector<SerializedResource>& resources, | 244 Vector<SerializedResource>& resources, |
| 244 Delegate& delegate) | 245 Delegate& delegate) |
| 245 : m_resources(&resources) | 246 : m_resources(&resources) |
| 247 , m_isSerializingCss(false) | |
| 246 , m_delegate(delegate) | 248 , m_delegate(delegate) |
| 247 { | 249 { |
| 248 } | 250 } |
| 249 | 251 |
| 250 void FrameSerializer::serializeFrame(const LocalFrame& frame) | 252 void FrameSerializer::serializeFrame(const LocalFrame& frame) |
| 251 { | 253 { |
| 252 TRACE_EVENT0("page-serialization", "FrameSerializer::serializeFrame"); | 254 TRACE_EVENT0("page-serialization", "FrameSerializer::serializeFrame"); |
| 253 ASSERT(frame.document()); | 255 ASSERT(frame.document()); |
| 254 Document& document = *frame.document(); | 256 Document& document = *frame.document(); |
| 255 KURL url = document.url(); | 257 KURL url = document.url(); |
| 256 | 258 |
| 257 // If frame is an image document, add the image and don't continue | 259 // If frame is an image document, add the image and don't continue |
| 258 if (document.isImageDocument()) { | 260 if (document.isImageDocument()) { |
| 259 ImageDocument& imageDocument = toImageDocument(document); | 261 ImageDocument& imageDocument = toImageDocument(document); |
| 260 addImageToResources(imageDocument.cachedImage(), url); | 262 addImageToResources(imageDocument.cachedImage(), url); |
| 261 return; | 263 return; |
| 262 } | 264 } |
| 263 | 265 |
| 264 TRACE_EVENT_BEGIN0("page-serialization", "FrameSerializer::serializeFrame HT ML"); | |
| 265 HeapVector<Member<Node>> serializedNodes; | 266 HeapVector<Member<Node>> serializedNodes; |
| 266 SerializerMarkupAccumulator accumulator(m_delegate, document, serializedNode s); | 267 { |
| 267 String text = serializeNodes<EditingStrategy>(accumulator, document, Include Node); | 268 TRACE_EVENT0("page-serialization", "FrameSerializer::serializeFrame HTML "); |
| 269 SCOPED_BLINK_UMA_HISTOGRAM_TIMER("PageSerialization.HtmlSerializationTim e"); | |
| 270 SerializerMarkupAccumulator accumulator(m_delegate, document, serialized Nodes); | |
| 271 String text = serializeNodes<EditingStrategy>(accumulator, document, Inc ludeNode); | |
| 268 | 272 |
| 269 CString frameHTML = document.encoding().encode(text, WTF::EntitiesForUnencod ables); | 273 CString frameHTML = document.encoding().encode(text, WTF::EntitiesForUne ncodables); |
| 270 m_resources->append(SerializedResource(url, document.suggestedMIMEType(), Sh aredBuffer::create(frameHTML.data(), frameHTML.length()))); | 274 m_resources->append(SerializedResource(url, document.suggestedMIMEType() , SharedBuffer::create(frameHTML.data(), frameHTML.length()))); |
| 271 TRACE_EVENT_END0("page-serialization", "FrameSerializer::serializeFrame HTML "); | 275 } |
| 272 | 276 |
| 273 for (Node* node: serializedNodes) { | 277 for (Node* node: serializedNodes) { |
| 274 ASSERT(node); | 278 ASSERT(node); |
| 275 if (!node->isElementNode()) | 279 if (!node->isElementNode()) |
| 276 continue; | 280 continue; |
| 277 | 281 |
| 278 Element& element = toElement(*node); | 282 Element& element = toElement(*node); |
| 279 // We have to process in-line style as it might contain some resources ( typically background images). | 283 // We have to process in-line style as it might contain some resources ( typically background images). |
| 280 if (element.isStyledElement()) { | 284 if (element.isStyledElement()) { |
| 281 retrieveResourcesForProperties(element.inlineStyle(), document); | 285 retrieveResourcesForProperties(element.inlineStyle(), document); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 305 if (CSSStyleSheet* sheet = styleElement.sheet()) | 309 if (CSSStyleSheet* sheet = styleElement.sheet()) |
| 306 serializeCSSStyleSheet(*sheet, KURL()); | 310 serializeCSSStyleSheet(*sheet, KURL()); |
| 307 } | 311 } |
| 308 } | 312 } |
| 309 } | 313 } |
| 310 | 314 |
| 311 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, const KU RL& url) | 315 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, const KU RL& url) |
| 312 { | 316 { |
| 313 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet" , | 317 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet" , |
| 314 "type", "CSS", "url", url.elidedString().utf8().data()); | 318 "type", "CSS", "url", url.elidedString().utf8().data()); |
| 319 // Only report UMA metric if this is not a reentrant CSS serialization call. | |
| 320 double cssStartTime = 0; | |
| 321 if (!m_isSerializingCss) { | |
| 322 m_isSerializingCss = true; | |
| 323 cssStartTime = WTF::monotonicallyIncreasingTime(); | |
| 324 } | |
| 325 | |
| 315 StringBuilder cssText; | 326 StringBuilder cssText; |
| 316 cssText.append("@charset \""); | 327 cssText.append("@charset \""); |
| 317 cssText.append(styleSheet.contents()->charset().lower()); | 328 cssText.append(styleSheet.contents()->charset().lower()); |
| 318 cssText.append("\";\n\n"); | 329 cssText.append("\";\n\n"); |
| 319 | 330 |
| 320 for (unsigned i = 0; i < styleSheet.length(); ++i) { | 331 for (unsigned i = 0; i < styleSheet.length(); ++i) { |
| 321 CSSRule* rule = styleSheet.item(i); | 332 CSSRule* rule = styleSheet.item(i); |
| 322 String itemText = rule->cssText(); | 333 String itemText = rule->cssText(); |
| 323 if (!itemText.isEmpty()) { | 334 if (!itemText.isEmpty()) { |
| 324 cssText.append(itemText); | 335 cssText.append(itemText); |
| 325 if (i < styleSheet.length() - 1) | 336 if (i < styleSheet.length() - 1) |
| 326 cssText.append("\n\n"); | 337 cssText.append("\n\n"); |
| 327 } | 338 } |
| 328 | 339 |
| 329 // Some rules have resources associated with them that we need to retrie ve. | 340 // Some rules have resources associated with them that we need to retrie ve. |
| 330 serializeCSSRule(rule); | 341 serializeCSSRule(rule); |
| 331 } | 342 } |
| 332 | 343 |
| 333 if (shouldAddURL(url)) { | 344 if (shouldAddURL(url)) { |
| 334 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); | 345 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); |
| 335 ASSERT(textEncoding.isValid()); | 346 ASSERT(textEncoding.isValid()); |
| 336 String textString = cssText.toString(); | 347 String textString = cssText.toString(); |
| 337 CString text = textEncoding.encode(textString, WTF::CSSEncodedEntitiesFo rUnencodables); | 348 CString text = textEncoding.encode(textString, WTF::CSSEncodedEntitiesFo rUnencodables); |
| 338 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); | 349 m_resources->append(SerializedResource(url, String("text/css"), SharedBu ffer::create(text.data(), text.length()))); |
| 339 m_resourceURLs.add(url); | 350 m_resourceURLs.add(url); |
| 340 } | 351 } |
| 352 | |
| 353 if (cssStartTime != 0) { | |
| 354 m_isSerializingCss = false; | |
| 355 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram, ("PageSerializat ion.CSSSerializationTime", 0, 10000000, 50)); | |
| 356 cssHistogram.count(static_cast<int64_t>((WTF::monotonicallyIncreasingTim e() - cssStartTime) * 1000000)); | |
| 357 } | |
| 341 } | 358 } |
| 342 | 359 |
| 343 void FrameSerializer::serializeCSSRule(CSSRule* rule) | 360 void FrameSerializer::serializeCSSRule(CSSRule* rule) |
| 344 { | 361 { |
| 345 ASSERT(rule->parentStyleSheet()->ownerDocument()); | 362 ASSERT(rule->parentStyleSheet()->ownerDocument()); |
| 346 Document& document = *rule->parentStyleSheet()->ownerDocument(); | 363 Document& document = *rule->parentStyleSheet()->ownerDocument(); |
| 347 | 364 |
| 348 switch (rule->type()) { | 365 switch (rule->type()) { |
| 349 case CSSRule::kStyleRule: | 366 case CSSRule::kStyleRule: |
| 350 retrieveResourcesForProperties(&toCSSStyleRule(rule)->styleRule()->prope rties(), document); | 367 retrieveResourcesForProperties(&toCSSStyleRule(rule)->styleRule()->prope rties(), document); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 407 m_resourceURLs.add(url); | 424 m_resourceURLs.add(url); |
| 408 } | 425 } |
| 409 | 426 |
| 410 void FrameSerializer::addImageToResources(ImageResource* image, const KURL& url) | 427 void FrameSerializer::addImageToResources(ImageResource* image, const KURL& url) |
| 411 { | 428 { |
| 412 if (!image || !image->hasImage() || image->errorOccurred() || !shouldAddURL( url)) | 429 if (!image || !image->hasImage() || image->errorOccurred() || !shouldAddURL( url)) |
| 413 return; | 430 return; |
| 414 | 431 |
| 415 TRACE_EVENT2("page-serialization", "FrameSerializer::addImageToResources", | 432 TRACE_EVENT2("page-serialization", "FrameSerializer::addImageToResources", |
| 416 "type", "image", "url", url.elidedString().utf8().data()); | 433 "type", "image", "url", url.elidedString().utf8().data()); |
| 434 double imageStartTime = WTF::monotonicallyIncreasingTime(); | |
| 435 | |
| 417 RefPtr<const SharedBuffer> data = image->getImage()->data(); | 436 RefPtr<const SharedBuffer> data = image->getImage()->data(); |
| 418 addToResources(*image, data, url); | 437 addToResources(*image, data, url); |
| 438 | |
| 439 // If we're already reporting time for CSS serialization don't report it for | |
| 440 // this image to avoid reporting the same time twice. | |
| 441 if (!m_isSerializingCss) { | |
| 442 DEFINE_STATIC_LOCAL(CustomCountHistogram, imageHistogram, ("PageSerializ ation.ImageSerializationTime", 0, 10000000, 50)); | |
| 443 imageHistogram.count(static_cast<int64_t>((WTF::monotonicallyIncreasingT ime() - imageStartTime) * 1000000)); | |
|
Ilya Sherman
2016/09/24 02:53:51
nit: Are there any utility functions in Blink that
carlosk
2016/09/27 21:07:29
I'm not very used to Blink's codebase but I think
Ilya Sherman
2016/09/28 00:42:15
Yep, it's a bit nicer this way -- thanks =)
| |
| 444 } | |
| 419 } | 445 } |
| 420 | 446 |
| 421 void FrameSerializer::addFontToResources(FontResource* font) | 447 void FrameSerializer::addFontToResources(FontResource* font) |
| 422 { | 448 { |
| 423 if (!font || !font->isLoaded() || !font->resourceBuffer() || !shouldAddURL(f ont->url())) | 449 if (!font || !font->isLoaded() || !font->resourceBuffer() || !shouldAddURL(f ont->url())) |
| 424 return; | 450 return; |
| 425 | 451 |
| 426 RefPtr<const SharedBuffer> data(font->resourceBuffer()); | 452 RefPtr<const SharedBuffer> data(font->resourceBuffer()); |
| 427 | 453 |
| 428 addToResources(*font, data, font->url()); | 454 addToResources(*font, data, font->url()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 484 continue; | 510 continue; |
| 485 } | 511 } |
| 486 emitsMinus = ch == '-'; | 512 emitsMinus = ch == '-'; |
| 487 builder.append(ch); | 513 builder.append(ch); |
| 488 } | 514 } |
| 489 CString escapedUrl = builder.toString().ascii(); | 515 CString escapedUrl = builder.toString().ascii(); |
| 490 return String::format("saved from url=(%04d)%s", static_cast<int>(escapedUrl .length()), escapedUrl.data()); | 516 return String::format("saved from url=(%04d)%s", static_cast<int>(escapedUrl .length()), escapedUrl.data()); |
| 491 } | 517 } |
| 492 | 518 |
| 493 } // namespace blink | 519 } // namespace blink |
| OLD | NEW |