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

Side by Side Diff: third_party/WebKit/Source/core/frame/FrameSerializer.cpp

Issue 2397983002: MHTML saving optimization: don't generate CSS code that will be discarded. (Closed)
Patch Set: Improved comments and variable naming. Created 4 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 } else if (isHTMLStyleElement(element)) { 321 } else if (isHTMLStyleElement(element)) {
322 HTMLStyleElement& styleElement = toHTMLStyleElement(element); 322 HTMLStyleElement& styleElement = toHTMLStyleElement(element);
323 if (CSSStyleSheet* sheet = styleElement.sheet()) 323 if (CSSStyleSheet* sheet = styleElement.sheet())
324 serializeCSSStyleSheet(*sheet, KURL()); 324 serializeCSSStyleSheet(*sheet, KURL());
325 } 325 }
326 } 326 }
327 } 327 }
328 328
329 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet, 329 void FrameSerializer::serializeCSSStyleSheet(CSSStyleSheet& styleSheet,
330 const KURL& url) { 330 const KURL& url) {
331 // If the URL is invalid or if it is a data URL this means that this CSS is
332 // defined inline, respectively in a <style> tag or in the data URL itself.
333 bool isInlineCss = !url.isValid() || url.protocolIsData();
334 // If this CSS is not inline then it is identifiable by its URL. So just skip
335 // it if it has already been analyzed before.
336 if (!isInlineCss && (m_resourceURLs.contains(url) ||
337 m_delegate.shouldSkipResourceWithURL(url))) {
338 return;
339 }
340
331 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet", 341 TRACE_EVENT2("page-serialization", "FrameSerializer::serializeCSSStyleSheet",
332 "type", "CSS", "url", url.elidedString().utf8().data()); 342 "type", "CSS", "url", url.elidedString().utf8().data());
333 // Only report UMA metric if this is not a reentrant CSS serialization call. 343 // Only report UMA metric if this is not a reentrant CSS serialization call.
334 double cssStartTime = 0; 344 double cssStartTime = 0;
335 if (!m_isSerializingCss) { 345 if (!m_isSerializingCss) {
336 m_isSerializingCss = true; 346 m_isSerializingCss = true;
337 cssStartTime = monotonicallyIncreasingTime(); 347 cssStartTime = monotonicallyIncreasingTime();
338 } 348 }
339 349
340 StringBuilder cssText; 350 // If this CSS is inlined its definition was already serialized with the frame
341 cssText.append("@charset \""); 351 // HTML code that was previously generated. No need to regenerate it here.
342 cssText.append(styleSheet.contents()->charset().lower()); 352 if (!isInlineCss) {
343 cssText.append("\";\n\n"); 353 StringBuilder cssText;
354 cssText.append("@charset \"");
355 cssText.append(styleSheet.contents()->charset().lower());
356 cssText.append("\";\n\n");
344 357
345 for (unsigned i = 0; i < styleSheet.length(); ++i) { 358 for (unsigned i = 0; i < styleSheet.length(); ++i) {
346 CSSRule* rule = styleSheet.item(i); 359 CSSRule* rule = styleSheet.item(i);
347 String itemText = rule->cssText(); 360 String itemText = rule->cssText();
348 if (!itemText.isEmpty()) { 361 if (!itemText.isEmpty()) {
349 cssText.append(itemText); 362 cssText.append(itemText);
350 if (i < styleSheet.length() - 1) 363 if (i < styleSheet.length() - 1)
351 cssText.append("\n\n"); 364 cssText.append("\n\n");
365 }
366
367 // Some rules have resources associated with them that we need to
dcheng 2016/10/07 22:34:28 Nit: maybe just do this in one for loop at the end
carlosk 2016/10/07 23:16:07 Yes, done. I talked to lukasza@ and understood his
368 // retrieve.
369 serializeCSSRule(rule);
352 } 370 }
353 371
354 // Some rules have resources associated with them that we need to retrieve.
355 serializeCSSRule(rule);
356 }
357
358 if (shouldAddURL(url)) {
359 WTF::TextEncoding textEncoding(styleSheet.contents()->charset()); 372 WTF::TextEncoding textEncoding(styleSheet.contents()->charset());
360 ASSERT(textEncoding.isValid()); 373 ASSERT(textEncoding.isValid());
361 String textString = cssText.toString(); 374 String textString = cssText.toString();
362 CString text = 375 CString text =
363 textEncoding.encode(textString, WTF::CSSEncodedEntitiesForUnencodables); 376 textEncoding.encode(textString, WTF::CSSEncodedEntitiesForUnencodables);
364 m_resources->append( 377 m_resources->append(
365 SerializedResource(url, String("text/css"), 378 SerializedResource(url, String("text/css"),
366 SharedBuffer::create(text.data(), text.length()))); 379 SharedBuffer::create(text.data(), text.length())));
367 m_resourceURLs.add(url); 380 m_resourceURLs.add(url);
381 } else {
382 // Sub resources need to be serialized even if the CSS definition doesn't
383 // need to be.
384 for (unsigned i = 0; i < styleSheet.length(); ++i)
385 serializeCSSRule(styleSheet.item(i));
368 } 386 }
369 387
370 if (cssStartTime != 0) { 388 if (cssStartTime != 0) {
371 m_isSerializingCss = false; 389 m_isSerializingCss = false;
372 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram, 390 DEFINE_STATIC_LOCAL(CustomCountHistogram, cssHistogram,
373 ("PageSerialization.SerializationTime.CSSElement", 0, 391 ("PageSerialization.SerializationTime.CSSElement", 0,
374 maxSerializationTimeUmaMicroseconds, 50)); 392 maxSerializationTimeUmaMicroseconds, 50));
375 cssHistogram.count( 393 cssHistogram.count(
376 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) * 394 static_cast<int64_t>((monotonicallyIncreasingTime() - cssStartTime) *
377 secondsToMicroseconds)); 395 secondsToMicroseconds));
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
541 emitsMinus = ch == '-'; 559 emitsMinus = ch == '-';
542 builder.append(ch); 560 builder.append(ch);
543 } 561 }
544 CString escapedUrl = builder.toString().ascii(); 562 CString escapedUrl = builder.toString().ascii();
545 return String::format("saved from url=(%04d)%s", 563 return String::format("saved from url=(%04d)%s",
546 static_cast<int>(escapedUrl.length()), 564 static_cast<int>(escapedUrl.length()),
547 escapedUrl.data()); 565 escapedUrl.data());
548 } 566 }
549 567
550 } // namespace blink 568 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698